aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.util; aoqi@0: aoqi@0: import java.lang.reflect.Field; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Modifier; aoqi@0: import java.security.AccessController; aoqi@0: import java.security.PrivilegedAction; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Collection; aoqi@0: import java.util.List; aoqi@0: import java.util.concurrent.Callable; aoqi@0: aoqi@0: import javax.annotation.Resource; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: aoqi@0: /** aoqi@0: * Encapsulates which field/method the injection is done, and performs the aoqi@0: * injection. aoqi@0: */ aoqi@0: public abstract class InjectionPlan { aoqi@0: /** aoqi@0: * Perform injection aoqi@0: * aoqi@0: * @param instance aoqi@0: * Instance aoqi@0: * @param resource aoqi@0: * Resource aoqi@0: */ aoqi@0: public abstract void inject(T instance, R resource); aoqi@0: aoqi@0: /** aoqi@0: * Perform injection, but resource is only generated if injection is aoqi@0: * necessary. aoqi@0: * aoqi@0: * @param instance aoqi@0: * @param resource aoqi@0: */ aoqi@0: public void inject(T instance, Callable resource) { aoqi@0: try { aoqi@0: inject(instance, resource.call()); aoqi@0: } catch(Exception e) { aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Injects to a field. aoqi@0: */ aoqi@0: public static class FieldInjectionPlan extends aoqi@0: InjectionPlan { aoqi@0: private final Field field; aoqi@0: aoqi@0: public FieldInjectionPlan(Field field) { aoqi@0: this.field = field; aoqi@0: } aoqi@0: aoqi@0: public void inject(final T instance, final R resource) { aoqi@0: AccessController.doPrivileged(new PrivilegedAction() { aoqi@0: public Object run() { aoqi@0: try { aoqi@0: if (!field.isAccessible()) { aoqi@0: field.setAccessible(true); aoqi@0: } aoqi@0: field.set(instance, resource); aoqi@0: return null; aoqi@0: } catch (IllegalAccessException e) { aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Injects to a method. aoqi@0: */ aoqi@0: public static class MethodInjectionPlan extends aoqi@0: InjectionPlan { aoqi@0: private final Method method; aoqi@0: aoqi@0: public MethodInjectionPlan(Method method) { aoqi@0: this.method = method; aoqi@0: } aoqi@0: aoqi@0: public void inject(T instance, R resource) { aoqi@0: invokeMethod(method, instance, resource); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Helper for invoking a method with elevated privilege. aoqi@0: */ aoqi@0: private static void invokeMethod(final Method method, final Object instance, final Object... args) { aoqi@0: if(method==null) return; aoqi@0: AccessController.doPrivileged(new PrivilegedAction() { aoqi@0: public Void run() { aoqi@0: try { aoqi@0: if (!method.isAccessible()) { aoqi@0: method.setAccessible(true); aoqi@0: } aoqi@0: method.invoke(instance,args); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: throw new WebServiceException(e); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Combines multiple {@link InjectionPlan}s into one. aoqi@0: */ aoqi@0: private static class Compositor extends InjectionPlan { aoqi@0: private final Collection> children; aoqi@0: aoqi@0: public Compositor(Collection> children) { aoqi@0: this.children = children; aoqi@0: } aoqi@0: aoqi@0: public void inject(T instance, R res) { aoqi@0: for (InjectionPlan plan : children) aoqi@0: plan.inject(instance, res); aoqi@0: } aoqi@0: aoqi@0: public void inject(T instance, Callable resource) { aoqi@0: if (!children.isEmpty()) { aoqi@0: super.inject(instance, resource); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Creates an {@link InjectionPlan} that injects the given resource type to the given class. aoqi@0: * aoqi@0: * @param isStatic aoqi@0: * Only look for static field/method aoqi@0: * aoqi@0: */ aoqi@0: public static aoqi@0: InjectionPlan buildInjectionPlan(Class clazz, Class resourceType, boolean isStatic) { aoqi@0: List> plan = new ArrayList>(); aoqi@0: aoqi@0: Class cl = clazz; aoqi@0: while(cl != Object.class) { aoqi@0: for(Field field: cl.getDeclaredFields()) { aoqi@0: Resource resource = field.getAnnotation(Resource.class); aoqi@0: if (resource != null) { aoqi@0: if(isInjectionPoint(resource, field.getType(), aoqi@0: "Incorrect type for field"+field.getName(), aoqi@0: resourceType)) { aoqi@0: aoqi@0: if(isStatic && !Modifier.isStatic(field.getModifiers())) aoqi@0: throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+field); aoqi@0: aoqi@0: plan.add(new FieldInjectionPlan(field)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: cl = cl.getSuperclass(); aoqi@0: } aoqi@0: aoqi@0: cl = clazz; aoqi@0: while(cl != Object.class) { aoqi@0: for(Method method : cl.getDeclaredMethods()) { aoqi@0: Resource resource = method.getAnnotation(Resource.class); aoqi@0: if (resource != null) { aoqi@0: Class[] paramTypes = method.getParameterTypes(); aoqi@0: if (paramTypes.length != 1) aoqi@0: throw new WebServiceException("Incorrect no of arguments for method "+method); aoqi@0: if(isInjectionPoint(resource,paramTypes[0], aoqi@0: "Incorrect argument types for method"+method.getName(), aoqi@0: resourceType)) { aoqi@0: aoqi@0: if(isStatic && !Modifier.isStatic(method.getModifiers())) aoqi@0: throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+method); aoqi@0: aoqi@0: plan.add(new MethodInjectionPlan(method)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: cl = cl.getSuperclass(); aoqi@0: } aoqi@0: aoqi@0: return new Compositor(plan); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Returns true if the combination of {@link Resource} and the field/method type aoqi@0: * are consistent for {@link WebServiceContext} injection. aoqi@0: */ aoqi@0: private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) { aoqi@0: Class t = resource.type(); aoqi@0: if (t.equals(Object.class)) { aoqi@0: return fieldType.equals(resourceType); aoqi@0: } else if (t.equals(resourceType)) { aoqi@0: if (fieldType.isAssignableFrom(resourceType)) { aoqi@0: return true; aoqi@0: } else { aoqi@0: // type compatibility error aoqi@0: throw new WebServiceException(errorMessage); aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }