src/share/jaxws_classes/com/sun/xml/internal/ws/model/Injector.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2008, 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.model;
aoqi@0 27
aoqi@0 28 import javax.xml.ws.WebServiceException;
aoqi@0 29 import java.lang.reflect.InvocationTargetException;
aoqi@0 30 import java.lang.reflect.Method;
aoqi@0 31 import java.net.URL;
aoqi@0 32 import java.security.AccessController;
aoqi@0 33 import java.security.PrivilegedAction;
aoqi@0 34 import java.util.logging.Level;
aoqi@0 35 import java.util.logging.Logger;
aoqi@0 36
aoqi@0 37 /**
aoqi@0 38 * A {@link ClassLoader} used to "inject" wrapper and exception bean classes
aoqi@0 39 * into the VM.
aoqi@0 40 *
aoqi@0 41 * @author Jitendra kotamraju
aoqi@0 42 */
aoqi@0 43 final class Injector {
aoqi@0 44
aoqi@0 45 private static final Logger LOGGER = Logger.getLogger(Injector.class.getName());
aoqi@0 46
aoqi@0 47 private static final Method defineClass;
aoqi@0 48 private static final Method resolveClass;
aoqi@0 49 private static final Method getPackage;
aoqi@0 50 private static final Method definePackage;
aoqi@0 51
aoqi@0 52 static {
aoqi@0 53 try {
aoqi@0 54 defineClass = ClassLoader.class.getDeclaredMethod("defineClass",String.class,byte[].class,Integer.TYPE,Integer.TYPE);
aoqi@0 55 resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass",Class.class);
aoqi@0 56 getPackage = ClassLoader.class.getDeclaredMethod("getPackage", String.class);
aoqi@0 57 definePackage = ClassLoader.class.getDeclaredMethod("definePackage",
aoqi@0 58 String.class, String.class, String.class, String.class,
aoqi@0 59 String.class, String.class, String.class, URL.class);
aoqi@0 60 } catch (NoSuchMethodException e) {
aoqi@0 61 // impossible
aoqi@0 62 throw new NoSuchMethodError(e.getMessage());
aoqi@0 63 }
aoqi@0 64 AccessController.doPrivileged(new PrivilegedAction<Void>() {
aoqi@0 65 public Void run() {
aoqi@0 66 // TODO: check security implication
aoqi@0 67 // do these setAccessible allow anyone to call these methods freely?s
aoqi@0 68 defineClass.setAccessible(true);
aoqi@0 69 resolveClass.setAccessible(true);
aoqi@0 70 getPackage.setAccessible(true);
aoqi@0 71 definePackage.setAccessible(true);
aoqi@0 72 return null;
aoqi@0 73 }
aoqi@0 74 });
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 static synchronized Class inject(ClassLoader cl, String className, byte[] image) {
aoqi@0 78 // To avoid race conditions let us check if the classloader
aoqi@0 79 // already contains the class
aoqi@0 80 try {
aoqi@0 81 return cl.loadClass(className);
aoqi@0 82 } catch (ClassNotFoundException e) {
aoqi@0 83 // nothing to do
aoqi@0 84 }
aoqi@0 85 try {
aoqi@0 86 int packIndex = className.lastIndexOf('.');
aoqi@0 87 if (packIndex != -1) {
aoqi@0 88 String pkgname = className.substring(0, packIndex);
aoqi@0 89 // Check if package already loaded.
aoqi@0 90 Package pkg = (Package)getPackage.invoke(cl, pkgname);
aoqi@0 91 if (pkg == null) {
aoqi@0 92 definePackage.invoke(cl, pkgname, null, null, null, null, null, null, null);
aoqi@0 93 }
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 Class c = (Class)defineClass.invoke(cl,className.replace('/','.'),image,0,image.length);
aoqi@0 97 resolveClass.invoke(cl, c);
aoqi@0 98 return c;
aoqi@0 99 } catch (IllegalAccessException e) {
aoqi@0 100 LOGGER.log(Level.FINE,"Unable to inject "+className,e);
aoqi@0 101 throw new WebServiceException(e);
aoqi@0 102 } catch (InvocationTargetException e) {
aoqi@0 103 LOGGER.log(Level.FINE,"Unable to inject "+className,e);
aoqi@0 104 throw new WebServiceException(e);
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 }

mercurial