GEODE-1809: Refactor GemFireVersion Clean up dead / broken / unused code and reduce the visible API methods. Store the version info as a Map to allow it to be extended more easily. Move scm info into utilities.gradle.
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/93985f96 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/93985f96 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/93985f96 Branch: refs/heads/feature/GEODE-1817 Commit: 93985f96fe5eca16ad301aa09f1719bc213b6034 Parents: 98531a1 Author: Anthony Baker <aba...@apache.org> Authored: Tue Aug 23 14:32:16 2016 -0700 Committer: Anthony Baker <aba...@apache.org> Committed: Thu Aug 25 10:00:52 2016 -0700 ---------------------------------------------------------------------- build.gradle | 1 - geode-core/build.gradle | 33 - .../distributed/internal/StartupMessage.java | 14 - .../gemfire/internal/GemFireVersion.java | 726 +++++-------------- .../gemstone/gemfire/internal/SystemAdmin.java | 10 +- .../internal/GemFireVersionJUnitTest.java | 75 +- .../gemfire/internal/VersionJUnitTest.java | 50 ++ gradle/utilities.gradle | 43 ++ 8 files changed, 275 insertions(+), 677 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/build.gradle ---------------------------------------------------------------------- diff --git a/build.gradle b/build.gradle index eac68d0..e112eb7 100755 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,6 @@ buildscript { dependencies { classpath "gradle.plugin.org.nosphere.apache:creadur-rat-gradle:0.2.0" - classpath "org.ajoberstar:gradle-git:1.3.2" classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1' classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.0.1' } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/build.gradle ---------------------------------------------------------------------- diff --git a/geode-core/build.gradle b/geode-core/build.gradle index 1d5ad4a..ea1fce2 100755 --- a/geode-core/build.gradle +++ b/geode-core/build.gradle @@ -133,39 +133,6 @@ sourceSets { } } -ext.readScmInfo = { - try { - def git = org.ajoberstar.grgit.Grgit.open(currentDir: projectDir) - try { - return [ - 'Source-Repository': git.branch.getCurrent().name, - 'Source-Revision' : git.head().id, - 'Source-Date' : git.head().getDate().format('yyyy-MM-dd HH:mm:ss Z') - ] as Properties - } finally { - git.close() - } - } catch (IllegalArgumentException e) { - // if we're building from the source distribution, we don't have git so - // use cached info written during the assembly process - def buildInfo = file "$rootDir/.buildinfo" - if (buildInfo.exists()) { - def props = new Properties() - new FileInputStream(buildInfo).withStream { fis -> - props.load(fis) - } - return props - } - - logger.warn( '***** Unable to find Git workspace. Using default version information *****' ) - return [ - 'Source-Repository': 'UNKNOWN', - 'Source-Revision' : 'UNKNOWN', - 'Source-Date' : new Date().format('yyyy-MM-dd HH:mm:ss Z') - ] as Properties - } -} - // Creates the version properties file and writes it to the classes dir task createVersionPropertiesFile { def propertiesFile = file(generatedResources + "/com/gemstone/gemfire/internal/GemFireVersion.properties"); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/StartupMessage.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/StartupMessage.java b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/StartupMessage.java index 82f5c88..7b6d853 100644 --- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/StartupMessage.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/StartupMessage.java @@ -178,20 +178,6 @@ public final class StartupMessage extends HighPriorityDistributionMessage implem String myVersion = GemFireVersion.getGemFireVersion(); String theirVersion = this.version; - int myMajorVersion = GemFireVersion.getMajorVersion(myVersion); - int theirMajorVersion = GemFireVersion.getMajorVersion(theirVersion); - int myMinorVersion = GemFireVersion.getMinorVersion(myVersion); - int theirMinorVersion = GemFireVersion.getMinorVersion(theirVersion); - // fix for bug 43608 - if (myMajorVersion != theirMajorVersion || myMinorVersion != theirMinorVersion) { - // now don't reject at this level since it will be handled at - // JGroups/Connection handshake level - /* - rejectionMessage = - LocalizedStrings.StartupMessage_REJECTED_NEW_SYSTEM_NODE_0_WITH_PRODUCT_VERSION_1_BECAUSE_THE_EXISTING_DISTRIBUTED_SYSTEM_NODE_2_HAS_A_PRODUCT_VERSION_OF_3 - .toLocalizedString(new Object[] {getSender(), this.version, dm.getId(), GemFireVersion.getGemFireVersion()}); - */ - } if (dm.getTransport().isMcastEnabled() != isMcastEnabled) { rejectionMessage = LocalizedStrings.StartupMessage_REJECTED_NEW_SYSTEM_NODE_0_BECAUSE_ISMCASTENABLED_1_DOES_NOT_MATCH_THE_DISTRIBUTED_SYSTEM_IT_IS_ATTEMPTING_TO_JOIN http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/src/main/java/com/gemstone/gemfire/internal/GemFireVersion.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/GemFireVersion.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/GemFireVersion.java index baf03b1..9bd3d4f 100644 --- a/geode-core/src/main/java/com/gemstone/gemfire/internal/GemFireVersion.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/GemFireVersion.java @@ -16,335 +16,101 @@ */ package com.gemstone.gemfire.internal; +import java.io.File; +import java.io.InputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URL; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.StringTokenizer; + +import com.gemstone.gemfire.InternalGemFireError; import com.gemstone.gemfire.SystemFailure; import com.gemstone.gemfire.internal.i18n.LocalizedStrings; -import java.io.*; -import java.net.*; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - /** * This class provides build and version information about GemFire. * It gathers this information from the resource property file * for this class. */ public class GemFireVersion { - private static String RESOURCE_NAME = "GemFireVersion.properties"; - - /** The singleton instance */ - private static GemFireVersion instance; - - /** Constant for the GemFire version Resource Property entry */ - private static final String PRODUCT_NAME = "Product-Name"; - - /** Constant for the GemFire version Resource Property entry */ - private static final String GEMFIRE_VERSION = "Product-Version"; - - /** Constant for the source code date Resource Property entry */ - private static final String SOURCE_DATE = "Source-Date"; - - /** Constant for the source code revision Resource Property entry */ - private static final String SOURCE_REVISION = "Source-Revision"; - - /** Constant for the source code repository Resource Property entry */ - private static final String SOURCE_REPOSITORY = "Source-Repository"; - - /** Constant for the build date Resource Property entry */ - private static final String BUILD_DATE = "Build-Date"; - - /** Constant for the build id Resource Property entry */ - private static final String BUILD_ID = "Build-Id"; - - /** Constant for the build Java version Resource Property entry */ - private static final String BUILD_PLATFORM = "Build-Platform"; - - /** Constant for the build Java version Resource Property entry */ - private static final String BUILD_JAVA_VERSION = "Build-Java-Version"; - - //////////////////// Instance Fields //////////////////// - - /** Error message to display instead of the version information */ - private String error = null; - - /** The name of this product */ - private String productName; - - /** This product's version */ - private String gemfireVersion; - - /** The version of GemFire native code library */ - private String nativeVersion; - - /** The date that the source code for GemFire was last updated */ - private String sourceDate; - /** The revision of the source code used to build GemFire */ - private String sourceRevision; - - /** The repository in which the source code for GemFire resides */ - private String sourceRepository; - - /** The date on which GemFire was built */ - private String buildDate; - - /** The ID of the GemFire build */ - private String buildId; - - /** The platform on which GemFire was built */ - private String buildPlatform; - - /** The version of Java that was used to build GemFire */ - private String buildJavaVersion; + /** The singleton instance */ + private static VersionDescription description; - //////////////////// Static Methods //////////////////// + private GemFireVersion() { + } - /** - * Returns (or creates) the singleton instance of this class - */ - private static GemFireVersion getInstance() { - if (instance == null) { - instance = new GemFireVersion(); + private static synchronized VersionDescription getDescription() { + if (description == null) { + String name = GemFireVersion.class.getPackage().getName() + .replace('.', '/') + "/" + RESOURCE_NAME; + description = new VersionDescription(name); } - - return instance; + return description; + } + + public static void main(String[] args) { + System.out.println(asString()); } - /** - * Returns the name of this product - */ public static String getProductName() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.productName; - } + return getDescription().getProperty(VersionDescription.PRODUCT_NAME); } - /** - * Returns the version of GemFire being used - */ public static String getGemFireVersion() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.gemfireVersion; - } - } - - private static String stripSpaces(String s) { - StringBuffer result = new StringBuffer(s); - while (result.charAt(0) == ' ') { - result.deleteCharAt(0); - } - while (result.charAt(result.length()-1) == ' ') { - result.deleteCharAt(result.length()-1); - } - return result.toString(); - } - - /** - * Returns the version of GemFire native code library being used - */ - public static String getNativeCodeVersion() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.nativeVersion; - } - } - public static String getJavaCodeVersion() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - StringBuffer result = new StringBuffer(80); - result.append(GemFireVersion.getGemFireVersion()) - .append(' ') - .append(GemFireVersion.getBuildId()) - .append(' ') - .append(GemFireVersion.getBuildDate()) - .append(" javac ") - .append(GemFireVersion.getBuildJavaVersion()); - return result.toString(); - } + return getDescription().getProperty(VersionDescription.GEMFIRE_VERSION); } - /** - * Returns the date of the source code from which GemFire was built - */ public static String getSourceDate() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.sourceDate; - } + return getDescription().getProperty(VersionDescription.SOURCE_DATE); } - /** - * Returns the revision of the source code on which GemFire was - * built. - * - * @since GemFire 4.0 - */ - public static String getSourceRevision() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.sourceRevision; - } - } - - /** - * Returns the source code repository from which GemFire was built. - * - * @since GemFire 4.0 - */ public static String getSourceRepository() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.sourceRepository; - } + return getDescription().getProperty(VersionDescription.SOURCE_REPOSITORY); } - /** - * Returns the date on which GemFire was built - */ - public static String getBuildDate() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.buildDate; - } + public static String getSourceRevision() { + return getDescription().getProperty(VersionDescription.SOURCE_REVISION); } - /** - * Returns the id of the GemFire build - */ public static String getBuildId() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; + return getDescription().getProperty(VersionDescription.BUILD_ID); + } - } else { - return v.buildId; - } + public static String getBuildDate() { + return getDescription().getProperty(VersionDescription.BUILD_DATE); } - /** - * Returns the platform on which GemFire was built - */ public static String getBuildPlatform() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.buildPlatform; - } + return getDescription().getProperty(VersionDescription.BUILD_PLATFORM); } - /** - * Returns the version of Java used to build GemFire - */ public static String getBuildJavaVersion() { - GemFireVersion v = getInstance(); - if (v.error != null) { - return v.error; - - } else { - return v.buildJavaVersion; - } + return getDescription().getProperty(VersionDescription.BUILD_JAVA_VERSION); } - //////////////////// Constructors //////////////////// + public static String getGemFireJarFileName() { + return "geode-core-" + GemFireVersion.getGemFireVersion() +".jar"; + } - /** - * Private constructor that read the resource properties - * and extracts interesting pieces of information from it - */ - private GemFireVersion() { - String name = - GemFireVersion.class.getPackage().getName().replace('.', '/'); - name = name + "/" + RESOURCE_NAME; - - InputStream is = ClassPathLoader.getLatest().getResourceAsStream(getClass(), name); - if (is == null) { - error = LocalizedStrings.GemFireVersion_COULD_NOT_FIND_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_0.toLocalizedString(RESOURCE_NAME); - return; - } - Properties props = new Properties(); - try { - props.load(is); - } catch (Exception ex) { - error = LocalizedStrings.GemFireVersion_COULD_NOT_READ_PROPERTIES_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_0_BECAUSE_1.toLocalizedString(new Object[] {RESOURCE_NAME, ex}); - return; - } + public static void print(PrintWriter pw) { + getDescription().print(pw); + } + + public static void print(PrintStream ps) { + print(new PrintWriter(ps, true)); + } - this.nativeVersion = SmHelper.getNativeVersion(); - this.productName = props.getProperty(PRODUCT_NAME); - if (this.productName == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {PRODUCT_NAME, RESOURCE_NAME}); - return; - } - this.gemfireVersion = props.getProperty(GEMFIRE_VERSION); - if (this.gemfireVersion == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {GEMFIRE_VERSION, RESOURCE_NAME}); - return; - } - this.sourceDate = props.getProperty(SOURCE_DATE); - if (this.sourceDate == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_DATE, RESOURCE_NAME}); - return; - } - this.sourceRevision = props.getProperty(SOURCE_REVISION); - if (this.sourceRevision == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_REVISION, RESOURCE_NAME}); - return; - } - this.sourceRepository = props.getProperty(SOURCE_REPOSITORY); - if (this.sourceRepository == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_REPOSITORY, RESOURCE_NAME}); - return; - } - this.buildDate = props.getProperty(BUILD_DATE); - if (this.buildDate == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_DATE, RESOURCE_NAME}); - return; - } - this.buildId = props.getProperty(BUILD_ID); - if (this.buildId == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_ID, RESOURCE_NAME}); - return; - } - this.buildPlatform = props.getProperty(BUILD_PLATFORM); - if (this.buildPlatform == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_PLATFORM, RESOURCE_NAME}); - return; - } - this.buildJavaVersion = props.getProperty(BUILD_JAVA_VERSION); - if (this.buildJavaVersion == null) { - error = LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_JAVA_VERSION, RESOURCE_NAME}); - return; - } + public static String asString() { + StringWriter sw = new StringWriter(256); + PrintWriter pw = new PrintWriter(sw); + print(pw); + pw.flush(); + return sw.toString(); } /** Public method that returns the URL of the gemfire jar file */ @@ -385,293 +151,141 @@ public class GemFireVersion { return csLoc; } - public static String getGemFireJarFileName() { - return "geode-core-" + GemFireVersion.getGemFireVersion() +".jar"; - } + static class VersionDescription { + /** Constant for the GemFire version Resource Property entry */ + static final String PRODUCT_NAME = "Product-Name"; - private final static String VER_FILE_NAME = "GemFireVersion.properties"; - private final static String JAR_VER_NAME = "gemfire-core-jar"; - - public static void createVersionFile() { - String jarVersion = stripSpaces(GemFireVersion.getJavaCodeVersion()); - File libDir = SystemAdmin.findGemFireLibDir(); - if (libDir == null) { - throw new RuntimeException(LocalizedStrings.GemFireVersion_COULD_NOT_DETERMINE_PRODUCT_LIB_DIRECTORY.toLocalizedString()); - } - File versionPropFile = new File(libDir, VER_FILE_NAME); - Properties props = new Properties(); - props.setProperty(JAR_VER_NAME, jarVersion); - try { - FileOutputStream out = new FileOutputStream(versionPropFile); - props.store(out, "Expected versions for this product build as of"); - out.close(); - } catch (IOException ex) { - throw new RuntimeException(LocalizedStrings.GemFireVersion_COULD_NOT_WRITE_0_BECAUSE_1.toLocalizedString(new Object[] {versionPropFile, ex.toString()})); - } - System.out.println("Created \"" + versionPropFile + "\""); - } - /** - * Encodes all available version information into a string and then - * returns that string. - */ - public static String asString() { - StringWriter sw = new StringWriter(256); - PrintWriter pw = new PrintWriter(sw); - print(pw); - pw.flush(); - return sw.toString(); - } - /** - * Prints all available version information (excluding source code - * information) to the given <code>PrintWriter</code> in a - * standard format. - */ - public static void print(PrintWriter pw) { - print(pw, true); + /** Constant for the GemFire version Resource Property entry */ + static final String GEMFIRE_VERSION = "Product-Version"; + + /** Constant for the source code date Resource Property entry */ + static final String SOURCE_DATE = "Source-Date"; + + /** Constant for the source code revision Resource Property entry */ + static final String SOURCE_REVISION = "Source-Revision"; + + /** Constant for the source code repository Resource Property entry */ + static final String SOURCE_REPOSITORY = "Source-Repository"; + + /** Constant for the build date Resource Property entry */ + static final String BUILD_DATE = "Build-Date"; + + /** Constant for the build id Resource Property entry */ + static final String BUILD_ID = "Build-Id"; + + /** Constant for the build Java version Resource Property entry */ + static final String BUILD_PLATFORM = "Build-Platform"; + + /** Constant for the build Java version Resource Property entry */ + static final String BUILD_JAVA_VERSION = "Build-Java-Version"; + + /** the version properties */ + private final Properties description; + + /** Error message to display instead of the version information */ + private final String error; + + public VersionDescription(String name) { + InputStream is = ClassPathLoader.getLatest().getResourceAsStream(getClass(), name); + if (is == null) { + error = LocalizedStrings.GemFireVersion_COULD_NOT_FIND_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_0.toLocalizedString(RESOURCE_NAME); + description = null; + return; + } + + description = new Properties(); + try { + description.load(is); + } catch (Exception ex) { + error = LocalizedStrings.GemFireVersion_COULD_NOT_READ_PROPERTIES_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_0_BECAUSE_1.toLocalizedString(new Object[] {RESOURCE_NAME, ex}); + return; + } + error = validate(); } - /** - * Prints all available version information to the given - * <code>PrintWriter</code> in a standard format. - * - * @param printSourceInfo - * Should information about the source code be printed? - */ - public static void print(PrintWriter pw, - boolean printSourceInfo) { - String jarVersion = stripSpaces(GemFireVersion.getJavaCodeVersion()); - pw.println("Java version: " + jarVersion); - String libVersion = stripSpaces(GemFireVersion.getNativeCodeVersion()); - pw.println("Native version: " + libVersion); - File libDir = SystemAdmin.findGemFireLibDir(); - if (libDir != null) { - File versionPropFile = new File(libDir, VER_FILE_NAME); - if (versionPropFile.exists()) { - try { - Properties props = new Properties(); - FileInputStream inStream = new FileInputStream(versionPropFile); - try { - props.load(inStream); - } - finally { - inStream.close(); - } - String expectedJarVersion = props.getProperty(JAR_VER_NAME); - if (expectedJarVersion != null) { - if (!expectedJarVersion.equals(jarVersion)) { - pw.println(LocalizedStrings.GemFireVersion_WARNING_EXPECTED_JAVA_VERSION_0.toLocalizedString(expectedJarVersion)); - } - } - } catch (IOException ex) { - pw.println(LocalizedStrings.GemFireVersion_WARNING_FAILED_TO_READ_0_BECAUSE_1.toLocalizedString(new Object[] {versionPropFile, ex})); - } -// } else { -// pw.println(LocalizedStrings.GemFireVersion_WARNING_COULD_NOT_FIND_0.toLocalizedString(versionPropFile)); - } -// } else { -// pw.println(LocalizedStrings.GemFireVersion_WARNING_COULD_NOT_DETERMINE_THE_PRODUCTS_LIB_DIRECTORY.toLocalizedString()); - } - - if (printSourceInfo) { - String sourceRevision = GemFireVersion.getSourceRevision(); - pw.println("Source revision: " + sourceRevision); - - String sourceRepository = - GemFireVersion.getSourceRepository(); - pw.println("Source repository: " + sourceRepository); - } - - InetAddress host = null; - try { - host = SocketCreator.getLocalHost(); - } - catch (VirtualMachineError err) { - SystemFailure.initiateFailure(err); - // If this ever returns, rethrow the error. We're poisoned - // now, so don't let this thread continue. - throw err; - } - catch (Throwable t) { - // Whenever you catch Error or Throwable, you must also - // catch VirtualMachineError (see above). However, there is - // _still_ a possibility that you are dealing with a cascading - // error condition, so you also need to check to see if the JVM - // is still usable: - SystemFailure.checkFailure(); - } - int cpuCount = Runtime.getRuntime().availableProcessors(); - pw.println(LocalizedStrings.GemFireVersion_RUNNING_ON_0.toLocalizedString( - host - + ", " + cpuCount + " cpu(s)" - + ", " + System.getProperty("os.arch") - + " " + System.getProperty("os.name") - + " " + System.getProperty("os.version") - )); + public String getProperty(String key) { + if (error != null) { + return error; + } + return description.getProperty(key); } - - /** - * Prints all available version information (excluding information - * about the source code) to the given <code>PrintStream</code> in - * a standard format. - */ - public static void print(PrintStream ps) { - print(ps, true); + + public String getNativeCodeVersion() { + return SmHelper.getNativeVersion(); } + + private void print(PrintWriter pw) { + for (Entry<?,?> props : description.entrySet()) { + pw.println(props.getKey() + ": " + props.getValue()); + } - /** - * Prints all available version information to the given - * <code>PrintStream</code> in a standard format. - * - * @param printSourceInfo - * Should information about the source code be printed? - */ - public static void print(PrintStream ps, - boolean printSourceInfo) { - PrintWriter pw = new PrintWriter(ps); - print(pw, printSourceInfo); - pw.flush(); + // not stored in the description map + pw.println("Native version: " + getNativeCodeVersion()); + printHostInfo(pw); + } + + private void printHostInfo(PrintWriter pw) + throws InternalGemFireError, Error, VirtualMachineError { + try { + StringBuffer sb = new StringBuffer(SocketCreator.getLocalHost().toString()) + .append(", ") + .append(Runtime.getRuntime().availableProcessors()).append(" cpu(s), ") + .append(System.getProperty("os.arch")).append(' ') + .append(System.getProperty("os.name")).append(' ') + .append(System.getProperty("os.version")).append(' '); + pw.println(LocalizedStrings.GemFireVersion_RUNNING_ON_0.toLocalizedString(sb.toString())); + } catch (VirtualMachineError err) { + SystemFailure.initiateFailure(err); + // If this ever returns, rethrow the error. We're poisoned + // now, so don't let this thread continue. + throw err; + } catch (Throwable t) { + // Whenever you catch Error or Throwable, you must also + // catch VirtualMachineError (see above). However, there is + // _still_ a possibility that you are dealing with a cascading + // error condition, so you also need to check to see if the JVM + // is still usable: + SystemFailure.checkFailure(); + } } - - /////////////////////// Main Program //////////////////// -// private static final PrintStream out = System.out; -// private static final PrintStream err = System.err; + private String validate() { + if (getProperty(PRODUCT_NAME) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {PRODUCT_NAME, RESOURCE_NAME}); + } - /** - * Populates the gemfireVersion.properties file - */ - public static void main(String[] args) { - print(System.out); - } + if (getProperty(GEMFIRE_VERSION) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {GEMFIRE_VERSION, RESOURCE_NAME}); + } - private static final Pattern MAJOR_MINOR = Pattern.compile("(\\d+)\\.(\\d*)(.*)"); - private static final Pattern RELEASE = Pattern.compile("\\.(\\d*)(.*)"); - private static final Pattern MAJOR_MINOR_RELEASE = Pattern.compile("(\\d+)\\.(\\d*)\\.(\\d*)(.*)"); - - public static int getMajorVersion(String v) { - int majorVersion = 0; - Matcher m = MAJOR_MINOR.matcher(v); - if (m.matches()) { - String digits = m.group(1); - if (digits != null && digits.length() > 0) { - majorVersion = Integer.decode(digits).intValue(); + if (getProperty(SOURCE_DATE) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_DATE, RESOURCE_NAME}); } - } - return majorVersion; - } - public static int getMinorVersion(String v) { - int minorVersion = 0; - Matcher m = MAJOR_MINOR.matcher(v); - if (m.matches()) { - String digits = m.group(2); - if (digits != null && digits.length() > 0) { - minorVersion = Integer.decode(digits).intValue(); + if (getProperty(SOURCE_REVISION) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_REVISION, RESOURCE_NAME}); } - } - return minorVersion; - } - public static int getRelease(String v) { - int release = 0; - Matcher m = MAJOR_MINOR.matcher(v); - if (m.matches()) { - String others = m.group(3); - Matcher r = RELEASE.matcher(others); - if (r.matches()) { - String digits = r.group(1); - if (digits != null && digits.length() > 0) { - try { - release = Integer.decode(digits).intValue(); - } catch (NumberFormatException e) { - release = 0; - } - } + if (getProperty(SOURCE_REPOSITORY) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {SOURCE_REPOSITORY, RESOURCE_NAME}); } - } - return release; - } - - public static int getBuild(String v) { - int build = 0; - Matcher m = MAJOR_MINOR_RELEASE.matcher(v); - if (m.matches()) { - String buildStr = m.group(4); - Matcher b = RELEASE.matcher(buildStr); - if (b.matches()) { - String digits = b.group(1); - if (digits != null && digits.length() > 0) { - try { - build = Integer.decode(digits).intValue(); - } catch (NumberFormatException e) { - build = 0; - } - } + + if (getProperty(BUILD_DATE) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_DATE, RESOURCE_NAME}); } - } - return build; - } - - /** - * Compare version's sections major, minor, release one by one - * - * @return >0: v1 is newer than v2 - * 0: same - * <0: v1 is older than v2 - * @deprecated please use the {@link Version} class to read the version - * of the local member and compare versions for backwards compatibility - * purposes. see also {@link SerializationVersions} for how to make backwards - * compatible messages. - */ - public static int compareVersions(String v1, String v2) { - return compareVersions(v1, v2, true); - } - - /* - * Compare version's sections major, minor, release one by one - * - * @param v1 the first version - * @param v2 the second version - * @param includeBuild whether to also compare the build numbers - * - * @return: >0: v1 is newer than v2 - * 0: same - * <0: v1 is older than v2 - */ - public static int compareVersions(String v1, String v2, boolean includeBuild) { - int major1, minor1, release1, build1; - int major2, minor2, release2, build2; - - if (v1 == null && v2 != null) return -1; - if (v1 != null && v2 == null) return 1; - if (v1 == null && v2 == null) return 0; - - major1 = getMajorVersion(v1); - major2 = getMajorVersion(v2); - - minor1 = getMinorVersion(v1); - minor2 = getMinorVersion(v2); - - release1 = getRelease(v1); - release2 = getRelease(v2); - if (major1 > major2) return 1; - if (major1 < major2) return -1; - - if (minor1 > minor2) return 1; - if (minor1 < minor2) return -1; + if (getProperty(BUILD_ID) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_ID, RESOURCE_NAME}); + } - if (release1 > release2) return 1; - if (release1 < release2) return -1; - - if (includeBuild) { - build1 = getBuild(v1); - build2 = getBuild(v2); - - if (build1 > build2) return 1; - if (build1 < build2) return -1; - } + if (getProperty(BUILD_PLATFORM) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_PLATFORM, RESOURCE_NAME}); + } - return 0; + if (getProperty(BUILD_JAVA_VERSION) == null) { + return LocalizedStrings.GemFireVersion_MISSING_PROPERTY_0_FROM_RESOURCE_COM_GEMSTONE_GEMFIRE_INTERNAL_1.toLocalizedString(new Object[] {BUILD_JAVA_VERSION, RESOURCE_NAME}); + } + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/src/main/java/com/gemstone/gemfire/internal/SystemAdmin.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/SystemAdmin.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/SystemAdmin.java index e52950f..1757d5c 100644 --- a/geode-core/src/main/java/com/gemstone/gemfire/internal/SystemAdmin.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/SystemAdmin.java @@ -2080,16 +2080,8 @@ public class SystemAdmin { LocalizedStrings.SystemAdmin_GEMFIRE_PRODUCT_DIRECTORY_0 .toLocalizedString(getProductDir())); - if (cmdLine.size() == 1 && "CREATE".equals(cmdLine.get(0))) { - GemFireVersion.createVersionFile(); + GemFireVersion.print(System.out); - } else if (cmdLine.size() == 1 && - "FULL".equalsIgnoreCase(String.valueOf(cmdLine.get(0)))) { - GemFireVersion.print(System.out, true); - - } else { - GemFireVersion.print(System.out, false); - } } else if (cmd.equalsIgnoreCase("help")) { if (cmdLine.size() > 1) { System.err.println( LocalizedStrings.SystemAdmin_ERROR_UNEXPECTED_COMMAND_LINE_ARGUMENTS_0.toLocalizedString(join(cmdLine))); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/src/test/java/com/gemstone/gemfire/internal/GemFireVersionJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/GemFireVersionJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/GemFireVersionJUnitTest.java index fc4ab1b..8369d1f 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/internal/GemFireVersionJUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/GemFireVersionJUnitTest.java @@ -16,10 +16,7 @@ */ package com.gemstone.gemfire.internal; -import static org.junit.Assert.*; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -40,67 +37,17 @@ public class GemFireVersionJUnitTest { */ @Test public void testPrintInfo() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - GemFireVersion.print(ps); - final String versionOutput = baos.toString(); + final String versionOutput = GemFireVersion.asString(); System.out.println(versionOutput); - assertTrue(versionOutput.contains("Java version:")); - assertTrue(versionOutput.contains("Native version:")); - assertTrue(versionOutput.contains("Source revision:")); - assertTrue(versionOutput.contains("Source repository:")); - assertTrue(versionOutput.contains("Running on:")); - } - - @Test - public void testMajorMinorVersions() { - assertEquals(1, GemFireVersion.getMajorVersion("1.0.3")); - assertEquals(33, GemFireVersion.getMajorVersion("33.0.3")); - assertEquals(7, GemFireVersion.getMinorVersion("1.7.3")); - assertEquals(79, GemFireVersion.getMinorVersion("1.79.3")); - assertEquals(0, GemFireVersion.getMinorVersion("1.RC1")); - assertEquals(5, GemFireVersion.getMinorVersion("1.5Beta2")); - - assertEquals(13, GemFireVersion.getBuild("7.0.2.13")); - assertEquals(0, GemFireVersion.getBuild("1.7.3")); - assertEquals(0, GemFireVersion.getBuild("1.79.3")); - assertEquals(0, GemFireVersion.getBuild("1.RC1")); - assertEquals(0, GemFireVersion.getBuild("1.5Beta2")); - - assertTrue("7.0 should be < 7.0.2.14", GemFireVersion.compareVersions("7.0", "7.0.2.14", true) < 0); - assertTrue("7.0.0 should be < 7.0.2.14", GemFireVersion.compareVersions("7.0.0", "7.0.2.14", true) < 0); - assertTrue("7.0.2 should be < 7.0.2.14", GemFireVersion.compareVersions("7.0.2", "7.0.2.14", true) < 0); - assertTrue("7.0.3 should be > 7.0.2.14", GemFireVersion.compareVersions("7.0.3", "7.0.2.14", true) > 0); - assertTrue("7.0.1.15 should be < 7.0.2.14", GemFireVersion.compareVersions("7.0.1.15", "7.0.2.14", true) < 0); - assertTrue("7.0.2.13 should be < 7.0.2.14", GemFireVersion.compareVersions("7.0.2.13", "7.0.2.14", true) < 0); - assertTrue("7.0.2.14 should be > 7.0.2.13", GemFireVersion.compareVersions("7.0.2.14", "7.0.2.13", true) > 0); - assertTrue("7.0.2.14 should be == 7.0.2.14", GemFireVersion.compareVersions("7.0.2.14", "7.0.2.14", true) == 0); - assertTrue("7.0.2.12 should be < 7.0.2.13", GemFireVersion.compareVersions("7.0.2.12", "7.0.2.13", true) < 0); - assertTrue("7.0.2.13 should be == 7.0.2.13", GemFireVersion.compareVersions("7.0.2.13", "7.0.2.13", true) == 0); - assertTrue("7.0.2.15 should be > 7.0.2.13", GemFireVersion.compareVersions("7.0.2.14", "7.0.2.13", true) > 0); - } - - @Test - public void testVersionClass() throws Exception { - compare(Version.GFE_662, Version.GFE_66); - compare(Version.GFE_6622, Version.GFE_662); - compare(Version.GFE_71, Version.GFE_70); - compare(Version.GFE_80, Version.GFE_70); - compare(Version.GFE_80, Version.GFE_71); - compare(Version.GFE_81, Version.GFE_70); - compare(Version.GFE_81, Version.GFE_71); - compare(Version.GFE_81, Version.GFE_80); - } - - private void compare(Version later, Version earlier) { - assertTrue(later.compareTo(earlier) > 0); - assertTrue(later.equals(later)); - assertTrue(later.compareTo(later) == 0); - assertTrue(earlier.compareTo(later) < 0); - - assertTrue(later.compareTo(earlier.ordinal()) > 0); - assertTrue(later.compareTo(later.ordinal()) == 0); - assertTrue(earlier.compareTo(later.ordinal()) < 0); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.PRODUCT_NAME)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.GEMFIRE_VERSION)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.SOURCE_DATE)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.SOURCE_REVISION)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.SOURCE_REPOSITORY)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.BUILD_DATE)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.BUILD_ID)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.BUILD_PLATFORM)); + assertTrue(versionOutput.contains(GemFireVersion.VersionDescription.BUILD_JAVA_VERSION)); } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/geode-core/src/test/java/com/gemstone/gemfire/internal/VersionJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/VersionJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/VersionJUnitTest.java new file mode 100644 index 0000000..dcc7049 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/VersionJUnitTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class VersionJUnitTest { + @Test + public void testVersionClass() throws Exception { + compare(Version.GFE_662, Version.GFE_66); + compare(Version.GFE_6622, Version.GFE_662); + compare(Version.GFE_71, Version.GFE_70); + compare(Version.GFE_80, Version.GFE_70); + compare(Version.GFE_80, Version.GFE_71); + compare(Version.GFE_81, Version.GFE_70); + compare(Version.GFE_81, Version.GFE_71); + compare(Version.GFE_81, Version.GFE_80); + } + + private void compare(Version later, Version earlier) { + assertTrue(later.compareTo(earlier) > 0); + assertTrue(later.equals(later)); + assertTrue(later.compareTo(later) == 0); + assertTrue(earlier.compareTo(later) < 0); + + assertTrue(later.compareTo(earlier.ordinal()) > 0); + assertTrue(later.compareTo(later.ordinal()) == 0); + assertTrue(earlier.compareTo(later.ordinal()) < 0); + } +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/93985f96/gradle/utilities.gradle ---------------------------------------------------------------------- diff --git a/gradle/utilities.gradle b/gradle/utilities.gradle index 5f597f6..d4d616f 100644 --- a/gradle/utilities.gradle +++ b/gradle/utilities.gradle @@ -14,6 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +buildscript { + repositories { + maven { url "https://plugins.gradle.org/m2/" } + } + + dependencies { + classpath "org.ajoberstar:gradle-git:1.3.2" + } +} + allprojects { //These methods will be added to each individual project. //Use this for methods that access project variables (like project.name) @@ -41,5 +51,38 @@ allprojects { signArchives.enabled = false } } + + readScmInfo = { + try { + def git = org.ajoberstar.grgit.Grgit.open(currentDir: projectDir) + try { + return [ + 'Source-Repository': git.branch.getCurrent().name, + 'Source-Revision' : git.head().id, + 'Source-Date' : git.head().getDate().format('yyyy-MM-dd HH:mm:ss Z') + ] as Properties + } finally { + git.close() + } + } catch (IllegalArgumentException e) { + // if we're building from the source distribution, we don't have git so + // use cached info written during the assembly process + def buildInfo = file "$rootDir/.buildinfo" + if (buildInfo.exists()) { + def props = new Properties() + new FileInputStream(buildInfo).withStream { fis -> + props.load(fis) + } + return props + } + + logger.warn( '***** Unable to find Git workspace. Using default version information *****' ) + return [ + 'Source-Repository': 'UNKNOWN', + 'Source-Revision' : 'UNKNOWN', + 'Source-Date' : new Date().format('yyyy-MM-dd HH:mm:ss Z') + ] as Properties + } + } } }