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:
016: package org.griphyn.vdl.router;
017:
018: import org.griphyn.vdl.classes.*;
019: import org.griphyn.vdl.parser.*;
020: import org.griphyn.vdl.router.*;
021: import org.griphyn.vdl.toolkit.*;
022: import org.griphyn.vdl.dbschema.*;
023: import org.griphyn.vdl.util.ChimeraProperties;
024: import org.griphyn.vdl.util.Logging;
025: import org.griphyn.vdl.directive.*;
026: import org.griphyn.common.util.Version;
027:
028: import java.io.Writer;
029: import java.io.PrintWriter;
030: import java.io.BufferedWriter;
031: import gnu.getopt.*;
032:
033: public class ToDAG extends Toolkit {
034: /**
035: * ctor: Constructs a new instance object with the given application name.
036: */
037: public ToDAG(String appName) {
038: super (appName);
039: }
040:
041: /**
042: * Shows the usage.
043: */
044: public void showUsage() {
045: String m_usage = "[-d dbname] [-l t|x] -f lfn";
046:
047: String linefeed = System.getProperty("line.separator", "\r\n");
048:
049: System.out
050: .println("$Id: ToDAG.java 50 2007-05-19 00:48:32Z gmehta $"
051: + linefeed
052: + "VDS version "
053: + Version.instance().toString() + linefeed);
054:
055: System.out.println("Usage: " + this .m_application
056: + " [-d db] [-l t|x] -f lfn" + linefeed);
057:
058: System.out
059: .println(" -V|--version print version information and exit."
060: + linefeed
061: + " -d|--dbase dbx associates the dbname with the database, unused."
062: + linefeed
063: + " -l|--list x|t output format, textual or XML, default is XML."
064: + linefeed
065: + " -f|--file lfn request a filename to be produced"
066: + linefeed);
067: }
068:
069: /**
070: * Creates a set of long options.
071: * @return the long option vector.
072: */
073: protected LongOpt[] generateValidOptions() {
074: LongOpt[] lo = new LongOpt[6];
075:
076: lo[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
077: lo[1] = new LongOpt("dbase", LongOpt.REQUIRED_ARGUMENT, null,
078: 'd');
079: lo[2] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
080:
081: lo[3] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null,
082: 'f');
083: lo[4] = new LongOpt("lfn", LongOpt.REQUIRED_ARGUMENT, null, 'f');
084: lo[5] = new LongOpt("list", LongOpt.REQUIRED_ARGUMENT, null,
085: 'l');
086:
087: return lo;
088: }
089:
090: public static void main(String[] args)
091: throws IllegalArgumentException {
092: // create debug output
093: Logging.instance().register("app", System.err);
094: Logging.instance().register("dag", System.err);
095: Logging.instance().register("state", System.err);
096: Logging.instance().register("route", System.err);
097: Logging.instance().register("trace", System.err);
098: Logging.instance().register("stack", System.err);
099:
100: try {
101: // new instance
102: ToDAG me = new ToDAG("ToDAG");
103:
104: // get the commandline options
105: Getopt opts = new Getopt(me.m_application, args,
106: "hd:f:l:V", me.generateValidOptions());
107: opts.setOpterr(false);
108:
109: String dbase = null;
110: String lfn = null;
111: String t = null;
112: int option = 0;
113: while ((option = opts.getopt()) != -1) {
114: switch (option) {
115: case 'V':
116: System.out
117: .println("$Id: ToDAG.java 50 2007-05-19 00:48:32Z gmehta $");
118: System.out.println("VDS version "
119: + Version.instance().toString());
120: return;
121:
122: case 'd':
123: dbase = opts.getOptarg();
124: break;
125:
126: case 'f':
127: lfn = opts.getOptarg();
128: break;
129:
130: case 'l':
131: t = opts.getOptarg().toLowerCase();
132: break;
133:
134: case 'h':
135: default:
136: me.showUsage();
137: return;
138: }
139: }
140:
141: // sanity check
142: if (lfn == null || lfn.length() == 0) {
143: me.showUsage();
144: System.err.println("You must specify a value for -f");
145: System.exit(1);
146: }
147:
148: // user supplied database, set up me.m_dbschema
149: String vdcSchemaName = ChimeraProperties.instance()
150: .getVDCSchemaName();
151: Connect connect = new Connect();
152: DatabaseSchema dbschema = connect
153: .connectDatabase(vdcSchemaName);
154:
155: // output format, defaults to XML
156: boolean wantText = (t != null && Character.toLowerCase(t
157: .charAt(0)) == 't');
158:
159: Definitions defs = null;
160: if (dbschema instanceof InMemorySchema) {
161: // already everything in main memory, use backdoor (Yuck! Dirty!)
162: // avoid duplicating DVs in main memory.
163: defs = ((InMemorySchema) dbschema).backdoor();
164: } else {
165: // Load all Definitions into an in-memory database (uiuiui)
166: me.m_logger.log("app", 1,
167: "loading *all* definitions into memory");
168: defs = new Definitions();
169: defs.setDefinition(((VDC) dbschema).searchDefinition(
170: null, null, null, -1));
171: }
172:
173: if (defs == null) {
174: System.err.println("No input data, nothing to route");
175: return;
176: }
177:
178: // create new route object, in memory classes
179: Route r = new Route(new InMemorySchema(defs));
180:
181: // actually do some routing
182: BookKeeper bk = r.requestLfn(lfn);
183:
184: // what is it to be, xml or text
185: Writer bw = new BufferedWriter(new PrintWriter(System.out));
186: if (wantText) {
187: if (bk != null)
188: bk.toString(bw);
189: else
190: bw.write("# no jobs generated"
191: + System.getProperty("line.separator",
192: "\r\n"));
193: } else {
194: if (bk != null)
195: bk.toXML(bw, "");
196: else
197: bw.write("<!-- no jobs generated -->"
198: + System.getProperty("line.separator",
199: "\r\n"));
200: }
201: } catch (Exception e) {
202: e.printStackTrace();
203: }
204: }
205: }
|