/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.geronimo.jaxws.builder;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

import org.apache.geronimo.j2ee.annotation.Holder;
import org.apache.geronimo.j2ee.annotation.Injection;
import org.apache.xbean.finder.ClassFinder;

public class WebServiceContextAnnotationHelper {
      
    public static final String RELATIVE_JNDI_NAME = "env/WebServiceContext";
    public static final String ABSOLUTE_JNDI_NAME = "java:comp/" + RELATIVE_JNDI_NAME;
    
    public static void addWebServiceContextInjections(Holder holder, Class<?> clazz) {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        while (clazz != Object.class) {
            classes.add(clazz);
            clazz = clazz.getSuperclass();
        }
        addWebServiceContextInjections(holder, new ClassFinder(classes));
    }
    
    public static void addWebServiceContextInjections(Holder holder, ClassFinder finder) {        
        List<Field> fields = finder.findAnnotatedFields(Resource.class);
        for (Field field : fields) {
            Resource resource = (Resource) field.getAnnotation(Resource.class);
            Class type = getInjectionType(resource.type(), null, field);
            if (WebServiceContext.class == type) {
                holder.addInjection(field.getDeclaringClass().getName(), 
                                    new Injection(field.getDeclaringClass().getName(), getInjectionName(null, field), ABSOLUTE_JNDI_NAME));
            }
        }
        List<Method> methods = finder.findAnnotatedMethods(Resource.class);
        for (Method method : methods) {
            Resource resource = (Resource) method.getAnnotation(Resource.class);
            Class type = getInjectionType(resource.type(), method, null);
            if (WebServiceContext.class == type) {
                holder.addInjection(method.getDeclaringClass().getName(), 
                                    new Injection(method.getDeclaringClass().getName(), getInjectionName(method, null), ABSOLUTE_JNDI_NAME));
            }            
        }
    }
    
    private static Class<?> getInjectionType(Class<?> type, Method method, Field field) {
        if (type == null || Object.class == type) {
            if (field != null) {
                return field.getType();
            } else if (method != null) {
                return method.getParameterTypes()[0];
            } else {
                throw new IllegalArgumentException("You must supply exactly one of Method, Field");
            }
        } else {
            return type;
        }
    }
    
    private static String getInjectionName(Method method, Field field) {
        if (method != null) {
            String injectionJavaType = method.getName().substring(3);
            StringBuilder stringBuilder = new StringBuilder(injectionJavaType);
            stringBuilder.setCharAt(0, Character.toLowerCase(stringBuilder.charAt(0)));
            return stringBuilder.toString();
        } else if (field != null) {
            return field.getName();
        } else {
            throw new IllegalArgumentException("You must supply exactly one of Method, Field");
        }
    }

}

@@ -1241,22 +1241,16 @@
 elper %7B%0A
-      
 %0A    pub
@@ -1273,24 +1273,42 @@
  String 
-RELATIVE
+BINDING_WEBSERVICE_CONTEXR
 _JNDI_NA
@@ -1313,16 +1313,21 @@
 NAME = %22
+comp/
 env/WebS
@@ -1346,99 +1346,8 @@
 t%22;%0A
-    public static final String ABSOLUTE_JNDI_NAME = %22java:comp/%22 + RELATIVE_JNDI_NAME;%0A    
 %0A   
@@ -1691,28 +1691,24 @@
 es));%0A    %7D%0A
-    
 %0A    public 
@@ -1786,24 +1786,16 @@
 inder) %7B
-        
 %0A       
@@ -1928,27 +1928,16 @@
 source =
- (Resource)
  field.g
@@ -2154,33 +2154,32 @@
 ass().getName(),
- 
 %0A               
@@ -2281,24 +2281,42 @@
 field), 
-ABSOLUTE
+BINDING_WEBSERVICE_CONTEXR
 _JNDI_NA
@@ -2496,19 +2496,8 @@
 ce =
- (Resource)
  met
@@ -2729,17 +2729,16 @@
 tName(),
- 
 %0A       
@@ -2854,16 +2854,34 @@
 l), 
-ABSOLUTE
+BINDING_WEBSERVICE_CONTEXR
 _JND
@@ -2903,28 +2903,16 @@
        %7D
-            
 %0A       
@@ -2912,37 +2912,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     private stat
@@ -3424,20 +3424,16 @@
 %7D%0A    %7D%0A
-    
 %0A    pri

Update WebServiceContext JNDI name