001: package org.acm.seguin.completer;
002:
003: //import anthelper.ClassNameCache;
004: //import anthelper.Context;
005: //import anthelper.ContextMgr;
006: //import anthelper.JavaUtils;
007: //import anthelper.utils.JEditLogger;
008: import org.acm.seguin.completer.info.ClassTable;
009: import org.acm.seguin.ide.jedit.Navigator;
010: import java.util.*;
011: import org.gjt.sp.jedit.Buffer;
012: import org.gjt.sp.jedit.View;
013: import org.gjt.sp.jedit.jEdit;
014: import org.gjt.sp.jedit.textarea.JEditTextArea;
015: import org.acm.seguin.ide.jedit.Navigator;
016:
017: /**
018: * Manages/Abstracts where the class path comes from.
019: *
020: * @author btateyama@yahoo.com
021: * @created February 15, 2003
022: */
023: public class ClassPathSrcMgr {
024: final static Navigator.NavigatorLogger logger = Completer
025: .getLogger(ClassPathSrcMgr.class);
026:
027: private static ClassPathSrcMgr instance = new ClassPathSrcMgr();
028: private static Map mapPathToCNC = new Hashtable();
029:
030: private static ClassNameCache getCNCFromCache(String argPath) {
031: ClassNameCache cnc = null;
032:
033: if (argPath == null) {
034: argPath = "";
035: }
036: cnc = (ClassNameCache) mapPathToCNC.get(argPath);
037: if (cnc == null) {
038: cnc = new ClassNameCache(argPath);
039: mapPathToCNC.put(argPath, cnc);
040: }
041:
042: return cnc;
043: }
044:
045: public static ClassPathSrcMgr getInstance() {
046: return instance;
047: }
048:
049: static boolean isAntHelperSrc() {
050: String strSrc = Config.CLASS_PATH_SOURCE.getAsString();
051: return Config.CP_SRC_ANT_HELPER.equals(strSrc);
052: }
053:
054: static boolean isJCompilerSrc() {
055: String strSrc = Config.CLASS_PATH_SOURCE.getAsString();
056: return Config.CP_SRC_JCOMPILER.equals(strSrc);
057: }
058:
059: static boolean isCustomSrc() {
060: String strSrc = Config.CLASS_PATH_SOURCE.getAsString();
061: return Config.CP_SRC_CUSTOM.equals(strSrc);
062: }
063:
064: Set _setClassPathSrcs = new HashSet();
065:
066: public ClassPathSrc getClassPathSrc(View argView, Buffer argBuffer) {
067: ClassPathSrc cpSrc = null;
068: if (isAntHelperSrc()) {
069: Context context = ContextMgr.getContext(argView, argBuffer,
070: false);
071: if (context != null) {
072: cpSrc = new AntHelperClassPathSrc(context);
073: }
074: } else if (isJCompilerSrc()) {
075: cpSrc = new JCompilerSrc();
076: } else if (isCustomSrc()) {
077: cpSrc = new CustomSrc();
078: } else {
079: logger.error("Unknown src type: "
080: + Config.CLASS_PATH_SOURCE.getAsString());
081: }
082:
083: // keep track of what's out there
084: _setClassPathSrcs.add(cpSrc);
085: return cpSrc;
086: }
087:
088: public void reload() {
089: // reload the cache!
090: mapPathToCNC.clear();
091: ClassPathSrc cpSrc;
092: logger.msg("ClassPathSrcs", _setClassPathSrcs.size());
093: for (Iterator it = _setClassPathSrcs.iterator(); it.hasNext();) {
094: cpSrc = (ClassPathSrc) it.next();
095: logger.msg("Reloading classes for source: "
096: + cpSrc.getDesc());
097: cpSrc.reload();
098: // now reinit the class table
099: ClassTable.getInstance(cpSrc).reinit(cpSrc);
100: }
101: }
102:
103: public void cleanup() {
104: ClassPathSrc cpSrc;
105: for (Iterator it = _setClassPathSrcs.iterator(); it.hasNext();) {
106: cpSrc = (ClassPathSrc) it.next();
107: ClassTable.removeInstance(cpSrc);
108: }
109:
110: _setClassPathSrcs.clear();
111: mapPathToCNC.clear();
112: }
113:
114: static boolean equals(Object arg1, Object arg2) {
115: return ((arg1 == arg2) || (arg1 != null && arg1.equals(arg2)));
116: }
117:
118: static class AntHelperClassPathSrc implements ClassPathSrc {
119: Context _context = null;
120:
121: public AntHelperClassPathSrc(Context argContext) {
122: _context = argContext;
123: }
124:
125: public String getDesc() {
126: return "[AntHelperClassPathSrc, Context="
127: + _context.getBuildFile().getPath() + "]";
128: }
129:
130: public ClassNameCache getCNC() {
131: return _context.getClassNameCache();
132: }
133:
134: public String getClassPath() {
135: return _context.getClassPath();
136: }
137:
138: public void reload() {
139: // force the reload of the cache
140: // @REVIEW: unfortunately, we can't use AntHelperPlugin.flushCache();
141: _context.resetClassNameCache();
142: }
143:
144: public Object getKey() {
145: return _context;
146: }
147:
148: public int hashCode() {
149: return (getClassPath() != null ? getClassPath().hashCode() + 41577
150: : 0);
151: }
152:
153: public boolean equals(Object obj) {
154: if (obj instanceof AntHelperClassPathSrc) {
155: AntHelperClassPathSrc antSrc = (AntHelperClassPathSrc) obj;
156: return ClassPathSrcMgr.equals(antSrc.getClassPath(),
157: getClassPath());
158: } else {
159: return false;
160: }
161: }
162: }
163:
164: static abstract class AbstractPathSrc implements ClassPathSrc {
165: String _strPath;
166: String _strDesc;
167: Object _objKey;
168: ClassNameCache _cnc = null;
169:
170: public AbstractPathSrc(String argDesc, Object argKey) {
171: _strDesc = argDesc;
172: _objKey = argKey;
173: }
174:
175: public ClassNameCache getCNC() {
176: return _cnc;
177: }
178:
179: public void reload() {
180: _strPath = getClassPath();
181: _cnc = getCNCFromCache(_strPath);
182: }
183:
184: public String getDesc() {
185: return _strDesc;
186: }
187:
188: public Object getKey() {
189: return _objKey;
190: }
191:
192: public int hashCode() {
193: return (_strPath != null ? _strPath.hashCode() + 41577 : 0);
194: }
195:
196: public boolean equals(Object obj) {
197: if (obj instanceof AbstractPathSrc) {
198: AbstractPathSrc src = (AbstractPathSrc) obj;
199: return (ClassPathSrcMgr.equals(src.getKey(), _objKey) && ClassPathSrcMgr
200: .equals(src.getClassPath(), _strPath));
201: } else {
202: return false;
203: }
204: }
205:
206: public abstract String getClassPath();
207: }
208:
209: static class JCompilerSrc extends AbstractPathSrc {
210: public JCompilerSrc() {
211: super ("[JCompilerSrc]", Config.CP_SRC_JCOMPILER);
212: reload();
213: }
214:
215: public String getClassPath() {
216: return jEdit.getProperty("jcompiler.classpath", "");
217: }
218: }
219:
220: static class CustomSrc extends AbstractPathSrc {
221: public CustomSrc() {
222: super ("[Custom]", Config.CP_SRC_CUSTOM);
223: reload();
224: }
225:
226: public String getClassPath() {
227: return Config.CLASS_PATH_CUSTOM.getAsString();
228: }
229: }
230: }
|