001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import org.apache.commons.cli.CommandLine;
008: import org.apache.commons.cli.CommandLineParser;
009: import org.apache.commons.cli.GnuParser;
010: import org.apache.commons.cli.HelpFormatter;
011: import org.apache.commons.cli.Options;
012: import org.apache.commons.lang.StringUtils;
013:
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.text.DateFormat;
017: import java.text.ParseException;
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.Properties;
021:
022: /**
023: * Utility class to retrieve the build information for the product.
024: */
025: public final class ProductInfo {
026: private static final ResourceBundleHelper bundleHelper = new ResourceBundleHelper(
027: ProductInfo.class);
028:
029: private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
030: "yyyyMMdd-HHmmss");
031:
032: private static final String BUILD_DATA_RESOURCE_NAME = "/build-data.txt";
033:
034: private static final String BUILD_DATA_ROOT_KEY = "terracotta.build.";
035: private static final String BUILD_DATA_VERSION_KEY = "version";
036: private static final String BUILD_DATA_EDITION_KEY = "edition";
037: private static final String BUILD_DATA_TIMESTAMP_KEY = "timestamp";
038: private static final String BUILD_DATA_HOST_KEY = "host";
039: private static final String BUILD_DATA_USER_KEY = "user";
040: private static final String BUILD_DATA_REVISION_KEY = "revision";
041: private static final String BUILD_DATA_BRANCH_KEY = "branch";
042: private static final String UNKNOWN_VALUE = "[unknown]";
043:
044: private final String moniker;
045: private final String version;
046: private final Date timestamp;
047: private final String host;
048: private final String user;
049: private final String branch;
050: private final String edition;
051: private final String revision;
052:
053: private ProductInfo(InputStream in, String fromWhere) {
054: Properties properties = new Properties();
055:
056: moniker = bundleHelper.getString("moniker");
057:
058: if (in != null) {
059: try {
060: properties.load(in);
061: } catch (IOException ioe) {
062: ioe.printStackTrace();
063: System.exit(1);
064: }
065: }
066:
067: this .version = getProperty(properties, BUILD_DATA_VERSION_KEY,
068: UNKNOWN_VALUE);
069: this .edition = getProperty(properties, BUILD_DATA_EDITION_KEY,
070: "opensource");
071:
072: String timestampString = getProperty(properties,
073: BUILD_DATA_TIMESTAMP_KEY, null);
074: this .host = getProperty(properties, BUILD_DATA_HOST_KEY,
075: UNKNOWN_VALUE);
076: this .user = getProperty(properties, BUILD_DATA_USER_KEY,
077: UNKNOWN_VALUE);
078: this .branch = getProperty(properties, BUILD_DATA_BRANCH_KEY,
079: UNKNOWN_VALUE);
080: this .revision = getProperty(properties,
081: BUILD_DATA_REVISION_KEY, UNKNOWN_VALUE);
082:
083: Date realTimestamp = null;
084: if (timestampString != null) {
085: try {
086: realTimestamp = DATE_FORMAT.parse(timestampString);
087: } catch (ParseException pe) {
088: pe.printStackTrace();
089: System.exit(1);
090: }
091: }
092:
093: this .timestamp = realTimestamp;
094: }
095:
096: private String getProperty(Properties properties, String name,
097: String defaultValue) {
098: String out = properties.getProperty(BUILD_DATA_ROOT_KEY + name);
099: if (StringUtils.isBlank(out))
100: out = defaultValue;
101: return out;
102: }
103:
104: private static ProductInfo PRODUCTINFO = null;
105:
106: public static synchronized ProductInfo getInstance() {
107: if (PRODUCTINFO == null) {
108: InputStream in = ProductInfo.class
109: .getResourceAsStream(BUILD_DATA_RESOURCE_NAME);
110: PRODUCTINFO = new ProductInfo(in, "resource '"
111: + BUILD_DATA_RESOURCE_NAME + "'");
112: }
113:
114: return PRODUCTINFO;
115: }
116:
117: public boolean isDevMode() {
118: return this .version.endsWith(UNKNOWN_VALUE);
119: }
120:
121: public String moniker() {
122: return this .moniker;
123: }
124:
125: public String edition() {
126: return this .edition;
127: }
128:
129: public String rawVersion() {
130: return this .version;
131: }
132:
133: public String buildVersion() {
134: return this .version;
135: }
136:
137: public Date buildTimestamp() {
138: return this .timestamp;
139: }
140:
141: public String buildTimestampAsString() {
142: if (this .timestamp == null)
143: return UNKNOWN_VALUE;
144: else
145: return DATE_FORMAT.format(this .timestamp);
146: }
147:
148: public String buildHost() {
149: return this .host;
150: }
151:
152: public String buildUser() {
153: return this .user;
154: }
155:
156: public String buildBranch() {
157: return this .branch;
158: }
159:
160: public String copyright() {
161: return bundleHelper.getString("copyright");
162: }
163:
164: public String buildRevision() {
165: return this .revision;
166: }
167:
168: public String toShortString() {
169: return this .moniker + " "
170: + ("opensource".equals(edition) ? "" : (edition + " "))
171: + buildVersion();
172: }
173:
174: public String toLongString() {
175: return toShortString() + ", as of " + buildTimestampAsString()
176: + " (Revision " + buildRevision() + " by "
177: + buildUser() + "@" + buildHost() + " from "
178: + buildBranch() + ")";
179: }
180:
181: public String toString() {
182: return toShortString();
183: }
184:
185: public static void main(String[] args) throws Exception {
186: Options options = new Options();
187: options.addOption("v", "verbose", false, bundleHelper
188: .getString("option.verbose"));
189: options.addOption("h", "help", false, bundleHelper
190: .getString("option.help"));
191:
192: CommandLineParser parser = new GnuParser();
193: CommandLine cli = parser.parse(options, args);
194:
195: if (cli.hasOption("h")) {
196: new HelpFormatter().printHelp("java "
197: + ProductInfo.class.getName(), options);
198: }
199:
200: if (cli.hasOption("v")) {
201: System.out.println(getInstance().toLongString());
202: } else {
203: System.out.println(getInstance().toShortString());
204: }
205: }
206: }
|