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.toolkit;
017:
018: import java.io.*;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.sql.SQLException;
022: import org.griphyn.common.util.Version;
023: import org.griphyn.common.util.Currently;
024: import org.griphyn.vdl.parser.VDLxParser;
025: import org.griphyn.vdl.classes.*;
026: import org.griphyn.vdl.dbschema.*;
027: import org.griphyn.vdl.util.Logging;
028: import org.griphyn.vdl.util.ChimeraProperties;
029: import org.griphyn.vdl.directive.*;
030: import gnu.getopt.*;
031:
032: /**
033: * This class searches definition's that match the namespace, name, version
034: * triple, then prints the search results in one of the formats: vdlx, vdlt,
035: * or name.
036: *
037: * @author Jens-S. Vöckler
038: * @author Yong Zhao
039: * @version $Revision: 50 $
040: *
041: * @see org.griphyn.vdl.parser.VDLxParser
042: */
043:
044: public class XSearchVDC extends Toolkit {
045: /**
046: * Constructor
047: */
048: public XSearchVDC(String appName) {
049: super (appName);
050: }
051:
052: /**
053: * Prints the usage string.
054: */
055: public void showUsage() {
056: String linefeed = System.getProperty("line.separator", "\r\n");
057:
058: System.out
059: .println("$Id: XSearchVDC.java 50 2007-05-19 00:48:32Z gmehta $"
060: + linefeed
061: + "VDS version "
062: + Version.instance().toString() + linefeed);
063:
064: System.out.println("Usage: " + this .m_application
065: + " [general] [-t tr|dv] [-n ns] [-i id] [-v vs]"
066: + linefeed + " or: " + this .m_application
067: + " [general] [-t i|o|io] -f lfn" + linefeed
068: + " or: " + this .m_application
069: + " [general] -q query qargs");
070:
071: System.out
072: .println(linefeed
073: + "General options:"
074: + linefeed
075: + " -V|--version print version information and exit."
076: + linefeed
077: + " -d|--dbase dbx associates the dbname with the database, unused."
078: + linefeed
079: + " --verbose increases the verbosity level."
080: + linefeed
081: + " -l|--list x|t|n print x:VDLx, t:VDLt or just a table of n:names."
082: + linefeed
083: + " -e|--error if present, return failure for an empty search."
084: + linefeed
085: + " -o|--output fn put the output into the file fn, default is stdout."
086: + linefeed
087: + linefeed
088: + "Group 1: Searching for definitions"
089: + linefeed
090: + " -t|--type tr|dv Search only for TR or DV, default is both."
091: + linefeed
092: + " -n|--namespace ns Search for matches with namespace ns, default wildcard."
093: + linefeed
094: + " -i|--name id Search for matches with name id, default wildcard."
095: + linefeed
096: + " -v|--ver vs Search for matches with version vs, default wildcard."
097: + linefeed
098: + linefeed
099: + "Group 2: Searching for logical filenames"
100: + linefeed
101: + " -t|--type i|o|io Limit search to (i)n, (o)ut or (io) filenames."
102: + linefeed
103: + " -f|--lfn lfn Limit search to filename lfn, default wildcard."
104: + linefeed + linefeed
105: + "Group 3: Searching by ... what? ... Yong?"
106: + linefeed + " -q|--query query ????"
107: + linefeed);
108: }
109:
110: /**
111: * Creates a set of options.
112: */
113: protected LongOpt[] generateValidOptions() {
114: LongOpt[] lo = new LongOpt[17];
115:
116: lo[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
117: lo[1] = new LongOpt("dbase", LongOpt.REQUIRED_ARGUMENT, null,
118: 'd');
119: lo[2] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
120: lo[3] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
121:
122: lo[4] = new LongOpt("list", LongOpt.REQUIRED_ARGUMENT, null,
123: 'l');
124: lo[5] = new LongOpt("error", LongOpt.NO_ARGUMENT, null, 'e');
125: lo[6] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null,
126: 'o');
127:
128: lo[7] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null,
129: 't');
130: lo[8] = new LongOpt("namespace", LongOpt.REQUIRED_ARGUMENT,
131: null, 'n');
132: lo[9] = new LongOpt("ns", LongOpt.REQUIRED_ARGUMENT, null, 'n');
133: lo[10] = new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null,
134: 'i');
135: lo[11] = new LongOpt("id", LongOpt.REQUIRED_ARGUMENT, null, 'i');
136: lo[12] = new LongOpt("ver", LongOpt.REQUIRED_ARGUMENT, null,
137: 'v');
138: lo[13] = new LongOpt("vs", LongOpt.REQUIRED_ARGUMENT, null, 'v');
139:
140: lo[14] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null,
141: 'f');
142: lo[15] = new LongOpt("lfn", LongOpt.REQUIRED_ARGUMENT, null,
143: 'f');
144: lo[16] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null,
145: 'q');
146:
147: return lo;
148: }
149:
150: /**
151: * search the database for specific TR's or DV's
152: */
153: public static void main(String[] args) {
154: int result = 0;
155: boolean emptyFailure = false;
156: boolean seenResults = false;
157:
158: try {
159: XSearchVDC me = new XSearchVDC("xsearchvdc");
160:
161: // get the commandline options
162: Getopt opts = new Getopt(me.m_application, args,
163: "hd:l:ef:q:o:t:n:i:v:V", me.generateValidOptions());
164: opts.setOpterr(false);
165:
166: String dbase = null;
167: String ns = null;
168: String name = null;
169: String ver = null;
170: String lfn = null;
171: String outfn = null;
172: String format = null;
173: String t = null;
174: String query = null;
175:
176: int option = 0;
177: while ((option = opts.getopt()) != -1) {
178: switch (option) {
179: case 1:
180: me.increaseVerbosity();
181: break;
182:
183: case 'V':
184: System.out
185: .println("$Id: XSearchVDC.java 50 2007-05-19 00:48:32Z gmehta $");
186: System.out.println("VDS version "
187: + Version.instance().toString());
188: return;
189:
190: case 'd':
191: dbase = opts.getOptarg();
192: break;
193:
194: case 'e':
195: emptyFailure = true;
196: break;
197:
198: case 'f':
199: lfn = opts.getOptarg();
200: break;
201:
202: case 'i':
203: name = opts.getOptarg();
204: break;
205:
206: case 'l':
207: format = opts.getOptarg().toLowerCase();
208: break;
209:
210: case 'n':
211: ns = opts.getOptarg();
212: break;
213:
214: case 'o':
215: outfn = opts.getOptarg();
216: break;
217:
218: case 'q':
219: query = opts.getOptarg();
220: break;
221:
222: case 't':
223: t = opts.getOptarg().toLowerCase();
224: break;
225:
226: case 'v':
227: ver = opts.getOptarg();
228: break;
229:
230: case 'h':
231: default:
232: me.showUsage();
233: return;
234: }
235: }
236:
237: boolean condition1 = (lfn != null);
238: boolean condition2 = (ns != null || name != null || ver != null);
239: boolean condition3 = (query != null);
240:
241: if ((condition1 && (condition2 || condition3))
242: || (condition2 && condition3)) {
243: me.showUsage();
244: throw new RuntimeException(
245: "ERROR: you must either specify the -n -i -v options, the -f option, or the -q option!");
246: }
247:
248: int link = -1;
249: int clsType = -1;
250: if (condition1) {
251: // to search for input/output/inout file?
252: if (t != null) {
253: if (t.equals("i"))
254: link = LFN.INPUT;
255: else if (t.equals("o"))
256: link = LFN.OUTPUT;
257: else if (t.equals("io"))
258: link = LFN.INOUT;
259: else {
260: System.err.println("Invalid value \"" + t
261: + "\" for option -t");
262: me.showUsage();
263: System.exit(1);
264: }
265: }
266: } else if (condition2) {
267: // to search for tr/dv, or both
268: if (t != null) {
269: char c = t.charAt(0);
270: if (c == 'd')
271: clsType = Definition.DERIVATION;
272: else if (c == 't')
273: clsType = Definition.TRANSFORMATION;
274: else {
275: System.err.println("Invalid value \"" + t
276: + "\" for option -t");
277: me.showUsage();
278: System.exit(1);
279: }
280: }
281: }
282:
283: // get the output format, default is "n"
284: if (format == null)
285: format = "n";
286: int f = Search.FORMAT_FQDN;
287: switch (format.charAt(0)) {
288: case 'x':
289: f = Search.FORMAT_VDLX;
290: break;
291: case 't':
292: f = Search.FORMAT_VDLT;
293: break;
294: case 'n':
295: f = Search.FORMAT_FQDN;
296: break;
297: default:
298: System.err.println("Invalid value \"" + format
299: + "\" for option -l");
300: me.showUsage();
301: System.exit(1);
302: }
303:
304: // Connect the database.
305: String schemaName = ChimeraProperties.instance()
306: .getVDCSchemaName();
307:
308: Connect connect = new Connect();
309: DatabaseSchema dbschema = connect
310: .connectDatabase(schemaName);
311:
312: // Search the database.
313: me.m_logger.log("app", 1, "searching the database");
314: java.util.List defList = null;
315: Search search = new Search(dbschema);
316:
317: if (condition1) {
318: //looking for lfn
319: defList = search.searchDefinition(lfn, link);
320: } else if (condition3) {
321: StringBuffer xquery = new StringBuffer(256);
322: int start = opts.getOptind();
323: int arg_len = args.length;
324: if (dbschema instanceof XDC) {
325: if (query.equals("tr_dv")) {
326: xquery
327: .append("for $t in //transformation[@name='");
328: xquery.append(args[start]);
329: xquery
330: .append("'] return //derivation[@uses=$t/@name]");
331: } else if (query.equals("dv_tr")) {
332: xquery.append("for $t in //derivation[@name='");
333: xquery.append(args[start]);
334: xquery
335: .append("'] return //transformation[@name=$t/@uses]");
336: } else if (query.equals("tr_call")) {
337: xquery.append("//transformation[call/@uses='");
338: xquery.append(args[start]);
339: xquery.append("']");
340: } else if (query.equals("tr_para")) {
341: xquery.append("//transformation");
342: int i = start;
343: while (i < arg_len) {
344: xquery.append("[declare[@name='").append(
345: args[i]).append("']");
346: i++;
347: if (i < arg_len && !args[i].equals("any")) {
348: xquery.append("[@link='").append(
349: args[i]).append("']");
350: }
351: i++;
352: xquery.append("]");
353: }
354: } else if (query.equals("tr_arg")) {
355: xquery
356: .append("//transformation[contains(argument/*, '");
357: xquery.append(args[start]);
358: xquery.append("')]");
359: } else if (query.equals("dv_pass")) {
360: xquery
361: .append("//derivation[contains(pass//*, '");
362: xquery.append(args[start]);
363: xquery.append("')]");
364: } else if (query.equals("tr_meta")) {
365: xquery
366: .append("for $m in //metadata[@subject='tr'][attribute[@name='");
367: xquery.append(args[start]);
368: xquery.append("']");
369:
370: xquery.append("='").append(args[start + 1])
371: .append("']");
372: xquery
373: .append(" let $mn:=$m/@name, $n := substring-before($mn, '::'), $na := substring-after($mn, '::'), $iv := if ($na) then $na else $mn, $v := substring-after($iv, ':'), $ib := substring-before($iv, ':'), $i := if ($ib) then $ib else $iv,");
374: xquery
375: .append(" $t := if ($n) then if ($v) then //transformation[@namespace=$n][@name=$i][@version=$v] else //transformation[@namespace=$n][@name=$i][empty(@version)] else if ($v) then //transformation[empty(@namespace)][@name=$i][@version=$v] else //transformation[empty(@namespace)][@name=$i][empty(@version)]");
376: xquery.append(" return $t[declare[@link='");
377: xquery.append(args[start + 2]);
378: xquery.append("'][@name = $m/@select]]");
379: } else if (query.equals("dv_tree")) {
380: xquery
381: .append("declare namespace v='http://www.griphyn.org/chimera';");
382: xquery
383: .append("declare function v:dv_tree($lfn as xs:string) as element()* {");
384: xquery
385: .append("let $d := //derivation[.//lfn[@file=$lfn][@link='input']]");
386: xquery.append("return ( $d,");
387: xquery
388: .append("for $out in $d//lfn[@link='output']/@file return v:dv_tree($out))");
389: xquery.append("};");
390: xquery.append("let $d := v:dv_tree('").append(
391: args[start]).append("') return $d");
392: } else if (query.equals("lfn_tree")) {
393: xquery
394: .append("declare namespace v='http://www.griphyn.org/chimera';");
395: xquery
396: .append("declare function v:lfn_tree($lfn as xs:string) as item()* {");
397: xquery
398: .append("let $d := //derivation[.//lfn[@file=$lfn][@link='input']]");
399: xquery.append("return ( $lfn,");
400: xquery
401: .append("for $out in $d//lfn[@link='output']/@file return v:lfn_tree($out))");
402: xquery.append("};");
403: xquery.append("let $f := v:lfn_tree('").append(
404: args[start]);
405: xquery.append("') return distinct-values($f)");
406: } else if (query.equals("lfn_meta")) {
407: xquery
408: .append("for $m in //metadata[@subject='lfn']");
409: int i = start;
410: while (i < arg_len) {
411: xquery.append("[attribute[@name='").append(
412: args[i]).append("']");
413: i++;
414: xquery.append("='").append(args[i]).append(
415: "']");
416: i++;
417: }
418: xquery.append(" return $m/@name");
419: } else {
420: xquery.append(query);
421: }
422:
423: if (query.equals("lfn_meta")
424: || query.equals("lfn_tree"))
425: defList = ((XDC) dbschema)
426: .searchElements(xquery.toString());
427: else
428: defList = ((XDC) dbschema)
429: .searchDefinition(xquery.toString());
430: }
431: } else {
432: //looking for definitions
433: if (dbschema instanceof Advanced)
434: defList = search.searchDefinitionEx(ns, name, ver,
435: clsType);
436: else
437: defList = search.searchDefinition(ns, name, ver,
438: clsType);
439: }
440:
441: if (defList != null && !defList.isEmpty()) {
442: seenResults = true;
443: Writer bw;
444: if (outfn != null) {
445: // save to file
446: me.m_logger.log("app", 1, "Saving to the file "
447: + outfn);
448: bw = new BufferedWriter(new FileWriter(outfn));
449: } else {
450: bw = new PrintWriter(System.out);
451: }
452:
453: if (query != null
454: && (query.equals("lfn_meta") || query
455: .equals("lfn_tree")))
456: for (Iterator i = defList.iterator(); i.hasNext();)
457: bw.write((String) i.next() + "\n");
458: else
459: search.printDefinitionList(bw, defList, f);
460: bw.close();
461: } else {
462: me.m_logger.log("app", 1, "no results");
463: }
464:
465: // done
466: dbschema.close();
467:
468: } catch (RuntimeException rte) {
469: System.err.println("ERROR: " + rte.getMessage());
470: result = 1;
471: } catch (Exception e) {
472: e.printStackTrace();
473: result = 2;
474: }
475:
476: // fail on empty result set, if requested
477: if (emptyFailure & !seenResults)
478: result = 2;
479: if (result != 0)
480: System.exit(result);
481: }
482: }
|