Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.ws.common.injection;
 
 
 
An injection helper class for javax.* annotations.

Author(s):
Richard Opalka
 
 public final class InjectionHelper
 {
    private static final ClassProcessor<MethodPRE_DESTROY_METHOD_FINDER = new PreDestroyMethodFinder();
    private static final ClassProcessor<MethodWEB_SERVICE_CONTEXT_METHOD_FINDER = new ResourceMethodFinder(WebServiceContext.classtrue);
    private static final ClassProcessor<FieldWEB_SERVICE_CONTEXT_FIELD_FINDER = new ResourceFieldFinder(WebServiceContext.classtrue);

   
Forbidden constructor.
 
    private InjectionHelper()
    {
       super();
    }

   
Injects

Parameters:
instance to operate on
ctx current web service context
Resource:
annotated accessible objects referencing WebServiceContext.
 
    public static void injectWebServiceContext(final Object instancefinal WebServiceContext ctx)
    {
       final Class<?> instanceClass = instance.getClass();
 
       // inject @Resource annotated methods accepting WebServiceContext parameter
       Collection<MethodresourceAnnotatedMethods = .process(instanceClass);
       for(Method method : resourceAnnotatedMethods)
       {
          try
          {
             invokeMethod(instancemethodnew Object[] {ctx});
          }
          catch (Exception e)
          {
             final String message = "Cannot inject @Resource annotated method: " + method;
             InjectionException.rethrow(messagee);
          }
       }
 
       // inject @Resource annotated fields of WebServiceContext type
       final Collection<FieldresourceAnnotatedFields = .process(instanceClass);
       for (Field field : resourceAnnotatedFields)
       {
          try
          {
             setField(instancefieldctx);
          }
          catch (Exception e)
          {
             final String message = "Cannot inject @Resource annotated field: " + field;
             InjectionException.rethrow(messagee);
          }
       }
    }

   
Calls

Parameters:
instance to invoke @PostConstruct annotated method on
See also:
org.jboss.ws.common.injection.finders.PostConstructMethodFinder
javax.annotation.PostConstruct
PostConstruct:
annotated method if exists.
   public static void callPostConstructMethod(final Object instance)
   {
      if (instance == null)
         throw new IllegalArgumentException();
      final Collection<Methodmethods = .process(instance.getClass());
      if (methods.size() > 0)
      {
         final Method method = methods.iterator().next();
         try
         {
            invokeMethod(instancemethodnull);
         }
         catch (Exception e)
         {
            final String message = "Calling of @PostConstruct annotated method failed: " + method;
            InjectionException.rethrow(messagee);
         }
      }
   }

   
Calls

Parameters:
instance to invoke @PreDestroy annotated method on
See also:
org.jboss.ws.common.injection.finders.PreDestroyMethodFinder
javax.annotation.PreDestroy
PreDestroy:
annotated method if exists.
   public static void callPreDestroyMethod(final Object instance)
   {
      if (instance == null)
         throw new IllegalArgumentException();
      final Collection<Methodmethods = .process(instance.getClass());
      if (methods.size() > 0)
      {
         final Method method = methods.iterator().next();
         try
         {
            invokeMethod(instancemethodnull);
         }
         catch (Exception e)
         {
            final String message = "Calling of @PreDestroy annotated method failed: " + method;
            InjectionException.rethrow(messagee);
         }
      }
   }

   
Invokes method on object with specified arguments.

Parameters:
instance to invoke method on
method method to invoke
args arguments to pass
   private static void invokeMethod(final Object instancefinal Method methodfinal Object[] args)
   {
      final boolean accessability = method.isAccessible();
      try
      {
         method.setAccessible(true);
         method.invoke(instanceargs);
      }
      catch (Exception e)
      {
         InjectionException.rethrow(e);
      }
      finally
      {
         method.setAccessible(accessability);
      }
   }

   
Sets field on object with specified value.

Parameters:
instance to set field on
field to set
value to be set
   private static void setField(final Object instancefinal Field fieldfinal Object value)
   {
      final boolean accessability = field.isAccessible();
      try
      {
         field.setAccessible(true);
         field.set(instancevalue);
      }
      catch (Exception e)
      {
         InjectionException.rethrow(e);
      }
      finally
      {
         field.setAccessible(accessability);
      }
   }
New to GrepCode? Check out our FAQ X