Home » geronimo-2.2-source-release » org.apache.geronimo.jaxws.annotations » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.jaxws.annotations;
   18   
   19   import java.lang.annotation.Annotation;
   20   import java.lang.reflect.Field;
   21   import java.lang.reflect.InvocationTargetException;
   22   import java.lang.reflect.Method;
   23   
   24   // TODO: Check for static methods and fields. They are only allowed for client apps.
   25   public abstract class InjectingAnnotationHandler implements AnnotationHandler {
   26   
   27       abstract public Object getAnnotationValue(Annotation annotation,
   28                                                 String name,
   29                                                 Class<?> type)
   30               throws InjectionException;
   31   
   32       public void processClassAnnotation(Object instance,
   33                                          Class clazz,
   34                                          Annotation annotation) {
   35           // injection is not done for annotations at class level
   36       }
   37   
   38       public String getJNDIName(Object instance, String name, Field field) {
   39           if (name != null && name.length() > 0) {
   40               return name;
   41           } else {
   42               return instance.getClass().getName() + "/" + field.getName();
   43           }
   44       }
   45   
   46       public String getJNDIName(Object instance, String name, Method method) {
   47           if (name != null && name.length() > 0) {
   48               return name;
   49           } else {
   50               String propName = method.getName();
   51               propName = propName.substring(3);
   52               propName = Character.toLowerCase(propName.charAt(0))
   53                       + propName.substring(1);
   54               return instance.getClass().getName() + "/" + propName;
   55           }
   56       }
   57   
   58       public Class<?> getType(Class<?> type, Field field) {
   59           return (type == null || Object.class == type) ? field.getType() : type;
   60       }
   61   
   62       public Class<?> getType(Class<?> type, Method method) {
   63           return (type == null || Object.class == type) ? method
   64                   .getParameterTypes()[0] : type;
   65       }
   66   
   67       protected void injectField(Object instance,
   68                                  Field field,
   69                                  Annotation annotation,
   70                                  String name,
   71                                  Class<?> type) throws InjectionException {
   72   
   73           String jndiName = getJNDIName(instance, name, field);
   74   
   75           Object lookedupResource = getAnnotationValue(annotation, jndiName,
   76                   getType(type, field));
   77   
   78           boolean accessibility = field.isAccessible();
   79           try {
   80               field.setAccessible(true);
   81               field.set(instance, lookedupResource);
   82           } catch (IllegalArgumentException e) {
   83               throw new InjectionException("Field injection failed", e);
   84           } catch (IllegalAccessException e) {
   85               throw new InjectionException("Field injection failed", e);
   86           } finally {
   87               field.setAccessible(accessibility);
   88           }
   89       }
   90   
   91       protected void injectMethod(Object instance,
   92                                   Method method,
   93                                   Annotation annotation,
   94                                   String name,
   95                                   Class<?> type) throws InjectionException {
   96   
   97           if (!method.getName().startsWith("set")
   98                   || method.getParameterTypes().length != 1
   99                   || !method.getReturnType().equals(Void.class)) {
  100               throw new IllegalArgumentException(
  101                       "Invalid method resource injection annotation");
  102           }
  103   
  104           String jndiName = getJNDIName(instance, name, method);
  105   
  106           Object lookedupResource = getAnnotationValue(annotation, jndiName,
  107                   getType(type, method));
  108   
  109           boolean accessibility = method.isAccessible();
  110           try {
  111               method.setAccessible(true);
  112               method.invoke(instance, lookedupResource);
  113           } catch (IllegalArgumentException e) {
  114               throw new InjectionException("Method injection failed", e);
  115           } catch (IllegalAccessException e) {
  116               throw new InjectionException("Method injection failed", e);
  117           } catch (InvocationTargetException e) {
  118               throw new InjectionException("Method injection failed", e);
  119           } finally {
  120               method.setAccessible(accessibility);
  121           }
  122       }
  123   
  124   }

Home » geronimo-2.2-source-release » org.apache.geronimo.jaxws.annotations » [javadoc | source]