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.ArrayList;
020: import java.util.Enumeration;
021: import java.util.Vector;
022: import java.util.Iterator;
023: import gnu.getopt.*;
024:
025: import org.griphyn.vdl.util.Logging;
026: import org.griphyn.vdl.util.ChimeraProperties;
027: import org.griphyn.vdl.classes.*;
028: import org.griphyn.vdl.dbschema.*;
029: import org.griphyn.vdl.directive.*;
030: import org.griphyn.vdl.annotation.*;
031: import org.griphyn.common.util.Separator;
032: import org.griphyn.common.util.Version;
033:
034: /**
035: * This class searches definitions or LFNs which match a query.
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: public class SearchMeta extends Toolkit {
044: /**
045: * Constructor
046: */
047: public SearchMeta(String appName) {
048: super (appName);
049: }
050:
051: /**
052: * Prints the usage string.
053: */
054: public void showUsage() {
055: String linefeed = System.getProperty("line.separator", "\r\n");
056:
057: System.out
058: .println("$Id: SearchMeta.java 50 2007-05-19 00:48:32Z gmehta $"
059: + linefeed
060: + "VDS version "
061: + Version.instance().toString() + linefeed);
062:
063: System.out.println("Usage: " + this .m_application
064: + " [-d db] -t tr|dv|lfn [-a arg] query_file");
065:
066: System.out
067: .println(linefeed
068: + " -V|--version print version information and exit."
069: + linefeed
070: + " -d|--dbase db associates the dbname with the database, unused."
071: + linefeed
072: + " --verbose increases the verbosity level."
073: + linefeed
074: + " -t|--type type limits candidates to either TR, DV, or LFN."
075: + linefeed
076: + " -a|--arg arg limits to formal arguments, requires -t tr option?"
077: + linefeed);
078: }
079:
080: /**
081: * Creates a set of options.
082: * @return the assembled long option list
083: */
084: protected LongOpt[] generateValidOptions() {
085: LongOpt[] lo = new LongOpt[7];
086:
087: lo[0] = new LongOpt("dbase", LongOpt.REQUIRED_ARGUMENT, null,
088: 'd');
089: lo[1] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
090: lo[2] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
091: lo[3] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
092:
093: lo[4] = new LongOpt("arg", LongOpt.REQUIRED_ARGUMENT, null, 'a');
094: lo[5] = new LongOpt("args", LongOpt.REQUIRED_ARGUMENT, null,
095: 'a');
096: lo[6] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null,
097: 't');
098:
099: return lo;
100: }
101:
102: /**
103: * Searches the database for specific TR's or DV's
104: */
105: public static void main(String[] args) {
106: int result = 0;
107:
108: try {
109: SearchMeta me = new SearchMeta("searchmeta");
110:
111: // obtain commandline options first -- we may need the database stuff
112: Getopt opts = new Getopt(me.m_application, args,
113: "hd:t:a:V", me.generateValidOptions());
114:
115: opts.setOpterr(false);
116: String arg = null;
117: String type = null;
118: int option = 0;
119: while ((option = opts.getopt()) != -1) {
120: switch (option) {
121: case 1:
122: me.increaseVerbosity();
123: break;
124:
125: case 'V':
126: System.out
127: .println("$Id: SearchMeta.java 50 2007-05-19 00:48:32Z gmehta $");
128: System.out.println("VDS version "
129: + Version.instance().toString());
130: return;
131:
132: case 'a':
133: arg = opts.getOptarg();
134: break;
135:
136: case 't':
137: type = opts.getOptarg();
138: break;
139:
140: case 'h':
141: default:
142: me.showUsage();
143: return;
144: }
145: }
146:
147: // sanity check
148: int classType = -1;
149: if (type == null || type.length() == 0) {
150: me.showUsage();
151: throw new RuntimeException(
152: "You must specify a -t argument");
153: } else {
154: switch (Character.toUpperCase(type.charAt(0))) {
155: case 'D': // DV
156: classType = Annotation.CLASS_DERIVATION;
157: break;
158: case 'T': // TR
159: classType = Annotation.CLASS_TRANSFORMATION;
160: if (arg != null && arg.length() > 0) {
161: classType = Annotation.CLASS_DECLARE;
162: }
163: break;
164: case 'L': // LFN
165: classType = Annotation.CLASS_FILENAME;
166: break;
167: default:
168: me.showUsage();
169: throw new RuntimeException("Illegal value " + type
170: + " for option -t");
171: }
172: }
173:
174: // Connect the database.
175: String schemaName = ChimeraProperties.instance()
176: .getVDCSchemaName();
177: Connect connect = new Connect();
178: DatabaseSchema dbschema = connect
179: .connectDatabase(schemaName);
180:
181: if (!(dbschema instanceof Annotation)) {
182: dbschema.close();
183: throw new RuntimeException(
184: "The database does not support metadata!");
185: } else {
186: // safe to cast now
187: Annotation annotation = (Annotation) dbschema;
188:
189: // open query file -- or read from stdin
190: String qfile = (opts.getOptind() < args.length ? args[opts
191: .getOptind()]
192: : null);
193: LineNumberReader lnr = null;
194: if (qfile == null || qfile.equals("-")) {
195: System.err
196: .println("# reminder: reading from stdin");
197: lnr = new LineNumberReader(new InputStreamReader(
198: System.in));
199: } else {
200: lnr = new LineNumberReader(new FileReader(qfile));
201: }
202:
203: QueryParser parser = new QueryParser(lnr);
204: QueryTree tree = parser.parse();
205:
206: Logging.instance().log("app", 1,
207: "searching the database");
208: java.util.List list = annotation.searchAnnotation(
209: classType, arg, tree);
210:
211: if (list != null && !list.isEmpty()) {
212: if (classType == Annotation.CLASS_FILENAME) {
213: for (Iterator i = list.iterator(); i.hasNext();) {
214: System.out.println((String) i.next());
215: }
216: } else {
217: for (Iterator i = list.iterator(); i.hasNext();) {
218: Definition def = (Definition) i.next();
219: System.out.println(def.identify());
220: }
221: }
222: } else {
223: Logging.instance().log("app", 1, "no results");
224: }
225: }
226:
227: // done
228: if (dbschema != null)
229: dbschema.close();
230: } catch (RuntimeException rte) {
231: Logging.instance().log("default", 0,
232: "runtime error: " + rte.getMessage());
233: System.err.println("ERROR: " + rte.getMessage());
234: result = 1;
235:
236: } catch (Exception e) {
237: Logging.instance().log("default", 0,
238: "FATAL: " + e.getMessage());
239: e.printStackTrace();
240: System.err.println("FATAL: " + e.getMessage());
241: result = 2;
242: }
243:
244: if (result != 0)
245: System.exit(result);
246: }
247: }
|