01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.instrument.classloading.weblogic;
18:
19: import java.lang.instrument.ClassFileTransformer;
20: import java.lang.instrument.IllegalClassFormatException;
21: import java.lang.reflect.InvocationHandler;
22: import java.lang.reflect.Method;
23: import java.util.Hashtable;
24:
25: /**
26: * Adapter that implements WebLogic ClassPreProcessor interface, delegating to a
27: * standard JDK {@link ClassFileTransformer} underneath.
28: *
29: * <p>To avoid compile time checks again the vendor API, a dynamic proxy is
30: * being used.
31: *
32: * @author Costin Leau
33: * @author Juergen Hoeller
34: * @since 2.5
35: */
36: class WebLogicClassPreProcessorAdapter implements InvocationHandler {
37:
38: private final ClassFileTransformer transformer;
39:
40: private final ClassLoader loader;
41:
42: /**
43: * Creates a new {@link WebLogicClassPreProcessorAdapter}.
44: * @param transformer the {@link ClassFileTransformer} to be adapted (must
45: * not be <code>null</code>)
46: */
47: public WebLogicClassPreProcessorAdapter(
48: ClassFileTransformer transformer, ClassLoader loader) {
49: this .transformer = transformer;
50: this .loader = loader;
51: }
52:
53: public Object invoke(Object proxy, Method method, Object[] args)
54: throws Throwable {
55: if ("equals".equals(method.getName())) {
56: return (Boolean.valueOf(proxy == args[0]));
57: } else if ("hashCode".equals(method.getName())) {
58: return hashCode();
59: } else if ("initialize".equals(method.getName())) {
60: initialize((Hashtable) args[0]);
61: return null;
62: } else if ("preProcess".equals(method.getName())) {
63: return preProcess((String) args[0], (byte[]) args[1]);
64: } else {
65: throw new IllegalArgumentException("Unknown method: "
66: + method);
67: }
68: }
69:
70: public void initialize(Hashtable params) {
71: }
72:
73: public byte[] preProcess(String className, byte[] classBytes) {
74: try {
75: byte[] result = this .transformer.transform(this .loader,
76: className, null, null, classBytes);
77: return (result != null ? result : classBytes);
78: } catch (IllegalClassFormatException ex) {
79: throw new IllegalStateException(
80: "Cannot transform due to illegal class format", ex);
81: }
82: }
83:
84: @Override
85: public String toString() {
86: StringBuilder builder = new StringBuilder(getClass().getName());
87: builder.append(" for transformer: ");
88: builder.append(this.transformer);
89: return builder.toString();
90: }
91:
92: }
|