1 4 package com.tc.aspectwerkz.hook.impl; 5 6 import com.tc.aspectwerkz.hook.ClassPreProcessor; 7 8 import java.security.ProtectionDomain ; 9 import java.lang.reflect.Method ; 10 11 19 public class ClassPreProcessorHelper { 20 23 private static ClassPreProcessor preProcessor; 24 25 28 private static boolean preProcessorInitialized; 29 30 33 private static String PRE_PROCESSOR_CLASSNAME_PROPERTY = "aspectwerkz.classloader.preprocessor"; 34 35 38 private static String PRE_PROCESSOR_CLASSNAME_DEFAULT = "com.tc.aspectwerkz.transform.AspectWerkzPreProcessor"; 39 40 static { 41 initializePreProcessor(); 42 } 43 44 49 public static ClassPreProcessor getClassPreProcessor() { 50 return preProcessor; 51 } 52 53 57 public static synchronized void initializePreProcessor() { 58 if (preProcessorInitialized) { 59 return; 60 } 61 preProcessorInitialized = true; 62 Class klass = null; 63 String s = System.getProperty(PRE_PROCESSOR_CLASSNAME_PROPERTY, PRE_PROCESSOR_CLASSNAME_DEFAULT); 64 try { 65 klass = Class.forName(s, true, ClassLoader.getSystemClassLoader()); 68 } catch (ClassNotFoundException _ex) { 69 System.err.println("AspectWerkz - WARN - Pre-processor class '" + s + "' not found"); 70 } 71 if (klass != null) { 72 try { 73 preProcessor = (ClassPreProcessor) klass.newInstance(); 74 preProcessor.initialize(); 75 System.err.println("AspectWerkz - INFO - Pre-processor " + s + " loaded and initialized"); 76 } catch (Throwable throwable) { 77 System.err.println("AspectWerkz - WARN - Error initializing pre-processor class " + s + ':'); 78 throwable.printStackTrace(); 79 } 80 } 81 } 82 83 86 public static byte[] defineClass0Pre(ClassLoader caller, 87 String name, 88 byte[] b, 89 int off, 90 int len, 91 ProtectionDomain pd) { 92 if (preProcessor == null) { 93 byte[] obyte = new byte[len]; 97 System.arraycopy(b, off, obyte, 0, len); 98 return obyte; 99 } else { 100 try { 101 byte[] ibyte = new byte[len]; 102 System.arraycopy(b, off, ibyte, 0, len); 103 return preProcessor.preProcess(name, ibyte, caller); 104 } catch (Throwable throwable) { 105 System.err.println( 106 "AspectWerkz - WARN - Error pre-processing class " 107 + name 108 + " in " 109 + Thread.currentThread() 110 ); 111 throwable.printStackTrace(); 112 byte[] obyte = new byte[len]; 114 System.arraycopy(b, off, obyte, 0, len); 115 return obyte; 116 } 117 } 118 } 119 120 134 public static Object defineClass0Pre(ClassLoader caller, 135 String name, 136 Object byteBuffer, 137 int off, 138 int len, 139 ProtectionDomain pd) { 140 byte[] bytes = new byte[len]; 141 byteBufferGet(byteBuffer, bytes, off, len); 143 byte[] newbytes = defineClass0Pre(caller, name, bytes, 0, bytes.length, pd); 144 Object newBuffer = byteBufferWrap(newbytes); 146 return newBuffer; 147 } 148 149 158 private static void byteBufferGet(Object byteBuffer, byte[] dest, int offset, int length) { 159 try { 160 Class cByteBuffer = Class.forName("java.nio.ByteBuffer"); 161 Method mGet = cByteBuffer.getDeclaredMethod("getDefault", new Class []{BYTE_ARRAY_CLASS, int.class, int.class}); 162 mGet.invoke(byteBuffer, new Object []{dest, new Integer (offset), new Integer (length)}); 163 } catch (Throwable t) { 164 System.err.println("AW : java.nio not supported"); 165 throw new RuntimeException (t.toString()); 166 } 167 } 168 169 176 private static Object byteBufferWrap(byte[] bytes) { 177 try { 178 Class cByteBuffer = Class.forName("java.nio.ByteBuffer"); 179 Method mGet = cByteBuffer.getDeclaredMethod("wrap", new Class []{BYTE_ARRAY_CLASS}); 180 Object byteBuffer = mGet.invoke(null, new Object []{bytes}); 181 return byteBuffer; 182 } catch (Throwable t) { 183 System.err.println("AW : java.nio not supported"); 184 throw new RuntimeException (t.toString()); 185 } 186 } 187 188 private final static byte[] EMPTY_BYTEARRAY = new byte[0]; 189 private final static Class BYTE_ARRAY_CLASS = EMPTY_BYTEARRAY.getClass(); 190 191 } | Popular Tags |