1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/model/Injector.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,108 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, 2010, 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.model; 1.30 + 1.31 +import javax.xml.ws.WebServiceException; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.lang.reflect.Method; 1.34 +import java.net.URL; 1.35 +import java.security.AccessController; 1.36 +import java.security.PrivilegedAction; 1.37 +import java.util.logging.Level; 1.38 +import java.util.logging.Logger; 1.39 + 1.40 +/** 1.41 + * A {@link ClassLoader} used to "inject" wrapper and exception bean classes 1.42 + * into the VM. 1.43 + * 1.44 + * @author Jitendra kotamraju 1.45 + */ 1.46 +final class Injector { 1.47 + 1.48 + private static final Logger LOGGER = Logger.getLogger(Injector.class.getName()); 1.49 + 1.50 + private static final Method defineClass; 1.51 + private static final Method resolveClass; 1.52 + private static final Method getPackage; 1.53 + private static final Method definePackage; 1.54 + 1.55 + static { 1.56 + try { 1.57 + defineClass = ClassLoader.class.getDeclaredMethod("defineClass",String.class,byte[].class,Integer.TYPE,Integer.TYPE); 1.58 + resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass",Class.class); 1.59 + getPackage = ClassLoader.class.getDeclaredMethod("getPackage", String.class); 1.60 + definePackage = ClassLoader.class.getDeclaredMethod("definePackage", 1.61 + String.class, String.class, String.class, String.class, 1.62 + String.class, String.class, String.class, URL.class); 1.63 + } catch (NoSuchMethodException e) { 1.64 + // impossible 1.65 + throw new NoSuchMethodError(e.getMessage()); 1.66 + } 1.67 + AccessController.doPrivileged(new PrivilegedAction<Void>() { 1.68 + public Void run() { 1.69 + // TODO: check security implication 1.70 + // do these setAccessible allow anyone to call these methods freely?s 1.71 + defineClass.setAccessible(true); 1.72 + resolveClass.setAccessible(true); 1.73 + getPackage.setAccessible(true); 1.74 + definePackage.setAccessible(true); 1.75 + return null; 1.76 + } 1.77 + }); 1.78 + } 1.79 + 1.80 + static synchronized Class inject(ClassLoader cl, String className, byte[] image) { 1.81 + // To avoid race conditions let us check if the classloader 1.82 + // already contains the class 1.83 + try { 1.84 + return cl.loadClass(className); 1.85 + } catch (ClassNotFoundException e) { 1.86 + // nothing to do 1.87 + } 1.88 + try { 1.89 + int packIndex = className.lastIndexOf('.'); 1.90 + if (packIndex != -1) { 1.91 + String pkgname = className.substring(0, packIndex); 1.92 + // Check if package already loaded. 1.93 + Package pkg = (Package)getPackage.invoke(cl, pkgname); 1.94 + if (pkg == null) { 1.95 + definePackage.invoke(cl, pkgname, null, null, null, null, null, null, null); 1.96 + } 1.97 + } 1.98 + 1.99 + Class c = (Class)defineClass.invoke(cl,className.replace('/','.'),image,0,image.length); 1.100 + resolveClass.invoke(cl, c); 1.101 + return c; 1.102 + } catch (IllegalAccessException e) { 1.103 + LOGGER.log(Level.FINE,"Unable to inject "+className,e); 1.104 + throw new WebServiceException(e); 1.105 + } catch (InvocationTargetException e) { 1.106 + LOGGER.log(Level.FINE,"Unable to inject "+className,e); 1.107 + throw new WebServiceException(e); 1.108 + } 1.109 + } 1.110 + 1.111 +}