001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
004: * http://www.globus.org/toolkit/download/license.html.
005: * This notice must appear in redistributions of this file
006: * with or without modification.
007: *
008: * Redistributions of this Software, with or without modification, must reproduce
009: * the GTPL in:
010: * (1) the Software, or
011: * (2) the Documentation or
012: * some other similar material which is provided with the Software (if any).
013: *
014: * Copyright 1999-2004
015: * University of Chicago and The University of Southern California.
016: * All rights reserved.
017: */package org.griphyn.cPlanner.toolkit;
018:
019: import org.griphyn.cPlanner.classes.PoolConfig;
020: import org.griphyn.cPlanner.classes.PoolConfigException;
021: import org.griphyn.cPlanner.classes.PoolConfigParser2;
022:
023: import org.griphyn.cPlanner.common.LogManager;
024:
025: import org.griphyn.cPlanner.parser.ConfigXmlParser;
026:
027: //import org.griphyn.cPlanner.poolinfo.MdsQuery;
028:
029: import gnu.getopt.Getopt;
030: import gnu.getopt.LongOpt;
031:
032: import java.io.BufferedWriter;
033: import java.io.File;
034: import java.io.FileReader;
035: import java.io.FileWriter;
036: import java.io.IOException;
037: import java.io.PrintWriter;
038: import java.util.ArrayList;
039: import java.util.Date;
040: import java.util.StringTokenizer;
041:
042: //import javax.naming.NamingEnumeration;
043: //import javax.naming.directory.SearchControls;
044: //import javax.naming.ldap.LdapContext;
045:
046: /**
047: * This client generates a xml poolconfig file by querying the MDS or local
048: * multiline poolconfig files.
049: *
050: * @author Gaurang Mehta gmehta@isi.edu
051: *
052: * @version $Revision: 120 $
053: */
054: public class SCClient extends Executable {
055:
056: private boolean mText;
057:
058: private ArrayList mLocalPoolConfig;
059:
060: private String mOutputXML;
061:
062: /**
063: * The data class containing the contents of the site catalog.
064: */
065: private PoolConfig mConfig;
066:
067: private static final String XML_NAMESPACE = "http://pegasus.isi.edu/schema";
068: private static final String XML_VERSION = "2.0";
069:
070: private boolean mLocalPrec;
071:
072: public SCClient() {
073: super ();
074: // mGIISPort = 2135;
075: mText = false;
076: mLocalPoolConfig = null;
077: mOutputXML = null;
078: mConfig = null;
079: mLocalPrec = false;
080: mConfig = new PoolConfig();
081: }
082:
083: /**
084: * Loads all the properties
085: * that would be needed
086: * by the Toolkit classes
087: */
088: public void loadProperties() {
089: // mGIISHost = mProps.getGIISHost();
090: //mGIISDN = mProps.getGIISDN();
091: }
092:
093: public LongOpt[] generateValidOptions() {
094: LongOpt[] longopts = new LongOpt[7];
095: longopts[0] = new LongOpt("local", LongOpt.NO_ARGUMENT, null,
096: 'l');
097: longopts[1] = new LongOpt("text", LongOpt.NO_ARGUMENT, null,
098: 't');
099: longopts[2] = new LongOpt("files", LongOpt.REQUIRED_ARGUMENT,
100: null, 'f');
101: longopts[3] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT,
102: null, 'o');
103: longopts[4] = new LongOpt("help", LongOpt.NO_ARGUMENT, null,
104: 'h');
105: longopts[5] = new LongOpt("version", LongOpt.NO_ARGUMENT, null,
106: 'V');
107: longopts[6] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null,
108: 'v');
109:
110: return longopts;
111:
112: }
113:
114: /**
115: * Call the correct commands depending on options.
116: * @param opts Command options
117: */
118:
119: public void executeCommand(String[] opts) {
120: LongOpt[] longOptions = generateValidOptions();
121:
122: Getopt g = new Getopt("SCClient", opts, "lthvVo:f:",
123: longOptions, false);
124:
125: int option = 0;
126: int noOfOptions = 0;
127: int level = 0;
128: while ((option = g.getopt()) != -1) {
129: switch (option) {
130: case 't': //text or xml
131: mText = true;
132:
133: break;
134:
135: case 'f': //local pool config file
136: StringTokenizer st = new StringTokenizer(g.getOptarg(),
137: ",");
138: mLocalPoolConfig = new ArrayList(st.countTokens());
139: while (st.hasMoreTokens()) {
140: mLocalPoolConfig.add(st.nextToken());
141: }
142: break;
143:
144: case 'o': //output
145: mOutputXML = g.getOptarg();
146: break;
147:
148: case 'h': //help
149: printLongVersion();
150: System.exit(0);
151: break;
152:
153: case 'V': //version
154: mLogger.log(getGVDSVersion(),
155: LogManager.INFO_MESSAGE_LEVEL);
156: System.exit(0);
157: break;
158: case 'l': // Precedence for local or remote
159: mLocalPrec = true;
160: break;
161: case 'v': //Verbose mode
162: level++;
163: break;
164:
165: default:
166: mLogger.log("Unrecognized Option : " + (char) option,
167: LogManager.FATAL_MESSAGE_LEVEL);
168: printShortVersion();
169: System.exit(1);
170: }
171: }
172: if (level > 0) {
173: //set the logging level only if -v was specified
174: //else bank upon the the default logging level
175: mLogger.setLevel(level);
176: }
177: try {
178: generatePoolConfig();
179: } catch (Exception e) {
180: e.printStackTrace();
181: }
182: }
183:
184: public void generatePoolConfig() throws Exception {
185: //convert the xml file to text file.
186: if (mLocalPoolConfig != null) {
187: if (mText) {
188: ConfigXmlParser p = new ConfigXmlParser(mProps);
189: p.startParser((String) mLocalPoolConfig.get(0));
190: mConfig = p.getPoolConfig();
191: } else {
192: mConfig = getLocalConfigInfo(mLocalPoolConfig);
193: }
194: } else {
195: mLogger.log(
196: "Provide thepool config file with --files option",
197: LogManager.ERROR_MESSAGE_LEVEL);
198: }
199:
200: if (mConfig != null) {
201: if (mText && mOutputXML == null) {
202: //not sure about this
203: System.out.println(this .toMultiLine(mConfig));
204: } else if (!mText && mOutputXML == null) {
205: System.out.println(this .toXML(mConfig));
206: } else if (mText && mOutputXML != null) {
207: toFile(mOutputXML, this .toMultiLine(mConfig));
208: mLogger.log("Written text output to file :"
209: + mOutputXML, LogManager.INFO_MESSAGE_LEVEL);
210: System.exit(0);
211: } else {
212: toFile(mOutputXML, this .toXML(mConfig));
213: mLogger.log("Written xml output to file : "
214: + mOutputXML, LogManager.INFO_MESSAGE_LEVEL);
215: System.exit(0);
216: }
217:
218: } else {
219: throw new Exception(
220: "Error: Something bad happened the config data is empty");
221: }
222: }
223:
224: public void printShortVersion() {
225: String text = "\n "
226: + this .getGVDSVersion()
227: + "\n Usage :sc-client [-f <list of files>] "
228: + "\n [-o <output filename>] [-l] [-t] [-v] [-V] "
229: + "\n Type sc-client -h for more details"
230: + "\n"
231: + "\n Usage :sc-client [--files <list of files>] "
232: + "\n [--local] [--text] [--output <output filename>] [--verbose] [--version]"
233: + "\n Type sc-client --help for more help";
234:
235: mLogger.log(text, LogManager.ERROR_MESSAGE_LEVEL);
236:
237: }
238:
239: public void printLongVersion() {
240: String text = "\n"
241: + this .getGVDSVersion()
242: + "\n sc-client - this is used to write the xml site catalog file"
243: + "\n from a local text config file. "
244: + "\n Usage: sc-client [OPTIONS]...."
245: +
246:
247: "\n\n Mandatory Options "
248: + "\n"
249: + "\n --text | -t To convert an xml site catalog file to the multiline site catalog file."
250: + "\n"
251: + "\n --files | -f The local text site catalog file|files to be converted to "
252: + "\n xml or text. This file needs to be in multiline textual "
253: + "\n format not the single line or in xml format if converting "
254: + "\n to text format. See $PEGASUS_HOME/etc/sample.sites.txt. "
255: + "\n"
256: + "\n --output | -o The name of the xml/text file to which you want the ouput "
257: + "\n written to. Default it writes to standard out."
258: + "\n"
259: + "\n\n Other Options "
260: + "\n"
261: + "\n -v | --verbose increases the verbosity level."
262: + "\n"
263: + "\n --version | -V Displays the version number of PEGASUS. "
264: + "\n"
265: + "\n --help | -h Generates this help."
266: + "\n"
267: + "\n\n Example Usages "
268: + "\n sc-client --files sites.txt --output sites.xml"
269: + "\n"
270: + "\n sc-client --files sites.txt,sites2.txt "
271: + "\n --output sites.xml"
272: + "\n"
273: + "\n sc-client --files sites.xml --text --output sites.txt \n";
274:
275: mLogger.log(text, LogManager.INFO_MESSAGE_LEVEL);
276:
277: }
278:
279: public PoolConfig getLocalConfigInfo(ArrayList localpoolconfig) {
280: for (int i = 0; i < localpoolconfig.size(); i++) {
281: String filename = null;
282: try {
283: filename = (String) localpoolconfig.get(i);
284: mLogger.log("Reading " + filename,
285: LogManager.INFO_MESSAGE_LEVEL);
286: PoolConfigParser2 p = new PoolConfigParser2(
287: new FileReader(filename));
288: mConfig.add(p.parse());
289: mLogger.logCompletion("Reading " + filename,
290: LogManager.INFO_MESSAGE_LEVEL);
291: } catch (PoolConfigException pce) {
292: mLogger.log(filename + ": " + pce.getMessage(),
293: LogManager.ERROR_MESSAGE_LEVEL);
294: mLogger.log(" ignoring rest, skipping to next file",
295: LogManager.ERROR_MESSAGE_LEVEL);
296: } catch (IOException ioe) {
297: mLogger.log(filename + ": " + ioe.getMessage(),
298: LogManager.ERROR_MESSAGE_LEVEL);
299: mLogger.log("ignoring rest, skipping to next file",
300: LogManager.ERROR_MESSAGE_LEVEL);
301: } catch (Exception e) {
302: mLogger.log(filename + ": " + e.getMessage(),
303: LogManager.ERROR_MESSAGE_LEVEL);
304: mLogger.log("ignoring rest, skipping to next file",
305: LogManager.ERROR_MESSAGE_LEVEL);
306: }
307: }
308: return mConfig;
309: }
310:
311: /**
312: * Returns the XML description of the contents of <code>PoolConfig</code>
313: * object passed, conforming to pool config schema found at
314: * http://pegasus.isi.edu/schema/sc-2.0.xsd.
315: *
316: * @param cfg the <code>PoolConfig</code> object whose xml description is
317: * desired.
318: *
319: * @return the xml description.
320: */
321: public String toXML(PoolConfig cfg) {
322: String output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
323: output += "<!--Generated " + new Date().toString() + "-->\n";
324: output += "<!--Generated by " + System.getProperty("user.name")
325: + " [" + System.getProperty("user.country") + "] "
326: + "-->\n";
327: output += "<sitecatalog";
328: output += " xmlns=\"" + XML_NAMESPACE + "/sitecatalog\"";
329: output += " xsi:schemaLocation=\"" + XML_NAMESPACE
330: + "/sitecatalog " + XML_NAMESPACE + "/sc-"
331: + XML_VERSION + ".xsd\"";
332: output += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
333: output += " version=\"" + XML_VERSION + "\">\n";
334: output += cfg.toXML();
335: output += "</sitecatalog>";
336:
337: return output;
338: }
339:
340: /**
341: * Returns the String description of the contents of <code>PoolConfig</code>
342: * object passed.
343: *
344: * @param cfg the <code>PoolConfig</code> object whose description is
345: * desired.
346: *
347: * @return the String description.
348: */
349: public String toMultiLine(PoolConfig cfg) {
350: String output = "#Text version of site catalog\n";
351: output += "#Generated by SCClient\n";
352: output += cfg.toMultiLine();
353: output += "\n";
354: return output;
355: }
356:
357: /**
358: * Writes out to a file, a string.
359: *
360: * @param filename the fully qualified path name to the file.
361: * @param output the text that needs to be written to the file.
362: *
363: * @throws IOException
364: */
365: public void toFile(String filename, String output)
366: throws IOException {
367: File outfile = new File(filename);
368: PrintWriter pw = new PrintWriter(new BufferedWriter(
369: new FileWriter(outfile)));
370: pw.println(output);
371: pw.close();
372:
373: }
374:
375: public static void main(String[] args) throws Exception {
376: SCClient client = new SCClient();
377: client.executeCommand(args);
378: }
379:
380: }
|