KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > VersionInfo


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * Get the version of SSL Explorer in use. If running in a development
30  * environment this will be retrieved from the build.properties file, otherwise.
31  * the build process should have replaced the static {@link #VERSION} constant
32  * with the real version.
33  * <p>
34  * Also contains a utility class that may be used to by other software
35  * components to represent a version.
36  *
37  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
38  */

39 public class VersionInfo {
40
41     private final static String JavaDoc VERSION = "0.2.15";
42
43     private static Version version;
44     private static boolean developmentVersion;
45
46     static {
47         if (VERSION.startsWith("999.")) {
48             developmentVersion = true;
49             Properties JavaDoc p = new Properties JavaDoc();
50             InputStream JavaDoc in = null;
51             try {
52                 in = new FileInputStream JavaDoc("build.properties");
53                 p.load(in);
54                 version = new Version(p.getProperty("version.major", "999") + "." + p.getProperty("version.minor", "999") + "."
55                                 + p.getProperty("version.build", "999") + p.getProperty("version.tag"));
56             } catch (IOException JavaDoc ioe) {
57                 version = new Version("0.2.15");
58             } finally {
59                 if (in != null) {
60                     try {
61                         in.close();
62                     } catch (IOException JavaDoc ioe) {
63                     }
64                 }
65             }
66         } else {
67             version = new Version(VERSION);
68         }
69     }
70
71     /**
72      * Determine if this is a development version. This is true if the
73      * {@link #VERSION} has not been replaced (i.e. is 0.2.15).
74      *
75      * @return is development version
76      */

77     public static boolean isDevelopmentVersion() {
78         return developmentVersion;
79     }
80
81     /**
82      * Get the current SSL-Explorer version
83      *
84      * @return version
85      */

86     public static Version getVersion() {
87         return version;
88     }
89
90     /**
91      * Represents the version number of a software component such as
92      * SSL-Explorer itself or perhaps an extension.
93      * <p>
94      * The object may be constructed from a dotted version string. An
95      * optional <i>tag</i> element may also be provided by appending an
96      * underscore (_) then the tag.
97      * <p>
98      * For example
99      * <p><code>0.1.14_alpha</code></p> would give a major version of 0,
100      * a minor version of 1, a build version of 14 and a tag of 'alpha'.
101      *
102      * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
103      */

104     public static class Version implements Comparable JavaDoc {
105         int minor;
106         int major;
107         int build;
108         String JavaDoc tag;
109         int tagOffset;
110
111         /**
112          * Constructor
113          *
114          * @param version version string (see class description)
115          */

116         public Version(String JavaDoc version) {
117             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(version, ".");
118             try {
119                 major = Integer.parseInt(t.nextToken());
120                 minor = Integer.parseInt(t.nextToken());
121
122                 if (t.hasMoreTokens()) {
123                     String JavaDoc s = t.nextToken();
124                     int pos = s.indexOf('_');
125                     if (pos > -1) {
126                         build = Integer.parseInt(s.substring(0, pos));
127                         tag = s.substring(pos);
128                     } else {
129                         build = Integer.parseInt(s);
130                         tag = "";
131                     }
132                 } else {
133                     build = 0;
134                     tag = "";
135                 }
136             } catch (Throwable JavaDoc ex) {
137                 major = 999;
138                 minor = 999;
139                 build = 999;
140                 tag = "";
141             }
142             
143             // The tag may have meaning in version comparsions
144
tagOffset = 0;
145             tag = tag.toUpperCase();
146             
147             // Release candidate phase (up to 100 releases with RCx as tag)
148
if(!tag.equals("")) {
149                 if(tag.startsWith("_RC")) {
150                     tagOffset = -100 + Integer.parseInt(tag.substring(3));
151                 }
152                 else {
153                     // Subrelease (tag is a number)
154
try {
155                         tagOffset = Integer.parseInt(tag.substring(1));
156                     }
157                     catch(NumberFormatException JavaDoc nfe) {
158                         // Tag has no meaning to comparison
159
}
160                 }
161             }
162             
163         }
164
165         /**
166          * Constructor.
167          *
168          * @param major major version
169          * @param minor minor version
170          * @param build build version
171          */

172         public Version(int major, int minor, int build) {
173             this.major = major;
174             this.minor = minor;
175             this.build = build;
176         }
177
178         /**
179          * Compare to versions. Version are equal if the major, minor and
180          * build elements are the same. The tag element is not taken into
181          * account.
182          *
183          * @param o other version
184          * @return comparison
185          */

186         public int compareTo(Object JavaDoc o) {
187             return new Integer JavaDoc(hashCode()).compareTo(new Integer JavaDoc(o.hashCode()));
188         }
189         
190         /* (non-Javadoc)
191          * @see java.lang.Object#equals(java.lang.Object)
192          */

193         public boolean equals(Object JavaDoc obj) {
194             return hashCode() == obj.hashCode();
195         }
196
197         /* (non-Javadoc)
198          * @see java.lang.Object#hashCode()
199          */

200         public int hashCode() {
201             return (int)(((long)major * 1000000000l) + ((long)minor * 1000000l) + ((long)build * 1000l) + tagOffset);
202         }
203
204         /* (non-Javadoc)
205          * @see java.lang.Object#toString()
206          */

207         public String JavaDoc toString() {
208             return major + "." + minor + "." + build + tag;
209         }
210
211         /**
212          * Get the major version number.
213          *
214          * @return major version number
215          */

216         public int getMajor() {
217             return major;
218         }
219
220         /**
221          * Get the minor version number.
222          *
223          * @return minor version number
224          */

225         public int getMinor() {
226             return minor;
227         }
228
229         /**
230          * Get the build version number.
231          *
232          * @return minor version number
233          */

234         public int getBuild() {
235             return build;
236         }
237
238         /**
239          * Get the tag.
240          *
241          * @return tag
242          */

243         public String JavaDoc getTag() {
244             return tag;
245         }
246     }
247 }
248
Popular Tags