(no commit message)
authorErik Brakkee <erik@brakkee.org>
Thu, 15 Jul 2010 12:55:04 +0000 (12:55 +0000)
committerErik Brakkee <erik@brakkee.org>
Thu, 15 Jul 2010 12:55:04 +0000 (12:55 +0000)
support/cdi/src/main/java/org/wamblee/cdi/CdiInjector.java
support/cdi/src/main/java/org/wamblee/cdi/CdiInjectorFactory.java
support/cdi/src/main/java/org/wamblee/cdi/package-info.java
support/cdi/src/main/java/org/wamblee/inject/Injectable.java [moved from support/cdi/src/main/java/org/wamblee/cdi/Injectable.java with 60% similarity]
support/cdi/src/main/java/org/wamblee/inject/Injector.java [moved from support/cdi/src/main/java/org/wamblee/cdi/Injector.java with 69% similarity]
support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java [moved from support/cdi/src/main/java/org/wamblee/cdi/InjectorCache.java with 97% similarity]
support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java [moved from support/cdi/src/main/java/org/wamblee/cdi/InjectorFactory.java with 60% similarity]
support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java [moved from support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java with 79% similarity]
support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java
support/inject/pom.xml [new file with mode: 0644]
support/pom.xml

index 46d434b4a3d4649123a777baab9da5db861b0da2..b0ce90713cb3988ae6f236fb34955b743a3d26a8 100644 (file)
@@ -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.
index 74d1a669227a8b3d393504813cec02007939cd2d..2237648b0b5b58f91521976886558970eec643a5 100644 (file)
@@ -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
index a01f4dae07c27c3d67d419d69855d39a4ca22258..927ef6c8b886aa579ca40250eae86909c2c63f62 100644 (file)
@@ -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;
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 2a9a8e883fe8bb6db2cb21056606b13cf96041dd..0c881d9b104d50a796a815d13f0bd5ce955ee309 100644 (file)
  * 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);
-    }
+    }    
 }
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 ad1648e8c6c17beaac1adbe60b4c22db4f6edf55..e42cc5f76343604bfd1e9a391fd5f615a16aa8bc 100644 (file)
  * 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
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 69595c6e10df3134ea6dc5a05e2756996fa9e805..24a0f2c4969c5826b43aed6b2a975884aa418f26 100644 (file)
  * 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.
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 1761d24ad57fe97da2ec2c95a65d114e0c7ba1cb..84f7a66bb5d4e1645370d5c41b6dd88c7a136742 100644 (file)
  * 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);
 
 }
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 fff8f113be13fe8525e6b850366f7fac05b42589..61bb55608b33839d8012e42b5006de2c90218b68 100644 (file)
  * 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;
  *   ...
  * }
  * </pre>
- * 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: 
  * <pre>
  *   Pojo pojo = new Pojo(); 
- *   SimpleInjector injector = new SimpleInjector(); 
+ *   SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); 
  *   injector.inject(pojo);
  * </pre>
  * 
@@ -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) { 
index 61a124a0a577a073a52accf5bb3158e89b9dc0ab..829f5ceb531cd192d6be8e7ecec2920660b16a87 100644 (file)
@@ -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 (file)
index 0000000..5904a4b
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <parent>
+    <groupId>org.wamblee</groupId>
+    <artifactId>wamblee-utils</artifactId>
+    <version>0.2.5-SNAPSHOT</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.wamblee</groupId>
+  <artifactId>wamblee-support-inject</artifactId>
+  <packaging>jar</packaging>
+  <name>/support/inject</name>
+  <url>http://wamblee.org</url>
+  
+  <distributionManagement>
+    <site>
+      <id>support-inject</id>
+      <url>file:${distrib}/support/inject</url>
+    </site>
+  </distributionManagement>
+
+</project>
index c31a72768d0dff2894d645d5c79b61eb01f1b7de..55def75932469a19e1eae34cb9cd2b31ecd69360 100644 (file)
@@ -15,7 +15,8 @@
   <url>http://wamblee.org</url>
 
   <modules>
-    <module>general</module>
+    <module>general</module>  
+    <module>inject</module>
     <module>cdi</module>
     <module>spring</module>
   </modules>