polishing
This commit is contained in:
parent
5937779bb1
commit
c0e429a9a5
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -34,6 +34,7 @@ import org.springframework.util.Assert;
|
||||||
* called through reflection) from the load-time weaver.
|
* called through reflection) from the load-time weaver.
|
||||||
*
|
*
|
||||||
* @author Costin Leau
|
* @author Costin Leau
|
||||||
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
class WebSphereClassLoaderAdapter {
|
class WebSphereClassLoaderAdapter {
|
||||||
|
|
||||||
|
@ -58,14 +59,14 @@ class WebSphereClassLoaderAdapter {
|
||||||
addPreDefinePlugin = classLoader.getClass().getMethod("addPreDefinePlugin", wsPreProcessorClass);
|
addPreDefinePlugin = classLoader.getClass().getMethod("addPreDefinePlugin", wsPreProcessorClass);
|
||||||
transformerList = wsCompoundClassLoaderClass.getDeclaredField(PLUGINS_FIELD);
|
transformerList = wsCompoundClassLoaderClass.getDeclaredField(PLUGINS_FIELD);
|
||||||
transformerList.setAccessible(true);
|
transformerList.setAccessible(true);
|
||||||
|
}
|
||||||
} catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available",
|
"Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available",
|
||||||
ex);
|
ex);
|
||||||
}
|
}
|
||||||
Assert.isInstanceOf(wsCompoundClassLoaderClass, classLoader, "ClassLoader must be instance of ["
|
Assert.isInstanceOf(wsCompoundClassLoaderClass, classLoader,
|
||||||
+ COMPOUND_CLASS_LOADER_NAME + "]");
|
"ClassLoader must be instance of [" + COMPOUND_CLASS_LOADER_NAME + "]");
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,9 +82,11 @@ class WebSphereClassLoaderAdapter {
|
||||||
new Class[] { this.wsPreProcessorClass }, adapter);
|
new Class[] { this.wsPreProcessorClass }, adapter);
|
||||||
this.addPreDefinePlugin.invoke(this.classLoader, adapterInstance);
|
this.addPreDefinePlugin.invoke(this.classLoader, adapterInstance);
|
||||||
|
|
||||||
} catch (InvocationTargetException ex) {
|
}
|
||||||
|
catch (InvocationTargetException ex) {
|
||||||
throw new IllegalStateException("WebSphere addPreDefinePlugin method threw exception", ex.getCause());
|
throw new IllegalStateException("WebSphere addPreDefinePlugin method threw exception", ex.getCause());
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException("Could not invoke WebSphere addPreDefinePlugin method", ex);
|
throw new IllegalStateException("Could not invoke WebSphere addPreDefinePlugin method", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,10 +99,13 @@ class WebSphereClassLoaderAdapter {
|
||||||
List list = (List) transformerList.get(loader);
|
List list = (List) transformerList.get(loader);
|
||||||
list.clear();
|
list.clear();
|
||||||
return loader;
|
return loader;
|
||||||
} catch (InvocationTargetException ex) {
|
}
|
||||||
|
catch (InvocationTargetException ex) {
|
||||||
throw new IllegalStateException("WebSphere CompoundClassLoader constructor failed", ex.getCause());
|
throw new IllegalStateException("WebSphere CompoundClassLoader constructor failed", ex.getCause());
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException("Could not construct WebSphere CompoundClassLoader", ex);
|
throw new IllegalStateException("Could not construct WebSphere CompoundClassLoader", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -23,26 +23,24 @@ import java.security.CodeSource;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter that implements WebSphere 7.0 ClassPreProcessPlugin interface, delegating to a
|
* Adapter that implements WebSphere 7.0 ClassPreProcessPlugin interface,
|
||||||
* standard JDK {@link ClassFileTransformer} underneath.
|
* delegating to a standard JDK {@link ClassFileTransformer} underneath.
|
||||||
*
|
*
|
||||||
* <p>To avoid compile time checks again the vendor API, a dynamic proxy is
|
* <p>To avoid compile time checks again the vendor API, a dynamic proxy is
|
||||||
* being used.
|
* being used.
|
||||||
*
|
*
|
||||||
* @author Costin Leau
|
* @author Costin Leau
|
||||||
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
class WebSphereClassPreDefinePlugin implements InvocationHandler {
|
class WebSphereClassPreDefinePlugin implements InvocationHandler {
|
||||||
|
|
||||||
private final ClassFileTransformer transformer;
|
private final ClassFileTransformer transformer;
|
||||||
|
|
||||||
private class Dummy {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link WebSphereClassPreDefinePlugin}.
|
* Create a new {@link WebSphereClassPreDefinePlugin}.
|
||||||
*
|
* @param transformer the {@link ClassFileTransformer} to be adapted
|
||||||
* @param transformer the {@link ClassFileTransformer} to be adapted (must
|
* (must not be <code>null</code>)
|
||||||
* not be <code>null</code>)
|
|
||||||
*/
|
*/
|
||||||
public WebSphereClassPreDefinePlugin(ClassFileTransformer transformer) {
|
public WebSphereClassPreDefinePlugin(ClassFileTransformer transformer) {
|
||||||
this.transformer = transformer;
|
this.transformer = transformer;
|
||||||
|
@ -53,33 +51,37 @@ class WebSphereClassPreDefinePlugin implements InvocationHandler {
|
||||||
String dummyClass = Dummy.class.getName().replace('.', '/');
|
String dummyClass = Dummy.class.getName().replace('.', '/');
|
||||||
byte[] bytes = FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream(dummyClass + ".class"));
|
byte[] bytes = FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream(dummyClass + ".class"));
|
||||||
transformer.transform(classLoader, dummyClass, null, null, bytes);
|
transformer.transform(classLoader, dummyClass, null, null, bytes);
|
||||||
} catch (Throwable ex) {
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
throw new IllegalArgumentException("Cannot load transformer", ex);
|
throw new IllegalArgumentException("Cannot load transformer", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
|
|
||||||
if ("equals".equals(name)) {
|
if ("equals".equals(name)) {
|
||||||
return (Boolean.valueOf(proxy == args[0]));
|
return (proxy == args[0]);
|
||||||
} else if ("hashCode".equals(name)) {
|
}
|
||||||
|
else if ("hashCode".equals(name)) {
|
||||||
return hashCode();
|
return hashCode();
|
||||||
} else if ("toString".equals(name)) {
|
}
|
||||||
|
else if ("toString".equals(name)) {
|
||||||
return toString();
|
return toString();
|
||||||
} else if ("transformClass".equals(name)) {
|
}
|
||||||
|
else if ("transformClass".equals(name)) {
|
||||||
return transform((String) args[0], (byte[]) args[1], (CodeSource) args[2], (ClassLoader) args[3]);
|
return transform((String) args[0], (byte[]) args[1], (CodeSource) args[2], (ClassLoader) args[3]);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw new IllegalArgumentException("Unknown method: " + method);
|
throw new IllegalArgumentException("Unknown method: " + method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] transform(String className, byte[] classfileBuffer, CodeSource codeSource, ClassLoader classLoader)
|
protected byte[] transform(String className, byte[] classfileBuffer, CodeSource codeSource, ClassLoader classLoader)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
// NB: WebSphere passes className as "." without class while the
|
|
||||||
// transformer expects a VM, "/" format
|
|
||||||
byte[] result = transformer.transform(classLoader, className.replace('.', '/'), null, null, classfileBuffer);
|
|
||||||
|
|
||||||
|
// NB: WebSphere passes className as "." without class while the transformer expects a VM, "/" format
|
||||||
|
byte[] result = transformer.transform(classLoader, className.replace('.', '/'), null, null, classfileBuffer);
|
||||||
return (result != null ? result : classfileBuffer);
|
return (result != null ? result : classfileBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,4 +92,9 @@ class WebSphereClassPreDefinePlugin implements InvocationHandler {
|
||||||
builder.append(this.transformer);
|
builder.append(this.transformer);
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static class Dummy {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -34,6 +34,7 @@ public class WebSphereLoadTimeWeaver implements LoadTimeWeaver {
|
||||||
|
|
||||||
private final WebSphereClassLoaderAdapter classLoader;
|
private final WebSphereClassLoaderAdapter classLoader;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
|
* Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
|
||||||
* the default {@link ClassLoader class loader}.
|
* the default {@link ClassLoader class loader}.
|
||||||
|
@ -46,14 +47,15 @@ public class WebSphereLoadTimeWeaver implements LoadTimeWeaver {
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
|
* Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
|
||||||
* the supplied {@link ClassLoader}.
|
* the supplied {@link ClassLoader}.
|
||||||
* @param classLoader the <code>ClassLoader</code> to delegate to for
|
* @param classLoader the <code>ClassLoader</code> to delegate to for weaving
|
||||||
* weaving (must not be <code>null</code>)
|
* (must not be <code>null</code>)
|
||||||
*/
|
*/
|
||||||
public WebSphereLoadTimeWeaver(ClassLoader classLoader) {
|
public WebSphereLoadTimeWeaver(ClassLoader classLoader) {
|
||||||
Assert.notNull(classLoader, "ClassLoader must not be null");
|
Assert.notNull(classLoader, "ClassLoader must not be null");
|
||||||
this.classLoader = new WebSphereClassLoaderAdapter(classLoader);
|
this.classLoader = new WebSphereClassLoaderAdapter(classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addTransformer(ClassFileTransformer transformer) {
|
public void addTransformer(ClassFileTransformer transformer) {
|
||||||
this.classLoader.addTransformer(transformer);
|
this.classLoader.addTransformer(transformer);
|
||||||
}
|
}
|
||||||
|
@ -65,4 +67,5 @@ public class WebSphereLoadTimeWeaver implements LoadTimeWeaver {
|
||||||
public ClassLoader getThrowawayClassLoader() {
|
public ClassLoader getThrowawayClassLoader() {
|
||||||
return this.classLoader.getThrowawayClassLoader();
|
return this.classLoader.getThrowawayClassLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue