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.cPlanner.toolkit;
016:
017: import org.griphyn.cPlanner.common.LogManager;
018: import org.griphyn.cPlanner.common.PegasusProperties;
019:
020: import org.griphyn.common.util.FactoryException;
021:
022: import org.griphyn.common.util.Version;
023: import org.griphyn.common.util.VDSProperties;
024:
025: import gnu.getopt.LongOpt;
026:
027: /**
028: * The interface which defines all the methods , any executable should implement.
029: *
030: * @author GAURANG MEHTA
031: * @author KARAN VAHI
032: * @version $Revision: 65 $
033: *
034: */
035: public abstract class Executable {
036:
037: /**
038: * The LogManager object which is used to log all the messages.
039: *
040: * @see org.griphyn.cPlanner.common.LogManager
041: */
042: protected LogManager mLogger;
043:
044: /**
045: * The object holding all the properties pertaining to Pegasus.
046: */
047: protected PegasusProperties mProps;
048:
049: /**
050: * It stores the verison of the Griphyn Virtual Data System software.
051: */
052: protected String mVersion;
053:
054: /**
055: * The error message to be logged.
056: */
057: protected String mLogMsg;
058:
059: /**
060: * The constructor which ends up initialising the PegasusProperties object.
061: */
062: public Executable() {
063: //setup logging before doing anything with properties
064: setupLogging();
065: mLogMsg = new String();
066: mProps = PegasusProperties.getInstance();
067: loadProperties();
068: mVersion = Version.instance().toString();
069: }
070:
071: /**
072: * Returns an error message that chains all the lower order error messages
073: * that might have been thrown.
074: *
075: * @param e the Exception for which the error message has to be composed.
076: * @return the error message.
077: */
078: public static String convertException(Exception e) {
079: StringBuffer message = new StringBuffer();
080: int i = 0;
081: //append all the causes
082: for (Throwable cause = e; cause != null; cause = cause
083: .getCause()) {
084: if (cause instanceof FactoryException) {
085: //do the specialized convert for Factory Exceptions
086: message.append(((FactoryException) cause)
087: .convertException(i));
088: break;
089: }
090: message.append("\n [").append(Integer.toString(++i))
091: .append("] ").append(cause.getClass().getName())
092: .append(": ").append(cause.getMessage());
093:
094: //append just one elment of stack trace for each exception
095: message.append(" at ").append(cause.getStackTrace()[0]);
096: }
097: return message.toString();
098: }
099:
100: /**
101: * Sets up the logging options for this class. Looking at the properties
102: * file, sets up the appropriate writers for output and stderr.
103: */
104: protected void setupLogging() {
105: //setup the logger for the default streams.
106: mLogger = LogManager.getInstance();
107:
108: //get the logging value set in properties
109: //cannot ask for PegasusProperties, as deprecation warnings could be
110: //logged. So get it directly from VDS Properties. Karan May 11, 2006.
111: String value = VDSProperties.noHassleInstance().getProperty(
112: "pegasus.log.*");
113:
114: //use defaults if nothing is set.
115: if (value == null) {
116: mLogger.log("Logging to default streams",
117: LogManager.DEBUG_MESSAGE_LEVEL);
118: return;
119: } else {
120: //log both output and error messages to value specified
121: mLogger.setWriters(value);
122: }
123:
124: }
125:
126: /**
127: * Loads all the properties that would be needed by the Toolkit classes.
128: */
129: public abstract void loadProperties();
130:
131: /**
132: * This method is used to print the long version of the command.
133: */
134: public abstract void printLongVersion();
135:
136: /**
137: * This is used to print the short version of the command.
138: */
139: public abstract void printShortVersion();
140:
141: /**
142: * This function is passed command line arguments. In this function you
143: * generate the valid options and parse the options specified at run time.
144: */
145: public abstract void executeCommand(String[] args);
146:
147: /**
148: * Generates an array of valid <code>LongOpt</code> objects which contain
149: * all the valid options to the Executable.
150: */
151: public abstract LongOpt[] generateValidOptions();
152:
153: /**
154: * Returns the version of the Griphyn Virtual Data System.
155: */
156: public String getGVDSVersion() {
157: StringBuffer sb = new StringBuffer();
158: sb.append("Pegasus Release Version ").append(mVersion);
159: return sb.toString();
160: }
161:
162: /**
163: * Logs messages to the singleton logger.
164: *
165: * @param msg is the message itself.
166: * @param level is the level to generate the log message for.
167: */
168: public void log(String msg, int level) {
169: mLogger.log(msg, level);
170: }
171:
172: /**
173: * Get the value of the environment variable.
174: *
175: * @param envVariable the environment variable whose value you want.
176: *
177: * @return String corresponding to the value of the environment
178: * variable if it is set.
179: * null if the environment variable is not set
180: */
181: public String getEnvValue(String envVariable) {
182: String value = null;
183: value = System.getProperty(envVariable);
184: return value;
185: }
186: }
|