Author: dblevins
Date: Fri Jun 27 19:39:54 2008
New Revision: 672458
URL: http://svn.apache.org/viewvc?rev=672458&view=rev
Log:
OPENEJB-126: Constructor Injection
Works for stateless and stateful beans
Switched to xbean libraries version 3.4.1
Added:
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
Modified:
openejb/trunk/openejb3/container/openejb-core/pom.xml
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulInstanceManager.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
openejb/trunk/openejb3/itests/openejb-itests-client/pom.xml
openejb/trunk/openejb3/pom.xml
openejb/trunk/openejb3/server/openejb-server/pom.xml
Modified: openejb/trunk/openejb3/container/openejb-core/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/pom.xml?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/pom.xml (original)
+++ openejb/trunk/openejb3/container/openejb-core/pom.xml Fri Jun 27 19:39:54
2008
@@ -304,11 +304,11 @@
</dependency>
<!-- End: JavaMail -->
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-reflect</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder</artifactId>
</dependency>
<dependency>
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulInstanceManager.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulInstanceManager.java?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulInstanceManager.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulInstanceManager.java
Fri Jun 27 19:39:54 2008
@@ -146,8 +146,9 @@
ObjectRecipe objectRecipe = new ObjectRecipe(beanClass);
objectRecipe.allow(Option.FIELD_INJECTION);
objectRecipe.allow(Option.PRIVATE_PROPERTIES);
-// objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
-
+ objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+ objectRecipe.allow(Option.NAMED_PARAMETERS);
+
ThreadContext callContext = ThreadContext.getThreadContext();
CoreDeploymentInfo deploymentInfo =
callContext.getDeploymentInfo();
Context ctx = deploymentInfo.getJndiEnc();
@@ -184,6 +185,7 @@
interceptorRecipe.allow(Option.FIELD_INJECTION);
interceptorRecipe.allow(Option.PRIVATE_PROPERTIES);
interceptorRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+ interceptorRecipe.allow(Option.NAMED_PARAMETERS);
fillInjectionProperties(interceptorRecipe, clazz,
deploymentInfo, ctx);
@@ -235,18 +237,35 @@
}
private static void fillInjectionProperties(ObjectRecipe objectRecipe,
Class clazz, CoreDeploymentInfo deploymentInfo, Context context) {
+ boolean usePrefix = true;
+
+ try {
+ clazz.getConstructor();
+ } catch (NoSuchMethodException e) {
+ // Using constructor injection
+ // xbean can't handle the prefix yet
+ usePrefix = false;
+ }
+
for (Injection injection : deploymentInfo.getInjections()) {
if (!injection.getTarget().isAssignableFrom(clazz)) continue;
try {
String jndiName = injection.getJndiName();
Object object = context.lookup("java:comp/env/" + jndiName);
+ String prefix;
+ if (usePrefix) {
+ prefix = injection.getTarget().getName() + "/";
+ } else {
+ prefix = "";
+ }
+
if (object instanceof String) {
String string = (String) object;
// Pass it in raw so it could be potentially converted to
// another data type by an xbean-reflect property editor
- objectRecipe.setProperty(injection.getTarget().getName() +
"/" + injection.getName(), string);
+ objectRecipe.setProperty(prefix + injection.getName(),
string);
} else {
- objectRecipe.setProperty(injection.getTarget().getName() +
"/" + injection.getName(), new StaticRecipe(object));
+ objectRecipe.setProperty(prefix + injection.getName(), new
StaticRecipe(object));
}
} catch (NamingException e) {
logger.warning("Injection data not found in enc: jndiName='" +
injection.getJndiName() + "', target=" + injection.getTarget() + "/" +
injection.getName());
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
Fri Jun 27 19:39:54 2008
@@ -115,6 +115,7 @@
objectRecipe.allow(Option.FIELD_INJECTION);
objectRecipe.allow(Option.PRIVATE_PROPERTIES);
objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+ objectRecipe.allow(Option.NAMED_PARAMETERS);
Operation originalOperation = callContext.getCurrentOperation();
BaseContext.State[] originalAllowedStates =
callContext.getCurrentAllowedStates();
@@ -170,6 +171,7 @@
interceptorRecipe.allow(Option.FIELD_INJECTION);
interceptorRecipe.allow(Option.PRIVATE_PROPERTIES);
interceptorRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+ interceptorRecipe.allow(Option.NAMED_PARAMETERS);
fillInjectionProperties(interceptorRecipe, clazz,
deploymentInfo, ctx);
@@ -224,18 +226,35 @@
}
private static void fillInjectionProperties(ObjectRecipe objectRecipe,
Class clazz, CoreDeploymentInfo deploymentInfo, Context context) {
+ boolean usePrefix = true;
+
+ try {
+ clazz.getConstructor();
+ } catch (NoSuchMethodException e) {
+ // Using constructor injection
+ // xbean can't handle the prefix yet
+ usePrefix = false;
+ }
+
for (Injection injection : deploymentInfo.getInjections()) {
if (!injection.getTarget().isAssignableFrom(clazz)) continue;
try {
String jndiName = injection.getJndiName();
Object object = context.lookup("java:comp/env/" + jndiName);
+ String prefix;
+ if (usePrefix) {
+ prefix = injection.getTarget().getName() + "/";
+ } else {
+ prefix = "";
+ }
+
if (object instanceof String) {
String string = (String) object;
// Pass it in raw so it could be potentially converted to
// another data type by an xbean-reflect property editor
- objectRecipe.setProperty(injection.getTarget().getName() +
"/" + injection.getName(), string);
+ objectRecipe.setProperty(prefix + injection.getName(),
string);
} else {
- objectRecipe.setProperty(injection.getTarget().getName() +
"/" + injection.getName(), new StaticRecipe(object));
+ objectRecipe.setProperty(prefix + injection.getName(), new
StaticRecipe(object));
}
} catch (NamingException e) {
logger.warning("Injection data not found in enc: jndiName='" +
injection.getJndiName() + "', target=" + injection.getTarget() + "/" +
injection.getName());
Added:
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java?rev=672458&view=auto
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
Fri Jun 27 19:39:54 2008
@@ -0,0 +1,113 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.core.stateful;
+
+import junit.framework.TestCase;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.assembler.classic.SecurityServiceInfo;
+import org.apache.openejb.assembler.classic.TransactionServiceInfo;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.core.ivm.naming.InitContextFactory;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.EnvEntry;
+import org.apache.openejb.jee.StatefulBean;
+import org.apache.openejb.jee.StatelessBean;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+
+/**
+ * @version $Revision: 581127 $ $Date: 2007-10-01 19:13:16 -0700 (Mon, 01 Oct
2007) $
+ */
+public class StatefulConstructorInjectionTest extends TestCase {
+
+ public void test() throws Exception {
+ InitialContext ctx = new InitialContext();
+
+ Widget widget = (Widget) ctx.lookup("WidgetBeanLocal");
+
+ Foo foo = (Foo) ctx.lookup("FooBeanLocal");
+
+ assertEquals("Widget.getCount()", 10, widget.getCount());
+ assertEquals("Widget.getFoo()", foo, widget.getFoo());
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
InitContextFactory.class.getName());
+
+ ConfigurationFactory config = new ConfigurationFactory();
+ Assembler assembler = new Assembler();
+
+
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+
+ // Setup the descriptor information
+
+ EjbJar ejbJar = new EjbJar();
+
+ ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
+
+ StatefulBean bean = ejbJar.addEnterpriseBean(new
StatefulBean(WidgetBean.class));
+ bean.getEnvEntry().add(new EnvEntry("count", Integer.class.getName(),
"10"));
+
+
+ assembler.createApplication(config.configureApplication(ejbJar));
+
+ }
+
+ public static interface Foo {
+ }
+
+ public static class FooBean implements Foo {
+ }
+
+ public static interface Widget {
+ public int getCount();
+
+ public Foo getFoo();
+ }
+
+ public static class WidgetBean implements Widget {
+
+ @EJB(beanName = "FooBean")
+ private final Foo foo;
+
+ @Resource(name = "count")
+ private final int count;
+
+ @Resource
+ private final DataSource ds;
+
+ public WidgetBean(Integer count, Foo foo, DataSource ds) {
+ this.count = count;
+ this.foo = foo;
+ this.ds = ds;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public Foo getFoo() {
+ return foo;
+ }
+ }
+}
Added:
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java?rev=672458&view=auto
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
Fri Jun 27 19:39:54 2008
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.core.stateless;
+
+import junit.framework.TestCase;
+
+import javax.naming.InitialContext;
+import javax.ejb.SessionContext;
+import javax.ejb.EJB;
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.util.List;
+import java.util.Arrays;
+import java.util.Stack;
+
+import org.apache.openejb.core.ivm.naming.InitContextFactory;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.assembler.classic.ProxyFactoryInfo;
+import org.apache.openejb.assembler.classic.TransactionServiceInfo;
+import org.apache.openejb.assembler.classic.SecurityServiceInfo;
+import org.apache.openejb.assembler.classic.StatelessSessionContainerInfo;
+import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.EnvEntry;
+
+/**
+ * @version $Revision: 581127 $ $Date: 2007-10-01 19:13:16 -0700 (Mon, 01 Oct
2007) $
+ */
+public class StatelessConstructorInjectionTest extends TestCase {
+
+ public void test() throws Exception {
+ InitialContext ctx = new InitialContext();
+
+ Widget widget = (Widget) ctx.lookup("WidgetBeanLocal");
+
+ Foo foo = (Foo) ctx.lookup("FooBeanLocal");
+
+ assertEquals("Widget.getCount()", 10, widget.getCount());
+ assertEquals("Widget.getFoo()", foo, widget.getFoo());
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
InitContextFactory.class.getName());
+
+ ConfigurationFactory config = new ConfigurationFactory();
+ Assembler assembler = new Assembler();
+
+
assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
+
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+
+ // containers
+ StatelessSessionContainerInfo statelessContainerInfo =
config.configureService(StatelessSessionContainerInfo.class);
+ assembler.createContainer(statelessContainerInfo);
+
+ // Setup the descriptor information
+
+ EjbJar ejbJar = new EjbJar();
+
+ ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
+
+ StatelessBean bean = ejbJar.addEnterpriseBean(new
StatelessBean(WidgetBean.class));
+ bean.getEnvEntry().add(new EnvEntry("count", Integer.class.getName(),
"10"));
+
+
+ assembler.createApplication(config.configureApplication(ejbJar));
+
+ }
+
+ public static interface Foo {}
+
+ public static class FooBean implements Foo {}
+
+ public static interface Widget {
+ public int getCount();
+
+ public Foo getFoo();
+ }
+
+ public static class WidgetBean implements Widget {
+
+ @EJB(beanName = "FooBean")
+ private final Foo foo;
+
+ @Resource(name="count")
+ private final int count;
+
+ @Resource
+ private final DataSource ds;
+
+ public WidgetBean(Integer count, Foo foo, DataSource ds) {
+ this.count = count;
+ this.foo = foo;
+ this.ds = ds;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public Foo getFoo() {
+ return foo;
+ }
+ }
+}
Modified: openejb/trunk/openejb3/itests/openejb-itests-client/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/itests/openejb-itests-client/pom.xml?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
--- openejb/trunk/openejb3/itests/openejb-itests-client/pom.xml (original)
+++ openejb/trunk/openejb3/itests/openejb-itests-client/pom.xml Fri Jun 27
19:39:54 2008
@@ -108,7 +108,7 @@
<scope>compile</scope>
</dependency>
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder</artifactId>
</dependency>
</dependencies>
Modified: openejb/trunk/openejb3/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/pom.xml?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
--- openejb/trunk/openejb3/pom.xml (original)
+++ openejb/trunk/openejb3/pom.xml Fri Jun 27 19:39:54 2008
@@ -93,7 +93,7 @@
<properties>
<!-- This is used by a manifest classpath entry -->
- <xbeanVersion>3.4-r636442</xbeanVersion>
+ <xbeanVersion>3.4.1</xbeanVersion>
<!-- OSGi bundles properties -->
<openejb.osgi.import.pkg>*</openejb.osgi.import.pkg>
@@ -831,19 +831,19 @@
<version>2.0.5</version>
</dependency>
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder</artifactId>
- <version>3.4-r636443-SNAPSHOT</version>
+ <version>${xbeanVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-naming</artifactId>
- <version>3.3</version>
+ <version>${xbeanVersion}</version>
</dependency>
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-reflect</artifactId>
- <version>3.4-r636443-SNAPSHOT</version>
+ <version>${xbeanVersion}</version>
<exclusions>
<exclusion>
<groupId>mx4j</groupId>
Modified: openejb/trunk/openejb3/server/openejb-server/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/pom.xml?rev=672458&r1=672457&r2=672458&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-server/pom.xml (original)
+++ openejb/trunk/openejb3/server/openejb-server/pom.xml Fri Jun 27 19:39:54
2008
@@ -70,7 +70,7 @@
<version>${version}</version>
</dependency>
<dependency>
- <groupId>org.apache.openejb</groupId>
+ <groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder</artifactId>
</dependency>
<dependency>