[jbossws-commits] JBossWS SVN: r9717 - in common/trunk/src/main/java/org/jboss/wsf/common/javax: finders and 1 other directory.

jbossws-commits at lists.jboss.org jbossws-commits at lists.jboss.org
Fri Apr 3 09:34:04 EDT 2009


Author: richard.opalka at jboss.com
Date: 2009-04-03 09:34:04 -0400 (Fri, 03 Apr 2009)
New Revision: 9717

Modified:
   common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.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] adding support for WebServiceContext injection

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-03 12:15:35 UTC (rev 9716)
+++ common/trunk/src/main/java/org/jboss/wsf/common/javax/JavaxAnnotationHelper.java	2009-04-03 13:34:04 UTC (rev 9717)
@@ -27,6 +27,7 @@
 
 import javax.annotation.Resource;
 import javax.naming.InitialContext;
+import javax.xml.ws.WebServiceContext;
 
 import org.jboss.logging.Logger;
 import org.jboss.wsf.common.javax.finders.InjectionFieldFinder;
@@ -51,8 +52,10 @@
    private static final String 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();
-   private static final ClassProcessor<Field> RESOURCE_FIELD_FINDER = new ResourceFieldFinder();
+   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> 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);
    
    /**
     * Forbidden constructor.
@@ -91,20 +94,32 @@
             Method method = getMethod(injectionMD, instanceClass);
             if (method != null)
             {
-               // inject descriptor driven annotated method
-               inject(instance, method, injectionMD.getEnvEntryName(), ctx);
+               try
+               {
+                  inject(instance, method, injectionMD.getEnvEntryName(), ctx);
+               }
+               catch (Exception e)
+               {
+                  LOG.warn("Cannot inject method (descriptor driven injection): " + injectionMD, e);
+               }
             }
             else
             {
                Field field = getField(injectionMD, instanceClass);
                if (field != null)
                {
-                  // inject descriptor driven annotated field
-                  inject(instance, field, injectionMD.getEnvEntryName(), ctx);
+                  try
+                  {
+                     inject(instance, field, injectionMD.getEnvEntryName(), ctx);
+                  }
+                  catch (Exception e)
+                  {
+                     LOG.warn("Cannot inject field (descriptor driven injection): " + injectionMD, e);
+                  }
                }
                else
                {
-                  throw new RuntimeException("Cannot find injection target for: " + injectionMD);
+                  LOG.warn("Cannot find injection target for: " + injectionMD);
                }
             }
          }
@@ -114,17 +129,64 @@
       Collection<Method> resourceAnnotatedMethods = RESOURCE_METHOD_FINDER.process(instanceClass);
       for(Method method : resourceAnnotatedMethods)
       {
-         inject(instance, method, method.getAnnotation(Resource.class).name(), ctx);
+         try
+         {
+            inject(instance, method, method.getAnnotation(Resource.class).name(), ctx);
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Cannot inject @Resource annotated method: " + method, e);
+         }
       }
       
       // inject @Resource annotated fields
       Collection<Field> resourceAnnotatedFields = RESOURCE_FIELD_FINDER.process(instanceClass);
       for (Field field : resourceAnnotatedFields)
       {
-         inject(instance, field, field.getAnnotation(Resource.class).name(), ctx);
+         try
+         {
+            inject(instance, field, field.getAnnotation(Resource.class).name(), ctx);
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Cannot inject @Resource annotated field: " + field, e);
+         }
       }
    }
    
+   public static void injectWebServiceContext(Object instance, WebServiceContext ctx)
+   {
+      final Class<?> instanceClass = instance.getClass();
+      
+      // inject @Resource annotated methods accepting WebServiceContext parameter
+      Collection<Method> resourceAnnotatedMethods = WEB_SERVICE_CONTEXT_METHOD_FINDER.process(instanceClass);
+      for(Method method : resourceAnnotatedMethods)
+      {
+         try
+         {
+            invokeMethod(instance, method, new Object[] {ctx});
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Cannot inject @Resource annotated method: " + method, e);
+         }
+      }
+      
+      // inject @Resource annotated fields of WebServiceContext type
+      Collection<Field> resourceAnnotatedFields = WEB_SERVICE_CONTEXT_FIELD_FINDER.process(instanceClass);
+      for (Field field : resourceAnnotatedFields)
+      {
+         try
+         {
+            setField(instance, field, ctx);
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Cannot inject @Resource annotated field: " + field, e);
+         }
+      }
+   }
+   
    /**
     * Calls @PostConstruct annotated method if exists.
     * 
@@ -144,7 +206,14 @@
       {
          Method method = methods.iterator().next();
          LOG.debug("Calling @PostConstruct annotated method: " + method);
-         invokeMethod(instance, method, null);
+         try
+         {
+            invokeMethod(instance, method, null);
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Calling of @PostConstruct annotated method failed: " + method, e);
+         }
       }
    }
    
@@ -167,7 +236,14 @@
       {
          Method method = methods.iterator().next();
          LOG.debug("Calling @PreDestroy annotated method: " + method);
-         invokeMethod(instance, method, null);
+         try
+         {
+            invokeMethod(instance, method, null);
+         }
+         catch (Exception e)
+         {
+            LOG.warn("Calling of @PreDestroy annotated method failed: " + method, e);
+         }
       }
    }
    
@@ -249,11 +325,6 @@
          method.setAccessible(true);
          method.invoke(instance, args);
       }
-      catch (Exception e)
-      {
-         LOG.error(e.getMessage(), e);
-         throw e; // propagate
-      }
       finally
       {
          method.setAccessible(accessability);
@@ -277,11 +348,6 @@
          field.setAccessible(true);
          field.set(instance, value);
       }
-      catch (Exception e)
-      {
-         LOG.error(e.getMessage(), e);
-         throw e; // propagate
-      }
       finally
       {
          field.setAccessible(accessability);

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-03 12:15:35 UTC (rev 9716)
+++ common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceFieldFinder.java	2009-04-03 13:34:04 UTC (rev 9717)
@@ -48,11 +48,30 @@
 {
    
    /**
+    * Parameter type to accept/ignore.
+    */
+   private final Class<?> accept;
+   /**
+    * If <b>accept</b> field is not null then:
+    * <ul>
+    *   <li><b>true</b> means include only methods with <b>accept</b> parameter,
+    *   <li><b>false</b> means exclude all methods with <b>accept</b> parameter
+    * </ul> 
+    */
+   private final boolean include;
+   
+   /**
     * Constructor.
+    * 
+    * @param accept filtering class
+    * @param include whether include/exclude filtering class
     */
-   public ResourceFieldFinder()
+   public ResourceFieldFinder(final Class<?> accept, boolean include)
    {
       super(Resource.class);
+      
+      this.accept = accept;
+      this.include = include;
    }
 
    @Override
@@ -70,13 +89,22 @@
    @Override
    public boolean matches(Field field)
    {
-      if (super.matches(field))
+      final boolean matches = super.matches(field);
+      
+      if (matches)
       {
-         // don't match @Resource annotated fields of type WebServiceContext
-         return !field.getType().equals(WebServiceContext.class);
+         // processing @Resource annotated method
+         if (this.accept != null)
+         {
+            // filtering
+            final Class<?> fieldType = field.getType();
+            final boolean parameterMatch = this.accept.equals(fieldType);
+            // include/exclude filtering
+            return this.include ? parameterMatch : !parameterMatch;
+         }
       }
-      
-      return false;
+
+      return matches;
    }
 
 }

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-03 12:15:35 UTC (rev 9716)
+++ common/trunk/src/main/java/org/jboss/wsf/common/javax/finders/ResourceMethodFinder.java	2009-04-03 13:34:04 UTC (rev 9717)
@@ -47,11 +47,30 @@
 {
    
    /**
+    * Parameter type to accept/ignore.
+    */
+   private final Class<?> accept;
+   /**
+    * If <b>accept</b> field is not null then:
+    * <ul>
+    *   <li><b>true</b> means include only methods with <b>accept</b> parameter,
+    *   <li><b>false</b> means exclude all methods with <b>accept</b> parameter
+    * </ul> 
+    */
+   private final boolean include;
+   
+   /**
     * Constructor.
+    * 
+    * @param accept filtering class
+    * @param include whether include/exclude filtering class
     */
-   public ResourceMethodFinder()
+   public ResourceMethodFinder(final Class<?> accept, boolean include)
    {
       super(Resource.class);
+      
+      this.accept = accept;
+      this.include = include;
    }
 
    @Override
@@ -72,13 +91,25 @@
    @Override
    public boolean matches(Method method)
    {
-      if (super.matches(method))
+      final boolean matches = super.matches(method);
+      
+      if (matches)
       {
-         // don't match @Resource annotated methods accepting WebServiceContext parameter
-         return !method.getParameterTypes()[0].equals(WebServiceContext.class);
+         // processing @Resource annotated method
+         if (this.accept != null)
+         {
+            // filtering
+            if (method.getParameterTypes().length == 1)
+            {
+               final Class<?> param = method.getParameterTypes()[0];
+               final boolean parameterMatch = this.accept.equals(param);
+               // include/exclude filtering
+               return this.include ? parameterMatch : !parameterMatch;
+            }
+         }
       }
-      
-      return false;
+
+      return matches;
    }
 
 }




More information about the jbossws-commits mailing list