testing and docs to be added.
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" output="target/classes" path="src/main/java"/>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>wamblee-support-cdi</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.maven.ide.eclipse.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.maven.ide.eclipse.maven2Nature</nature>
+ </natures>
+</projectDescription>
--- /dev/null
+#Wed Jul 14 20:45:46 CEST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.6
--- /dev/null
+#Wed Jul 14 20:45:45 CEST 2010
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+includeModules=false
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1
--- /dev/null
+
+* move maven dependency version to root pom
+* add test code for complete behavior.
+* setup site
+* integrate with wamblee-utils.
+
--- /dev/null
+<?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-cdi</artifactId>
+ <packaging>jar</packaging>
+ <name>/support/cdi</name>
+ <url>http://wamblee.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-se</artifactId>
+ <version>1.0.1-Final</version>
+ </dependency>
+
+ <!-- always include the APIs last because they contain garbled/unusable classes -->
+ <!-- dependency>
+ <groupId>javax</groupId>
+ <artifactId>javaee-api</artifactId>
+ </dependency -->
+
+ </dependencies>
+
+ <distributionManagement>
+ <site>
+ <id>support-cdi</id>
+ <url>file:${distrib}/support/cdi</url>
+ </site>
+ </distributionManagement>
+
+</project>
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+import java.util.logging.Logger;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+/**
+ * Class that encapsulates beanmanager lookup in a way so that the lookup can be
+ * explicitly overriden (e.g. for unit test).
+ *
+ * In case no bean manager is found the beanmanager is set to null and the
+ * problem is logged.
+ *
+ * @author Erik Brakkee
+ */
+public class BeanManagerLookup {
+
+ private static final Logger LOGGER = Logger
+ .getLogger(BeanManagerLookup.class.getName());
+
+ private static final String BEAN_MANAGER_JNDI = "java:comp/BeanManager";
+ private static BeanManager mgr = null;
+
+ /**
+ * Sets the bean manager (mainly for testability).
+ *
+ * @param aMgr
+ * Bean manager.
+ */
+ public static void setBeanManager(BeanManager aMgr) {
+ mgr = aMgr;
+ }
+
+ /**
+ * Looks up the bean manager if not already cached and returns it.
+ *
+ * @return Bean manager.
+ */
+ public static BeanManager lookup() {
+ if (mgr == null) {
+ try {
+ InitialContext ctx = new InitialContext();
+ mgr = (BeanManager) ctx.lookup(BEAN_MANAGER_JNDI);
+ LOGGER.info("Beanmanager successfully located");
+ } catch (NamingException e) {
+ LOGGER.warning("No beanmanager was found, using null");
+ mgr = null;
+ }
+ }
+ return mgr;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+/**
+ * Class encapsulating bean injection into a specific non-contextual object of a
+ * given class.
+ *
+ * @author Erik Brakkee
+ */
+public class CdiInjector implements Injector {
+ private Class clazz;
+ private InjectionTarget target;
+ private CreationalContext ctx;
+
+ /**
+ * Constructs the injector.
+ *
+ * @param aMgr
+ * Bean manager.
+ * @param aClass
+ * Class to analyse for injection.
+ */
+ public CdiInjector(BeanManager aMgr, Class<?> aClass) {
+ clazz = aClass;
+ AnnotatedType<?> type = aMgr.createAnnotatedType(aClass);
+ target = aMgr.createInjectionTarget(type);
+ ctx = aMgr.createCreationalContext(null);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.cdi.Injector#inject(java.lang.Object)
+ */
+ public void inject(Object aComponent) {
+ if (aComponent != null) {
+ if (!clazz.isInstance(aComponent)) {
+ throw new RuntimeException("Object '" + aComponent +
+ "' is of type " + aComponent.getClass().getName() +
+ " but expected " + clazz.getName());
+ }
+ target.inject(aComponent, ctx);
+ }
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+import java.util.logging.Logger;
+
+import javax.enterprise.inject.spi.BeanManager;
+
+/**
+ * Factory that creates CDI injectors. In case no beanmanager is found then
+ * injectors will do nothing. This class may be subclassed for testing to
+ * override the injectors that are returned.
+ *
+ * @author Erik Brakkee
+ */
+public class CdiInjectorFactory implements InjectorFactory {
+
+ private static final Logger LOGGER = Logger
+ .getLogger(CdiInjectorFactory.class.getName());
+
+ private BeanManager beanManager;
+
+ /**
+ * Constructs the factory using the default bean manager.
+ */
+ public CdiInjectorFactory() {
+ this(BeanManagerLookup.lookup());
+ }
+
+ /**
+ * Constructs the factory using an explicit bean manager.
+ *
+ * @param aBeanManager
+ */
+ public CdiInjectorFactory(BeanManager aBeanManager) {
+ beanManager = aBeanManager;
+ }
+
+ @Override
+ public Injector create(Class aClass) {
+ if (beanManager == null) {
+ // Typically for unit test.
+ return new Injector() {
+ @Override
+ public void inject(Object aComponent) {
+ // Empty.
+ }
+ };
+ }
+ return new CdiInjector(beanManager, aClass);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+/**
+ * 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()).
+ *
+ * @author Erik Brakkee
+ */
+public abstract class Injectable {
+
+ /**
+ * Inheritance style constructor.
+ */
+ protected Injectable() {
+ SimpleInjector.inject(this);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+/**
+ * Interface used to perform injection with.
+ *
+ * @author Erik Brakkee
+ */
+public interface Injector {
+
+ /**
+ * Injects beans into a given component of the required class.
+ *
+ * @param aComponent
+ * Component to inject into.
+ */
+ public abstract void inject(Object aComponent);
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Cache of {@link CdiInjector}s for efficiency to avoid duplicate analysis of a
+ * given class.
+ *
+ * @author Erik Brakkee
+ */
+public class InjectorCache {
+
+ private Map<String, Injector> injectors;
+
+ private InjectorFactory injectorFactory;
+
+ /**
+ * Constructs an empty cache.
+ *
+ * @param aMgr
+ * Bean manager.
+ */
+ public InjectorCache(InjectorFactory aInjectorFactory) {
+ injectorFactory = aInjectorFactory;
+ injectors = new ConcurrentHashMap<String, Injector>();
+ }
+
+ /**
+ * Gets the injector for a given class. This returns a cached injector or
+ * creates a new injector and caches it.
+ *
+ * @param aClass
+ * Class to find injector for.
+ * @return Injector.
+ */
+ public Injector getInjector(Class aClass) {
+ Injector injector = injectors.get(aClass.getName());
+ if (injector == null) {
+ // create and add injector
+ // NOTE: in rare circumstances this will lead to parallel
+ // creation of
+ // an injector for the same class. However, only one of them
+ // will be the final one
+ // in the map. There are no side effects of this duplicate
+ // creation of injectors.
+ injector = injectorFactory.create(aClass);
+ injectors.put(aClass.getName(), injector);
+ }
+ return injector;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+/**
+ * Injector factory used. This may be subclasses for testing.
+ *
+ * @author Erik Brakkee
+ */
+public interface InjectorFactory {
+
+ Injector create(Class aClass);
+
+}
--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.cdi;
+
+/**
+ * Singleton injector access. This should be used as main entry point for
+ * injection. A different {@link InjectorFactory} can be plugged in for testing.
+ *
+ * @author Erik Brakkee
+ */
+public class SimpleInjector {
+
+ private static InjectorCache cache = new InjectorCache(
+ new CdiInjectorFactory());
+
+ /**
+ * Override the injector factory (mainly fo runit test).
+ *
+ * @param aFactory
+ * Factory.
+ */
+ public static void setInjectorFactory(InjectorFactory aFactory) {
+ cache = new InjectorCache(aFactory);
+ }
+
+ /**
+ * Injects into a given object.
+ *
+ * @param aObject
+ * Object to inject into.
+ */
+ public static void inject(Object aObject) {
+ cache.getInjector(aObject.getClass()).inject(aObject);
+ }
+}
--- /dev/null
+package org.wamblee.cdi;
+
+import javax.inject.Inject;
+
+public class MyPojo {
+
+ @Inject
+ private MySingleton singleton;
+
+ public MyPojo() {
+ // Empty.
+ }
+
+ public MySingleton getSingleton() {
+ return singleton;
+ }
+}
--- /dev/null
+package org.wamblee.cdi;
+
+import javax.enterprise.context.ApplicationScoped;
+
+
+@ApplicationScoped
+public class MySingleton {
+
+ private static int instances = 0;
+
+ public MySingleton() {
+ System.out.println("Constructing object.");
+ instances++;
+ }
+
+ public static int getInstances() {
+ return instances;
+ }
+}
--- /dev/null
+package org.wamblee.cdi;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+import org.jboss.weld.environment.se.Weld;
+import org.jboss.weld.environment.se.WeldContainer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import static junit.framework.TestCase.*;
+
+public class WeldTest {
+
+ private Weld weld;
+ private WeldContainer container;
+ private BeanManager beanManager;
+
+ @Before
+ public void setUp() {
+ weld = new Weld();
+ container = weld.initialize();
+ beanManager = container.getBeanManager();
+ }
+
+ @After
+ public void tearDown() {
+ weld.shutdown();
+ }
+
+ @Test
+ public void testGetSingleton() {
+ AnnotatedType<MyPojo> type = beanManager.createAnnotatedType(MyPojo.class);
+ InjectionTarget<MyPojo> target = beanManager.createInjectionTarget(type);
+ CreationalContext<MyPojo> ctx = beanManager.createCreationalContext(null);
+
+ MyPojo pojo = new MyPojo();
+
+ target.inject(pojo, ctx);
+
+ MySingleton obj = pojo.getSingleton();
+
+ assertNotNull(obj);
+
+ MyPojo pojo2 = new MyPojo();
+ target.inject(pojo2, ctx);
+
+ // Objects will not be the same as they are contextual references to the same object.
+ // assertSame(pojo2, pojo);
+
+ assertEquals(1, MySingleton.getInstances());
+ }
+}
--- /dev/null
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+
+</beans>
\ No newline at end of file
<distributionManagement>
<site>
- <id>test-enterprise-site</id>
+ <id>support-general</id>
<url>file:${distrib}/support/general</url>
</site>
</distributionManagement>
<modules>
<module>general</module>
+ <module>cdi</module>
<module>spring</module>
</modules>