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.*;
020: import gnu.getopt.*;
021:
022: import org.griphyn.vdl.util.Logging;
023: import org.griphyn.vdl.util.ChimeraProperties;
024: import org.griphyn.vdl.dbschema.*;
025: import org.griphyn.vdl.directive.*;
026: import org.griphyn.vdl.annotation.*;
027: import org.griphyn.common.util.Separator;
028: import org.griphyn.common.util.Version;
029:
030: /**
031: * This class deletes annotations for definition's and lfn's
032: *
033: * @author Jens-S. Vöckler
034: * @author Yong Zhao
035: * @version $Revision: 50 $
036: *
037: */
038: public class DeleteMeta extends Toolkit {
039: /**
040: * Constructor
041: */
042: public DeleteMeta(String appName) {
043: super (appName);
044: }
045:
046: /**
047: * Prints the usage string.
048: */
049: public void showUsage() {
050: String linefeed = System.getProperty("line.separator", "\r\n");
051:
052: System.out
053: .println("$Id: DeleteMeta.java 50 2007-05-19 00:48:32Z gmehta $"
054: + linefeed
055: + "VDS version "
056: + Version.instance().toString() + linefeed);
057:
058: System.out.println("Usage: " + this .m_application
059: + " [general] [-n ns] [-i id] [-v vs] [-a arg] keys"
060: + linefeed + "Or : " + this .m_application
061: + " [general] [-f lfn] keys");
062:
063: System.out
064: .println(linefeed
065: + "General options: "
066: + linefeed
067: + " -V|--version print version information and exit."
068: + linefeed
069: + " -d|--dbase db associates the dbname with the database, unused."
070: + linefeed
071: + " --verbose increases the verbosity level."
072: + linefeed
073: + " -t|--type tr|dv limits candidates to either TR or DV, default is both."
074: + linefeed
075: + linefeed
076: + "Option group 1: Arguments dealing with VDL definitions "
077: + linefeed
078: + " -n|--vdlns ns limits candidates to definition namespace matches."
079: + linefeed
080: + " -i|--vdlid id limits candidates to definition name matches."
081: + linefeed
082: + " -v|--vdlvs vs limits candidates to definition version matches."
083: + linefeed
084: + " -a|--arg arg limits to formal arguments, requires -t tr."
085: + linefeed
086: + linefeed
087: + "Option group 2: Arguments dealing with VDL logical filenames"
088: + linefeed
089: + " -f|--file lfn limits candidates to logical filename matches."
090: + linefeed
091: + linefeed
092: + "Option groups 1 and 2 are mutually exclusive."
093: + linefeed);
094: }
095:
096: /**
097: * Creates a set of options.
098: * @return the assembled long option list
099: */
100: protected LongOpt[] generateValidOptions() {
101: LongOpt[] lo = new LongOpt[17];
102:
103: lo[0] = new LongOpt("dbase", LongOpt.REQUIRED_ARGUMENT, null,
104: 'd');
105: lo[1] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
106: lo[2] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
107: lo[3] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
108:
109: lo[4] = new LongOpt("vdlns", LongOpt.REQUIRED_ARGUMENT, null,
110: 'n');
111: lo[5] = new LongOpt("namespace", LongOpt.REQUIRED_ARGUMENT,
112: null, 'n');
113: lo[6] = new LongOpt("ns", LongOpt.REQUIRED_ARGUMENT, null, 'n');
114:
115: lo[7] = new LongOpt("vdlid", LongOpt.REQUIRED_ARGUMENT, null,
116: 'i');
117: lo[8] = new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null,
118: 'i');
119: lo[9] = new LongOpt("identifier", LongOpt.REQUIRED_ARGUMENT,
120: null, 'i');
121: lo[10] = new LongOpt("id", LongOpt.REQUIRED_ARGUMENT, null, 'i');
122:
123: lo[11] = new LongOpt("vdlvs", LongOpt.REQUIRED_ARGUMENT, null,
124: 'v');
125: lo[12] = new LongOpt("vs", LongOpt.REQUIRED_ARGUMENT, null, 'v');
126:
127: lo[13] = new LongOpt("arg", LongOpt.REQUIRED_ARGUMENT, null,
128: 'a');
129: lo[14] = new LongOpt("args", LongOpt.REQUIRED_ARGUMENT, null,
130: 'a');
131: lo[15] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null,
132: 't');
133:
134: lo[16] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null,
135: 'f');
136:
137: return lo;
138: }
139:
140: /**
141: * Searches the database for specific TR's or DV's
142: */
143: public static void main(String[] args) {
144: int result = 0;
145:
146: try {
147: DeleteMeta me = new DeleteMeta("deletemeta");
148:
149: // no arguments -- yikes
150: if (args.length == 0) {
151: me.showUsage();
152: return;
153: }
154:
155: // obtain commandline options first -- we may need the database stuff
156: Getopt opts = new Getopt(me.m_application, args,
157: "hd:t:n:i:v:a:f:V", me.generateValidOptions());
158:
159: opts.setOpterr(false);
160: String secondary = null;
161: String primary = new String();
162: String type = null;
163: String vdlns = null;
164: String vdlid = null;
165: String vdlvs = null;
166: String lfn = null;
167: String arg = null;
168:
169: int option = 0;
170: while ((option = opts.getopt()) != -1) {
171: switch (option) {
172: case 1:
173: me.increaseVerbosity();
174: break;
175:
176: case 'V':
177: System.out
178: .println("$Id: DeleteMeta.java 50 2007-05-19 00:48:32Z gmehta $");
179: System.out.println("VDS version "
180: + Version.instance().toString());
181: return;
182:
183: case 'a':
184: arg = opts.getOptarg();
185: break;
186:
187: case 'd':
188: // currently inactive option
189: opts.getOptarg();
190: break;
191:
192: case 'f':
193: lfn = opts.getOptarg();
194: break;
195:
196: case 'i':
197: vdlid = opts.getOptarg();
198: break;
199:
200: case 'n':
201: vdlns = opts.getOptarg();
202: break;
203:
204: case 't':
205: type = opts.getOptarg();
206: break;
207:
208: case 'v':
209: vdlvs = opts.getOptarg();
210: break;
211:
212: case 'h':
213: default:
214: me.showUsage();
215: return;
216: }
217: }
218:
219: // check that there are remaining arguments?
220: if (opts.getOptind() == args.length - 1) {
221: me.showUsage();
222: throw new RuntimeException(
223: "You must specify keys to search for");
224: }
225:
226: boolean condition1 = (lfn != null);
227: boolean condition2 = (vdlns != null || vdlid != null || vdlvs != null);
228: // (a XOR b) <=> (a AND !b) OR (!a AND b)
229: // !(a XOR b) <=> (a OR !b) AND (!a OR b)
230: if ((condition1 || !condition2)
231: && (!condition1 || condition2)) {
232: me.showUsage();
233: throw new RuntimeException(
234: "You must either specify the -n -i -v options, or\n"
235: + "\tuse the -f option!");
236: }
237:
238: int classType = -1;
239: if (condition2) {
240: // metadata for TR or DV?
241: switch (Character.toUpperCase(type.charAt(0))) {
242: case 'D': // DV
243: classType = Annotation.CLASS_DERIVATION;
244: break;
245:
246: case 'T': // TR
247: classType = Annotation.CLASS_TRANSFORMATION;
248: if (arg != null) {
249: classType = Annotation.CLASS_DECLARE;
250: secondary = arg;
251: }
252: break;
253:
254: default:
255: me.showUsage();
256: throw new RuntimeException("invalid argument \""
257: + type + "\" for option t");
258: }
259:
260: // will be used for 'D' and 'T' only
261: primary = Separator.combine(vdlns, vdlid, vdlvs);
262: } else {
263: // must be condition1
264: classType = Annotation.CLASS_FILENAME;
265: primary = lfn;
266: }
267:
268: // Connect the database.
269: String schemaName = ChimeraProperties.instance()
270: .getVDCSchemaName();
271: Connect connect = new Connect();
272: DatabaseSchema dbschema = connect
273: .connectDatabase(schemaName);
274:
275: // sanity check
276: if (!(dbschema instanceof Annotation)) {
277: dbschema.close();
278: throw new RuntimeException(
279: "The database does not support metadata!");
280: } else {
281: Annotation annotation = (Annotation) dbschema;
282: for (int i = opts.getOptind(); i < args.length; ++i) {
283: Logging.instance().log("app", 2,
284: "deleting key[" + i + "] " + args[i]);
285: annotation.deleteAnnotation(primary, secondary,
286: classType, args[i]);
287: }
288: Logging.instance().log("app", 1,
289: "Metadata deleted successfully!");
290: }
291: if (dbschema != null)
292: dbschema.close();
293: } catch (RuntimeException rte) {
294: Logging.instance().log("default", 0,
295: "runtime error: " + rte.getMessage());
296: System.err.println("ERROR: " + rte.getMessage());
297: result = 1;
298:
299: } catch (Exception e) {
300: Logging.instance().log("default", 0,
301: "FATAL: " + e.getMessage());
302: e.printStackTrace();
303: System.err.println("FATAL: " + e.getMessage());
304: result = 2;
305: }
306:
307: if (result != 0)
308: System.exit(result);
309: }
310: }
|