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 org.griphyn.common.util.Version;
020: import gnu.getopt.*;
021:
022: /**
023: * This class just prints the current version number on stdout.
024: *
025: * @author Jens-S. Vöckler
026: * @author Yong Zhao
027: * @version $Revision: 146 $
028: */
029: public class VersionNumber {
030: /**
031: * application's own name.
032: */
033: private String m_application = null;
034:
035: /**
036: * ctor: Constructs a new instance with the given application name.
037: * @param appName is the name of the application
038: */
039: public VersionNumber(String appName) {
040: m_application = appName;
041: }
042:
043: /**
044: * Prints the usage string.
045: */
046: public void showUsage() {
047: String linefeed = System.getProperty("line.separator", "\r\n");
048:
049: System.out
050: .println("$Id: VersionNumber.java 146 2007-05-29 21:26:10Z gmehta $"
051: + linefeed
052: + "PEGASUS version "
053: + Version.instance().toString() + linefeed);
054:
055: System.out.println("Usage: " + m_application
056: + " [-f | -V | -m]");
057: System.out
058: .println(linefeed
059: + "Options:"
060: + linefeed
061: + " -V|--version print version information about itself and exit."
062: + linefeed
063: + " --verbose increases the verbosity level (ignored)."
064: + linefeed
065: + " -f|--full also shows the internal built time stamp."
066: + linefeed
067: + " -m|--match matches internal version with installation."
068: + linefeed
069: + " -q|--quiet in match mode, no news are good news, use exit code."
070: + linefeed
071: + linefeed
072: + "The following exit codes are produced:"
073: + linefeed
074: + " 0 :-) Success"
075: + linefeed
076: + " 1 :-| Detected a mis-match in match mode. Your installation is bad!"
077: + linefeed
078: + " 2 :-( Runtime error detected, please read the message."
079: + linefeed
080: + " 3 8-O Fatal error merits a program abortion."
081: + linefeed);
082: }
083:
084: /**
085: * Creates a set of options.
086: * @return the assembled long option list
087: */
088: protected LongOpt[] generateValidOptions() {
089: LongOpt[] lo = new LongOpt[7];
090:
091: lo[0] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
092: lo[1] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
093: lo[2] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
094:
095: lo[3] = new LongOpt("full", LongOpt.NO_ARGUMENT, null, 'f');
096: lo[4] = new LongOpt("build", LongOpt.NO_ARGUMENT, null, 'f');
097: lo[5] = new LongOpt("match", LongOpt.NO_ARGUMENT, null, 'm');
098: lo[6] = new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q');
099:
100: return lo;
101: }
102:
103: public static void main(String args[]) {
104: int result = 0;
105: VersionNumber me = null;
106:
107: try {
108: me = new VersionNumber("vds-version");
109: Getopt opts = new Getopt(me.m_application, args, "Vfhmq",
110: me.generateValidOptions());
111: opts.setOpterr(false);
112: String installed = null;
113: String internal = null;
114: boolean build = false;
115: boolean quiet = false;
116: Version v = Version.instance();
117:
118: int option = 0;
119: while ((option = opts.getopt()) != -1) {
120: switch (option) {
121: case 1:
122: break;
123:
124: case 'V':
125: System.out
126: .println("$Id: VersionNumber.java 146 2007-05-29 21:26:10Z gmehta $");
127: System.out.println("PEGASUS version "
128: + v.toString());
129: return;
130:
131: case 'f':
132: build = true;
133: break;
134:
135: case 'm':
136: installed = v.determineInstalled();
137: internal = v.determineBuilt() + " "
138: + v.determinePlatform();
139: if (!quiet) {
140: System.out.println("Compiled into PEGASUS: "
141: + internal);
142: System.out.println("Provided by inst.: "
143: + installed);
144: }
145:
146: if (v.matches()) {
147: if (!quiet)
148: System.out
149: .println("OK: Internal version matches installation.");
150: return;
151: } else {
152: System.out
153: .println("ERROR: Internal version does not match installed version!");
154: System.out
155: .println("Your installation is suspicious, please correct!");
156: System.exit(1);
157: }
158: break;
159:
160: case 'q':
161: quiet = true;
162: break;
163:
164: case 'h':
165: default:
166: me.showUsage();
167: return;
168: }
169: }
170:
171: // action
172: System.out.print(v.toString());
173: if (build)
174: System.out.print('-' + v.determinePlatform() + '-'
175: + v.determineBuilt());
176: System.out.println();
177:
178: } catch (RuntimeException rte) {
179: System.err.println("ERROR: " + rte.getMessage());
180: result = 2;
181:
182: } catch (Exception e) {
183: e.printStackTrace();
184: System.err.println("FATAL: " + e.getMessage());
185: result = 3;
186: }
187:
188: if (result != 0)
189: System.exit(result);
190: }
191: }
|