001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file ../GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.toolkit;
016:
017: import java.io.*;
018: import java.sql.SQLException;
019: import java.util.Collection;
020: import java.util.MissingResourceException;
021: import org.griphyn.vdl.util.Logging;
022: import org.griphyn.vdl.util.ChimeraProperties;
023: import org.griphyn.vdl.dbschema.*;
024:
025: /**
026: * This class is the base class for other toolkits. It provides
027: * basical common functions, to be extended, used or restricted
028: * by siblings as necessary.
029: *
030: * @author Jens-S. Vöckler
031: * @author Yong Zhao
032: * @version $Revision: 50 $
033: *
034: * @see org.griphyn.vdl.util.Logging
035: * @see org.griphyn.vdl.util.ChimeraProperties
036: * @see org.griphyn.vdl.dbschema.DatabaseSchema
037: */
038: public abstract class Toolkit {
039: /**
040: * Maintains an application specific set of properties
041: */
042: protected ChimeraProperties m_props;
043:
044: /**
045: * Stores the name of the application as which this one was run
046: */
047: protected String m_application;
048:
049: /**
050: * Stores the name of the home directory for the Chimera system.
051: */
052: protected String m_home;
053:
054: /**
055: * Stores the logging instance
056: */
057: protected Logging m_logger;
058:
059: /**
060: * Stores the verbosity level of the app logging queue.
061: */
062: protected int m_verbosity;
063:
064: /**
065: * Helper for the constructors
066: *
067: * @param self is the reference to self (the unit to be constructed)
068: */
069: private static void setup(Toolkit self) {
070: // set up logger
071: self.m_logger = Logging.instance();
072:
073: // setting up properties -- might throw things at us
074: try {
075: self.m_props = ChimeraProperties.instance();
076: } catch (IOException e) {
077: Logging.instance().log(
078: "default",
079: 0,
080: "error while reading properties file: "
081: + e.getMessage());
082: } catch (MissingResourceException e) {
083: Logging
084: .instance()
085: .log("default", 0,
086: "You probably forgot to set -Dvds.home=$PEGASUS_HOME");
087: System.exit(1);
088: }
089:
090: self.m_props.setupLogging(self.m_logger);
091: }
092:
093: /**
094: * Default ctor: Sets up the system
095: *
096: * @param appName is the name of the shell wrapper with which to report.
097: */
098: public Toolkit(String appName) {
099: m_application = appName;
100: setup(this );
101:
102: // Register app by default, but only with level 0
103: // jsv: 20050815, bugzilla#79
104: if (m_logger.isUnset("app")) {
105: m_verbosity = 0;
106: m_logger.register("app", System.out, m_verbosity);
107: } else {
108: m_verbosity = m_logger.getLevel("app");
109: if (m_verbosity == Integer.MAX_VALUE || m_verbosity < 0) {
110: m_verbosity = 0;
111: m_logger.setLevel("app", m_verbosity);
112: }
113: }
114: }
115:
116: /**
117: * Default ctor: Sets up the system
118: *
119: * @param appName is the name of the shell wrapper with which to report.
120: * @param verbosity sets the verbosity level of the "app" logging queue.
121: */
122: public Toolkit(String appName, int verbosity) {
123: m_application = appName;
124: setup(this );
125:
126: // Register app by default, but only with level 0
127: // jsv: 20050815, bugzilla#79
128: if (m_logger.isUnset("app")) {
129: m_verbosity = verbosity;
130: m_logger.register("app", System.out, m_verbosity);
131: } else {
132: m_verbosity = m_logger.getLevel("app");
133: if (m_verbosity == Integer.MAX_VALUE || m_verbosity < 0) {
134: m_verbosity = 0;
135: m_logger.setLevel("app", m_verbosity);
136: }
137: }
138: }
139:
140: /**
141: * Helper function to set the verbosity level of the app logging
142: * queue.
143: *
144: * @param verbosity is the new verbosity level
145: * @see #getVerbosity()
146: * @see #increaseVerbosity()
147: */
148: public void setVerbosity(int verbosity) {
149: this .m_verbosity = verbosity;
150: this .m_logger.setLevel("app", verbosity);
151: }
152:
153: /**
154: * Helper function to obtain the verbosity level of the app logging
155: * queue.
156: *
157: * @return the verbosity level as non-negative integer, or -1 for
158: * error.
159: * @see #setVerbosity( int )
160: * @see #increaseVerbosity()
161: */
162: public int getVerbosity() {
163: int result = this .m_logger.getLevel("app");
164: this .m_verbosity = result;
165: return result;
166: }
167:
168: /**
169: * Helper function to increase the verbosity level of the app logging
170: * queue using the internal verbosity state.
171: *
172: * @return the new verbosity level
173: * @see #setVerbosity( int )
174: * @see #getVerbosity()
175: */
176: public int increaseVerbosity() {
177: this .m_logger.setLevel("app", ++this .m_verbosity);
178: return this .m_verbosity;
179: }
180:
181: /**
182: * Prints the short usage string onto stdout. No exiting.
183: * The application name is maintained by the c'tor.
184: */
185: public abstract void showUsage();
186:
187: /**
188: * This helper method reads lines from a file and add non-empty,
189: * no-comment lines to a collection of lines. Invoke with a set to
190: * prohibit duplicates, or with a list to permit duplicates.
191: *
192: * @param fn is the name of the file to read.
193: * @param list is the container to add lines to
194: * @return number of items read.
195: * @throws IOException if any file operation failed.
196: */
197: protected int readFile(String fn, Collection list)
198: throws IOException {
199: String line;
200: int count = 0;
201: LineNumberReader in = null;
202: if (fn == null)
203: return 0;
204:
205: in = new LineNumberReader(new FileReader(fn));
206: while ((line = in.readLine()) != null) {
207: // remove comments
208: int p = line.indexOf('#');
209: if (p != -1)
210: line = line.substring(0, p);
211:
212: // remove superflous whitespace
213: line = line.trim();
214:
215: // add anything non-empty
216: if (line.length() >= 1) {
217: list.add(line);
218: count++;
219: }
220: }
221:
222: in.close();
223: return count;
224: }
225: }
|