001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.viewer;
028:
029: import org.cougaar.tools.csmart.util.FileParseUtil;
030: import org.cougaar.util.ConfigFinder;
031: import org.cougaar.util.StringUtility;
032:
033: import javax.swing.*;
034: import java.io.File;
035: import java.io.FileNotFoundException;
036: import java.io.IOException;
037: import java.io.InputStream;
038: import java.net.MalformedURLException;
039: import java.net.URL;
040: import java.util.ArrayList;
041: import java.util.HashMap;
042: import java.util.List;
043: import java.util.Map;
044: import java.util.Vector;
045:
046: /**
047: * Most of this was taken from ConfigFinder. ConfigFinder is used
048: * to find the society file that the user chooses, so we want to open
049: * a file chooser on the first path that ConfigFinder will use.
050: * @see org.cougaar.util.ConfigFinder
051: */
052: public final class SocietyFinder {
053: private static final String defaultConfigPath = "$CWD;$HOME/.alp;$INSTALL/configs/$CONFIG;$INSTALL/configs/common";
054: private static List configPath = new ArrayList();
055: private static Map defaultProperties;
056: private static Map properties = null;
057: private static SocietyFinder defaultSocietyFinder;
058:
059: static {
060: Map m = new HashMap();
061: defaultProperties = m;
062:
063: File ipf = new File(System.getProperty(
064: "org.cougaar.install.path", "."));
065: try {
066: ipf = ipf.getCanonicalFile();
067: } catch (IOException ioe) {
068: }
069: String ipath = ipf.toString();
070: m.put("INSTALL", ipath);
071:
072: m.put("HOME", System.getProperty("user.home"));
073: m.put("CWD", System.getProperty("user.dir"));
074:
075: File csf = new File(ipath, "configs");
076: try {
077: csf = csf.getCanonicalFile();
078: } catch (IOException ioe) {
079: }
080: String cspath = csf.toString();
081: m.put("CONFIGS", cspath);
082:
083: String cs = System.getProperty("org.cougaar.config", "common");
084: if (cs != null)
085: m.put("CONFIG", cs);
086:
087: defaultSocietyFinder = new SocietyFinder(System
088: .getProperty("org.cougaar.config.path"),
089: defaultProperties);
090: }
091:
092: /**
093: * Use SocietyFinder.getInstance instead of constructor.
094: * @param s configuration path
095: * @param p properties
096: */
097: public SocietyFinder(String s, Map p) {
098: properties = p;
099: if (s == null) {
100: s = defaultConfigPath;
101: } else {
102: s = s.replace('\\', '/'); // Make sure its a URL and not a file path
103: }
104:
105: // append the default if we end with a ';'
106: if (s.endsWith(";"))
107: s += defaultConfigPath;
108:
109: Vector v = StringUtility.parseCSV(s, ';');
110: int l = v.size();
111: for (int i = 0; i < l; i++) {
112: appendPathElement((String) v.elementAt(i));
113: }
114: }
115:
116: /**
117: * Return an instance of <code>SocietyFinder</code>.
118: */
119: public static SocietyFinder getInstance() {
120: return defaultSocietyFinder;
121: }
122:
123: private void appendPathElement(String el) {
124: String s = el;
125: try {
126: s = substituteProperties(el);
127: s = s.replace('\\', '/').replace('\\', '/'); // These should be URL-like
128: try {
129: if (!s.endsWith("/"))
130: s += "/";
131: appendPathElement(new URL(s));
132: } catch (MalformedURLException mue) {
133: File f = new File(s);
134: if (f.isDirectory()) {
135: appendPathElement(new File(s).getCanonicalFile()
136: .toURL());
137: } // else skip it.
138: }
139: } catch (Exception e) {
140: System.err.println("Failed to interpret " + el
141: + " as url: " + e);
142: }
143: }
144:
145: private void appendPathElement(URL url) {
146: configPath.add(url);
147: }
148:
149: private int indexOfNonAlpha(String s, int i) {
150: int l = s.length();
151: for (int j = i; j < l; j++) {
152: char c = s.charAt(j);
153: if (!Character.isLetterOrDigit(c))
154: return j;
155: }
156: return -1;
157: }
158:
159: private String substituteProperties(String s) {
160: int i = s.indexOf('$');
161: if (i >= 0) {
162: int j = indexOfNonAlpha(s, i + 1);
163: String s0 = s.substring(0, i);
164: String s2 = (j < 0) ? "" : s.substring(j);
165: String k = s.substring(i + 1, (j < 0) ? s.length() : j);
166: Object o = properties.get(k);
167: if (o == null) {
168: throw new IllegalArgumentException(
169: "No such path property \"" + k + "\"");
170: }
171: return substituteProperties(s0 + o.toString() + s2);
172: }
173: return s;
174: }
175:
176: /**
177: * Get the first path that ConfigFinder will search.
178: * @return the path
179: */
180: public String getPath() {
181: for (int i = 0; i < configPath.size(); i++) {
182: URL url = (URL) configPath.get(i);
183: if (url.getProtocol().equals("file"))
184: return url.getFile();
185: }
186: return ".";
187: }
188:
189: /**
190: * Return names of all agent files in the specified directory.
191: * Currently searches all files whose names end with ".ini" for
192: * the pattern: [ Cluster ]
193: * @param directory to search
194: * @return string array of agent filenames
195: */
196: public String[] getAgentFilenames(File directory) {
197: File[] files = directory.listFiles();
198: ArrayList agentFilenames = new ArrayList(files.length);
199: for (int i = 0; i < files.length; i++) {
200: String path = files[i].getPath(); // FileParseUtil needs complete path
201: if (!path.endsWith(".ini"))
202: continue;
203: if (FileParseUtil.containsPattern(path, "\\[ Cluster \\]"))
204: agentFilenames.add(path);
205: }
206: return (String[]) agentFilenames
207: .toArray(new String[agentFilenames.size()]);
208: }
209:
210: /**
211: * Locate an actual file in the config path. This will skip over
212: * elements of org.cougaar.config.path that are not file: urls.
213: **/
214: public File locateFile(String aFilename) {
215: if (aFilename == null)
216: return null;
217: File result = new File(aFilename);
218: if (result.exists())
219: return result;
220: for (int i = 0; i < configPath.size(); i++) {
221: URL url = (URL) configPath.get(i);
222: if (url.getProtocol().equals("file")) {
223: try {
224: URL fileURL = new URL(url, aFilename);
225: result = new File(fileURL.getFile());
226: //if (verbose) { System.err.print("Looking for "+result+": "); }
227: if (result.exists()) {
228: //if (verbose) { System.err.println("Found it. File " + aFilename +
229: // " is " + fileURL); }
230: return result;
231: } else {
232: //if (verbose) { System.err.println(); }
233: }
234: } catch (MalformedURLException mue) {
235: continue;
236: }
237: }
238: }
239: return null;
240: }
241:
242: /**
243: * Opens an InputStream to access the named file. The file is sought
244: * in all the places specified in configPath.
245: * @throws IOException if the resource cannot be found.
246: **/
247: public InputStream open(String aURL) throws IOException {
248: // First, see if the file can be opened as is
249: try {
250: File url = new File(aURL);
251: //System.out.println("Trying "+url+": ");
252: InputStream is = url.toURL().openStream();
253: //System.err.println("Found it. File " + aURL + " is " + url);
254: if (is != null)
255: return is;
256: }
257: // catch (MalformedURLException mue) {
258: // System.out.println("Got exception" + mue);
259: // }
260: catch (IOException ioe) {
261: //System.out.println("Got exception" + ioe);
262: }
263:
264: // Then try all the things on the ConfigPath
265: for (int i = 0; i < configPath.size(); i++) {
266: URL base = (URL) configPath.get(i);
267: try {
268: URL url = new URL(base, aURL);
269: //System.out.println("Trying "+url+": ");
270: InputStream is = url.openStream();
271: if (is == null)
272: continue; // Don't return null
273: //System.out.println("Found it. File " + aURL + " is " + url);
274: return is;
275: } catch (MalformedURLException mue) {
276: //if (verbose) { System.err.println(); }
277: //System.out.println("Got exception" + mue);
278: continue;
279: } catch (IOException ioe) {
280: //if (verbose) { System.err.println(); }
281: //System.out.println("Got exception" + ioe);
282: continue;
283: }
284: }
285: throw new FileNotFoundException("Exhausted options for: "
286: + aURL);
287: }
288:
289: public static void main(String[] args) {
290: JFileChooser chooser = new JFileChooser(SocietyFinder
291: .getInstance().getPath());
292: chooser
293: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
294: File file = null;
295: while (file == null) {
296: int result = chooser.showDialog(null, "OK");
297: if (result != JFileChooser.APPROVE_OPTION)
298: return;
299: file = chooser.getSelectedFile();
300: }
301: String name = "";
302: name = file.getName();
303: if (name.endsWith(".ini"))
304: name = name.substring(0, name.length() - 4);
305: if (file.isDirectory()) {
306: String[] filenames = SocietyFinder.getInstance()
307: .getAgentFilenames(file);
308: if (filenames == null || filenames.length == 0) {
309: // Found no Agents
310: System.out.println("Found no agent in dir "
311: + file.getPath());
312: return;
313: }
314: for (int i = 0; i < filenames.length; i++)
315: System.out.println(filenames[i]);
316: }
317: }
318: }
|