From: <ma...@rh...> - 2009-08-04 14:48:26
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg DL { border : 1px #006 solid; background-color : #369; padding : 6px; color : #fff; } #msg DT { float : left; width : 6em; font-weight : bold; } #msg DL, #msg DT, #msg UL, #msg LI { font-family : arial,helvetica,sans-serif; font-size : 10pt; } h3 { font-family : arial,helvetica,sans-serif; font-size : 10pt; font-weight : bold; } #msg PRE { overflow : auto; white-space : normal; background-color : #ffc; border : 1px #fc0 solid; padding : 6px; } #msg UL, PRE, .diff { overflow : auto; } #patch h4 { font-family : arial,helvetica,sans-serif; font-size : 10pt; } #patch h4 { padding: 8px; background : #369; color : #fff; margin : 0; } #patch .propset h4, #patch .binary h4 {margin: 0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {background:#eeeeee;padding: 0 0 10px 0;} #patch .propset .diff, #patch .binary .diff {padding: 10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch .add {background:#ddffdd;} #patch .rem {background:#ffdddd;} #patch .lines, .info {color:#888888;background:#ffffff;} .diff { width : 100%; } #msg DL { border : 1px #006 solid; background-color : #369; padding : 6px; color : #fff; } #msg DT { float : left; width : 6em; font-weight : bold; } #msg DL, #msg DT, #msg UL, #msg LI { font-family : arial,helvetica,sans-serif; font-size : 10pt; } h3 { font-family : arial,helvetica,sans-serif; font-size : 10pt; font-weight : bold; } #msg PRE { overflow : auto; white-space : normal; background-color : #ffc; border : 1px #fc0 solid; padding : 6px; } #msg UL, PRE, .diff { overflow : auto; } #patch h4 { font-family : arial,helvetica,sans-serif; font-size : 10pt; } #patch h4 { padding: 8px; background : #369; color : #fff; margin : 0; } #patch .propset h4, #patch .binary h4 {margin: 0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {background:#eeeeee;padding: 0 0 10px 0;} #patch .propset .diff, #patch .binary .diff {padding: 10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch .add {background:#ddffdd;} #patch .rem {background:#ffdddd;} #patch .lines, .info {color:#888888;background:#ffffff;} .diff { width : 100%; } --></style> <title>[rhq-project.org rhq] [4721] [RHS-2290] cli dumps short version info on startup...</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>4721</dd> <dt>Author</dt> <dd>mazz</dd> <dt>Date</dt> <dd>2009-08-04 09:48:16 -0500 (Tue, 04 Aug 2009)</dd> </dl> <h3>Log Message</h3> <pre>[RHS-2290] cli dumps short version info on startup... --version provides build info. ClientMain hasn't a log variable... it needs one so we can log the verison. i didn't do that in this checkin</pre> <h3>Modified Paths</h3> <ul> <li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientClientMainjava">rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/ClientMain.java</a></li> </ul> <h3>Added Paths</h3> <ul> <li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientVersionjava">rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/Version.java</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientClientMainjava"></a> <div class="modfile"><h4>Modified: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/ClientMain.java (4720 => 4721)</h4> <pre class="diff"> <span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/ClientMain.java 2009-08-04 14:11:27 UTC (rev 4720) +++ rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/ClientMain.java 2009-08-04 14:48:16 UTC (rev 4721) </span><span class="lines">@@ -35,7 +35,6 @@ </span><span class="cx"> import java.util.HashMap; import java.util.List; import java.util.Map; </span><span class="rem">-import java.util.Arrays; </span><span class="cx"> import jline.ArgumentCompletor; import jline.Completor; </span><span class="lines">@@ -47,7 +46,6 @@ </span><span class="cx"> import org.rhq.core.domain.auth.Subject; import org.rhq.enterprise.client.commands.ClientCommand; import org.rhq.enterprise.client.commands.ScriptCommand; </span><span class="rem">-import org.apache.commons.lang.ArrayUtils; </span><span class="cx"> /** * @author Greg Hinkle </span><span class="lines">@@ -107,6 +105,8 @@ </span><span class="cx"> // process startup arguments main.processArguments(args); </span><span class="add">+ main.outputWriter.println(Version.getProductNameAndVersion()); + </span><span class="cx"> if (main.interactiveMode) { // begin client access loop main.inputLoop(); </span><span class="lines">@@ -236,9 +236,9 @@ </span><span class="cx"> try { recorder.record(cmd); </span><span class="rem">- } catch (IOException e) { - e.printStackTrace(); - } </span><span class="add">+ } catch (IOException e) { + e.printStackTrace(); + } </span><span class="cx"> try { // parse the command into separate arguments and execute </span><span class="lines">@@ -392,9 +392,8 @@ </span><span class="cx"> } void processArguments(String[] args) throws IllegalArgumentException, IOException { </span><span class="rem">- String sopts = "-:hu:p:Ps:t:c:f:"; - LongOpt[] lopts = { - new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'), </span><span class="add">+ String sopts = "-:hu:p:Ps:t:c:f:v"; + LongOpt[] lopts = { new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'), </span><span class="cx"> new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'), new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'p'), new LongOpt("prompt", LongOpt.OPTIONAL_ARGUMENT, null, 'P'), </span><span class="lines">@@ -402,8 +401,8 @@ </span><span class="cx"> new LongOpt("port", LongOpt.REQUIRED_ARGUMENT, null, 't'), new LongOpt("command", LongOpt.REQUIRED_ARGUMENT, null, 'c'), new LongOpt("file", LongOpt.NO_ARGUMENT, null, 'f'), </span><span class="rem">- new LongOpt("args-style", LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), -2) - }; </span><span class="add">+ new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v'), + new LongOpt("args-style", LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), -2) }; </span><span class="cx"> Getopt getopt = new Getopt("Cli", args, sopts, lopts, false); int code; </span><span class="lines">@@ -439,7 +438,7 @@ </span><span class="cx"> break; } case 'P': { </span><span class="rem">- this.pass = this.consoleReader.readLine("password: ", (char)0); </span><span class="add">+ this.pass = this.consoleReader.readLine("password: ", (char) 0); </span><span class="cx"> break; } case 'c': { </span><span class="lines">@@ -452,7 +451,12 @@ </span><span class="cx"> command = createExecArgs(args); break; } </span><span class="add">+ case 'v': { + String versionString = Version.getProductNameAndVersionBuildInfo(); + outputWriter.println(versionString); + break; </span><span class="cx"> } </span><span class="add">+ } </span><span class="cx"> } if (user != null && pass != null) { </span></pre></div> <a id="rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientVersionjava"></a> <div class="addfile"><h4>Added: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/Version.java (0 => 4721)</h4> <pre class="diff"> <span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/Version.java (rev 0) +++ rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/Version.java 2009-08-04 14:48:16 UTC (rev 4721) </span><span class="lines">@@ -0,0 +1,226 @@ </span><span class="add">+/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package org.rhq.enterprise.client; + +import java.io.File; +import java.net.URL; +import java.util.Map; +import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +/** + * Utility that can be used to determine the version of the software. + * + * @author John Mazzitelli + */ +public class Version { + /** + * Version property whose value is the product name. + */ + public static final String PROP_PRODUCT_NAME = "Implementation-Title"; + + /** + * Version property whose value is the product version. + */ + public static final String PROP_PRODUCT_VERSION = "Implementation-Version"; + + /** + * Version property whose value is the source code revision number used to make the build. + */ + public static final String PROP_BUILD_NUMBER = "Build-Number"; + + /** + * Version property whose value is the date when this version of the product was built. + */ + public static final String PROP_BUILD_DATE = "Build-Time"; + + /** + * Version property whose value is the vendor of the JDK that built this version of the product. + */ + public static final String PROP_BUILD_JDK_VENDOR = "Java-Vendor"; + + /** + * Version property whose value is the version of the JDK that built this version of the product. + */ + public static final String PROP_BUILD_JDK_VERSION = "Java-Version"; + + /** + * Version property whose value identifies the build machine's operating system. + */ + public static final String PROP_BUILD_OS_NAME = "Os-Name"; + + /** + * Version property whose value is the build machine's operating system version. + */ + public static final String PROP_BUILD_OS_VERSION = "Os-Version"; + + /** + * Caches the version properties so we don't have to keep reading the file. + * These properties should never change during the lifetime of the agent, so + * we can cache these forever in memory. + */ + private static Properties propertiesCache = null; + + /** + * A main method that can be used to determine the version information from a command line. + * + * @param args the version properties to print to stdout; if no arguments are given then all version properties are + * printed + */ + public static void main(String[] args) { + System.out.println("=========="); + + if (args.length == 0) { + System.out.println(getVersionPropertiesAsString()); + } else { + Properties props = getVersionProperties(); + for (int i = 0; i < args.length; i++) { + String key = args[i]; + String value = props.getProperty(key); + + if (value == null) { + value = "<unknown>"; + } + + System.out.println(key + "=" + value); + } + } + + System.out.println("=========="); + } + + /** + * Returns the product name and its version string. + * + * @return "productName Version" + */ + public static String getProductNameAndVersion() { + Properties props = getVersionProperties(); + String name = props.getProperty(PROP_PRODUCT_NAME); + String version = props.getProperty(PROP_PRODUCT_VERSION); + return name + ' ' + version; + } + + /** + * Returns the product name and its version string, with more build details about the version. + * + * @return "productName Version" + */ + public static String getProductNameAndVersionBuildInfo() { + Properties props = getVersionProperties(); + String name = props.getProperty(PROP_PRODUCT_NAME); + String version = props.getProperty(PROP_PRODUCT_VERSION); + String buildNum = props.getProperty(PROP_BUILD_NUMBER); + String buildDate = props.getProperty(PROP_BUILD_DATE); + return name + ' ' + version + " (" + buildNum + ") " + buildDate; + } + + /** + * Returns just the product name. + * + * @return product name + */ + public static String getProductName() { + Properties props = getVersionProperties(); + String name = props.getProperty(PROP_PRODUCT_NAME); + return name; + } + + /** + * Returns just the product version. + * + * @return product version + */ + public static String getProductVersion() { + Properties props = getVersionProperties(); + String version = props.getProperty(PROP_PRODUCT_VERSION); + return version; + } + + /** + * Returns just the product build date. + * + * @return product build date + */ + public static String getBuildDate() { + Properties props = getVersionProperties(); + String build_date = props.getProperty(PROP_BUILD_DATE); + return build_date; + } + + /** + * Returns just the product build number. + * + * @return product build number + */ + public static String getBuildNumber() { + Properties props = getVersionProperties(); + String build_num = props.getProperty(PROP_BUILD_NUMBER); + return build_num; + } + + /** + * Returns a set of properties that can be used to identify this version of the product. + * + * @return properties identifying this version + * + * @throws RuntimeException if there is no version info found in the current thread's class loader + */ + public static Properties getVersionProperties() { + if (propertiesCache == null) { + Manifest manifest; + try { + URL jarUrl = Version.class.getProtectionDomain().getCodeSource().getLocation(); + JarFile jarFile = new JarFile(new File(jarUrl.toURI())); + manifest = jarFile.getManifest(); + } catch (Exception e) { + return new Properties(); + } + + Attributes mainAttributes = manifest.getMainAttributes(); + Properties newProps = new Properties(); + for (Map.Entry<Object, Object> entry : mainAttributes.entrySet()) { + String name = entry.getKey().toString(); + String value = entry.getValue().toString(); + newProps.setProperty(name, value); + } + propertiesCache = newProps; + } + + Properties retProps = new Properties(); + retProps.putAll(propertiesCache); + return retProps; + } + + /** + * Returns the version properties in a single string with all properties on a single line separated with a newline. + * + * @return the version properties in one big string + */ + public static String getVersionPropertiesAsString() { + Properties props = getVersionProperties(); + StringBuilder str = new StringBuilder(); + for (Map.Entry<Object, Object> entry : props.entrySet()) { + str.append(entry.getKey().toString() + '=' + entry.getValue().toString() + '\n'); + } + return str.toString(); + } +} </span><span class="cx">\ No newline at end of file Property changes on: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/Version.java ___________________________________________________________________ </span></pre></div> <a id="svnmimetype"></a> <div class="addfile"><h4>Added: svn:mime-type ( => )</h4> <pre class="diff"> <span class="info">Added: svn:keywords + Date Author Id Revision HeadURL </span></pre></div> <a id="svneolstyle"></a> <div class="addfile"><h4>Added: svn:eol-style ( => )</h4> <pre class="diff"> <span class="info"> </span></pre> </div> </div> </body> </html> |