001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.results.simple;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.Reader;
031: import java.lang.ref.Reference;
032: import java.lang.ref.SoftReference;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.hammurapi.HammurapiRuntimeException;
037: import org.hammurapi.WaiverSet;
038: import org.hammurapi.results.ReviewResults;
039:
040: import com.pavelvlasov.jsel.CompilationUnit;
041: import com.pavelvlasov.jsel.JselException;
042: import com.pavelvlasov.jsel.impl.CompilationUnitImpl;
043: import com.pavelvlasov.logging.Logger;
044:
045: /**
046: * @author Pavel Vlasov
047: * @version $Revision: 1.3 $
048: */
049: public class ReparsingReviewResults extends SimpleDetailedResults
050: implements ReviewResults {
051: /**
052: * Comment for <code>serialVersionUID</code>
053: */
054: private static final long serialVersionUID = -349746437092882478L;
055: private String path;
056:
057: private static class Context {
058: private Reference ref;
059: private ClassLoader classLoader;
060: private Logger logger;
061: }
062:
063: private static ThreadLocal contextMap = new ThreadLocal() {
064: protected Object initialValue() {
065: return new HashMap();
066: }
067: };
068: private int tabSize;
069: private String encoding;
070:
071: ReparsingReviewResults(CompilationUnit cu, WaiverSet waiverSet,
072: ClassLoader classLoader, int tabSize, Logger logger) {
073: super (cu.getName(), waiverSet);
074: path = cu.getFile().getAbsolutePath();
075: this .encoding = cu.getEncoding();
076: Context context = new Context();
077: context.ref = new SoftReference(cu);
078: context.classLoader = classLoader;
079: context.logger = logger;
080: ((Map) contextMap.get()).put(path, context);
081: this .tabSize = tabSize;
082: }
083:
084: public CompilationUnit getCompilationUnit() {
085: Context context = (Context) ((Map) contextMap.get()).get(path);
086:
087: CompilationUnit cu = (CompilationUnit) (context.ref == null ? null
088: : context.ref.get());
089: if (cu == null) {
090: try {
091: InputStream is = new FileInputStream(new File(path));
092: Reader reader = encoding == null ? new InputStreamReader(
093: is)
094: : new InputStreamReader(is, encoding);
095: try {
096: cu = new CompilationUnitImpl(reader, encoding,
097: null, path, tabSize, context.classLoader,
098: context.logger);
099: } finally {
100: reader.close();
101: is.close();
102: }
103: } catch (JselException e) {
104: throw new HammurapiRuntimeException(e);
105: } catch (IOException e) {
106: throw new HammurapiRuntimeException(e);
107: }
108: context.ref = new SoftReference(cu);
109: }
110: return cu;
111: }
112: }
|