KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > instrument > classloading > oc4j > OC4JClassPreprocessorAdapter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
23 import java.lang.instrument.IllegalClassFormatException JavaDoc;
24 import java.security.ProtectionDomain JavaDoc;
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 JavaDoc transformer;
39
40
41     /**
42      * Creates a new instance of the {@link OC4JClassPreprocessorAdapter} class.
43      * @param transformer the {@link ClassFileTransformer} to be adapted (must not be <code>null</code>)
44      * @throws IllegalArgumentException if the supplied <code>transformer</code> is <code>null</code>
45      */

46     public OC4JClassPreprocessorAdapter(ClassFileTransformer JavaDoc transformer) {
47         Assert.notNull(transformer, "Transformer must not be null");
48         this.transformer = transformer;
49     }
50
51
52     public ClassPreprocessor initialize(ClassLoader JavaDoc loader) {
53         return this;
54     }
55
56     public byte[] processClass(String JavaDoc className, byte origClassBytes[], int offset, int length, ProtectionDomain JavaDoc pd,
57                                ClassLoader JavaDoc loader) {
58         try {
59             byte[] tempArray = new byte[length];
60             System.arraycopy(origClassBytes, offset, tempArray, 0, 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, className.replace('.', '/'), null, pd, tempArray);
65             return (result != null ? result : origClassBytes);
66         }
67         catch (IllegalClassFormatException JavaDoc ex) {
68             throw new IllegalStateException JavaDoc("Cannot transform because of illegal class format", ex);
69         }
70     }
71
72
73     @Override JavaDoc
74     public String JavaDoc toString() {
75         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(getClass().getName());
76         builder.append(" for transformer: ");
77         builder.append(this.transformer);
78         return builder.toString();
79     }
80
81 }
82
Popular Tags