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 java.sql.SQLException;
021: import gnu.getopt.*;
022:
023: import org.griphyn.common.util.Version;
024: import org.griphyn.vdl.parser.*;
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:
031: /**
032: * This class deletes definition's that match the namespace, identifier,
033: * version triple.
034: *
035: * @author Jens-S. Vöckler
036: * @author Yong Zhao
037: * @version $Revision: 50 $
038: *
039: * @see org.griphyn.vdl.parser.VDLxParser
040: */
041: public class DeleteVDC extends Toolkit {
042: /**
043: * Constructor
044: */
045: public DeleteVDC(String appName) {
046: super (appName);
047: }
048:
049: /**
050: * Print the usage string
051: */
052: public void showUsage() {
053: String linefeed = System.getProperty("line.separator", "\r\n");
054:
055: System.out
056: .println("$Id: DeleteVDC.java 50 2007-05-19 00:48:32Z gmehta $"
057: + linefeed
058: + "VDS version "
059: + Version.instance().toString() + linefeed);
060:
061: System.out
062: .println("Usage: "
063: + this .m_application
064: + " [-d db] [-t tr|dv] [-f] [-n ns] [-i id] [-v vs] [-c fn]");
065:
066: System.out
067: .println(linefeed
068: + "Options: "
069: + linefeed
070: + " -V|--version print version information and exit."
071: + linefeed
072: + " -d|--dbase db associates the dbname with the database, unused."
073: + linefeed
074: + " --verbose increases the verbosity level."
075: + linefeed
076: + " -t|--type tr|dv limits candidates to either TR or DV, default is both."
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: + " -f|--force permits removals in batch mode, assuming wildcards."
085: + linefeed
086: + " Beware, without any other options, everything will be removed!"
087: + linefeed
088: + " -c|--capture fn captures removed definitions into file fn."
089: + linefeed);
090: }
091:
092: /**
093: * Creates a set of options.
094: */
095: protected LongOpt[] generateValidOptions() {
096: LongOpt[] lo = new LongOpt[16];
097:
098: lo[0] = new LongOpt("dbase", LongOpt.REQUIRED_ARGUMENT, null,
099: 'd');
100: lo[1] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
101: lo[2] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
102: lo[3] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
103:
104: lo[4] = new LongOpt("vdlns", LongOpt.REQUIRED_ARGUMENT, null,
105: 'n');
106: lo[5] = new LongOpt("namespace", LongOpt.REQUIRED_ARGUMENT,
107: null, 'n');
108: lo[6] = new LongOpt("ns", LongOpt.REQUIRED_ARGUMENT, null, 'n');
109:
110: lo[7] = new LongOpt("vdlid", LongOpt.REQUIRED_ARGUMENT, null,
111: 'i');
112: lo[8] = new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null,
113: 'i');
114: lo[9] = new LongOpt("identifier", LongOpt.REQUIRED_ARGUMENT,
115: null, 'i');
116: lo[10] = new LongOpt("id", LongOpt.REQUIRED_ARGUMENT, null, 'i');
117:
118: lo[11] = new LongOpt("vdlvs", LongOpt.REQUIRED_ARGUMENT, null,
119: 'v');
120: lo[12] = new LongOpt("vs", LongOpt.REQUIRED_ARGUMENT, null, 'v');
121:
122: lo[13] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null,
123: 't');
124: lo[14] = new LongOpt("force", LongOpt.NO_ARGUMENT, null, 'f');
125: lo[15] = new LongOpt("capture", LongOpt.REQUIRED_ARGUMENT,
126: null, 'c');
127:
128: return lo;
129: }
130:
131: /**
132: * Delete definition's given the triple: namespace, id and version.
133: */
134: public static void main(String[] args) {
135: String arg;
136: Writer capfile = null;
137: int classType = -1;
138: String vdlns = null;
139: String vdlid = null;
140: String vdlvs = null;
141: boolean force = false;
142:
143: DeleteVDC me = new DeleteVDC("deletevdc");
144:
145: try {
146: // obtain commandline options first -- we may need the database stuff
147: Getopt opts = new Getopt(me.m_application, args,
148: "c:d:fhi:n:t:v:V", me.generateValidOptions());
149: opts.setOpterr(false);
150: int option = 0;
151: while ((option = opts.getopt()) != -1) {
152: switch (option) {
153: case 1:
154: me.increaseVerbosity();
155: break;
156:
157: case 'V':
158: System.out
159: .println("$Id: DeleteVDC.java 50 2007-05-19 00:48:32Z gmehta $");
160: System.out.println("VDS version "
161: + Version.instance().toString());
162: return;
163:
164: case 'c':
165: arg = opts.getOptarg();
166: if (arg != null && arg.length() != 0) {
167: // yes, risk IOException, will be caught at bottom
168: capfile = new BufferedWriter(
169: new FileWriter(arg));
170: }
171: break;
172:
173: case 'd':
174: // currently inactive option
175: opts.getOptarg();
176: break;
177:
178: case 'f':
179: force = !force;
180: break;
181:
182: case 'i':
183: vdlid = opts.getOptarg();
184: break;
185:
186: case 'n':
187: vdlns = opts.getOptarg();
188: break;
189:
190: case 't':
191: // type, must be TR or DV (or T or D)
192: arg = opts.getOptarg();
193: switch (Character.toUpperCase(arg.charAt(0))) {
194: case 'D': // derivation
195: classType = Definition.DERIVATION;
196: break;
197: case 'T': // transformation
198: classType = Definition.TRANSFORMATION;
199: break;
200: default:
201: me.showUsage();
202: throw new RuntimeException(
203: "invalid argument \"" + arg
204: + "\" for option t");
205: }
206: break;
207:
208: case 'v':
209: vdlvs = opts.getOptarg();
210: break;
211:
212: case '?':
213: System.out.println("Invalid option '"
214: + (char) opts.getOptopt() + "'");
215: default:
216: case 'h':
217: me.showUsage();
218: return;
219: }
220: }
221:
222: // Connect the database.
223: String schemaName = ChimeraProperties.instance()
224: .getVDCSchemaName();
225:
226: Connect connect = new Connect();
227: DatabaseSchema dbschema = connect
228: .connectDatabase(schemaName);
229:
230: // Search the database, delete the matched definitions
231: me.m_logger.log("app", 1, "Searching the database");
232: Delete delete = new Delete(dbschema);
233: java.util.List defList = delete.deleteDefinition(vdlns,
234: vdlid, vdlvs, classType, capfile, force);
235:
236: if (defList.isEmpty()) {
237: me.m_logger.log("app", 1,
238: "no matching definitions in the database");
239: } else {
240: me.m_logger.log("app", 1, "removed " + defList.size()
241: + " definitions");
242: }
243:
244: try {
245: if (capfile != null) {
246: capfile.flush();
247: capfile.close();
248: }
249: } catch (IOException e) {
250: me.m_logger.log("default", 0, "I/O error: "
251: + e.getMessage() + ", ignoring");
252: }
253:
254: // done
255: if (dbschema != null)
256: dbschema.close();
257:
258: } catch (SQLException sql) {
259: // database problems
260: for (int i = 0; sql != null; ++i) {
261: Logging.instance().log(
262: "default",
263: 0,
264: "SQL error " + i + ": " + sql.getErrorCode()
265: + ": " + sql.getMessage());
266: sql = sql.getNextException();
267: }
268: System.exit(1);
269:
270: } catch (IOException e) {
271: me.m_logger.log("default", 0, "I/O error");
272: System.err.println(e.getMessage());
273: System.exit(1);
274: } catch (RuntimeException rte) {
275: me.m_logger.log("default", 0, "runtime error");
276: System.err.println(rte.getMessage());
277: System.exit(1);
278: } catch (Exception e) {
279: e.printStackTrace();
280: System.exit(1);
281: }
282: }
283: }
|