1 21 package oracle.toplink.essentials.internal.weaving; 23 24 import java.lang.instrument.*; 26 import java.io.FileOutputStream ; 27 import java.security.ProtectionDomain ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 import java.io.File ; 31 import javax.persistence.spi.ClassTransformer; 32 33 import oracle.toplink.libraries.asm.*; 35 import oracle.toplink.libraries.asm.attrs.Attributes; 36 37 import oracle.toplink.essentials.logging.SessionLog; 39 import oracle.toplink.essentials.sessions.Session; 40 41 42 48 public class TopLinkWeaver implements ClassTransformer { 49 50 public static final String WEAVING_OUTPUT_PATH = "toplink.weaving.output.path"; 51 public static final String WEAVING_SHOULD_OVERWRITE = "toplink.weaving.overwrite.existing"; 52 public static final String WEAVER_NOT_OVERWRITING = "weaver_not_overwriting"; 53 public static final String WEAVER_COULD_NOT_WRITE = "weaver_could_not_write"; 54 55 protected Session session; protected Map classDetailsMap; 58 59 public TopLinkWeaver(Session session, Map classDetailsMap) { 60 this.session = session; 61 this.classDetailsMap = classDetailsMap; 62 } 63 64 public Map getClassDetailsMap() { 65 return classDetailsMap; 66 } 67 68 public byte[] transform(ClassLoader loader, String className, 71 Class classBeingRedefined, ProtectionDomain protectionDomain, 72 byte[] classfileBuffer) throws IllegalClassFormatException { 73 74 84 ClassDetails classDetails = (ClassDetails)classDetailsMap.get(className); 85 if (classDetails != null) { 86 ClassReader cr = new ClassReader(classfileBuffer); 87 ClassWriter cw = new ClassWriter(true, true); 88 TopLinkClassWeaver tcw = new TopLinkClassWeaver(cw, classDetails); 89 cr.accept(tcw, Attributes.getDefaultAttributes(), false); 90 if (tcw.alreadyWeaved) { 91 return null; 92 } 93 byte[] bytes = cw.toByteArray(); 94 95 String outputPath = System.getProperty(WEAVING_OUTPUT_PATH, ""); 96 97 if (!outputPath.equals("")) { 98 outputFile(className, bytes, outputPath); 99 } 100 if (tcw.weavedVH) { 101 return bytes; 102 } 103 } 104 return null; } 106 107 protected void outputFile(String className, byte[] classBytes, String outputPath){ 108 StringBuffer directoryName = new StringBuffer ();; 109 StringTokenizer tokenizer = new StringTokenizer (className, "\n\\/"); 110 String token = null; 111 while (tokenizer.hasMoreTokens()){ 112 token = tokenizer.nextToken(); 113 if (tokenizer.hasMoreTokens()){ 114 directoryName.append(token + File.separator); 115 } 116 } 117 try{ 118 String usedOutputPath = outputPath; 119 if (!outputPath.endsWith(File.separator)){ 120 usedOutputPath = outputPath + File.separator; 121 } 122 File file = new File (usedOutputPath + directoryName); 123 file.mkdirs(); 124 file = new File (file, token + ".class"); 125 if (!file.exists()){ 126 file.createNewFile(); 127 } else { 128 if (!System.getProperty(WEAVING_SHOULD_OVERWRITE, "false").equalsIgnoreCase("true")){ 129 ((oracle.toplink.essentials.internal.sessions.AbstractSession)session).log( 130 SessionLog.WARNING, SessionLog.WEAVER, WEAVER_NOT_OVERWRITING, className); 131 return; 132 } 133 } 134 FileOutputStream fos = new FileOutputStream (file); 135 fos.write(classBytes); 136 fos.close(); 137 } catch (Exception e){ 138 ((oracle.toplink.essentials.internal.sessions.AbstractSession)session).log( 139 SessionLog.WARNING, SessionLog.WEAVER, WEAVER_COULD_NOT_WRITE, className, e); 140 } 141 } 142 143 protected static String getShortName(String name) { 146 int pos = name.lastIndexOf('/'); 147 if (pos >= 0) { 148 name = name.substring(pos+1); 149 if (name.endsWith(";")) { 150 name = name.substring(0, name.length()-1); 151 } 152 return name; 153 } 154 return ""; 155 } 156 } 157 | Popular Tags |