1 19 20 package org.apache.cayenne.jpa.instrument; 21 22 import java.lang.instrument.ClassFileTransformer ; 23 import java.lang.instrument.IllegalClassFormatException ; 24 import java.lang.instrument.Instrumentation ; 25 import java.security.ProtectionDomain ; 26 27 import javax.persistence.spi.ClassTransformer; 28 29 import org.apache.cayenne.instrument.InstrumentUtil; 30 import org.apache.cayenne.jpa.JpaUnit; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 39 public class InstrumentingUnit extends JpaUnit { 40 41 protected Log logger; 42 43 @Override 44 public void addTransformer(final ClassTransformer transformer) { 45 46 ClassFileTransformer transformerWrapper = new ClassFileTransformer () { 48 49 public byte[] transform( 50 ClassLoader loader, 51 String className, 52 Class <?> classBeingRedefined, 53 ProtectionDomain protectionDomain, 54 byte[] classfileBuffer) throws IllegalClassFormatException { 55 56 return transformer.transform( 57 loader, 58 className, 59 classBeingRedefined, 60 protectionDomain, 61 classfileBuffer); 62 } 63 }; 64 65 getLogger().info("*** Adding transformer: " + transformer); 66 67 Instrumentation i = InstrumentUtil.getInstrumentation(); 68 if (i == null) { 69 throw new IllegalStateException ("Attempt to add a transformer failed - " 70 + "instrumentation is not initialized."); 71 } 72 73 i.addTransformer(transformerWrapper); 74 } 75 76 protected Log getLogger() { 77 if (logger == null) { 78 logger = LogFactory.getLog(getClass()); 79 } 80 81 return logger; 82 } 83 } 84 | Popular Tags |