Fri, 14 Feb 2014 11:13:45 +0100
8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Field; |
aoqi@0 | 29 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 30 | import java.lang.reflect.Method; |
aoqi@0 | 31 | import java.lang.reflect.Modifier; |
aoqi@0 | 32 | import java.security.AccessController; |
aoqi@0 | 33 | import java.security.PrivilegedAction; |
aoqi@0 | 34 | import java.util.ArrayList; |
aoqi@0 | 35 | import java.util.Collection; |
aoqi@0 | 36 | import java.util.List; |
aoqi@0 | 37 | import java.util.concurrent.Callable; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import javax.annotation.Resource; |
aoqi@0 | 40 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * Encapsulates which field/method the injection is done, and performs the |
aoqi@0 | 44 | * injection. |
aoqi@0 | 45 | */ |
aoqi@0 | 46 | public abstract class InjectionPlan<T, R> { |
aoqi@0 | 47 | /** |
aoqi@0 | 48 | * Perform injection |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * @param instance |
aoqi@0 | 51 | * Instance |
aoqi@0 | 52 | * @param resource |
aoqi@0 | 53 | * Resource |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | public abstract void inject(T instance, R resource); |
aoqi@0 | 56 | |
aoqi@0 | 57 | /** |
aoqi@0 | 58 | * Perform injection, but resource is only generated if injection is |
aoqi@0 | 59 | * necessary. |
aoqi@0 | 60 | * |
aoqi@0 | 61 | * @param instance |
aoqi@0 | 62 | * @param resource |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | public void inject(T instance, Callable<R> resource) { |
aoqi@0 | 65 | try { |
aoqi@0 | 66 | inject(instance, resource.call()); |
aoqi@0 | 67 | } catch(Exception e) { |
aoqi@0 | 68 | throw new WebServiceException(e); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | /* |
aoqi@0 | 73 | * Injects to a field. |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public static class FieldInjectionPlan<T, R> extends |
aoqi@0 | 76 | InjectionPlan<T, R> { |
aoqi@0 | 77 | private final Field field; |
aoqi@0 | 78 | |
aoqi@0 | 79 | public FieldInjectionPlan(Field field) { |
aoqi@0 | 80 | this.field = field; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | public void inject(final T instance, final R resource) { |
aoqi@0 | 84 | AccessController.doPrivileged(new PrivilegedAction<Object>() { |
aoqi@0 | 85 | public Object run() { |
aoqi@0 | 86 | try { |
aoqi@0 | 87 | if (!field.isAccessible()) { |
aoqi@0 | 88 | field.setAccessible(true); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | field.set(instance, resource); |
aoqi@0 | 91 | return null; |
aoqi@0 | 92 | } catch (IllegalAccessException e) { |
aoqi@0 | 93 | throw new WebServiceException(e); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | }); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | /* |
aoqi@0 | 101 | * Injects to a method. |
aoqi@0 | 102 | */ |
aoqi@0 | 103 | public static class MethodInjectionPlan<T, R> extends |
aoqi@0 | 104 | InjectionPlan<T, R> { |
aoqi@0 | 105 | private final Method method; |
aoqi@0 | 106 | |
aoqi@0 | 107 | public MethodInjectionPlan(Method method) { |
aoqi@0 | 108 | this.method = method; |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | public void inject(T instance, R resource) { |
aoqi@0 | 112 | invokeMethod(method, instance, resource); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | /* |
aoqi@0 | 117 | * Helper for invoking a method with elevated privilege. |
aoqi@0 | 118 | */ |
aoqi@0 | 119 | private static void invokeMethod(final Method method, final Object instance, final Object... args) { |
aoqi@0 | 120 | if(method==null) return; |
aoqi@0 | 121 | AccessController.doPrivileged(new PrivilegedAction<Void>() { |
aoqi@0 | 122 | public Void run() { |
aoqi@0 | 123 | try { |
aoqi@0 | 124 | if (!method.isAccessible()) { |
aoqi@0 | 125 | method.setAccessible(true); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | method.invoke(instance,args); |
aoqi@0 | 128 | } catch (IllegalAccessException e) { |
aoqi@0 | 129 | throw new WebServiceException(e); |
aoqi@0 | 130 | } catch (InvocationTargetException e) { |
aoqi@0 | 131 | throw new WebServiceException(e); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | return null; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | }); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | /* |
aoqi@0 | 139 | * Combines multiple {@link InjectionPlan}s into one. |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | private static class Compositor<T, R> extends InjectionPlan<T, R> { |
aoqi@0 | 142 | private final Collection<InjectionPlan<T, R>> children; |
aoqi@0 | 143 | |
aoqi@0 | 144 | public Compositor(Collection<InjectionPlan<T, R>> children) { |
aoqi@0 | 145 | this.children = children; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | public void inject(T instance, R res) { |
aoqi@0 | 149 | for (InjectionPlan<T, R> plan : children) |
aoqi@0 | 150 | plan.inject(instance, res); |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | public void inject(T instance, Callable<R> resource) { |
aoqi@0 | 154 | if (!children.isEmpty()) { |
aoqi@0 | 155 | super.inject(instance, resource); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /* |
aoqi@0 | 161 | * Creates an {@link InjectionPlan} that injects the given resource type to the given class. |
aoqi@0 | 162 | * |
aoqi@0 | 163 | * @param isStatic |
aoqi@0 | 164 | * Only look for static field/method |
aoqi@0 | 165 | * |
aoqi@0 | 166 | */ |
aoqi@0 | 167 | public static <T,R> |
aoqi@0 | 168 | InjectionPlan<T,R> buildInjectionPlan(Class<? extends T> clazz, Class<R> resourceType, boolean isStatic) { |
aoqi@0 | 169 | List<InjectionPlan<T,R>> plan = new ArrayList<InjectionPlan<T,R>>(); |
aoqi@0 | 170 | |
aoqi@0 | 171 | Class<?> cl = clazz; |
aoqi@0 | 172 | while(cl != Object.class) { |
aoqi@0 | 173 | for(Field field: cl.getDeclaredFields()) { |
aoqi@0 | 174 | Resource resource = field.getAnnotation(Resource.class); |
aoqi@0 | 175 | if (resource != null) { |
aoqi@0 | 176 | if(isInjectionPoint(resource, field.getType(), |
aoqi@0 | 177 | "Incorrect type for field"+field.getName(), |
aoqi@0 | 178 | resourceType)) { |
aoqi@0 | 179 | |
aoqi@0 | 180 | if(isStatic && !Modifier.isStatic(field.getModifiers())) |
aoqi@0 | 181 | throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+field); |
aoqi@0 | 182 | |
aoqi@0 | 183 | plan.add(new FieldInjectionPlan<T,R>(field)); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |
aoqi@0 | 187 | cl = cl.getSuperclass(); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | cl = clazz; |
aoqi@0 | 191 | while(cl != Object.class) { |
aoqi@0 | 192 | for(Method method : cl.getDeclaredMethods()) { |
aoqi@0 | 193 | Resource resource = method.getAnnotation(Resource.class); |
aoqi@0 | 194 | if (resource != null) { |
aoqi@0 | 195 | Class[] paramTypes = method.getParameterTypes(); |
aoqi@0 | 196 | if (paramTypes.length != 1) |
aoqi@0 | 197 | throw new WebServiceException("Incorrect no of arguments for method "+method); |
aoqi@0 | 198 | if(isInjectionPoint(resource,paramTypes[0], |
aoqi@0 | 199 | "Incorrect argument types for method"+method.getName(), |
aoqi@0 | 200 | resourceType)) { |
aoqi@0 | 201 | |
aoqi@0 | 202 | if(isStatic && !Modifier.isStatic(method.getModifiers())) |
aoqi@0 | 203 | throw new WebServiceException("Static resource "+resourceType+" cannot be injected to non-static "+method); |
aoqi@0 | 204 | |
aoqi@0 | 205 | plan.add(new MethodInjectionPlan<T,R>(method)); |
aoqi@0 | 206 | } |
aoqi@0 | 207 | } |
aoqi@0 | 208 | } |
aoqi@0 | 209 | cl = cl.getSuperclass(); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | return new Compositor<T,R>(plan); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | /* |
aoqi@0 | 216 | * Returns true if the combination of {@link Resource} and the field/method type |
aoqi@0 | 217 | * are consistent for {@link WebServiceContext} injection. |
aoqi@0 | 218 | */ |
aoqi@0 | 219 | private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) { |
aoqi@0 | 220 | Class t = resource.type(); |
aoqi@0 | 221 | if (t.equals(Object.class)) { |
aoqi@0 | 222 | return fieldType.equals(resourceType); |
aoqi@0 | 223 | } else if (t.equals(resourceType)) { |
aoqi@0 | 224 | if (fieldType.isAssignableFrom(resourceType)) { |
aoqi@0 | 225 | return true; |
aoqi@0 | 226 | } else { |
aoqi@0 | 227 | // type compatibility error |
aoqi@0 | 228 | throw new WebServiceException(errorMessage); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | } |
aoqi@0 | 231 | return false; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |