KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > instrument > UnitClassTransformer


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.jpa.instrument;
20
21 import java.lang.instrument.ClassFileTransformer JavaDoc;
22 import java.lang.instrument.IllegalClassFormatException JavaDoc;
23 import java.security.ProtectionDomain JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.persistence.spi.ClassTransformer;
27
28 import org.apache.cayenne.jpa.map.JpaClassDescriptor;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * A ClassFileTransformer decorator that wraps a Java instrumentation ClassFileTransformer
34  * instance in a JPA ClassTransformer. UnitClassTranformer would only do transformation of
35  * the mapped classes.
36  *
37  * @author Andrus Adamchik
38  */

39 public class UnitClassTransformer implements ClassTransformer {
40
41     protected ClassLoader JavaDoc tempClassLoader;
42     protected Log logger;
43     protected ClassFileTransformer JavaDoc transformer;
44     protected Map JavaDoc<String JavaDoc, JpaClassDescriptor> managedClasses;
45
46     public UnitClassTransformer(Map JavaDoc<String JavaDoc, JpaClassDescriptor> managedClasses,
47             ClassLoader JavaDoc tempClassLoader, ClassFileTransformer JavaDoc transformer) {
48         this.transformer = transformer;
49         this.managedClasses = managedClasses;
50         this.tempClassLoader = tempClassLoader;
51         this.logger = LogFactory.getLog(getClass());
52     }
53
54     public byte[] transform(
55             ClassLoader JavaDoc loader,
56             String JavaDoc className,
57             Class JavaDoc<?> classBeingRedefined,
58             ProtectionDomain JavaDoc protectionDomain,
59             byte[] classfileBuffer) throws IllegalClassFormatException JavaDoc {
60
61         if (tempClassLoader == loader) {
62             return null;
63         }
64
65         if (isManagedClass(className)) {
66             logger.warn("Will transform: " + className);
67
68             return transformer.transform(
69                     loader,
70                     className,
71                     classBeingRedefined,
72                     protectionDomain,
73                     classfileBuffer);
74         }
75         else {
76             return null;
77         }
78     }
79
80     /**
81      * Returns true if a classname os a part of an entity map. Note that the class name is
82      * expected in the internal format, separated by "/", not ".".
83      */

84     protected boolean isManagedClass(String JavaDoc className) {
85         return managedClasses.containsKey(className.replace('/', '.'));
86     }
87 }
88
Popular Tags