1 5 package com.tc.util; 6 7 import org.apache.commons.cli.CommandLine; 8 import org.apache.commons.cli.CommandLineParser; 9 import org.apache.commons.cli.GnuParser; 10 import org.apache.commons.cli.HelpFormatter; 11 import org.apache.commons.cli.Options; 12 import org.apache.commons.lang.StringUtils; 13 14 import com.tc.logging.TCLogger; 15 import com.tc.logging.TCLogging; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.text.DateFormat ; 20 import java.text.ParseException ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.Properties ; 24 25 28 public final class ProductInfo { 29 private static final ResourceBundleHelper bundleHelper = new ResourceBundleHelper(ProductInfo.class); 30 31 private static final DateFormat DATE_FORMAT = new SimpleDateFormat ("yyyyMMdd-HHmmss"); 32 33 private static final String BUILD_DATA_RESOURCE_NAME = "/build-data.txt"; 34 35 private static final String BUILD_DATA_ROOT_KEY = "terracotta.build."; 36 private static final String BUILD_DATA_VERSION_KEY = "version"; 37 private static final String BUILD_DATA_TIMESTAMP_KEY = "timestamp"; 38 private static final String BUILD_DATA_HOST_KEY = "host"; 39 private static final String BUILD_DATA_USER_KEY = "user"; 40 private static final String BUILD_DATA_CHANGESET_KEY = "revision"; 41 private static final String BUILD_DATA_CHANGE_TAG_KEY = "change-tag"; 42 private static final String BUILD_DATA_BRANCH_KEY = "branch"; 43 44 private static final String UNKNOWN_VALUE = "[unknown]"; 45 46 private static final TCLogger logger = TCLogging.getLogger(ProductInfo.class); 61 62 private final String moniker; 63 private final String version; 64 private final Date timestamp; 65 private final String host; 66 private final String user; 67 private final String changeset; 68 private final String changeTag; 69 private final String branch; 70 71 private ProductInfo(InputStream in, String fromWhere) { 72 Properties properties = new Properties (); 73 74 moniker = bundleHelper.getString("moniker"); 75 76 if (in != null) { 77 try { 78 properties.load(in); 79 } catch (IOException ioe) { 80 logger.warn(bundleHelper.format("load.properties.failure", new Object [] { fromWhere })); 81 } 82 } 83 84 this.version = getProperty(properties, BUILD_DATA_VERSION_KEY, UNKNOWN_VALUE); 85 String timestampString = getProperty(properties, BUILD_DATA_TIMESTAMP_KEY, null); 86 this.host = getProperty(properties, BUILD_DATA_HOST_KEY, UNKNOWN_VALUE); 87 this.user = getProperty(properties, BUILD_DATA_USER_KEY, UNKNOWN_VALUE); 88 this.changeset = getProperty(properties, BUILD_DATA_CHANGESET_KEY, UNKNOWN_VALUE); 89 this.changeTag = getProperty(properties, BUILD_DATA_CHANGE_TAG_KEY, null); 90 this.branch = getProperty(properties, BUILD_DATA_BRANCH_KEY, UNKNOWN_VALUE); 91 92 Date realTimestamp = null; 93 if (timestampString != null) { 94 try { 95 realTimestamp = DATE_FORMAT.parse(timestampString); 96 } catch (ParseException pe) { 97 logger.warn(bundleHelper.format("invalid.timestamp", new Object [] { timestampString })); 98 } 99 } 100 101 this.timestamp = realTimestamp; 102 } 103 104 private String getProperty(Properties properties, String name, String defaultValue) { 105 String out = properties.getProperty(BUILD_DATA_ROOT_KEY + name); 106 if (StringUtils.isBlank(out)) out = defaultValue; 107 return out; 108 } 109 110 private static ProductInfo thisProductInfo = null; 111 112 public static synchronized ProductInfo getThisProductInfo() { 113 if (thisProductInfo == null) { 114 InputStream in = ProductInfo.class.getResourceAsStream(BUILD_DATA_RESOURCE_NAME); 115 thisProductInfo = new ProductInfo(in, "resource '" + BUILD_DATA_RESOURCE_NAME + "'"); 116 } 117 118 return thisProductInfo; 119 } 120 121 public boolean isDevMode() { 122 return this.version.endsWith(UNKNOWN_VALUE); 123 } 124 125 public String moniker() { 126 return this.moniker; 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) return UNKNOWN_VALUE; 143 else return DATE_FORMAT.format(this.timestamp); 144 } 145 146 public String buildHost() { 147 return this.host; 148 } 149 150 public String buildUser() { 151 return this.user; 152 } 153 154 public String buildChangeset() { 155 return this.changeset; 156 } 157 158 public String buildChangeTag() { 159 return this.changeTag; 160 } 161 162 public String buildBranch() { 163 return this.branch; 164 } 165 166 public String copyright() { 167 return bundleHelper.getString("copyright"); 168 } 169 170 public String toShortString() { 171 return this.moniker + " version " + buildVersion(); 172 } 173 174 public String toLongString() { 175 return toShortString() + ", as of " + buildTimestampAsString() + " (Revision " + buildChangeset() 176 + (buildChangeTag() != null ? " (" + buildChangeTag() + ")" : "") + " by " + buildUser() + "@" + buildHost() 177 + " from " + buildBranch() + ")"; 178 } 179 180 public String toString() { 181 return toShortString(); 182 } 183 184 public static void main(String [] args) throws Exception { 185 Options options = new Options(); 186 options.addOption("v", "verbose", false, bundleHelper.getString("option.verbose")); 187 options.addOption("h", "help", false, bundleHelper.getString("option.help")); 188 189 CommandLineParser parser = new GnuParser(); 190 CommandLine cli = parser.parse(options, args); 191 192 if (cli.hasOption("h")) { 193 new HelpFormatter().printHelp("java " + ProductInfo.class.getName(), options); 194 } 195 196 if (cli.hasOption("v")) { 197 System.out.println(getThisProductInfo().toLongString()); 198 } else { 199 System.out.println(getThisProductInfo().toShortString()); 200 } 201 } 202 } 203 | Popular Tags |