325N/A/*
325N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.model;
325N/A
325N/Aimport javax.xml.ws.WebServiceException;
325N/Aimport java.lang.reflect.InvocationTargetException;
325N/Aimport java.lang.reflect.Method;
325N/Aimport java.net.URL;
325N/Aimport java.security.AccessController;
325N/Aimport java.security.PrivilegedAction;
325N/Aimport java.util.logging.Level;
325N/Aimport java.util.logging.Logger;
325N/A
325N/A/**
325N/A * A {@link ClassLoader} used to "inject" wrapper and exception bean classes
325N/A * into the VM.
325N/A *
325N/A * @author Jitendra kotamraju
325N/A */
325N/Afinal class Injector {
325N/A
325N/A private static final Logger LOGGER = Logger.getLogger(Injector.class.getName());
325N/A
325N/A private static final Method defineClass;
325N/A private static final Method resolveClass;
325N/A private static final Method getPackage;
325N/A private static final Method definePackage;
325N/A
325N/A static {
325N/A try {
325N/A defineClass = ClassLoader.class.getDeclaredMethod("defineClass",String.class,byte[].class,Integer.TYPE,Integer.TYPE);
325N/A resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass",Class.class);
325N/A getPackage = ClassLoader.class.getDeclaredMethod("getPackage", String.class);
325N/A definePackage = ClassLoader.class.getDeclaredMethod("definePackage",
325N/A String.class, String.class, String.class, String.class,
325N/A String.class, String.class, String.class, URL.class);
325N/A } catch (NoSuchMethodException e) {
325N/A // impossible
325N/A throw new NoSuchMethodError(e.getMessage());
325N/A }
325N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
325N/A public Void run() {
325N/A // TODO: check security implication
325N/A // do these setAccessible allow anyone to call these methods freely?s
325N/A defineClass.setAccessible(true);
325N/A resolveClass.setAccessible(true);
325N/A getPackage.setAccessible(true);
325N/A definePackage.setAccessible(true);
325N/A return null;
325N/A }
325N/A });
325N/A }
325N/A
325N/A static synchronized Class inject(ClassLoader cl, String className, byte[] image) {
325N/A // To avoid race conditions let us check if the classloader
325N/A // already contains the class
325N/A try {
325N/A return cl.loadClass(className);
325N/A } catch (ClassNotFoundException e) {
325N/A // nothing to do
325N/A }
325N/A try {
325N/A int packIndex = className.lastIndexOf('.');
325N/A if (packIndex != -1) {
325N/A String pkgname = className.substring(0, packIndex);
325N/A // Check if package already loaded.
325N/A Package pkg = (Package)getPackage.invoke(cl, pkgname);
325N/A if (pkg == null) {
325N/A definePackage.invoke(cl, pkgname, null, null, null, null, null, null, null);
325N/A }
325N/A }
325N/A
325N/A Class c = (Class)defineClass.invoke(cl,className.replace('/','.'),image,0,image.length);
325N/A resolveClass.invoke(cl, c);
325N/A return c;
325N/A } catch (IllegalAccessException e) {
325N/A LOGGER.log(Level.FINE,"Unable to inject "+className,e);
325N/A throw new WebServiceException(e);
325N/A } catch (InvocationTargetException e) {
325N/A LOGGER.log(Level.FINE,"Unable to inject "+className,e);
325N/A throw new WebServiceException(e);
325N/A }
325N/A }
325N/A
325N/A}