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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.ws.model;
27
28 import javax.xml.ws.WebServiceException;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.net.URL;
32 import java.security.AccessController;
33 import java.security.PrivilegedAction;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36
37 /**
38 * A {@link ClassLoader} used to "inject" wrapper and exception bean classes
39 * into the VM.
40 *
41 * @author Jitendra kotamraju
42 */
43 final class Injector {
44
45 private static final Logger LOGGER = Logger.getLogger(Injector.class.getName());
46
47 private static final Method defineClass;
48 private static final Method resolveClass;
49 private static final Method getPackage;
50 private static final Method definePackage;
51
52 static {
53 try {
54 defineClass = ClassLoader.class.getDeclaredMethod("defineClass",String.class,byte[].class,Integer.TYPE,Integer.TYPE);
55 resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass",Class.class);
56 getPackage = ClassLoader.class.getDeclaredMethod("getPackage", String.class);
57 definePackage = ClassLoader.class.getDeclaredMethod("definePackage",
58 String.class, String.class, String.class, String.class,
59 String.class, String.class, String.class, URL.class);
60 } catch (NoSuchMethodException e) {
61 // impossible
62 throw new NoSuchMethodError(e.getMessage());
63 }
64 AccessController.doPrivileged(new PrivilegedAction<Void>() {
65 public Void run() {
66 // TODO: check security implication
67 // do these setAccessible allow anyone to call these methods freely?s
68 defineClass.setAccessible(true);
69 resolveClass.setAccessible(true);
70 getPackage.setAccessible(true);
71 definePackage.setAccessible(true);
72 return null;
73 }
74 });
75 }
76
77 static synchronized Class inject(ClassLoader cl, String className, byte[] image) {
78 // To avoid race conditions let us check if the classloader
79 // already contains the class
80 try {
81 return cl.loadClass(className);
82 } catch (ClassNotFoundException e) {
83 // nothing to do
84 }
85 try {
86 int packIndex = className.lastIndexOf('.');
87 if (packIndex != -1) {
88 String pkgname = className.substring(0, packIndex);
89 // Check if package already loaded.
90 Package pkg = (Package)getPackage.invoke(cl, pkgname);
91 if (pkg == null) {
92 definePackage.invoke(cl, pkgname, null, null, null, null, null, null, null);
93 }
94 }
95
96 Class c = (Class)defineClass.invoke(cl,className.replace('/','.'),image,0,image.length);
97 resolveClass.invoke(cl, c);
98 return c;
99 } catch (IllegalAccessException e) {
100 LOGGER.log(Level.FINE,"Unable to inject "+className,e);
101 throw new WebServiceException(e);
102 } catch (InvocationTargetException e) {
103 LOGGER.log(Level.FINE,"Unable to inject "+className,e);
104 throw new WebServiceException(e);
105 }
106 }
107
108 }

mercurial