001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: Version.java 11245 2008-01-07 23:28:52Z elu $
023: */
024:
025: package com.bostechcorp.cbesb.common.version;
026:
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.Serializable;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.security.CodeSource;
033: import java.security.ProtectionDomain;
034: import java.util.Properties;
035:
036: public class Version implements Serializable {
037:
038: /**
039: *
040: */
041: private static final long serialVersionUID = 6708783295325671457L;
042:
043: private static final String RESOURCE = "/META-INF/version.properties";
044:
045: private int majorVersion = 0;
046: private int minorVersion = 0;
047: private int patchVersion = 0;
048: private String buildType = "";
049: private String buildTimestamp = "";
050:
051: private Version() {
052: }
053:
054: // always to get the version information from the classpath
055: public static Version getInstance() {
056: Version instance;
057: try {
058: instance = new Version();
059:
060: // //Load settings from version.properties file
061: // //Contained in the same jar file as the object.
062: // ProtectionDomain pDomain = versionedClass.getProtectionDomain();
063: // CodeSource cSource = pDomain.getCodeSource();
064: // URL loc = cSource.getLocation();
065: InputStream in = null;
066: // URL newURL = new URL("jar:" + loc.toString() + "!" + RESOURCE);
067: in = (new String()).getClass()
068: .getResourceAsStream(RESOURCE);
069: // in = newURL.openStream();
070: Properties properties = new Properties();
071: properties.load(in);
072: in.close();
073: String value = properties.getProperty("Version.Major");
074: if (value != null && !value.equals("")) {
075: instance.majorVersion = Integer.parseInt(value);
076: }
077: value = properties.getProperty("Version.Minor");
078: if (value != null && !value.equals("")) {
079: instance.minorVersion = Integer.parseInt(value);
080: }
081: value = properties.getProperty("Version.Patch");
082: if (value != null && !value.equals("")) {
083: instance.patchVersion = Integer.parseInt(value);
084: }
085: value = properties.getProperty("Version.BuildType");
086: if (value != null && !value.equals("")) {
087: instance.buildType = value;
088: }
089: value = properties.getProperty("Version.BuildDate");
090: if (value != null && !value.equals("")) {
091: instance.buildTimestamp = value;
092: }
093: } catch (MalformedURLException e) {
094: instance = null;
095: } catch (IOException e) {
096: instance = null;
097: } catch (Exception e) {
098: instance = null;
099: }
100:
101: return instance;
102: }
103:
104: public static Version getInstance(Class versionedClass) {
105: Version instance;
106: try {
107: instance = new Version();
108: //Load settings from version.properties file
109: //Contained in the same jar file as the object.
110: ProtectionDomain pDomain = versionedClass
111: .getProtectionDomain();
112: CodeSource cSource = pDomain.getCodeSource();
113: URL loc = cSource.getLocation();
114: InputStream in = null;
115: URL newURL = new URL("jar:" + loc.toString() + "!"
116: + RESOURCE);
117: in = newURL.openStream();
118: Properties properties = new Properties();
119: properties.load(in);
120: in.close();
121: String value = properties.getProperty("Version.Major");
122: if (value != null && !value.equals("")) {
123: instance.majorVersion = Integer.parseInt(value);
124: }
125: value = properties.getProperty("Version.Minor");
126: if (value != null && !value.equals("")) {
127: instance.minorVersion = Integer.parseInt(value);
128: }
129: value = properties.getProperty("Version.Patch");
130: if (value != null && !value.equals("")) {
131: instance.patchVersion = Integer.parseInt(value);
132: }
133: value = properties.getProperty("Version.BuildType");
134: if (value != null && !value.equals("")) {
135: instance.buildType = value;
136: }
137: value = properties.getProperty("Version.BuildDate");
138: if (value != null && !value.equals("")) {
139: instance.buildTimestamp = value;
140: }
141: } catch (MalformedURLException e) {
142: instance = null;
143: } catch (IOException e) {
144: instance = null;
145: }
146:
147: return instance;
148: }
149:
150: /**
151: * @return the buildTimestamp
152: */
153: public String getBuildTimestamp() {
154: return buildTimestamp;
155: }
156:
157: /**
158: * @return the buildType
159: */
160: public String getBuildType() {
161: return buildType;
162: }
163:
164: /**
165: * @return the majorVersion
166: */
167: public int getMajorVersion() {
168: return majorVersion;
169: }
170:
171: /**
172: * @return the minorVersion
173: */
174: public int getMinorVersion() {
175: return minorVersion;
176: }
177:
178: /**
179: * @return the patchVersion
180: */
181: public int getPatchVersion() {
182: return patchVersion;
183: }
184:
185: /**
186: * Returns the version number in a string format like
187: * majorVersion.minorVersion.patchVersion
188: * If a buildType is defined, then it is appended to the end:
189: * majorVersion.minorVersion.patchVersion_buildType
190: *
191: * Examples:
192: * 1.0.0
193: * 1.1.0_BETA1
194: */
195: public String toString() {
196: String versionStr = majorVersion + "." + minorVersion + "."
197: + patchVersion;
198:
199: if (buildType != null && !buildType.equals("")) {
200: versionStr = versionStr + "_" + buildType;
201: }
202: return versionStr;
203: }
204: }
|