001: package com.bostechcorp.cbesb.console.server;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.security.CodeSource;
008: import java.security.ProtectionDomain;
009: import java.util.Enumeration;
010: import java.util.Locale;
011: import java.util.Properties;
012: import java.util.ResourceBundle;
013:
014: import com.bostechcorp.cbesb.common.version.Version;
015: import com.bostechcorp.cbesb.console.common.VersionInfo;
016:
017: public class VersionUtil {
018:
019: private static final long serialVersionUID = 6708783295325671457L;
020:
021: private static final String RESOURCE_0 = "META-INF/version.properties";
022:
023: private int majorVersion = 0;
024: private int minorVersion = 0;
025: private int patchVersion = 0;
026: private String buildType = "";
027: private String buildTimestamp = "";
028:
029: // always to get the version information from the classpath
030: public static VersionUtil getInstance() {
031: VersionUtil instance;
032: try {
033: instance = new VersionUtil();
034:
035: InputStream in = null;
036: in = VersionUtil.class.getClassLoader()
037: .getResourceAsStream(RESOURCE_0);
038: Properties properties = new Properties();
039: properties.load(in);
040:
041: in.close();
042: String value = properties.getProperty("Version.Major");
043: if (value != null && !value.equals("")) {
044: instance.majorVersion = Integer.parseInt(value);
045: }
046: value = properties.getProperty("Version.Minor");
047: if (value != null && !value.equals("")) {
048: instance.minorVersion = Integer.parseInt(value);
049: }
050: value = properties.getProperty("Version.Patch");
051: if (value != null && !value.equals("")) {
052: instance.patchVersion = Integer.parseInt(value);
053: }
054: value = properties.getProperty("Version.BuildType");
055: if (value != null && !value.equals("")) {
056: instance.buildType = value;
057: }
058: value = properties.getProperty("Version.BuildDate");
059: if (value != null && !value.equals("")) {
060: instance.buildTimestamp = value;
061: }
062: } catch (MalformedURLException e) {
063: instance = null;
064: } catch (IOException e) {
065: instance = null;
066: } catch (Exception e) {
067: instance = null;
068: }
069:
070: return instance;
071: }
072:
073: /**
074: * @return the buildTimestamp
075: */
076: public String getBuildTimestamp() {
077: return buildTimestamp;
078: }
079:
080: /**
081: * @return the buildType
082: */
083: public String getBuildType() {
084: return buildType;
085: }
086:
087: /**
088: * @return the majorVersion
089: */
090: public int getMajorVersion() {
091: return majorVersion;
092: }
093:
094: /**
095: * @return the minorVersion
096: */
097: public int getMinorVersion() {
098: return minorVersion;
099: }
100:
101: /**
102: * @return the patchVersion
103: */
104: public int getPatchVersion() {
105: return patchVersion;
106: }
107:
108: /**
109: * Returns the version number in a string format like
110: * majorVersion.minorVersion.patchVersion
111: * If a buildType is defined, then it is appended to the end:
112: * majorVersion.minorVersion.patchVersion_buildType
113: *
114: * Examples:
115: * 1.0.0
116: * 1.1.0_BETA1
117: */
118: public String toString() {
119: String versionStr = majorVersion + "." + minorVersion + "."
120: + patchVersion;
121:
122: if (buildType != null && !buildType.equals("")) {
123: versionStr = versionStr + "_" + buildType;
124: }
125: return versionStr;
126: }
127:
128: public static VersionInfo getVersionInfo() {
129:
130: VersionInfo ver = new VersionInfo();
131:
132: VersionUtil v = VersionUtil.getInstance();
133: if (v != null) {
134: ver.setBuildTime(v.getBuildTimestamp());
135: ver.setVersion(v.getMajorVersion() + "."
136: + v.getMinorVersion() + "." + v.getPatchVersion()
137: + "_" + v.getBuildType());
138: }
139:
140: return ver;
141: }
142: }
|