001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.jaxws.builder;
018    
019    import java.lang.reflect.Field;
020    import java.lang.reflect.Method;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.annotation.Resource;
025    import javax.xml.ws.WebServiceContext;
026    
027    import org.apache.geronimo.j2ee.annotation.Holder;
028    import org.apache.geronimo.j2ee.annotation.Injection;
029    import org.apache.xbean.finder.ClassFinder;
030    
031    public class WebServiceContextAnnotationHelper {
032          
033        public static final String RELATIVE_JNDI_NAME = "env/WebServiceContext";
034        public static final String ABSOLUTE_JNDI_NAME = "java:comp/" + RELATIVE_JNDI_NAME;
035        
036        public static void addWebServiceContextInjections(Holder holder, Class clazz) {
037            List<Class> classes = new ArrayList<Class>();
038            while (clazz != Object.class) {
039                classes.add(clazz);
040                clazz = clazz.getSuperclass();
041            }
042            addWebServiceContextInjections(holder, new ClassFinder(classes));
043        }
044        
045        public static void addWebServiceContextInjections(Holder holder, ClassFinder finder) {        
046            List<Field> fields = finder.findAnnotatedFields(Resource.class);
047            for (Field field : fields) {
048                Resource resource = (Resource) field.getAnnotation(Resource.class);
049                Class type = getInjectionType(resource.type(), null, field);
050                if (WebServiceContext.class == type) {
051                    holder.addInjection(field.getDeclaringClass().getName(), 
052                                        new Injection(field.getDeclaringClass().getName(), getInjectionName(null, field), ABSOLUTE_JNDI_NAME));
053                }
054            }
055            List<Method> methods = finder.findAnnotatedMethods(Resource.class);
056            for (Method method : methods) {
057                Resource resource = (Resource) method.getAnnotation(Resource.class);
058                Class type = getInjectionType(resource.type(), method, null);
059                if (WebServiceContext.class == type) {
060                    holder.addInjection(method.getDeclaringClass().getName(), 
061                                        new Injection(method.getDeclaringClass().getName(), getInjectionName(method, null), ABSOLUTE_JNDI_NAME));
062                }            
063            }
064        }
065        
066        private static Class<?> getInjectionType(Class<?> type, Method method, Field field) {
067            if (type == null || Object.class == type) {
068                if (field != null) {
069                    return field.getType();
070                } else if (method != null) {
071                    return method.getParameterTypes()[0];
072                } else {
073                    throw new IllegalArgumentException("You must supply exactly one of Method, Field");
074                }
075            } else {
076                return type;
077            }
078        }
079        
080        private static String getInjectionName(Method method, Field field) {
081            if (method != null) {
082                String injectionJavaType = method.getName().substring(3);
083                StringBuilder stringBuilder = new StringBuilder(injectionJavaType);
084                stringBuilder.setCharAt(0, Character.toLowerCase(stringBuilder.charAt(0)));
085                return stringBuilder.toString();
086            } else if (field != null) {
087                return field.getName();
088            } else {
089                throw new IllegalArgumentException("You must supply exactly one of Method, Field");
090            }
091        }
092    
093    }