From a41b8bcead005cec5fd144578f05d695ef89c98b Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 15 Jul 2010 12:55:04 +0000 Subject: [PATCH] --- .../java/org/wamblee/cdi/CdiInjector.java | 2 ++ .../org/wamblee/cdi/CdiInjectorFactory.java | 3 +++ .../java/org/wamblee/cdi/package-info.java | 3 ++- .../wamblee/{cdi => inject}/Injectable.java | 27 ++++++++++++------- .../org/wamblee/{cdi => inject}/Injector.java | 9 ++++--- .../{cdi => inject}/InjectorCache.java | 4 ++- .../{cdi => inject}/InjectorFactory.java | 14 ++++++++-- .../{cdi => inject}/SimpleInjector.java | 22 +++++++-------- .../org/wamblee/cdi/SimpleInjectorTest.java | 5 +++- support/inject/pom.xml | 24 +++++++++++++++++ support/pom.xml | 3 ++- 11 files changed, 85 insertions(+), 31 deletions(-) rename support/cdi/src/main/java/org/wamblee/{cdi => inject}/Injectable.java (60%) rename support/cdi/src/main/java/org/wamblee/{cdi => inject}/Injector.java (69%) rename support/cdi/src/main/java/org/wamblee/{cdi => inject}/InjectorCache.java (97%) rename support/cdi/src/main/java/org/wamblee/{cdi => inject}/InjectorFactory.java (60%) rename support/cdi/src/main/java/org/wamblee/{cdi => inject}/SimpleInjector.java (79%) create mode 100644 support/inject/pom.xml diff --git a/support/cdi/src/main/java/org/wamblee/cdi/CdiInjector.java b/support/cdi/src/main/java/org/wamblee/cdi/CdiInjector.java index 46d434b4..b0ce9071 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/CdiInjector.java +++ b/support/cdi/src/main/java/org/wamblee/cdi/CdiInjector.java @@ -20,6 +20,8 @@ import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.InjectionTarget; +import org.wamblee.inject.Injector; + /** * Class encapsulating bean injection into a specific non-contextual object of a * given class. diff --git a/support/cdi/src/main/java/org/wamblee/cdi/CdiInjectorFactory.java b/support/cdi/src/main/java/org/wamblee/cdi/CdiInjectorFactory.java index 74d1a669..2237648b 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/CdiInjectorFactory.java +++ b/support/cdi/src/main/java/org/wamblee/cdi/CdiInjectorFactory.java @@ -19,6 +19,9 @@ import java.util.logging.Logger; import javax.enterprise.inject.spi.BeanManager; +import org.wamblee.inject.Injector; +import org.wamblee.inject.InjectorFactory; + /** * Factory that creates CDI injectors. In case no beanmanager is found then * injectors will do nothing. This class may be subclassed for testing to diff --git a/support/cdi/src/main/java/org/wamblee/cdi/package-info.java b/support/cdi/src/main/java/org/wamblee/cdi/package-info.java index a01f4dae..927ef6c8 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/package-info.java +++ b/support/cdi/src/main/java/org/wamblee/cdi/package-info.java @@ -19,4 +19,5 @@ * CDI can be used to inject dependencies such as for example @EJB, @Resource, @PersistenceContext, * and @Inject into any object. */ -package org.wamblee.cdi; \ No newline at end of file +package org.wamblee.cdi; +import org.wamblee.inject.SimpleInjector; diff --git a/support/cdi/src/main/java/org/wamblee/cdi/Injectable.java b/support/cdi/src/main/java/org/wamblee/inject/Injectable.java similarity index 60% rename from support/cdi/src/main/java/org/wamblee/cdi/Injectable.java rename to support/cdi/src/main/java/org/wamblee/inject/Injectable.java index 2a9a8e88..0c881d9b 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/Injectable.java +++ b/support/cdi/src/main/java/org/wamblee/inject/Injectable.java @@ -13,26 +13,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.cdi; +package org.wamblee.inject; + +import java.util.ServiceLoader; + /** - * This abstract base class performs injection at construction. - * Be sure not to initialize fields of derived classes to null as these will override the initializations - * of this base class. - * - * Use this by subclassing through - * {@link #Injectable()). + * This abstract base class performs injection at construction. Be sure not to + * initialize fields of derived classes to null as these will override the + * initializations of this base class. + * + * Use this by subclassing through {@link #Injectable()). + * + * To use this class, the {@link ServiceLoader} mechanism must be used to locate + * a {@link InjectorFactory}. The first implementation that is found will be + * used for injection. * * @author Erik Brakkee */ public abstract class Injectable { - - private static final SimpleInjector INJECTOR = new SimpleInjector(); + + private static final SimpleInjector INJECTOR = new SimpleInjector( + InjectorFactoryBuilder.getInjectorFactory()); /** * Inheritance style constructor. */ protected Injectable() { INJECTOR.inject(this); - } + } } diff --git a/support/cdi/src/main/java/org/wamblee/cdi/Injector.java b/support/cdi/src/main/java/org/wamblee/inject/Injector.java similarity index 69% rename from support/cdi/src/main/java/org/wamblee/cdi/Injector.java rename to support/cdi/src/main/java/org/wamblee/inject/Injector.java index ad1648e8..e42cc5f7 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/Injector.java +++ b/support/cdi/src/main/java/org/wamblee/inject/Injector.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.cdi; +package org.wamblee.inject; /** - * Interface used to perform injection with. + * Interface used to perform injection with. An injector instance knows how to perform injection in + * one or more specific classes. * * @author Erik Brakkee */ @@ -27,7 +28,9 @@ public interface Injector { * * @param aComponent * Component to inject into. + * @throws IllegalArgumentException In case the argument passed is not supported for injection + * @throws NullPointerException In case the argument passed is null. */ - public abstract void inject(Object aComponent); + void inject(Object aComponent); } \ No newline at end of file diff --git a/support/cdi/src/main/java/org/wamblee/cdi/InjectorCache.java b/support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java similarity index 97% rename from support/cdi/src/main/java/org/wamblee/cdi/InjectorCache.java rename to support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java index 69595c6e..24a0f2c4 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/InjectorCache.java +++ b/support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.cdi; +package org.wamblee.inject; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.wamblee.cdi.CdiInjector; + /** * Cache of {@link CdiInjector}s for efficiency to avoid duplicate analysis of a * given class. diff --git a/support/cdi/src/main/java/org/wamblee/cdi/InjectorFactory.java b/support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java similarity index 60% rename from support/cdi/src/main/java/org/wamblee/cdi/InjectorFactory.java rename to support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java index 1761d24a..84f7a66b 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/InjectorFactory.java +++ b/support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java @@ -13,15 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.cdi; +package org.wamblee.inject; + /** - * Injector factory used. This may be subclasses for testing. + * Injector factory used. This creates an injector that is appropriate for a certain class. + * May be subclassed for testing or other advanced usage (even replacing CDI with another + * injection framework). + * + * Implementations of this calss must have a default no-arg constructor. * * @author Erik Brakkee */ public interface InjectorFactory { + /** + * Creates an injector. + * @return Injector fot he given class. + * @throws IllegalArgumentException In case the given class cannot be used. + */ Injector create(Class aClass); } diff --git a/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java b/support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java similarity index 79% rename from support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java rename to support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java index fff8f113..61bb5560 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java +++ b/support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java @@ -13,7 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.cdi; +package org.wamblee.inject; + +import java.util.ServiceLoader; + /** * Singleton injector access. This should be used as main entry point for @@ -28,10 +31,10 @@ package org.wamblee.cdi; * ... * } * - * injecting the EJB into a POJO is accomplished as follows: + * injecting the EJB into a POJO using Contexts and Dependency Injection is accomplished as follows: *
  *   Pojo pojo = new Pojo(); 
- *   SimpleInjector injector = new SimpleInjector(); 
+ *   SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); 
  *   injector.inject(pojo);
  * 
* @@ -39,22 +42,17 @@ package org.wamblee.cdi; * of the types that it injects into. Caching the injector makes sure that a class is not * analysed again for annotation every time injection is used. * + * The {@link InjectorFactory} to be used can also be found using a {@link ServiceLoader} discovery + * mechanism as provided by {@link InjectorFactoryBuilder}. + * * @author Erik Brakkee */ public class SimpleInjector { - private InjectorCache cache = new InjectorCache( - new CdiInjectorFactory()); + private InjectorCache cache; /** * Constructs the injector. - */ - public SimpleInjector() { - cache = new InjectorCache(new CdiInjectorFactory()); - } - - /** - * Construcst the injector. * @param aFactory Factory to use. */ public SimpleInjector(InjectorFactory aFactory) { diff --git a/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java b/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java index 61a124a0..829f5ceb 100644 --- a/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java +++ b/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java @@ -23,6 +23,9 @@ import javax.naming.InitialContext; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.wamblee.inject.Injector; +import org.wamblee.inject.InjectorFactory; +import org.wamblee.inject.SimpleInjector; import org.wamblee.support.jndi.StubInitialContextFactory; public class SimpleInjectorTest { @@ -48,7 +51,7 @@ public class SimpleInjectorTest { @Test public void testGetSingleton() { MyPojo pojo = new MyPojo(); - SimpleInjector injector = new SimpleInjector(); + SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); injector.inject(pojo); MySingleton obj = pojo.getSingleton(); diff --git a/support/inject/pom.xml b/support/inject/pom.xml new file mode 100644 index 00000000..5904a4ba --- /dev/null +++ b/support/inject/pom.xml @@ -0,0 +1,24 @@ + + + + + org.wamblee + wamblee-utils + 0.2.5-SNAPSHOT + + + 4.0.0 + org.wamblee + wamblee-support-inject + jar + /support/inject + http://wamblee.org + + + + support-inject + file:${distrib}/support/inject + + + + diff --git a/support/pom.xml b/support/pom.xml index c31a7276..55def759 100644 --- a/support/pom.xml +++ b/support/pom.xml @@ -15,7 +15,8 @@ http://wamblee.org - general + general + inject cdi spring -- 2.31.1