com.sun.xml.ws.model.Injector Maven / Gradle / Ivy
package com.sun.xml.ws.model;
import javax.xml.ws.WebServiceException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A {@link ClassLoader} used to "inject" wrapper and exception bean classes
* into the VM.
*
* @author Jitendra kotamraju
*/
final class Injector {
private static final Logger LOGGER = Logger.getLogger(Injector.class.getName());
private static final Method defineClass;
private static final Method resolveClass;
static {
try {
defineClass = ClassLoader.class.getDeclaredMethod("defineClass",String.class,byte[].class,Integer.TYPE,Integer.TYPE);
resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass",Class.class);
} catch (NoSuchMethodException e) {
// impossible
throw new NoSuchMethodError(e.getMessage());
}
AccessController.doPrivileged(new PrivilegedAction() {
public Void run() {
// TODO: check security implication
// do these setAccessible allow anyone to call these methods freely?s
defineClass.setAccessible(true);
resolveClass.setAccessible(true);
return null;
}
});
}
static synchronized Class inject(ClassLoader cl, String className, byte[] image) {
// To avoid race conditions let us check if the classloader
// already contains the class
try {
return cl.loadClass(className);
} catch (ClassNotFoundException e) {
// nothing to do
}
try {
Class c = (Class)defineClass.invoke(cl,className.replace('/','.'),image,0,image.length);
resolveClass.invoke(cl, c);
return c;
} catch (IllegalAccessException e) {
LOGGER.log(Level.FINE,"Unable to inject "+className,e);
throw new WebServiceException(e);
} catch (InvocationTargetException e) {
LOGGER.log(Level.FINE,"Unable to inject "+className,e);
throw new WebServiceException(e);
}
}
}