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.cPlanner.toolkit;
017:
018: import org.griphyn.cPlanner.common.LogManager;
019:
020: import org.griphyn.common.util.FactoryException;
021: import org.griphyn.common.util.Currently;
022:
023: import gnu.getopt.Getopt;
024: import gnu.getopt.LongOpt;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.io.FileOutputStream;
030: import java.io.FileInputStream;
031: import java.io.PrintWriter;
032: import java.io.FileWriter;
033:
034: import java.util.Collections;
035: import java.util.List;
036: import java.util.Properties;
037: import java.util.Enumeration;
038: import java.util.Iterator;
039: import java.util.Set;
040: import java.util.HashSet;
041: import java.util.Map;
042: import java.util.TreeMap;
043: import java.util.HashMap;
044:
045: import java.util.regex.Pattern;
046:
047: /**
048: * A Central Properties class that keeps track of all the properties used by
049: * Pegasus. All other classes access the methods in this class to get the value
050: * of the property. It access the VDSProperties class to read the property file.
051: *
052: * @author Karan Vahi
053: * @author Gaurang Mehta
054: *
055: * @version $Revision: 203 $
056: *
057: * @see org.griphyn.common.util.VDSProperties
058: */
059: public class VDS2PegasusProperties extends Executable {
060:
061: /**
062: * The handle to the internal map, that maps vds properties to pegasus
063: * properties.
064: */
065: private static Map mVDSToPegasusPropertiesTable;
066:
067: /**
068: * An internal table that resolves the old transfer mode property, to
069: * the corresponding transfer implementation.
070: */
071: private static Map mTXFERImplTable;
072:
073: /**
074: * An internal table that resolves the old transfer mode property, to
075: * the corresponding transfer refiner.
076: */
077: private static Map mTXFERRefinerTable;
078:
079: /**
080: * Store the regular expressions necessary to match the * properties.
081: */
082: private static final String mRegexExpression[] = {
083: "(vds.replica.)([a-zA-Z_0-9]*[-]*)+(.prefer.stagein.sites)",
084: "(vds.replica.)([a-zA-Z_0-9]*[-]*)+(.ignore.stagein.sites)",
085: "(vds.site.selector.env.)([a-zA-Z_0-9]*[-]*)+",
086: "(vds.exitcode.path.)([a-zA-Z_0-9]*[-]*)+",
087: "(vds.partitioner.horizontal.bundle.)([a-zA-Z_0-9]*[-]*)+",
088: "(vds.partitioner.horizontal.collapse.)([a-zA-Z_0-9]*[-]*)+",
089: "(vds.transfer.rft.)([a-zA-Z_0-9]*[-]*)+",
090: "(vds.transfer.crft.)([a-zA-Z_0-9]*[-]*)+",
091: //"(vds.db.)([a-zA-Z_0-9]*[-]*)+(.)([a-zA-Z_0-9.]*[-]*)+"
092: "(vds.db.tc.driver)[.]+([a-zA-Z_0-9]*[-]*)+",
093: "(vds.db.ptc.driver)[.]+([a-zA-Z_0-9]*[-]*)+",
094: "(vds.db.\\*.driver)[.]+([a-zA-Z_0-9]*[-]*)+", };
095:
096: /**
097: * Replacement 2 D Array for the above properties.
098: */
099: private static final String mStarReplacements[][] = {
100: { "vds.replica.", "pegasus.selector.replica." },
101: { "vds.replica.", "pegasus.selector.replica." },
102: { "vds.site.selector.env.", "pegasus.selector.site.env." },
103: { "vds.exitcode.path.", "pegasus.exitcode.path." },
104: { "vds.partitioner.horizontal.bundle",
105: "pegasus.partitioner.horizontal.bundle." },
106: { "vds.partitioner.horizontal.collapse",
107: "pegasus.partitioner.horizontal.collapse." },
108: { "vds.transfer.rft.", "pegasus.transfer.rft." },
109: { "vds.transfer.crft.", "pegasus.transfer.crft." },
110: //{ "vds.db.", "pegasus.db." }
111: { "vds.db.tc.driver.", "pegasus.catalog.transformation.db." },
112: { "vds.db.ptc.driver.", "pegasus.catalog.provenance.db." },
113: { "vds.db.\\*.driver.", "pegasus.catalog.*.db." }, };
114:
115: /**
116: * Stores compiled patterns at first use, quasi-Singleton.
117: */
118: private static Pattern mCompiledPatterns[] = null;
119:
120: /**
121: * The input directory containing the kickstart records.
122: */
123: private String mInputFile;
124:
125: /**
126: * The output directory where to generate the ploticus output.
127: */
128: private String mOutputDir;
129:
130: /**
131: * The default constructor. Compiles the patterns only once.
132: */
133: public VDS2PegasusProperties() {
134: // initialize the compiled expressions once
135: if (mCompiledPatterns == null) {
136: mCompiledPatterns = new Pattern[mRegexExpression.length];
137: for (int i = 0; i < mRegexExpression.length; i++)
138: mCompiledPatterns[i] = Pattern
139: .compile(mRegexExpression[i]);
140: }
141: }
142:
143: /**
144: * Singleton access to the transfer implementation table.
145: * Contains the mapping of the old transfer property value to the
146: * new transfer implementation property value.
147: *
148: * @return map
149: */
150: private static Map transferImplementationTable() {
151: //singleton access
152: if (mTXFERImplTable == null) {
153: mTXFERImplTable = new HashMap(13);
154: mTXFERImplTable.put("Bundle", "Transfer");
155: mTXFERImplTable.put("Chain", "Transfer");
156: mTXFERImplTable.put("CRFT", "CRFT");
157: mTXFERImplTable.put("GRMS", "GRMS");
158: mTXFERImplTable.put("multiple", "Transfer");
159: mTXFERImplTable.put("Multiple", "Transfer");
160: mTXFERImplTable.put("MultipleTransfer", "Transfer");
161: mTXFERImplTable.put("RFT", "RFT");
162: mTXFERImplTable.put("single", "OldGUC");
163: mTXFERImplTable.put("Single", "OldGUC");
164: mTXFERImplTable.put("SingleTransfer", "OldGUC");
165: mTXFERImplTable.put("StorkSingle", "Stork");
166: mTXFERImplTable.put("T2", "T2");
167: }
168: return mTXFERImplTable;
169: }
170:
171: /**
172: * Singleton access to the transfer refiner table.
173: * Contains the mapping of the old transfer property value to the
174: * new transfer refiner property value.
175: *
176: * @return map
177: */
178: private static Map transferRefinerTable() {
179: //singleton access
180: if (mTXFERRefinerTable == null) {
181: mTXFERRefinerTable = new HashMap(13);
182: mTXFERRefinerTable.put("Bundle", "Bundle");
183: mTXFERRefinerTable.put("Chain", "Chain");
184: mTXFERRefinerTable.put("CRFT", "Default");
185: mTXFERRefinerTable.put("GRMS", "GRMS");
186: mTXFERRefinerTable.put("multiple", "Default");
187: mTXFERRefinerTable.put("Multiple", "Default");
188: mTXFERRefinerTable.put("MultipleTransfer", "Default");
189: mTXFERRefinerTable.put("RFT", "Default");
190: mTXFERRefinerTable.put("single", "SDefault");
191: mTXFERRefinerTable.put("Single", "SDefault");
192: mTXFERRefinerTable.put("SingleTransfer", "SDefault");
193: mTXFERRefinerTable.put("StorkSingle", "Single");
194: mTXFERRefinerTable.put("T2", "Default");
195: }
196: return mTXFERRefinerTable;
197: }
198:
199: /**
200: * Singleton access to the transfer implementation table.
201: * Contains the mapping of the old transfer property value to the
202: * new transfer implementation property value.
203: *
204: * @return map
205: */
206: private static Map vdsToPegasusPropertiesTable() {
207: //return the already existing one if possible
208: if (mVDSToPegasusPropertiesTable != null) {
209: return mVDSToPegasusPropertiesTable;
210: }
211:
212: associate("vds.home", "pegasus.home");
213:
214: //PROPERTIES RELATED TO SCHEMAS
215: associate("vds.schema.dax", "pegasus.schema.dax");
216: associate("vds.schema.pdax", "pegasus.schema.pdax");
217: associate("vds.schema.poolconfig", "pegasus.schema.sc");
218: associate("vds.schema.sc", "pegasus.schema.sc");
219: associate("vds.db.ptc.schema", "pegasus.catalog.provenance");
220:
221: //PROPERTIES RELATED TO DIRECTORIES
222: associate("vds.dir.exec", "pegasus.dir.exec");
223: associate("vds.dir.storage", "pegasus.dir.storage");
224: associate("vds.dir.create.mode", "pegasus.dir.create");
225: associate("vds.dir.create", "pegasus.dir.create");
226: associate("vds.dir.timestamp.extended",
227: "pegasus.dir.timestamp.extended");
228:
229: //PROPERTIES RELATED TO THE TRANSFORMATION CATALOG
230: associate("vds.tc.mode", "pegasus.catalog.transformation");
231: associate("vds.tc", "pegasus.catalog.transformation");
232: associate("vds.tc.file", "pegasus.catalog.transformation.file");
233: associate("vds.tc.mapper",
234: "pegasus.catalog.transformation.mapper");
235:
236: //REPLICA CATALOG PROPERTIES
237: associate("vds.replica.mode", "pegasus.catalog.replica");
238: associate("vds.rc", "pegasus.catalog.replica");
239: associate("vds.rls.url", "pegasus.catalog.replica.url");
240: associate("vds.rc.url", "pegasus.catalog.replica.url");
241: associate("vds.rc.lrc.ignore",
242: "pegasus.catalog.replica.lrc.ignore");
243: associate("vds.rc.lrc.restrict",
244: "pegasus.catalog.replica.lrc.restrict");
245: associate("vds.cache.asrc",
246: "pegasus.catalog.replica.cache.asrc");
247: associate("vds.rls.query", "");
248: associate("vds.rls.query.attrib", "");
249: associate("vds.rls.exit", "");
250: associate("vds.rc.rls.timeout", "");
251:
252: //SITE CATALOG PROPERTIES
253: associate("vds.pool.mode", "pegasus.catalog.site");
254: associate("vds.sc", "pegasus.catalog.site");
255: associate("vds.pool.file", "pegasus.catalog.site.file");
256: associate("vds.sc.file", "pegasus.catalog.site.file");
257:
258: //PROPERTIES RELATED TO SELECTION
259: associate("vds.transformation.selector",
260: "pegasus.selector.transformation");
261:
262: associate("vds.rc.selector", "pegasus.selector.replica");
263: associate("vds.replica.selector", "pegasus.selector.replica");
264: // associate( "vds.replica.*.prefer.stagein.sites", "pegasus.selector.replica.*.prefer.stagein.sites" );
265: associate("vds.rc.restricted.sites",
266: "pegasus.selector.replica.*.ignore.stagein.sites");
267: // associate( "vds.replica.*.ignore.stagein.sites", "pegasus.selector.replica.*.ignore.stagein.sites" );
268:
269: associate("vds.site.selector", "pegasus.selector.site");
270: associate("vds.site.selector.path",
271: "pegasus.selector.site.path");
272: associate("vds.site.selector.timeout",
273: "pegasus.selector.site.timeout");
274: associate("vds.site.selector.keep.tmp",
275: "pegasus.selector.site.keep.tmp");
276:
277: //TRANSFER MECHANISM PROPERTIES
278: associate("vds.transfer.*.impl", "pegasus.transfer.*.impl");
279: associate("vds.transfer.stagein.impl",
280: "pegasus.transfer.stagein.impl");
281: associate("vds.transfer.stageout.impl",
282: "pegasus.transfer.stageout.impl");
283: associate("vds.transfer.stagein.impl",
284: "pegasus.transfer.inter.impl");
285: associate("vds.transfer.refiner", "pegasus.transfer.refiner");
286: associate("vds.transfer.single.quote",
287: "pegasus.transfer.single.quote");
288: associate("vds.transfer.throttle.processes",
289: "pegasus.transfer.throttle.processes");
290: associate("vds.transfer.throttle.streams",
291: "pegasus.transfer.throttle.streams");
292: associate("vds.transfer.force", "pegasus.transfer.force");
293: associate("vds.transfer.mode.links", "pegasus.transfer.links");
294: associate("vds.transfer.links", "pegasus.transfer.links");
295: associate("vds.transfer.thirdparty.sites",
296: "pegasus.transfer.*.thirdparty.sites");
297: associate("vds.transfer.thirdparty.pools",
298: "pegasus.transfer.*.thirdparty.sites");
299: associate("vds.transfer.*.thirdparty.sites",
300: "pegasus.transfer.*.thirdparty.sites");
301: associate("vds.transfer.stagein.thirdparty.sites",
302: "pegasus.transfer.stagein.thirdparty.sites");
303: associate("vds.transfer.stageout.thirdparty.sites",
304: "pegasus.transfer.stageout.thirdparty.sites");
305: associate("vds.transfer.inter.thirdparty.sites",
306: "pegasus.transfer.inter.thirdparty.sites");
307: associate("vds.transfer.staging.delimiter",
308: "pegasus.transfer.staging.delimiter");
309: associate("vds.transfer.disable.chmod.sites",
310: "pegasus.transfer.disable.chmod.sites");
311: associate("vds.transfer.proxy", "pegasus.transfer.proxy");
312: associate("vds.transfer.arguments",
313: "pegasus.transfer.arguments");
314: associate("vds.transfer.*.priority",
315: "pegasus.transfer.*.priority");
316: associate("vds.transfer.stagein.priority",
317: "pegasus.transfer.stagein.priority");
318: associate("vds.transfer.stageout.priority",
319: "pegasus.transfer.stageout.priority");
320: associate("vds.transfer.inter.priority",
321: "pegasus.transfer.inter.priority");
322: associate("vds.scheduler.stork.cred",
323: "pegasus.transfer.stork.cred");
324:
325: //PROPERTIES RELATED TO KICKSTART AND EXITCODE
326: associate("vds.gridstart", "pegasus.gridstart");
327: associate("vds.gridstart.invoke.always",
328: "pegasus.gristart.invoke.always");
329: associate("vds.gridstart.invoke.length",
330: "pegasus.gridstart.invoke.length");
331: associate("vds.gridstart.kickstart.stat",
332: "pegasus.gridstart.kickstart.stat");
333: associate("vds.gridstart.label", "pegasus.gristart.label");
334:
335: associate("vds.exitcode.impl", "pegasus.exitcode.impl");
336: associate("vds.exitcode.mode", "pegasus.exitcode.scope");
337: associate("vds.exitcode", "pegasus.exitcode.scope");
338: // associate( "vds.exitcode.path.[value]","pegasus.exitcode.path.[value]" );
339: associate("vds.exitcode.arguments",
340: "pegasus.exitcode.arguments");
341: associate("vds.exitcode.debug", "pegasus.exitcode.debug");
342: associate("vds.prescript.arguments",
343: "pegasus.prescript.arguments");
344:
345: //PROPERTIES RELATED TO REMOTE SCHEDULERS
346: associate("vds.scheduler.remote.projects",
347: "pegasus.remote.scheduler.projects");
348: associate("vds.scheduler.remote.queues",
349: "pegasus.remote.scheduler.queues");
350: // associate( "vds.scheduler.remote.maxwalltimes", "pegasus.remote.scheduler.maxwalltimes" );
351: associate("vds.scheduler.remote.min.maxtime",
352: "pegasus.remote.scheduler.min.maxtime");
353: associate("vds.scheduler.remote.min.maxwalltime",
354: "pegasus.remote.scheduler.min.maxwalltime");
355: associate("vds.scheduler.remote.min.maxcputime",
356: "pegasus.remote.scheduler.min.maxcputime");
357:
358: //PROPERTIES RELATED TO Condor and DAGMAN
359: associate("vds.scheduler.condor.release",
360: "pegasus.condor.release");
361: associate("vds.scheduler.condor.remove",
362: "pegasus.condor.remove");
363: associate("vds.scheduler.condor.arguments.quote",
364: "pegasus.condor.arguments.quote");
365: associate("vds.scheduler.condor.output.stream",
366: "pegasus.condor.output.stream");
367: associate("vds.scheduler.condor.error.stream",
368: "pegasus.condor.error.stream");
369: associate("vds.scheduler.condor.retry", "pegasus.dagman.retry");
370:
371: //JOB CLUSTERING
372: associate("vds.exec.node.collapse", "pegasus.clusterer.nodes");
373: associate("vds.job.aggregator",
374: "pegasus.clusterer.job.aggregator");
375: associate("vds.job.aggregator.seqexec.isgloballog",
376: "pegasus.clusterer.job.aggregator.hasgloballog");
377: associate("vds.clusterer.label.key",
378: "pegasus.clusterer.label.key");
379:
380: //MISCELLANEOUS
381: associate("vds.auth.gridftp.timeout",
382: "pegasus.auth.gridftp.timeout");
383: associate("vds.submit.mode", "pegasus.submit");
384: associate("vds.job.priority", "pegasus.job.priority");
385:
386: associate("vds.dax.callback", "pegasus.parser.dax.callback");
387: associate("vds.label.key", "pegasus.partitioner.label.key");
388: associate("vds.partitioner.label.key",
389: "pegasus.partitioner.label.key");
390: associate("vds.partition.parser.mode",
391: "pegasus.partitioner.parser.load");
392: // associate( "vds.partitioner.horizontal.bundle.", "pegasus.partitioner.horizontal.bundle." );
393: // associate( "vds.partitioner.horizontal.collapse.", "pegasus.partitioner.horizontal.collapse." );
394:
395: //SOME DB DRIVER PROPERTIES
396: associate("vds.db.*.driver", "pegasus.catalog.*.db.driver");
397: associate("vds.db.tc.driver",
398: "pegasus.catalog.transformation.db.driver");
399: associate("vds.db.ptc.driver",
400: "pegasus.catalog.provenance.db.driver");
401:
402: //WORK DB PROPERTIES
403: associate("work.db", "pegasus.catalog.work.db");
404: associate("work.db.hostname",
405: "pegasus.catalog.work.db.hostname");
406: associate("work.db.database",
407: "pegasus.catalog.work.db.database");
408: associate("work.db.user", "pegasus.catalog.work.db.user");
409: associate("work.db.password",
410: "pegasus.catalog.work.db.password");
411:
412: return mVDSToPegasusPropertiesTable;
413: }
414:
415: /**
416: * Convert a VDS Properties file to Pegasus properties.
417: *
418: * @param input the path to the VDS Properties file.
419: * @param directory the directory where the Pegasus properties file needs to be written out to.
420: *
421: * @return path to the properties file that is written.
422: *
423: * @exception IOException
424: */
425: public String convert(String input, String directory)
426: throws IOException {
427: File dir = new File(directory);
428:
429: //sanity check on the directory
430: sanityCheck(dir);
431:
432: //we only want to write out the VDS properties for time being
433: Properties ipProperties = new Properties();
434: ipProperties.load(new FileInputStream(input));
435: Properties vdsProperties = this .matchingSubset(ipProperties,
436: "vds", true);
437:
438: //traverse through the VDS properties and convert them to
439: //the new names
440: Properties temp = new Properties();
441: for (Iterator it = vdsProperties.keySet().iterator(); it
442: .hasNext();) {
443: String vds = (String) it.next();
444: String vdsValue = (String) vdsProperties.get(vds);
445: String pgs = (String) vdsToPegasusPropertiesTable()
446: .get(vds);
447:
448: //if pgs is not null store the pgs with the vds value
449: //if null then barf
450: if (pgs == null) {
451: //match for star properties
452: pgs = matchForStarProperties(vds);
453: if (pgs == null) {
454: System.err
455: .println("Unable to associate VDS property "
456: + vds);
457: continue;
458: }
459: } else {
460: if (pgs.length() == 0) {
461: //ignore
462: continue;
463: }
464: }
465: //put the pegasus property with the vds value
466: temp.setProperty(pgs, vdsValue);
467: }
468:
469: //put the properties in temp into PegasusProperties in a sorted order
470: //does not work, as the store method does not store it in that manner
471: Map pegasusProperties = new TreeMap();
472: for (Iterator it = temp.keySet().iterator(); it.hasNext();) {
473: String key = (String) it.next();
474: pegasusProperties.put(key, (String) temp.get(key));
475: }
476:
477: //create a temporary file in directory
478: File f = File.createTempFile("pegasus.", ".properties", dir);
479: PrintWriter pw = new PrintWriter(new FileWriter(f));
480:
481: //the header of the file
482: StringBuffer header = new StringBuffer(64);
483: header
484: .append("############################################################################\n");
485: header
486: .append(
487: "# PEGASUS USER PROPERTIES GENERATED FROM VDS PROPERTY FILE \n")
488: .append("# ( " + input + " ) \n").append(
489: "# GENERATED AT ").append(
490: Currently.iso8601(false, true, false,
491: new java.util.Date())).append("\n");
492: header
493: .append("############################################################################");
494:
495: pw.println(header.toString());
496:
497: for (Iterator it = pegasusProperties.entrySet().iterator(); it
498: .hasNext();) {
499: Map.Entry entry = (Map.Entry) it.next();
500: String line = entry.getKey() + " = " + entry.getValue();
501: pw.println(line);
502: }
503: pw.close();
504:
505: /*
506: //the header of the file
507: StringBuffer header = new StringBuffer(64);
508: header.append( "############################################################################\n" );
509: header.append( "# PEGASUS USER PROPERTIES GENERATED FROM VDS PROPERTY FILE \n#( " + input + " ) \n" )
510: .append( "# ESCAPES IN VALUES ARE INTRODUCED \n");
511: header.append( "############################################################################" );
512:
513:
514: //create an output stream to this file and write out the properties
515: OutputStream os = new FileOutputStream( f );
516: pegasusProperties.store( os, header.toString() );
517: os.close();
518:
519:
520: //convert the properties file into a sorted properties file
521: convertToSorted( f, dir );
522: */
523:
524: return f.getAbsolutePath();
525:
526: }
527:
528: /**
529: * Returns a matching pegasus property for a VDS star property.
530: *
531: * @param vds the vds property.
532: *
533: * @return the new Pegasus Property if found else, null.
534: */
535: protected String matchForStarProperties(String vds) {
536: String pgs = null;
537:
538: // match against pattern
539: for (int i = 0; i < mRegexExpression.length; i++) {
540: //if a vds property matches against existing patterns
541: if (mCompiledPatterns[i].matcher(vds).matches()) {
542: //get the replacement value
543: pgs = vds.replaceFirst(mStarReplacements[i][0],
544: mStarReplacements[i][1]);
545: System.out
546: .println("The matching pegasus * property for "
547: + vds + " is " + pgs);
548: break;
549: }
550: }
551: return pgs;
552: }
553:
554: /**
555: * The main test program.
556: *
557: * @param args the arguments to the program.
558: */
559: public static void main(String[] args) {
560: VDS2PegasusProperties me = new VDS2PegasusProperties();
561: int result = 0;
562:
563: try {
564: me.executeCommand(args);
565: } catch (FactoryException fe) {
566: me.log(fe.convertException(),
567: LogManager.FATAL_MESSAGE_LEVEL);
568: result = 2;
569: } catch (RuntimeException rte) {
570: //catch all runtime exceptions including our own that
571: //are thrown that may have chained causes
572: me.log(convertException(rte),
573: LogManager.FATAL_MESSAGE_LEVEL);
574: result = 1;
575: } catch (Exception e) {
576: //unaccounted for exceptions
577: me.log(e.getMessage(), LogManager.FATAL_MESSAGE_LEVEL);
578: e.printStackTrace();
579: result = 3;
580: }
581:
582: // warn about non zero exit code
583: if (result != 0) {
584: me.log("Non-zero exit-code " + result,
585: LogManager.WARNING_MESSAGE_LEVEL);
586: }
587:
588: System.exit(result);
589:
590: }
591:
592: /**
593: * Executes the command on the basis of the options specified.
594: *
595: * @param args the command line options.
596: */
597: public void executeCommand(String[] args) {
598: parseCommandLineArguments(args);
599:
600: //sanity check on output directory
601: mOutputDir = (mOutputDir == null) ? "." : mOutputDir;
602: File dir = new File(mOutputDir);
603: if (dir.exists()) {
604: //directory already exists.
605: if (dir.isDirectory()) {
606: if (!dir.canWrite()) {
607: throw new RuntimeException(
608: "Cannot write out to output directory "
609: + mOutputDir);
610: }
611: } else {
612: //directory is a file
613: throw new RuntimeException(mOutputDir
614: + " is not a directory ");
615: }
616:
617: } else {
618: dir.mkdirs();
619: }
620:
621: String output;
622: try {
623: output = this .convert(mInputFile, mOutputDir);
624: System.out
625: .println("Pegasus Properties Written out to file "
626: + output);
627: } catch (IOException ioe) {
628: throw new RuntimeException(
629: "Unable to convert properties file ", ioe);
630: }
631:
632: }
633:
634: /**
635: * Parses the command line arguments using GetOpt and returns a
636: * <code>PlannerOptions</code> contains all the options passed by the
637: * user at the command line.
638: *
639: * @param args the arguments passed by the user at command line.
640: */
641: public void parseCommandLineArguments(String[] args) {
642: LongOpt[] longOptions = generateValidOptions();
643:
644: Getopt g = new Getopt("properties-converter", args, "i:o:h",
645: longOptions, false);
646: g.setOpterr(false);
647:
648: int option = 0;
649:
650: while ((option = g.getopt()) != -1) {
651: //System.out.println("Option tag " + (char)option);
652: switch (option) {
653: case 'i'://input
654: this .mInputFile = g.getOptarg();
655: break;
656:
657: case 'h'://help
658: printLongVersion();
659: System.exit(0);
660: return;
661:
662: case 'o'://output directory
663: this .mOutputDir = g.getOptarg();
664: break;
665:
666: default: //same as help
667: printShortVersion();
668: throw new RuntimeException(
669: "Incorrect option or option usage "
670: + (char) option);
671:
672: }
673: }
674:
675: }
676:
677: /**
678: * Tt generates the LongOpt which contain the valid options that the command
679: * will accept.
680: *
681: * @return array of <code>LongOpt</code> objects , corresponding to the valid
682: * options
683: */
684: public LongOpt[] generateValidOptions() {
685: LongOpt[] longopts = new LongOpt[3];
686:
687: longopts[0] = new LongOpt("input", LongOpt.REQUIRED_ARGUMENT,
688: null, 'i');
689: longopts[1] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT,
690: null, 'o');
691: longopts[2] = new LongOpt("help", LongOpt.NO_ARGUMENT, null,
692: 'h');
693: return longopts;
694: }
695:
696: /**
697: * Prints out a short description of what the command does.
698: */
699: public void printShortVersion() {
700: String text = "\n $Id: VDS2PegasusProperties.java 203 2007-06-06 04:36:40Z gmehta $ "
701: + "\n "
702: + getGVDSVersion()
703: + "\n Usage : properties-converter [-Dprop [..]] -i <input directory> "
704: + " [-o output directory] [-h]";
705:
706: System.out.println(text);
707: }
708:
709: /**
710: * Prints the long description, displaying in detail what the various options
711: * to the command stand for.
712: */
713: public void printLongVersion() {
714:
715: String text = "\n $Id: VDS2PegasusProperties.java 203 2007-06-06 04:36:40Z gmehta $ "
716: + "\n "
717: + getGVDSVersion()
718: + "\n properties-converter - A tool that converts the VDS properties file to "
719: + "\n the corresponding Pegasus properties file "
720: + "\n Usage: properties-converter [-Dprop [..]] --input <input file> "
721: + "\n [--output output directory] [--help] "
722: + "\n"
723: + "\n Mandatory Options "
724: + "\n --input the path to the VDS properties file."
725: + "\n Other Options "
726: + "\n -o |--output the output directory where to generate the pegasus property file."
727: + "\n -h |--help generates this help." + "\n ";
728:
729: System.out.println(text);
730: //mLogger.log(text,LogManager.INFO_MESSAGE_LEVEL);
731: }
732:
733: /**
734: * Loads all the properties that would be needed by the Toolkit classes.
735: */
736: public void loadProperties() {
737: //empty for time being
738: }
739:
740: /**
741: * Returns the transfer implementation.
742: *
743: * @param property property name.
744: *
745: * @return the transfer implementation,
746: * else the one specified by "pegasus.transfer.*.impl",
747: * else the DEFAULT_TRANSFER_IMPLEMENTATION.
748: */
749: /*
750: public String getTransferImplementation(String property){
751: String value = mProps.getProperty(property,
752: getDefaultTransferImplementation());
753:
754: if(value == null){
755: //check for older deprecated properties
756: value = mProps.getProperty("pegasus.transfer");
757: value = (value == null)?
758: mProps.getProperty("pegasus.transfer.mode"):
759: value;
760:
761: //convert a non null value to the corresponding
762: //transfer implementation
763: if(value != null){
764: value = (String)transferImplementationTable().get(value);
765: logDeprecatedWarning("pegasus.transfer","pegasus.transfer.*.impl and " +
766: "pegasus.transfer.refiner");
767: }
768:
769: }
770:
771: //put in default if still we have a non null
772: value = (value == null)?
773: DEFAULT_TRANSFER_IMPLEMENTATION:
774: value;
775: return value;
776: }
777: */
778:
779: /**
780: * Returns the transfer refiner that is to be used for adding in the
781: * transfer jobs in the workflow
782: *
783: * Referred to by the "pegasus.transfer.refiner" property.
784: *
785: * @return the transfer refiner, else the DEFAULT_TRANSFER_REFINER.
786: *
787: * @see #DEFAULT_TRANSFER_REFINER
788: */
789: /*
790: public String getTransferRefiner(){
791: String value = mProps.getProperty("pegasus.transfer.refiner");
792: if(value == null){
793: //check for older deprecated properties
794: value = mProps.getProperty("pegasus.transfer");
795: value = (value == null)?
796: mProps.getProperty("pegasus.transfer.mode"):
797: value;
798:
799: //convert a non null value to the corresponding
800: //transfer refiner
801: if(value != null){
802: value = (String)transferRefinerTable().get(value);
803: logDeprecatedWarning("pegasus.transfer","pegasus.transfer.impl and " +
804: "pegasus.transfer.refiner");
805: }
806: }
807:
808: //put in default if still we have a non null
809: value = (value == null)?
810: DEFAULT_TRANSFER_REFINER:
811: value;
812: return value;
813:
814: }
815: */
816:
817: //SOME LOGGING PROPERTIES
818: /**
819: * Returns the file to which all the logging needs to be directed to.
820: *
821: * Referred to by the "vds.log.*" property.
822: *
823: * @return the value of the property that is specified, else
824: * null
825: */
826: // public String getLoggingFile(){
827: // return mProps.getProperty("vds.log.*");
828: // }
829:
830: /**
831: * Returns the location of the local log file where you want the messages to
832: * be logged. Not used for the moment.
833: *
834: * Referred to by the "vds.log4j.log" property.
835: *
836: * @return the value specified in the property file,else null.
837: */
838: // public String getLog4JLogFile() {
839: // return getProperty( "vds.log.file", "vds.log4j.log" );
840: // }
841:
842: /**
843: * Return returns the environment string specified for the local pool. If
844: * specified the registration jobs are set with these environment variables.
845: *
846: * Referred to by the "vds.local.env" property
847: *
848: * @return the environment string for local pool in properties file if
849: * defined, else null.
850: */
851: // public String getLocalPoolEnvVar() {
852: // return mProps.getProperty( "vds.local.env" );
853: // }
854: /**
855: * Returns a boolean indicating whether to treat the entries in the cache
856: * files as a replica catalog or not.
857: *
858: * @return boolean
859: */
860: // public boolean treatCacheAsRC(){
861: // return Boolean.parse(mProps.getProperty( "vds.cache.asrc"),
862: // false);
863: // }
864:
865: /**
866: * Checks the destination location for existence, if it can
867: * be created, if it is writable etc.
868: *
869: * @param dir is the new base directory to optionally create.
870: *
871: * @throws IOException in case of error while writing out files.
872: */
873: protected static void sanityCheck(File dir) throws IOException {
874: if (dir.exists()) {
875: // location exists
876: if (dir.isDirectory()) {
877: // ok, isa directory
878: if (dir.canWrite()) {
879: // can write, all is well
880: return;
881: } else {
882: // all is there, but I cannot write to dir
883: throw new IOException(
884: "Cannot write to existing directory "
885: + dir.getPath());
886: }
887: } else {
888: // exists but not a directory
889: throw new IOException("Destination " + dir.getPath()
890: + " already "
891: + "exists, but is not a directory.");
892: }
893: } else {
894: // does not exist, try to make it
895: if (!dir.mkdirs()) {
896: throw new IOException(
897: "Unable to create directory destination "
898: + dir.getPath());
899: }
900: }
901: }
902:
903: /**
904: * Extracts a specific property key subset from the known properties.
905: * The prefix may be removed from the keys in the resulting dictionary,
906: * or it may be kept. In the latter case, exact matches on the prefix
907: * will also be copied into the resulting dictionary.
908: *
909: * @param properties is the properties from where to get the subset.
910: * @param prefix is the key prefix to filter the properties by.
911: * @param keepPrefix if true, the key prefix is kept in the resulting
912: * dictionary. As side-effect, a key that matches the prefix
913: * exactly will also be copied. If false, the resulting
914: * dictionary's keys are shortened by the prefix. An
915: * exact prefix match will not be copied, as it would
916: * result in an empty string key.
917: *
918: * @return a property dictionary matching the filter key. May be
919: * an empty dictionary, if no prefix matches were found.
920: *
921: * @see #getProperty( String ) is used to assemble matches
922: */
923: public Properties matchingSubset(Properties properties,
924: String prefix, boolean keepPrefix) {
925: Properties result = new Properties();
926:
927: // sanity check
928: if (prefix == null || prefix.length() == 0)
929: return result;
930:
931: String prefixMatch; // match prefix strings with this
932: String prefixSelf; // match self with this
933: if (prefix.charAt(prefix.length() - 1) != '.') {
934: // prefix does not end in a dot
935: prefixSelf = prefix;
936: prefixMatch = prefix + '.';
937: } else {
938: // prefix does end in one dot, remove for exact matches
939: prefixSelf = prefix.substring(0, prefix.length() - 1);
940: prefixMatch = prefix;
941: }
942: // POSTCONDITION: prefixMatch and prefixSelf are initialized!
943:
944: // now add all matches into the resulting properties.
945: // Remark 1: #propertyNames() will contain the System properties!
946: // Remark 2: We need to give priority to System properties. This is done
947: // automatically by calling this class's getProperty method.
948: String key;
949: for (Enumeration e = properties.propertyNames(); e
950: .hasMoreElements();) {
951: key = (String) e.nextElement();
952:
953: if (keepPrefix) {
954: // keep full prefix in result, also copy direct matches
955: if (key.startsWith(prefixMatch)
956: || key.equals(prefixSelf))
957: result.setProperty(key, (String) properties
958: .get(key));
959: } else {
960: // remove full prefix in result, dont copy direct matches
961: if (key.startsWith(prefixMatch))
962: result.setProperty(key.substring(prefixMatch
963: .length()), (String) properties.get(key));
964: }
965: }
966:
967: // done
968: return result;
969: }
970:
971: /**
972: * Associates a VDS property with the new pegasus property.
973: *
974: * @param vdsProperty the old VDS property.
975: * @param pegasusProperty the new Pegasus property.
976: *
977: */
978: private static void associate(String vdsProperty,
979: String pegasusProperty) {
980: if (mVDSToPegasusPropertiesTable == null) {
981: mVDSToPegasusPropertiesTable = new HashMap(13);
982: }
983:
984: mVDSToPegasusPropertiesTable.put(vdsProperty, pegasusProperty);
985: }
986:
987: }
|