1 19 package org.apache.cayenne.jpa.instrument; 20 21 import java.lang.instrument.ClassFileTransformer ; 22 import java.lang.instrument.IllegalClassFormatException ; 23 import java.security.ProtectionDomain ; 24 import java.util.Map ; 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 39 public class UnitClassTransformer implements ClassTransformer { 40 41 protected ClassLoader tempClassLoader; 42 protected Log logger; 43 protected ClassFileTransformer transformer; 44 protected Map <String , JpaClassDescriptor> managedClasses; 45 46 public UnitClassTransformer(Map <String , JpaClassDescriptor> managedClasses, 47 ClassLoader tempClassLoader, ClassFileTransformer 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 loader, 56 String className, 57 Class <?> classBeingRedefined, 58 ProtectionDomain protectionDomain, 59 byte[] classfileBuffer) throws IllegalClassFormatException { 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 84 protected boolean isManagedClass(String className) { 85 return managedClasses.containsKey(className.replace('/', '.')); 86 } 87 } 88 | Popular Tags |