1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/InjectionPlan.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,233 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.util; 1.30 + 1.31 +import java.lang.reflect.Field; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.lang.reflect.Method; 1.34 +import java.lang.reflect.Modifier; 1.35 +import java.security.AccessController; 1.36 +import java.security.PrivilegedAction; 1.37 +import java.util.ArrayList; 1.38 +import java.util.Collection; 1.39 +import java.util.List; 1.40 +import java.util.concurrent.Callable; 1.41 + 1.42 +import javax.annotation.Resource; 1.43 +import javax.xml.ws.WebServiceException; 1.44 + 1.45 +/** 1.46 + * Encapsulates which field/method the injection is done, and performs the 1.47 + * injection. 1.48 + */ 1.49 +public abstract class InjectionPlan<T, R> { 1.50 + /** 1.51 + * Perform injection 1.52 + * 1.53 + * @param instance 1.54 + * Instance 1.55 + * @param resource 1.56 + * Resource 1.57 + */ 1.58 + public abstract void inject(T instance, R resource); 1.59 + 1.60 + /** 1.61 + * Perform injection, but resource is only generated if injection is 1.62 + * necessary. 1.63 + * 1.64 + * @param instance 1.65 + * @param resource 1.66 + */ 1.67 + public void inject(T instance, Callable<R> resource) { 1.68 + try { 1.69 + inject(instance, resource.call()); 1.70 + } catch(Exception e) { 1.71 + throw new WebServiceException(e); 1.72 + } 1.73 + } 1.74 + 1.75 + /* 1.76 + * Injects to a field. 1.77 + */ 1.78 + public static class FieldInjectionPlan<T, R> extends 1.79 + InjectionPlan<T, R> { 1.80 + private final Field field; 1.81 + 1.82 + public FieldInjectionPlan(Field field) { 1.83 + this.field = field; 1.84 + } 1.85 + 1.86 + public void inject(final T instance, final R resource) { 1.87 + AccessController.doPrivileged(new PrivilegedAction<Object>() { 1.88 + public Object run() { 1.89 + try { 1.90 + if (!field.isAccessible()) { 1.91 + field.setAccessible(true); 1.92 + } 1.93 + field.set(instance, resource); 1.94 + return null; 1.95 + } catch (IllegalAccessException e) { 1.96 + throw new WebServiceException(e); 1.97 + } 1.98 + } 1.99 + }); 1.100 + } 1.101 + } 1.102 + 1.103 + /* 1.104 + * Injects to a method. 1.105 + */ 1.106 + public static class MethodInjectionPlan<T, R> extends 1.107 + InjectionPlan<T, R> { 1.108 + private final Method method; 1.109 + 1.110 + public MethodInjectionPlan(Method method) { 1.111 + this.method = method; 1.112 + } 1.113 + 1.114 + public void inject(T instance, R resource) { 1.115 + invokeMethod(method, instance, resource); 1.116 + } 1.117 + } 1.118 + 1.119 + /* 1.120 + * Helper for invoking a method with elevated privilege. 1.121 + */ 1.122 + private static void invokeMethod(final Method method, final Object instance, final Object... args) { 1.123 + if(method==null) return; 1.124 + AccessController.doPrivileged(new PrivilegedAction<Void>() { 1.125 + public Void run() { 1.126 + try { 1.127 + if (!method.isAccessible()) { 1.128 + method.setAccessible(true); 1.129 + } 1.130 + method.invoke(instance,args); 1.131 + } catch (IllegalAccessException e) { 1.132 + throw new WebServiceException(e); 1.133 + } catch (InvocationTargetException e) { 1.134 + throw new WebServiceException(e); 1.135 + } 1.136 + return null; 1.137 + } 1.138 + }); 1.139 + } 1.140 + 1.141 + /* 1.142 + * Combines multiple {@link InjectionPlan}s into one. 1.143 + */ 1.144 + private static class Compositor<T, R> extends InjectionPlan<T, R> { 1.145 + private final Collection<InjectionPlan<T, R>> children; 1.146 + 1.147 + public Compositor(Collection<InjectionPlan<T, R>> children) { 1.148 + this.children = children; 1.149 + } 1.150 + 1.151 + public void inject(T instance, R res) { 1.152 + for (InjectionPlan<T, R> plan : children) 1.153 + plan.inject(instance, res); 1.154 + } 1.155 + 1.156 + public void inject(T instance, Callable<R> resource) { 1.157 + if (!children.isEmpty()) { 1.158 + super.inject(instance, resource); 1.159 + } 1.160 + } 1.161 + } 1.162 + 1.163 + /* 1.164 + * Creates an {@link InjectionPlan} that injects the given resource type to the given class. 1.165 + * 1.166 + * @param isStatic 1.167 + * Only look for static field/method 1.168 + * 1.169 + */ 1.170 + public static <T,R> 1.171 + InjectionPlan<T,R> buildInjectionPlan(Class<? extends T> clazz, Class<R> resourceType, boolean isStatic) { 1.172 + List<InjectionPlan<T,R>> plan = new ArrayList<InjectionPlan<T,R>>(); 1.173 + 1.174 + Class<?> cl = clazz; 1.175 + while(cl != Object.class) { 1.176 + for(Field field: cl.getDeclaredFields()) { 1.177 + Resource resource = field.getAnnotation(Resource.class); 1.178 + if (resource != null) { 1.179 + if(isInjectionPoint(resource, field.getType(), 1.180 + "Incorrect type for field"+field.getName(), 1.181 + resourceType)) { 1.182 + 1.183 + if(isStatic && !Modifier.isStatic(field.getModifiers())) 1.184 + throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+field); 1.185 + 1.186 + plan.add(new FieldInjectionPlan<T,R>(field)); 1.187 + } 1.188 + } 1.189 + } 1.190 + cl = cl.getSuperclass(); 1.191 + } 1.192 + 1.193 + cl = clazz; 1.194 + while(cl != Object.class) { 1.195 + for(Method method : cl.getDeclaredMethods()) { 1.196 + Resource resource = method.getAnnotation(Resource.class); 1.197 + if (resource != null) { 1.198 + Class[] paramTypes = method.getParameterTypes(); 1.199 + if (paramTypes.length != 1) 1.200 + throw new WebServiceException("Incorrect no of arguments for method "+method); 1.201 + if(isInjectionPoint(resource,paramTypes[0], 1.202 + "Incorrect argument types for method"+method.getName(), 1.203 + resourceType)) { 1.204 + 1.205 + if(isStatic && !Modifier.isStatic(method.getModifiers())) 1.206 + throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+method); 1.207 + 1.208 + plan.add(new MethodInjectionPlan<T,R>(method)); 1.209 + } 1.210 + } 1.211 + } 1.212 + cl = cl.getSuperclass(); 1.213 + } 1.214 + 1.215 + return new Compositor<T,R>(plan); 1.216 + } 1.217 + 1.218 + /* 1.219 + * Returns true if the combination of {@link Resource} and the field/method type 1.220 + * are consistent for {@link WebServiceContext} injection. 1.221 + */ 1.222 + private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) { 1.223 + Class t = resource.type(); 1.224 + if (t.equals(Object.class)) { 1.225 + return fieldType.equals(resourceType); 1.226 + } else if (t.equals(resourceType)) { 1.227 + if (fieldType.isAssignableFrom(resourceType)) { 1.228 + return true; 1.229 + } else { 1.230 + // type compatibility error 1.231 + throw new WebServiceException(errorMessage); 1.232 + } 1.233 + } 1.234 + return false; 1.235 + } 1.236 +}