01: /*
02: * Copyright 2002-2006 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.oc4j;
18:
19: import oracle.classloader.util.ClassPreprocessor;
20: import org.springframework.util.Assert;
21:
22: import java.lang.instrument.ClassFileTransformer;
23: import java.lang.instrument.IllegalClassFormatException;
24: import java.security.ProtectionDomain;
25:
26: /**
27: * {@link ClassPreprocessor} adapter for OC4J, delegating to a standard
28: * JDK {@link ClassFileTransformer} underneath.
29: *
30: * <p>Many thanks to <a href="mailto:mike.keith@oracle.com">Mike Keith</a>
31: * for his assistance.
32: *
33: * @author Costin Leau
34: * @since 2.0
35: */
36: class OC4JClassPreprocessorAdapter implements ClassPreprocessor {
37:
38: private final ClassFileTransformer transformer;
39:
40: /**
41: * Creates a new instance of the {@link OC4JClassPreprocessorAdapter} class.
42: * @param transformer the {@link ClassFileTransformer} to be adapted (must not be <code>null</code>)
43: * @throws IllegalArgumentException if the supplied <code>transformer</code> is <code>null</code>
44: */
45: public OC4JClassPreprocessorAdapter(ClassFileTransformer transformer) {
46: Assert.notNull(transformer, "Transformer must not be null");
47: this .transformer = transformer;
48: }
49:
50: public ClassPreprocessor initialize(ClassLoader loader) {
51: return this ;
52: }
53:
54: public byte[] processClass(String className, byte origClassBytes[],
55: int offset, int length, ProtectionDomain pd,
56: ClassLoader loader) {
57: try {
58: byte[] tempArray = new byte[length];
59: System.arraycopy(origClassBytes, offset, tempArray, 0,
60: length);
61:
62: // NB: OC4J passes className as "." without class while the
63: // transformer expects a VM, "/" format
64: byte[] result = this .transformer.transform(loader,
65: className.replace('.', '/'), null, pd, tempArray);
66: return (result != null ? result : origClassBytes);
67: } catch (IllegalClassFormatException ex) {
68: throw new IllegalStateException(
69: "Cannot transform because of illegal class format",
70: ex);
71: }
72: }
73:
74: @Override
75: public String toString() {
76: StringBuilder builder = new StringBuilder(getClass().getName());
77: builder.append(" for transformer: ");
78: builder.append(this.transformer);
79: return builder.toString();
80: }
81:
82: }
|