001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 1999-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: package org.cougaar.glm.plugins;
027:
028: import org.cougaar.util.ConfigFinder;
029: import org.cougaar.util.log.Logger;
030: import org.cougaar.util.log.Logging;
031:
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035: import java.io.IOException;
036: import java.io.InputStreamReader;
037: import java.io.LineNumberReader;
038: import java.io.PrintWriter;
039: import java.util.Enumeration;
040: import java.util.Vector;
041:
042: public final class FileUtils {
043:
044: // Constants
045: private static final int NOTFOUND = -1;
046: private static Logger logger = Logging.getLogger(FileUtils.class);
047:
048: public static Vector findFields(String line, char delim) {
049:
050: Vector fields = new Vector();
051: int start = 0;
052: int end = 0;
053: boolean DONE = false;
054:
055: while (!DONE) {
056: end = line.indexOf(delim, start);
057: if (end == NOTFOUND) {
058: DONE = true;
059: end = line.length();
060: }
061:
062: String f = line.substring(start, end);
063:
064: f = f.trim();
065: if ((f.length() > 0)) {
066: fields.addElement(new String(f));
067: }
068: start = end + 1;
069:
070: }
071: return fields;
072: // end of findFields
073: }
074:
075: public static Enumeration readFile(String fullPath) {
076: Vector readLines = new Vector();
077: try {
078: FileInputStream fis = new FileInputStream(
079: new File(fullPath));
080: InputStreamReader isr = new InputStreamReader(fis);
081: LineNumberReader reader = new LineNumberReader(isr);
082: String line;
083: while ((line = reader.readLine()) != null) {
084: readLines.add(line);
085: }
086: reader.close();
087: } catch (IOException ioe) {
088: if (logger.isErrorEnabled()) {
089: logger.error("readFile()" + " IOEXception for "
090: + fullPath + ": " + ioe);
091: }
092: }
093: return readLines.elements();
094: }
095:
096: /**
097: * @deprecated Use readConfigFile(filename,configFinder) with plugins or clusters config finder passed *
098: */
099: public static Enumeration readConfigFile(String filename) {
100: return readConfigFile(filename, ConfigFinder.getInstance());
101: }
102:
103: public static Enumeration readConfigFile(String filename,
104: ConfigFinder finder) {
105: Vector readLines = new Vector();
106: try {
107: InputStreamReader isr = new InputStreamReader(finder
108: .open(filename));
109: LineNumberReader lnr = new LineNumberReader(isr);
110: String line;
111: while ((line = lnr.readLine()) != null) {
112: readLines.add(line);
113: }
114: isr.close();
115: } catch (IOException ioe) {
116: // The file may not exist in normal operation (Inventory files).
117: // Print a debug message and return null so calling module can handle it
118: if (logger.isErrorEnabled()) {
119: logger.error("readConfigFile()" + " IOEXception for "
120: + filename + ": " + ioe);
121: }
122: return null;
123: }
124: return readLines.elements();
125: }
126:
127: public static void writeFile(String fullPath, boolean append,
128: Enumeration data) {
129: try {
130: FileOutputStream fos = new FileOutputStream(fullPath,
131: append);
132: PrintWriter pw = new PrintWriter(fos, true);
133: while (data.hasMoreElements()) {
134: pw.println(data.nextElement());
135: }
136: pw.close();
137: } catch (IOException ioe) {
138: if (logger.isErrorEnabled()) {
139: logger.error("writeFile()" + "IOExceptionfor "
140: + fullPath + ": " + ioe);
141: }
142: }
143: }
144:
145: public static boolean renameFile(String from, String to) {
146: // This method is actually rename file
147: File orig_file = new File(from);
148: try {
149: if (!orig_file.exists()) {
150: if (logger.isErrorEnabled()) {
151: logger.error("moveFile()"
152: + "Source file does not exist. " + from);
153: }
154: return false;
155: }
156: if (!orig_file.canWrite()) {
157: if (logger.isErrorEnabled()) {
158: logger.error("moveFile()"
159: + "Permissions problem: Cannot move file "
160: + from);
161: }
162: return false;
163: }
164: File new_file = new File(to);
165: orig_file.renameTo(new_file);
166:
167: } catch (SecurityException se) {
168: if (logger.isErrorEnabled()) {
169: logger.error("moveFile()" + "Security Exception for "
170: + from + ": " + se);
171: }
172: return false;
173: }
174: return true;
175: }
176:
177: public static boolean deleteFile(String filename) {
178: File discardFile = new File(filename);
179: if (discardFile.exists()) {
180: discardFile.delete();
181: }
182: return true;
183: }
184:
185: public static boolean copyFile(String from, String to) {
186: Enumeration fileContents = readFile(from);
187: writeFile(to, false, fileContents);
188: return true;
189: }
190:
191: public static boolean asciiToBinary(String bin_path, String type,
192: String file_name) {
193: // Warning: uses a linux executable.
194: String fail_msg = type + " failure to convert input file: "
195: + file_name + " because: ";
196: if (!System.getProperty("os.name").equals("Linux")) {
197: if (logger.isErrorEnabled()) {
198: logger
199: .error("asciiToBinary()"
200: + fail_msg
201: + "icisatob exec only expected to run on Linux.");
202: }
203: return false;
204: }
205: Runtime rt = Runtime.getRuntime();
206: try {
207: Process atob = rt.exec(bin_path + "icisatob " + type + " "
208: + file_name);
209: atob.waitFor();
210: } catch (InterruptedException ie) {
211: if (logger.isErrorEnabled()) {
212: logger.error("asciiToBinary()" + fail_msg + ie);
213: }
214: return false;
215: } catch (IOException ioe) {
216: if (logger.isErrorEnabled()) {
217: logger.error("asciiToBinary()" + fail_msg + ioe);
218: }
219: return false;
220: }
221: return true;
222: }
223:
224: public static boolean binaryToAscii(String bin_path, String type,
225: String file_name) {
226: // Warning: uses a linux executable.
227: String fail_msg = type + " failure to convert input file: "
228: + file_name + " because: ";
229: if (!System.getProperty("os.name").equals("Linux")) {
230: if (logger.isErrorEnabled()) {
231: logger
232: .error("binaryToAscii()"
233: + fail_msg
234: + "icisbtoa exec only expected to run on Linux.");
235: }
236: return false;
237: }
238: Runtime rt = Runtime.getRuntime();
239: try {
240: Process atob = rt.exec(bin_path + "icisbtoa " + type + " "
241: + file_name);
242: atob.waitFor();
243: } catch (InterruptedException ie) {
244: if (logger.isErrorEnabled()) {
245: logger.error("binaryToAscii()" + fail_msg + ie);
246: }
247: return false;
248: } catch (IOException ioe) {
249: if (logger.isErrorEnabled()) {
250: logger.error("binaryToAscii()" + fail_msg + ioe);
251: }
252: return false;
253: }
254: return true;
255: }
256:
257: public static boolean sortFile(String input_file,
258: String output_file, String keys) {
259: // Warning: uses a unix sort
260: String os = System.getProperty("os.name");
261: if (!(os.equals("Linux") || (os.equals("SunOS")))) {
262: if (logger.isErrorEnabled()) {
263: logger.error("fileSort()"
264: + "Uses unix sort, Not a unix system.");
265: }
266: return false;
267: }
268: String command = "sort " + keys + " " + input_file + " -o "
269: + output_file + " -T /tmp";
270: try {
271: Runtime rt = Runtime.getRuntime();
272: Process sort = rt.exec(command);
273: sort.waitFor();
274: } catch (Exception ie) {
275: if (logger.isErrorEnabled()) {
276: logger.error("fileSort()"
277: + "Sorting InterruptedException: " + ie);
278: }
279: return false;
280: }
281: return true;
282:
283: }
284: }
|