1 23 package org.hammurapi.results.simple; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.Reader ; 31 import java.lang.ref.Reference ; 32 import java.lang.ref.SoftReference ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import org.hammurapi.HammurapiRuntimeException; 37 import org.hammurapi.WaiverSet; 38 import org.hammurapi.results.ReviewResults; 39 40 import com.pavelvlasov.jsel.CompilationUnit; 41 import com.pavelvlasov.jsel.JselException; 42 import com.pavelvlasov.jsel.impl.CompilationUnitImpl; 43 import com.pavelvlasov.logging.Logger; 44 45 49 public class ReparsingReviewResults extends SimpleDetailedResults implements ReviewResults { 50 53 private static final long serialVersionUID = -349746437092882478L; 54 private String path; 55 56 private static class Context { 57 private Reference ref; 58 private ClassLoader classLoader; 59 private Logger logger; 60 } 61 62 private static ThreadLocal contextMap=new ThreadLocal () { 63 protected Object initialValue() { 64 return new HashMap (); 65 } 66 }; 67 private int tabSize; 68 private String encoding; 69 70 ReparsingReviewResults(CompilationUnit cu, WaiverSet waiverSet, ClassLoader classLoader, int tabSize, Logger logger) { 71 super(cu.getName(), waiverSet); 72 path=cu.getFile().getAbsolutePath(); 73 this.encoding=cu.getEncoding(); 74 Context context=new Context(); 75 context.ref=new SoftReference (cu); 76 context.classLoader=classLoader; 77 context.logger=logger; 78 ((Map ) contextMap.get()).put(path, context); 79 this.tabSize=tabSize; 80 } 81 82 public CompilationUnit getCompilationUnit() { 83 Context context=(Context) ((Map ) contextMap.get()).get(path); 84 85 CompilationUnit cu = (CompilationUnit) (context.ref==null ? null : context.ref.get()); 86 if (cu==null) { 87 try { 88 InputStream is=new FileInputStream (new File (path)); 89 Reader reader = encoding==null ? new InputStreamReader (is) : new InputStreamReader (is, encoding); 90 try { 91 cu=new CompilationUnitImpl(reader, encoding, null, path, tabSize, context.classLoader, context.logger); 92 } finally { 93 reader.close(); 94 is.close(); 95 } 96 } catch (JselException e) { 97 throw new HammurapiRuntimeException(e); 98 } catch (IOException e) { 99 throw new HammurapiRuntimeException(e); 100 } 101 context.ref=new SoftReference (cu); 102 } 103 return cu; 104 } 105 } 106 | Popular Tags |