[jboss-cvs] JBossAS SVN: r59010 - in projects/ejb3/trunk/injection: . src/main/java/org/jboss/injection src/main/java/org/jboss/injection/aop src/main/java/org/jboss/injection/lang/reflect src/test/java/org/jboss/injection/test src/test/java/org/jboss/injection/test/annotated src/test/java/org/jboss/injection/test/annotated/unit src/test/java/org/jboss/injection/test/programatically src/test/java/org/jboss/injection/test/programatically/unit
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 13 06:13:20 EST 2006
Author: wolfc
Date: 2006-12-13 06:12:42 -0500 (Wed, 13 Dec 2006)
New Revision: 59010
Added:
projects/ejb3/trunk/injection/.classpath
projects/ejb3/trunk/injection/.project
projects/ejb3/trunk/injection/README.txt
projects/ejb3/trunk/injection/pom.xml
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractInjector.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AnnotatedPropertyProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ClassPropertyProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injection.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectionProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injector.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorFactory.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiInjectorFactory.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiPropertyInjector.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapInjectorFactory.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapPropertyInjector.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/PostConstructProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Processor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceClassProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceInjectorFactory.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourcePropertyProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/SimpleValueInjector.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/annotation/
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/jndi/
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/simple/
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Counter.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/InjectedBean.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Simple.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/SimplePropertyProcessor.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/unit/
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/unit/AnnotatedTestCase.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/Counter.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/InjectedBean.java
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/unit/
projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/unit/InjectorProcessorTestCase.java
Modified:
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorProcessor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/aop/ConstructorInterceptor.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/BeanProperty.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/FieldBeanProperty.java
projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/MethodBeanProperty.java
Log:
First prototype
Added: projects/ejb3/trunk/injection/.classpath
===================================================================
--- projects/ejb3/trunk/injection/.classpath 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/.classpath 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" output="target/eclipse-test-classes" path="src/test/java"/>
+ <classpathentry kind="src" output="target/eclipse-test-classes" path="src/test/resources"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="var" path="M2_REPO/apache-slide/webdavlib/2.0/webdavlib-2.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/trove/trove/2.1.1/trove-2.1.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-aop/2.0.0.alpha2/jboss-aop-2.0.0.alpha2.jar"/>
+ <classpathentry kind="var" path="M2_REPO/oswego-concurrent/concurrent/1.3.4/concurrent-1.3.4.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-ejb3x/1.0.0.CR1/jboss-ejb3x-1.0.0.CR1.jar" sourcepath="M2_REPO/jboss/jboss-ejb3x/1.0.0.CR1/jboss-ejb3x-1.0.0.CR1-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/apache-xerces/xml-apis/2.7.1/xml-apis-2.7.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/javassist/javassist/3.4.GA/javassist-3.4.GA.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/apache-httpclient/commons-httpclient/2.0.2/commons-httpclient-2.0.2.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-logging-spi/2.0.2.GA/jboss-logging-spi-2.0.2.GA.jar" sourcepath="M2_REPO/jboss/jboss-logging-spi/2.0.2.GA/jboss-logging-spi-2.0.2.GA-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-j2ee/4.0.2/jboss-j2ee-4.0.2.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-common-core/2.0.2.GA/jboss-common-core-2.0.2.GA.jar" sourcepath="M2_REPO/jboss/jboss-common-core/2.0.2.GA/jboss-common-core-2.0.2.GA-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/jboss/jboss-logging-log4j/2.0.2.GA/jboss-logging-log4j-2.0.2.GA.jar"/>
+ <classpathentry kind="output" path="target/eclipse-classes"/>
+</classpath>
Added: projects/ejb3/trunk/injection/.project
===================================================================
--- projects/ejb3/trunk/injection/.project 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/.project 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,14 @@
+<projectDescription>
+ <name>injection</name>
+ <comment>JBoss POJO Injection Framework</comment>
+ <projects/>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments/>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
\ No newline at end of file
Added: projects/ejb3/trunk/injection/README.txt
===================================================================
--- projects/ejb3/trunk/injection/README.txt 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/README.txt 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,23 @@
+Injection Framework
+
+With the injection framework it should be possible to easily inject
+values into beans. Note that the injection framework doesn't specify
+properties of a bean in the same way as JavaBeans does. Any field or
+setter method can constitute a property.
+
+The injection works in two phases:
+1. Setup of the environment
+2. Injection of objects
+
+The environment could be setup in JNDI, but this is not required
+by the injection framework. For JNDI setup an implementation is
+provided.
+
+Injection of objects can be specified programatically. For
+annotation based injection an implementation is provided.
+
+It's not IoC or MC injection, because it doesn't take into
+account dependencies.
+
+Although the injection framework contains annotation processors JSR-269
+is left out of scope.
\ No newline at end of file
Added: projects/ejb3/trunk/injection/pom.xml
===================================================================
--- projects/ejb3/trunk/injection/pom.xml 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/pom.xml 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,156 @@
+<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">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>jboss</groupId>
+ <artifactId>injection</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>injection</name>
+ <url>http://www.jboss.org</url>
+ <description>JBoss POJO Injection Framework</description>
+
+ <licenses>
+ <license>
+ <name>lgpl</name>
+ <url>http://repository.jboss.com/licenses/lgpl.txt</url>
+ </license>
+ </licenses>
+
+ <organization>
+ <name>JBoss, Red Hat Middleware LLC</name>
+ <url>http://www.jboss.org</url>
+ </organization>
+
+ <repositories>
+ <repository>
+ <id>jboss</id>
+ <name>JBoss Inc. Repository</name>
+ <layout>default</layout>
+ <url>http://repository.jboss.com/maven2/</url>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>jbosspluginrepo</id>
+ <name>jboss plugin repository</name>
+ <url>http://repository.jboss.com/maven2</url>
+ <layout>default</layout>
+ <snapshots>
+ <enabled>false</enabled>
+ <updatePolicy>never</updatePolicy>
+ </snapshots>
+ </pluginRepository>
+ </pluginRepositories>
+
+ <build>
+ <!--sourceDirectory>src/main</sourceDirectory-->
+ <!--finalName>${artifactId}</finalName-->
+ <plugins>
+ <!-- define how we want compilation to take place
+ here, we accept most of the defaults but say that we want the
+ optimization flag set, and define the source and target to be 1.4,
+ these setting will be inherited by child projects -->
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0</version>
+ <configuration>
+ <optimize>true</optimize>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+
+ <!-- define that we wish to create src jars -->
+ <plugin>
+ <artifactId>maven-source-plugin</artifactId>
+ <inherited>true</inherited>
+ <executions>
+ <execution>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <argLine>-ea</argLine>
+ <!-- doesn't work either
+ <argLine>-Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader -cp ${localRepository}</argLine>
+ -->
+ <excludes>
+ <!-- exclude simple because it requires AOP -->
+ <exclude>org/jboss/injection/test/simple/unit/SimpleTestCase.java</exclude>
+ </excludes>
+ <!-- doesn't work
+ <systemProperties>
+ <property>
+ <name>java.system.class.loader</name>
+ <value>org.jboss.aop.standalone.SystemClassLoader</value>
+ </property>
+ </systemProperties>
+ -->
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-aop</artifactId>
+ <version>2.0.0.alpha2</version>
+ </dependency>
+ <dependency>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-ejb3x</artifactId>
+ <version>1.0.0.CR1</version>
+ </dependency>
+ <dependency>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-logging-log4j</artifactId>
+ <version>2.0.2.GA</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- TODO: or do we want jboss-common-core? -->
+ <dependency>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-logging-spi</artifactId>
+ <version>2.0.2.GA</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- non-transitive dependencies from aop -->
+ <dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>3.4.GA</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-common-core</artifactId>
+ <version>2.0.2.GA</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>trove</groupId>
+ <artifactId>trove</artifactId>
+ <version>2.1.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractInjector.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractInjector.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractInjector.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.logging.Logger;
+
+/**
+ * Injects a single property.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractInjector implements Injector
+{
+ private static final Logger log = Logger.getLogger(AbstractInjector.class);
+
+ private BeanProperty property;
+
+ protected AbstractInjector(BeanProperty property)
+ {
+ assert property != null;
+
+ this.property = property;
+ }
+
+ public void inject(Object instance)
+ {
+ Object value = getValue();
+ if(log.isTraceEnabled())
+ log.trace("injecting property " + property + " with value " + value);
+ property.set(instance, value);
+ }
+
+ protected abstract Object getValue();
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AbstractProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Makes it easier to return an empty injector collection.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractProcessor<T> implements InjectionProcessor<T>
+{
+ public abstract Injector processOne(T t);
+
+ public Collection<Injector> process(T t)
+ {
+ Collection<Injector> list = new ArrayList<Injector>();
+ Injector injector = processOne(t);
+ if(injector != null)
+ list.add(injector);
+ return list;
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AnnotatedPropertyProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AnnotatedPropertyProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/AnnotatedPropertyProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ * @deprecated NYI
+ */
+public class AnnotatedPropertyProcessor<FactoryType extends InjectorFactory, AnnotationType extends Annotation> extends AbstractProcessor<BeanProperty>
+{
+ private FactoryType factory;
+
+ protected AnnotatedPropertyProcessor(FactoryType factory)
+ {
+ assert factory != null;
+
+ this.factory = factory;
+ }
+
+ public Injector processOne(BeanProperty property)
+ {
+ throw new RuntimeException("NYI");
+// AnnotationType resource = property.getAnnotation(AnnotationType);
+// if(resource == null) return null;
+//
+// return factory.create(property, resource);
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ClassPropertyProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ClassPropertyProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ClassPropertyProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.injection.lang.reflect.FieldBeanProperty;
+import org.jboss.injection.lang.reflect.MethodBeanProperty;
+
+/**
+ * Processes the properties of a class for injection.
+ *
+ * Usually used to find annotations on properties.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ClassPropertyProcessor implements InjectionProcessor<Class<?>>
+{
+ private InjectionProcessor<BeanProperty> propertyProcessor;
+
+ public ClassPropertyProcessor(InjectionProcessor<BeanProperty> propertyProcessor)
+ {
+ assert propertyProcessor != null;
+
+ this.propertyProcessor = propertyProcessor;
+ }
+
+ /**
+ * Find all properties defined in the class and pass
+ * the property to the propertyProcessor.
+ *
+ * @param cls
+ */
+ public Collection<Injector> process(Class<?> cls)
+ {
+ Collection<Injector> list = new ArrayList<Injector>();
+
+ Field fields[] = cls.getDeclaredFields();
+ for(Field field : fields)
+ {
+ BeanProperty property = new FieldBeanProperty(field);
+ list.addAll(propertyProcessor.process(property));
+ }
+
+ Method methods[] = cls.getDeclaredMethods();
+ for(Method method : methods)
+ {
+ if(MethodBeanProperty.isValid(method))
+ {
+ BeanProperty property = new MethodBeanProperty(method);
+ list.addAll(propertyProcessor.process(property));
+ }
+ }
+
+ return list;
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injection.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injection.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injection.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * The facade for the injection framework.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class Injection
+{
+ /**
+ * Fires of a collection of processors on a given object.
+ *
+ * @param <T1> the object type
+ * @param <T2> the generic type of the resultant collection
+ * @param t the object on which processing takes place
+ * @param c the collection of processors
+ * @return the resultant collection
+ */
+ public static <T1, T2> Collection<T2> doIt(T1 t, Collection<Processor<T1, Collection<T2>>> c)
+ {
+ Collection<T2> list = new ArrayList<T2>();
+ for(Processor<T1, Collection<T2>> processor : c)
+ {
+ list.addAll(processor.process(t));
+ }
+ return list;
+ }
+
+ /**
+ * Process an object for injection.
+ * Find out which injectors are registered for this object's class and
+ * run them.
+ *
+ * @param instance
+ */
+ public static void process(Object instance)
+ {
+
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectionProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectionProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectionProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.util.Collection;
+
+/**
+ * Processes objects to produce injector lists.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface InjectionProcessor<T> extends Processor<T, Collection<Injector>>
+{
+
+}
Copied: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injector.java (from rev 58873, trunk/ejb3/src/main/org/jboss/injection/Injector.java)
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/Injector.java 2006-12-06 18:05:12 UTC (rev 58873)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Injector.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+/**
+ * Inject something into an object.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision$
+ */
+public interface Injector
+{
+ void inject(Object instance);
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorFactory.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorFactory.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorFactory.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Creates injector based on some sort of metaData.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface InjectorFactory<T>
+{
+ Injector create(BeanProperty property, T metaData);
+}
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/InjectorProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -21,6 +21,10 @@
*/
package org.jboss.injection;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+
/**
* Processes instances by injecting their properties and calling their postconstructs.
*
@@ -29,8 +33,61 @@
*/
public class InjectorProcessor
{
- public static void process(Object instance)
+ public static void process(Object instance, Collection<Injector> injectors, Collection<Method> postConstructs)
{
+ assert instance != null;
+ assert injectors != null;
+ assert postConstructs != null : "postConstructs is null";
+ for(Injector injector : injectors)
+ {
+ injector.inject(instance);
+ }
+
+ Class<?> cls = instance.getClass();
+ for(Method method : postConstructs)
+ {
+ Object obj;
+ if(cls.isAssignableFrom(method.getDeclaringClass()))
+ {
+ obj = instance;
+ }
+ else
+ {
+ try
+ {
+ // TODO: this is probably a bad idea
+ obj = method.getDeclaringClass().newInstance();
+ }
+ catch (InstantiationException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ Object args[] = null;
+ method.setAccessible(true);
+ try
+ {
+ method.invoke(obj, args);
+ }
+ catch (IllegalAccessException e)
+ {
+ // should not happen
+ throw new RuntimeException(e);
+ }
+ catch (InvocationTargetException e)
+ {
+ Throwable t = e.getCause();
+ if(t instanceof Error)
+ throw (Error) t;
+ if(t instanceof RuntimeException)
+ throw (RuntimeException) t;
+ throw new RuntimeException(t);
+ }
+ }
}
}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiInjectorFactory.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiInjectorFactory.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiInjectorFactory.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Creates a JNDI property injector based on a Resource annotation.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JndiInjectorFactory implements InjectorFactory<String>
+{
+ /* (non-Javadoc)
+ * @see org.jboss.injection.InjectorFactory#create(org.jboss.injection.lang.reflect.BeanProperty, java.lang.Object)
+ */
+ public Injector create(BeanProperty property, String name)
+ {
+ assert property != null;
+ assert name != null;
+
+ return new JndiPropertyInjector(property, name);
+ }
+
+}
Copied: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiPropertyInjector.java (from rev 58873, trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java)
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java 2006-12-06 18:05:12 UTC (rev 58873)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/JndiPropertyInjector.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.logging.Logger;
+
+/**
+ * Injects a jndi dependency into a bean property.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JndiPropertyInjector implements Injector
+{
+ @SuppressWarnings("unused")
+ private static final Logger log = Logger.getLogger(JndiPropertyInjector.class);
+
+ private BeanProperty property;
+ private String jndiName;
+ private Context ctx;
+
+ public JndiPropertyInjector(BeanProperty property)
+ {
+ this(property, property.getDeclaringClass().getName() + "/" + property.getName());
+ }
+
+ public JndiPropertyInjector(BeanProperty property, String relativeJndiName)
+ {
+ this(property, relativeJndiName, getContext("java:comp/env"));
+ }
+
+ public JndiPropertyInjector(BeanProperty property, String relativeJndiName, Context ctx)
+ {
+ assert property != null;
+ assert relativeJndiName != null;
+ assert ctx != null;
+
+ this.property = property;
+ this.jndiName = relativeJndiName;
+ this.ctx = ctx;
+ }
+
+ private static Context getContext(String jndiName)
+ {
+ try
+ {
+ return (Context) new InitialContext().lookup(jndiName);
+ }
+ catch(NamingException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Class<?> getInjectionClass()
+ {
+ return property.getType();
+ }
+
+ protected Object lookup(String jndiName)
+ {
+ Object dependency = null;
+
+ try
+ {
+ dependency = ctx.lookup(jndiName);
+ }
+ catch (NamingException e)
+ {
+ throw new RuntimeException("Unable to inject jndi dependency: " + jndiName + " into property " + property, e);
+ }
+ return dependency;
+ }
+
+ public void inject(Object instance)
+ {
+ Object value = lookup(jndiName);
+ property.set(instance, value);
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapInjectorFactory.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapInjectorFactory.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapInjectorFactory.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.util.Map;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MapInjectorFactory implements InjectorFactory<String>
+{
+ private Map<String, Object> map;
+
+ public MapInjectorFactory(Map<String, Object> map)
+ {
+ assert map != null;
+
+ this.map = map;
+ }
+
+ public Injector create(BeanProperty property, String name)
+ {
+ assert property != null;
+
+ if(name == null) return null;
+
+ return new MapPropertyInjector(property, map, name);
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapPropertyInjector.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapPropertyInjector.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/MapPropertyInjector.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.util.Map;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Injects a value from a map.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MapPropertyInjector extends AbstractInjector
+{
+ private Map<String, Object> map;
+ private String name;
+
+ public MapPropertyInjector(BeanProperty property, Map<String, Object> map, String name)
+ {
+ super(property);
+
+ assert map != null;
+ assert name != null;
+
+ this.map = map;
+ this.name = name;
+ }
+
+ @Override
+ protected Object getValue()
+ {
+ return map.get(name);
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/PostConstructProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/PostConstructProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/PostConstructProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * Scans a class for PostConstruct annotations.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class PostConstructProcessor implements Processor<Class<?>, Collection<Method>>
+{
+ public Collection<Method> process(Class<?> cls)
+ {
+ Collection<Method> list = new ArrayList<Method>();
+ if(cls == null) return list;
+
+ Method methods[] = cls.getDeclaredMethods();
+ for(Method method : methods)
+ {
+ PostConstruct pc = method.getAnnotation(PostConstruct.class);
+ if(pc != null)
+ {
+ list.add(method);
+ }
+ }
+
+ list.addAll(process(cls.getSuperclass()));
+
+ return list;
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Processor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Processor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/Processor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+/**
+ * Processes an object to produce something.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface Processor<T1, T2>
+{
+ T2 process(T1 t);
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceClassProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceClassProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceClassProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+/**
+ * Processes Resource annotation on classes for injection.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ResourceClassProcessor extends ClassPropertyProcessor
+{
+ public ResourceClassProcessor(InjectorFactory<String> factory)
+ {
+ super(new ResourcePropertyProcessor(factory));
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceInjectorFactory.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceInjectorFactory.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourceInjectorFactory.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import javax.annotation.Resource;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface ResourceInjectorFactory extends InjectorFactory<Resource>
+{
+
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourcePropertyProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourcePropertyProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/ResourcePropertyProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import javax.annotation.Resource;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ResourcePropertyProcessor extends AbstractProcessor<BeanProperty>
+{
+ private InjectorFactory<String> factory;
+
+ protected ResourcePropertyProcessor(InjectorFactory<String> factory)
+ {
+ assert factory != null;
+
+ this.factory = factory;
+ }
+
+ public Injector processOne(BeanProperty property)
+ {
+ Resource resource = property.getAnnotation(Resource.class);
+ if(resource == null) return null;
+
+ String name = resource.name();
+ if(name.equals(""))
+ name = property.getDeclaringClass().getName() + "/" + property.getName();
+
+ return factory.create(property, name);
+ }
+}
Added: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/SimpleValueInjector.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/SimpleValueInjector.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/SimpleValueInjector.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection;
+
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Injects the set value into a property.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SimpleValueInjector implements Injector
+{
+ private BeanProperty property;
+ private Object value;
+
+ public SimpleValueInjector(BeanProperty property, Object value)
+ {
+ assert property != null;
+
+ this.property = property;
+ this.value = value;
+ }
+
+ public void inject(Object instance)
+ {
+ property.set(instance, value);
+ }
+}
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/aop/ConstructorInterceptor.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/aop/ConstructorInterceptor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/aop/ConstructorInterceptor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -40,7 +40,8 @@
public Object invoke(Invocation invocation) throws Throwable
{
- InjectorProcessor.process(invocation.getTargetObject());
+ System.err.println("here");
+ //InjectorProcessor.process(invocation.getTargetObject());
return invocation.invokeNext();
}
}
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -21,6 +21,7 @@
*/
package org.jboss.injection.lang.reflect;
+import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
/**
@@ -40,8 +41,13 @@
obj.setAccessible(true);
}
- public T getAccessibleObject()
+ protected T getAccessibleObject()
{
return obj;
}
+
+ public <T1 extends Annotation> T1 getAnnotation(Class<T1> annotationClass)
+ {
+ return obj.getAnnotation(annotationClass);
+ }
}
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/BeanProperty.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/BeanProperty.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/BeanProperty.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -21,7 +21,7 @@
*/
package org.jboss.injection.lang.reflect;
-import java.lang.reflect.AccessibleObject;
+import java.lang.annotation.Annotation;
/**
* A bean property defines a propery of a pojo from the EJB3 point of view.
@@ -32,8 +32,7 @@
*/
public interface BeanProperty
{
- // TODO: should not be exposed
- AccessibleObject getAccessibleObject();
+ <T extends Annotation> T getAnnotation(Class<T> annotationClass);
Class<?> getDeclaringClass();
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/FieldBeanProperty.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/FieldBeanProperty.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/FieldBeanProperty.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -85,4 +85,9 @@
throw new IllegalArgumentException(msg);
}
}
+
+ public String toString()
+ {
+ return super.toString() + "{" + getDeclaringClass() + "/" + getName() + "}";
+ }
}
Modified: projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/MethodBeanProperty.java
===================================================================
--- projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/MethodBeanProperty.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/main/java/org/jboss/injection/lang/reflect/MethodBeanProperty.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -46,7 +46,7 @@
super(method);
assert method.getReturnType() == Void.TYPE;
- assert method.getParameterTypes().length == 1;
+ assert method.getParameterTypes().length == 1 : "invalid number of parameters on " + method;
assert method.getName().startsWith("set");
}
@@ -83,6 +83,14 @@
return getMethod().getParameterTypes()[0];
}
+ public static boolean isValid(Method method)
+ {
+ if(method.getReturnType() != Void.TYPE) return false;
+ if(method.getParameterTypes().length != 1) return false;
+ if(!method.getName().startsWith("set")) return false;
+ return true;
+ }
+
/* (non-Javadoc)
* @see org.jboss.injection.lang.reflect.BeanProperty#set(java.lang.Object, java.lang.Object)
*/
@@ -115,5 +123,9 @@
throw new RuntimeException(cause);
}
}
-
+
+ public String toString()
+ {
+ return super.toString() + "{" + getDeclaringClass() + "/" + getName() + "}";
+ }
}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Counter.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Counter.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Counter.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.annotated;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class Counter
+{
+ public static int postConstructs;
+ public static int preDestroys;
+
+ public static void reset()
+ {
+ postConstructs = 0;
+ preDestroys = 0;
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/InjectedBean.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/InjectedBean.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/InjectedBean.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.annotated;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.annotation.Resource;
+
+/**
+ * Do injection based on annotation.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class InjectedBean
+{
+ @Resource
+ private String value;
+
+ /*
+ * To prove extendibility of annotated injection.
+ * This should create a SimpleValueInjector.
+ */
+ @Simple(value="Hello world")
+ private String simpleValue;
+
+ public InjectedBean()
+ {
+ if(value != null)
+ throw new IllegalStateException("value must be null");
+
+ if(simpleValue != null)
+ throw new IllegalStateException("simpleValue must be null");
+ }
+
+ public void check()
+ {
+ if(value == null)
+ throw new IllegalStateException("value must not be null");
+
+ if(simpleValue == null)
+ throw new IllegalStateException("simpleValue must no be null");
+ }
+
+ @PostConstruct
+ public void postConstruct()
+ {
+ if(value == null)
+ throw new IllegalStateException("value must not be null");
+
+ if(simpleValue == null)
+ throw new IllegalStateException("simpleValue must no be null");
+
+ Counter.postConstructs++;
+ }
+
+ @PreDestroy
+ public void preDestroy()
+ {
+ Counter.preDestroys++;
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Simple.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Simple.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/Simple.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.annotated;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Do a simple value injection.
+ * This annotation is pretty useless, but it should
+ * prove that the injection framework can be extended easily
+ * with new annotations.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Target({FIELD, METHOD})
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface Simple {
+ String value();
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/SimplePropertyProcessor.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/SimplePropertyProcessor.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/SimplePropertyProcessor.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.annotated;
+
+import org.jboss.injection.AbstractProcessor;
+import org.jboss.injection.Injector;
+import org.jboss.injection.SimpleValueInjector;
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SimplePropertyProcessor extends AbstractProcessor<BeanProperty>
+{
+ @Override
+ public Injector processOne(BeanProperty property)
+ {
+ Simple simple = property.getAnnotation(Simple.class);
+ if(simple == null) return null;
+
+ // TODO: transform the value
+ return new SimpleValueInjector(property, simple.value());
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/unit/AnnotatedTestCase.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/unit/AnnotatedTestCase.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/annotated/unit/AnnotatedTestCase.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,99 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.annotated.unit;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.jboss.injection.ClassPropertyProcessor;
+import org.jboss.injection.Injection;
+import org.jboss.injection.Injector;
+import org.jboss.injection.InjectorProcessor;
+import org.jboss.injection.MapInjectorFactory;
+import org.jboss.injection.PostConstructProcessor;
+import org.jboss.injection.Processor;
+import org.jboss.injection.ResourceClassProcessor;
+import org.jboss.injection.test.annotated.Counter;
+import org.jboss.injection.test.annotated.InjectedBean;
+import org.jboss.injection.test.annotated.SimplePropertyProcessor;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class AnnotatedTestCase extends TestCase
+{
+ public void test1() throws Exception
+ {
+ Counter.reset();
+
+ // There are multiple ways to initialize the injectors, annotation
+ // is one of them.
+
+ //
+ // the environment to use
+ //
+
+ Map<String, Object> env = new HashMap<String, Object>();
+ env.put(InjectedBean.class.getName() + "/value", "Hello world");
+
+ //
+ // the setup of injectors
+ //
+
+ // TODO: weird, shouldn't this work?
+ //Collection<InjectionProcessor<Class<?>>> handlers = new ArrayList<InjectionProcessor<Class<?>>>();
+ Collection<Processor<Class<?>, Collection<Injector>>> handlers = new ArrayList<Processor<Class<?>, Collection<Injector>>>();
+ handlers.add(new ResourceClassProcessor(new MapInjectorFactory(env)));
+ handlers.add(new ClassPropertyProcessor(new SimplePropertyProcessor()));
+ Collection<Injector> injectors = Injection.doIt(InjectedBean.class, handlers);
+
+ assertEquals("Wrong number of injectors", 2, injectors.size());
+
+ Collection<Processor<Class<?>, Collection<Method>>> postConstructProcessors = new ArrayList<Processor<Class<?>, Collection<Method>>>();
+ postConstructProcessors.add(new PostConstructProcessor());
+ Collection<Method> postConstructs = Injection.doIt(InjectedBean.class, postConstructProcessors);
+
+ //
+ // the target
+ //
+
+ InjectedBean bean = new InjectedBean();
+
+ //
+ // the injection
+ //
+
+ InjectorProcessor.process(bean, injectors, postConstructs);
+
+ bean.check();
+
+ assertEquals("postConstruct should have been called once", 1, Counter.postConstructs);
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/Counter.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/Counter.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/Counter.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.programatically;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class Counter
+{
+ public static int postConstructs;
+ public static int preDestroys;
+
+ public static void reset()
+ {
+ postConstructs = 0;
+ preDestroys = 0;
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/InjectedBean.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/InjectedBean.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/InjectedBean.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.programatically;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class InjectedBean
+{
+ private String value;
+
+ public InjectedBean()
+ {
+ if(value != null)
+ throw new IllegalStateException("value must be null");
+ }
+
+ public void check()
+ {
+ if(value == null)
+ throw new IllegalStateException("value must not be null");
+ }
+
+ public void postConstruct()
+ {
+ if(value == null)
+ throw new IllegalStateException("value must not be null");
+ Counter.postConstructs++;
+ }
+
+ public void preDestroy()
+ {
+ Counter.preDestroys++;
+ }
+}
Added: projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/unit/InjectorProcessorTestCase.java
===================================================================
--- projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/unit/InjectorProcessorTestCase.java 2006-12-13 11:12:34 UTC (rev 59009)
+++ projects/ejb3/trunk/injection/src/test/java/org/jboss/injection/test/programatically/unit/InjectorProcessorTestCase.java 2006-12-13 11:12:42 UTC (rev 59010)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.injection.test.programatically.unit;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jboss.injection.Injector;
+import org.jboss.injection.InjectorProcessor;
+import org.jboss.injection.SimpleValueInjector;
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.injection.lang.reflect.FieldBeanProperty;
+import org.jboss.injection.test.programatically.Counter;
+import org.jboss.injection.test.programatically.InjectedBean;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class InjectorProcessorTestCase extends TestCase
+{
+ public void test1() throws Exception
+ {
+ Counter.reset();
+
+ InjectedBean bean = new InjectedBean();
+
+ BeanProperty property = new FieldBeanProperty(bean.getClass().getDeclaredField("value"));
+
+ List<Injector> injectors = new ArrayList<Injector>();
+ injectors.add(new SimpleValueInjector(property, "Hello world"));
+
+ List<Method> postConstructs = new ArrayList<Method>();
+ postConstructs.add(bean.getClass().getDeclaredMethod("postConstruct", new Class[]{}));
+
+ InjectorProcessor.process(bean, injectors, postConstructs);
+
+ bean.check();
+
+ assertEquals(1, Counter.postConstructs);
+ }
+}
More information about the jboss-cvs-commits
mailing list