Author: richard.opalka(a)jboss.com
Date: 2009-04-30 06:09:29 -0400 (Thu, 30 Apr 2009)
New Revision: 9926
Added:
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBFieldFinder.java
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBMethodFinder.java
Modified:
common/trunk/pom.xml
common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.java
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/InjectionFieldFinder.java
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ReflectionUtils.java
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceFieldFinder.java
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceMethodFinder.java
Log:
[JBWS-2074][JBWS-2634] improving SPI API + adding initial support for EJB injections
Modified: common/trunk/pom.xml
===================================================================
--- common/trunk/pom.xml 2009-04-30 10:07:08 UTC (rev 9925)
+++ common/trunk/pom.xml 2009-04-30 10:09:29 UTC (rev 9926)
@@ -48,7 +48,14 @@
<version>2.1</version>
<scope>provided</scope>
</dependency>
-
+
+ <dependency>
+ <groupId>javax.ejb</groupId>
+ <artifactId>ejb-api</artifactId>
+ <version>3.0</version>
+ <scope>provided</scope>
+ </dependency>
+
<!-- jboss provided -->
<dependency>
<groupId>org.jboss</groupId>
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.java
===================================================================
---
common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.java 2009-04-30
10:07:08 UTC (rev 9925)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -26,10 +26,14 @@
import java.util.Collection;
import javax.annotation.Resource;
+import javax.ejb.EJB;
import javax.naming.Context;
+import javax.naming.InitialContext;
import javax.xml.ws.WebServiceContext;
import org.jboss.logging.Logger;
+import org.jboss.wsf.common.javax.finders.EJBFieldFinder;
+import org.jboss.wsf.common.javax.finders.EJBMethodFinder;
import org.jboss.wsf.common.javax.finders.InjectionFieldFinder;
import org.jboss.wsf.common.javax.finders.InjectionMethodFinder;
import org.jboss.wsf.common.javax.finders.PostConstructMethodFinder;
@@ -41,18 +45,22 @@
import org.jboss.wsf.spi.metadata.injection.InjectionsMetaData;
/**
- * A helper class for <b>javax.annotation</b> annotations.
+ * An injection helper class for <b>javax.*</b> annotations.
*
- * @author ropalka(a)redhat.com
+ * @author <a href="mailto:richard.opalka@jboss.org">Richard
Opalka</a>
*/
public final class JavaxAnnotationHelper
{
private static final Logger LOG = Logger.getLogger(JavaxAnnotationHelper.class);
+ private static final String POJO_JNDI_PREFIX = "java:comp/env/";
+
private static final ClassProcessor<Method> POST_CONSTRUCT_METHOD_FINDER = new
PostConstructMethodFinder();
private static final ClassProcessor<Method> PRE_DESTROY_METHOD_FINDER = new
PreDestroyMethodFinder();
private static final ClassProcessor<Method> RESOURCE_METHOD_FINDER = new
ResourceMethodFinder(WebServiceContext.class, false);
private static final ClassProcessor<Field> RESOURCE_FIELD_FINDER = new
ResourceFieldFinder(WebServiceContext.class, false);
+ private static final ClassProcessor<Method> EJB_METHOD_FINDER = new
EJBMethodFinder();
+ private static final ClassProcessor<Field> EJB_FIELD_FINDER = new
EJBFieldFinder();
private static final ClassProcessor<Method> WEB_SERVICE_CONTEXT_METHOD_FINDER =
new ResourceMethodFinder(WebServiceContext.class, true);
private static final ClassProcessor<Field> WEB_SERVICE_CONTEXT_FIELD_FINDER =
new ResourceFieldFinder(WebServiceContext.class, true);
@@ -83,77 +91,159 @@
if (injections == null)
return;
- Class<?> instanceClass = instance.getClass();
+ // get JNDI context
+ Context ctx = injections.getContext();
+ if (ctx == null)
+ {
+ ctx = (Context)new InitialContext().lookup(POJO_JNDI_PREFIX);
+ }
// inject descriptor driven annotations
- final Context ctx = injections.getContext();
- final String envPrefix = injections.getContextRoot();
-
- Collection<InjectionMetaData> injectionMDs =
injections.getInjectionsMetaData(instanceClass);
+ final Collection<InjectionMetaData> injectionMDs =
injections.getInjectionsMetaData(instance.getClass());
for (InjectionMetaData injectionMD : injectionMDs)
{
- Method method = getMethod(injectionMD, instanceClass);
- if (method != null)
+ injectDescriptorDrivenInjections(instance, ctx, injectionMD);
+ }
+
+ // inject @Resource annotated methods and fields
+ injectResourceAnnotatedMethods(instance, ctx);
+ injectResourceAnnotatedFields(instance, ctx);
+
+ // inject @EJB annotated methods and fields
+ injectEJBAnnotatedMethods(instance, ctx);
+ injectEJBAnnotatedFields(instance, ctx);
+ }
+
+ /**
+ * Performs descriptor driven injections.
+ * @param instance to operate on
+ * @param ctx JNDI context
+ * @param envPrefix env prefix to be used
+ * @param injectionMD injections metadata
+ */
+ private static void injectDescriptorDrivenInjections(final Object instance, final
Context ctx, final InjectionMetaData injectionMD)
+ {
+ Method method = getMethod(injectionMD, instance.getClass());
+ if (method != null)
+ {
+ try
{
+ inject(instance, method, injectionMD.getEnvEntryName(), ctx);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Cannot inject method (descriptor driven injection): " +
injectionMD, e);
+ }
+ }
+ else
+ {
+ Field field = getField(injectionMD, instance.getClass());
+ if (field != null)
+ {
try
{
- inject(instance, method, injectionMD.getEnvEntryName(), ctx, envPrefix);
+ inject(instance, field, injectionMD.getEnvEntryName(), ctx);
}
catch (Exception e)
{
- LOG.warn("Cannot inject method (descriptor driven injection): "
+ injectionMD, e);
+ LOG.error("Cannot inject field (descriptor driven injection): "
+ injectionMD, e);
}
}
else
{
- Field field = getField(injectionMD, instanceClass);
- if (field != null)
- {
- try
- {
- inject(instance, field, injectionMD.getEnvEntryName(), ctx,
envPrefix);
- }
- catch (Exception e)
- {
- LOG.warn("Cannot inject field (descriptor driven injection):
" + injectionMD, e);
- }
- }
- else
- {
- LOG.warn("Cannot find injection target for: " + injectionMD);
- }
+ LOG.error("Cannot find injection target for: " + injectionMD);
}
}
+ }
- // inject @Resource annotated methods
- Collection<Method> resourceAnnotatedMethods =
RESOURCE_METHOD_FINDER.process(instanceClass);
+ /**
+ * Injects @Resource annotated fields
+ * @param instance to operate on
+ * @param ctx JNDI context
+ * @param envPrefix environment prefix to be used
+ */
+ private static void injectResourceAnnotatedFields(final Object instance, final Context
ctx)
+ {
+ Collection<Field> resourceAnnotatedFields =
RESOURCE_FIELD_FINDER.process(instance.getClass());
+ for (Field field : resourceAnnotatedFields)
+ {
+ try
+ {
+ inject(instance, field, field.getAnnotation(Resource.class).name(), ctx);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Cannot inject field annotated with @Resource annotation:
" + field, e);
+ }
+ }
+ }
+
+ /**
+ * Injects @Resource annotated methods
+ * @param instance to operate on
+ * @param ctx JNDI context
+ * @param envPrefix environment prefix to be used
+ */
+ private static void injectResourceAnnotatedMethods(final Object instance, final
Context ctx)
+ {
+ Collection<Method> resourceAnnotatedMethods =
RESOURCE_METHOD_FINDER.process(instance.getClass());
for(Method method : resourceAnnotatedMethods)
{
try
{
- inject(instance, method, method.getAnnotation(Resource.class).name(), ctx,
envPrefix);
+ inject(instance, method, method.getAnnotation(Resource.class).name(), ctx);
}
catch (Exception e)
{
- LOG.warn("Cannot inject @Resource annotated method: " + method,
e);
+ LOG.error("Cannot inject method annotated with @Resource annotation:
" + method, e);
}
}
+ }
- // inject @Resource annotated fields
- Collection<Field> resourceAnnotatedFields =
RESOURCE_FIELD_FINDER.process(instanceClass);
- for (Field field : resourceAnnotatedFields)
+ /**
+ * Injects @EJB annotated fields
+ * @param instance to operate on
+ * @param ctx JNDI context
+ * @param envPrefix environment prefix to be used
+ */
+ private static void injectEJBAnnotatedFields(final Object instance, final Context
ctx)
+ {
+ final Collection<Field> ejbAnnotatedFields =
EJB_FIELD_FINDER.process(instance.getClass());
+ for (Field field : ejbAnnotatedFields)
{
try
{
- inject(instance, field, field.getAnnotation(Resource.class).name(), ctx,
envPrefix);
+ inject(instance, field, field.getAnnotation(EJB.class).name(), ctx);
}
catch (Exception e)
{
- LOG.warn("Cannot inject @Resource annotated field: " + field, e);
+ LOG.error("Cannot inject field annotated with @EJB annotation: " +
field, e);
}
}
}
+ /**
+ * Injects @EJB annotated methods
+ * @param instance to operate on
+ * @param ctx JNDI context
+ * @param envPrefix environment prefix to be used
+ */
+ private static void injectEJBAnnotatedMethods(final Object instance, final Context
ctx)
+ {
+ final Collection<Method> ejbAnnotatedMethods =
EJB_METHOD_FINDER.process(instance.getClass());
+ for(Method method : ejbAnnotatedMethods)
+ {
+ try
+ {
+ inject(instance, method, method.getAnnotation(EJB.class).name(), ctx);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Cannot inject method annotated with @EJB annotation: " +
method, e);
+ }
+ }
+ }
+
public static void injectWebServiceContext(final Object instance, final
WebServiceContext ctx)
{
final Class<?> instanceClass = instance.getClass();
@@ -168,7 +258,7 @@
}
catch (Exception e)
{
- LOG.warn("Cannot inject @Resource annotated method: " + method,
e);
+ LOG.error("Cannot inject @Resource annotated method: " + method,
e);
}
}
@@ -182,7 +272,7 @@
}
catch (Exception e)
{
- LOG.warn("Cannot inject @Resource annotated field: " + field, e);
+ LOG.error("Cannot inject @Resource annotated field: " + field, e);
}
}
}
@@ -212,7 +302,7 @@
}
catch (Exception e)
{
- LOG.warn("Calling of @PostConstruct annotated method failed: " +
method, e);
+ LOG.error("Calling of @PostConstruct annotated method failed: " +
method, e);
}
}
}
@@ -242,7 +332,7 @@
}
catch (Exception e)
{
- LOG.warn("Calling of @PreDestroy annotated method failed: " +
method, e);
+ LOG.error("Calling of @PreDestroy annotated method failed: " +
method, e);
}
}
}
@@ -258,11 +348,11 @@
* @throws Exception if any error occurs
* @see org.jboss.wsf.common.javax.finders.ResourceMethodFinder
*/
- private static void inject(final Object instance, final Method method, final String
resourceName, final Context ctx, final String envPrefix)
+ private static void inject(final Object instance, final Method method, final String
resourceName, final Context ctx)
throws Exception
{
final String beanName = convertToBeanName(method.getName());
- final Object value = ctx.lookup(envPrefix + getName(resourceName, beanName));
+ final Object value = ctx.lookup(getName(resourceName, beanName));
LOG.debug("Injecting method: " + method);
invokeMethod(instance, method, new Object[] {value});
@@ -279,11 +369,11 @@
* @throws Exception if any error occurs
* @see org.jboss.wsf.common.javax.finders.ResourceFieldFinder
*/
- private static void inject(final Object instance, final Field field, final String
resourceName, final Context ctx, final String envPrefix)
+ private static void inject(final Object instance, final Field field, final String
resourceName, final Context ctx)
throws Exception
{
final String beanName = field.getName();
- final Object value = ctx.lookup(envPrefix + getName(resourceName, beanName));
+ final Object value = ctx.lookup(getName(resourceName, beanName));
LOG.debug("Injecting field: " + field);
setField(instance, field, value);
Added: common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBFieldFinder.java
===================================================================
--- common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBFieldFinder.java
(rev 0)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBFieldFinder.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.wsf.common.javax.finders;
+
+import java.lang.reflect.Field;
+
+import javax.ejb.EJB;
+
+import org.jboss.wsf.common.reflection.AnnotatedFieldFinder;
+
+/**
+ * Field based EJB injection.
+ *
+ * @author ropalka(a)redhat.com
+ */
+public final class EJBFieldFinder
+extends AnnotatedFieldFinder<EJB>
+{
+
+ /**
+ * Constructor.
+ */
+ public EJBFieldFinder()
+ {
+ super(EJB.class);
+ }
+
+ @Override
+ public void validate(Field field)
+ {
+ super.validate(field);
+
+ // Ensure all method preconditions
+ Class<EJB> annotation = getAnnotation();
+ ReflectionUtils.assertNotVoidType(field, annotation);
+ ReflectionUtils.assertNotStatic(field, annotation);
+ ReflectionUtils.assertNotFinal(field, annotation);
+ ReflectionUtils.assertNotPrimitiveType(field, annotation);
+ }
+
+}
Added: common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBMethodFinder.java
===================================================================
--- common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBMethodFinder.java
(rev 0)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/EJBMethodFinder.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.wsf.common.javax.finders;
+
+import java.lang.reflect.Method;
+
+import javax.ejb.EJB;
+
+import org.jboss.wsf.common.reflection.AnnotatedMethodFinder;
+
+/**
+ * Setter based EJB injection.
+ *
+ * @author ropalka(a)redhat.com
+ */
+public final class EJBMethodFinder
+extends AnnotatedMethodFinder<EJB>
+{
+
+ /**
+ * Constructor.
+ */
+ public EJBMethodFinder()
+ {
+ super(EJB.class);
+ }
+
+ @Override
+ public void validate(Method method)
+ {
+ super.validate(method);
+
+ // Ensure all method preconditions
+ Class<EJB> annotation = getAnnotation();
+ ReflectionUtils.assertVoidReturnType(method, annotation);
+ ReflectionUtils.assertOneParameter(method, annotation);
+ ReflectionUtils.assertNoPrimitiveParameters(method, annotation);
+ ReflectionUtils.assertValidSetterName(method, annotation);
+ ReflectionUtils.assertNoCheckedExceptionsAreThrown(method, annotation);
+ ReflectionUtils.assertNotStatic(method, annotation);
+ }
+
+}
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/InjectionFieldFinder.java
===================================================================
---
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/InjectionFieldFinder.java 2009-04-30
10:07:08 UTC (rev 9925)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/InjectionFieldFinder.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -93,6 +93,7 @@
ReflectionUtils.assertNotVoidType(field);
ReflectionUtils.assertNotStatic(field);
+ ReflectionUtils.assertNotFinal(field);
ReflectionUtils.assertNotPrimitiveType(field);
}
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ReflectionUtils.java
===================================================================
---
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ReflectionUtils.java 2009-04-30
10:07:08 UTC (rev 9925)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ReflectionUtils.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -232,7 +232,7 @@
throw new RuntimeException("Field " + getAnnotationMessage(annotation)
+ "cannot be static: " + field);
}
}
-
+
/**
* Asserts field is not static.
*
@@ -244,6 +244,30 @@
}
/**
+ * Asserts field is not final.
+ *
+ * @param field to validate
+ * @param annotation annotation to propagate in exception message
+ */
+ public static void assertNotFinal(final Field field, Class<? extends Annotation>
annotation)
+ {
+ if (Modifier.isFinal(field.getModifiers()))
+ {
+ throw new RuntimeException("Field " + getAnnotationMessage(annotation)
+ "cannot be final: " + field);
+ }
+ }
+
+ /**
+ * Asserts field is not final.
+ *
+ * @param field to validate
+ */
+ public static void assertNotFinal(final Field field)
+ {
+ assertNotFinal(field, null);
+ }
+
+ /**
* Asserts method have exactly one parameter.
*
* @param method to validate
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceFieldFinder.java
===================================================================
---
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceFieldFinder.java 2009-04-30
10:07:08 UTC (rev 9925)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceFieldFinder.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -30,7 +30,7 @@
import org.jboss.wsf.common.reflection.AnnotatedFieldFinder;
/**
- * Field based injection.
+ * Field based resource injection.
*
* To access a resource a developer declares a setter method and annotates it as being a
* resource reference. The name and type of resource maybe inferred by inspecting the
@@ -83,6 +83,7 @@
Class<Resource> annotation = getAnnotation();
ReflectionUtils.assertNotVoidType(field, annotation);
ReflectionUtils.assertNotStatic(field, annotation);
+ ReflectionUtils.assertNotFinal(field, annotation);
ReflectionUtils.assertNotPrimitiveType(field, annotation);
}
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceMethodFinder.java
===================================================================
---
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceMethodFinder.java 2009-04-30
10:07:08 UTC (rev 9925)
+++
common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceMethodFinder.java 2009-04-30
10:09:29 UTC (rev 9926)
@@ -29,7 +29,7 @@
import org.jboss.wsf.common.reflection.AnnotatedMethodFinder;
/**
- * Setter based injection.
+ * Setter based resource injection.
*
* To access a resource a developer declares a setter method and annotates it as being a
* resource reference. The name and type of resource maybe inferred by inspecting the