KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > Version


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import java.util.Collections JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Provides access to JBoss version (and build) properties.
33  *
34  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 57108 $
37  */

38 public final class Version
39 {
40    public final static String JavaDoc VERSION_MAJOR = "version.major";
41    public final static String JavaDoc VERSION_MINOR = "version.minor";
42    public final static String JavaDoc VERSION_REVISION = "version.revision";
43    public final static String JavaDoc VERSION_TAG = "version.tag";
44    public final static String JavaDoc VERSION_NAME = "version.name";
45    public final static String JavaDoc VERSION_CVSTAG = "version.cvstag";
46
47    public final static String JavaDoc BUILD_NUMBER = "build.number";
48    public final static String JavaDoc BUILD_ID = "build.id";
49    public final static String JavaDoc BUILD_DATE = "build.day";
50    public final static String JavaDoc BUILD_JVM_VERSION = "java.vm.version";
51    public final static String JavaDoc BUILD_JVM_VENDOR = "java.vendor";
52    public final static String JavaDoc BUILD_OS = "os.name";
53    public final static String JavaDoc BUILD_OS_ARCH = "os.arch";
54    public final static String JavaDoc BUILD_OS_VERSION = "os.version";
55
56    /**
57     * The single instance.
58     */

59    private static Version instance = null;
60
61    /**
62     * The version properties.
63     */

64    private Properties JavaDoc props;
65
66    /**
67     * Do not allow direct public construction.
68     */

69    private Version()
70    {
71       props = loadProperties();
72    }
73
74    /**
75     * Get the single <tt>Version</tt> instance.
76     *
77     * @return The single <tt>Version</tt> instance.
78     */

79    public static Version getInstance()
80    {
81       if (instance == null)
82       {
83          instance = new Version();
84       }
85       return instance;
86    }
87
88    /**
89     * Returns an unmodifiable map of version properties.
90     *
91     * @return An unmodifiable map of version properties.
92     */

93    public Map JavaDoc getProperties()
94    {
95       return Collections.unmodifiableMap(props);
96    }
97
98    /**
99     * Returns the value for the given property name.
100     *
101     * @param name - The name of the property.
102     * @return The property value or null if the property is not set.
103     */

104    public String JavaDoc getProperty(final String JavaDoc name)
105    {
106       return props.getProperty(name);
107    }
108
109    /**
110     * Returns the major number of the version.
111     *
112     * @return The major number of the version.
113     */

114    public int getMajor()
115    {
116       return getIntProperty(VERSION_MAJOR);
117    }
118
119    /**
120     * Returns the minor number of the version.
121     *
122     * @return The minor number of the version.
123     */

124    public int getMinor()
125    {
126       return getIntProperty(VERSION_MINOR);
127    }
128
129    /**
130     * Returns the revision number of the version.
131     *
132     * @return The revision number of the version.
133     */

134    public int getRevision()
135    {
136       return getIntProperty(VERSION_REVISION);
137    }
138
139    /**
140     * Returns the tag of the version.
141     *
142     * @return The tag of the version.
143     */

144    public String JavaDoc getTag()
145    {
146       return props.getProperty(VERSION_TAG);
147    }
148    /**
149     * Returns the CVS tag of the version.
150     *
151     * @return The CVS tag of the version.
152     */

153    public String JavaDoc getCvsTag()
154    {
155       return props.getProperty(VERSION_CVSTAG);
156    }
157
158    /**
159     * Returns the name number of the version.
160     *
161     * @return The name of the version.
162     */

163    public String JavaDoc getName()
164    {
165       return props.getProperty(VERSION_NAME);
166    }
167
168    /**
169     * Returns the build identifier for this version.
170     *
171     * @return The build identifier for this version.
172     */

173    public String JavaDoc getBuildID()
174    {
175       return props.getProperty(BUILD_ID);
176    }
177
178    /**
179     * Returns the build number for this version.
180     *
181     * @return The build number for this version.
182     */

183    public String JavaDoc getBuildNumber()
184    {
185       return props.getProperty(BUILD_NUMBER);
186    }
187
188    /**
189     * Returns the build date for this version.
190     *
191     * @return The build date for this version.
192     */

193    public String JavaDoc getBuildDate()
194    {
195       return props.getProperty(BUILD_DATE);
196    }
197
198    /** Returns the BUILD_JVM_VERSION (BUILD_JVM_VENDOR) which should look like:
199     * 1.4.2_05-b04 (Sun Microsystems Inc.)
200     * @return
201     */

202    public String JavaDoc getBuildJVM()
203    {
204       String JavaDoc vm = props.getProperty(BUILD_JVM_VERSION);
205       String JavaDoc vendor = props.getProperty(BUILD_JVM_VENDOR);
206       return vm + '(' + vendor + ')';
207    }
208
209    /** Returns the BUILD_OS (BUILD_OS_ARCH,BUILD_OS_VERSION) which should look
210     * like:
211     * Windows XP (x86,5.1)
212     * Linux (i386,2.4.21-4.ELsmp)
213     * @return
214     */

215    public String JavaDoc getBuildOS()
216    {
217       String JavaDoc os = props.getProperty(BUILD_OS);
218       String JavaDoc arch = props.getProperty(BUILD_OS_ARCH);
219       String JavaDoc version = props.getProperty(BUILD_OS_VERSION);
220       return os + '(' + arch +',' + version + ')';
221    }
222
223    /**
224     * Returns the version information as a string.
225     *
226     * @return Basic information as a string.
227     */

228    public String JavaDoc toString()
229    {
230       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
231       
232       buff.append(getMajor()).append(".");
233       buff.append(getMinor()).append(".");
234       buff.append(getRevision()).append(getTag());
235       buff.append("(build: CVSTag=");
236       buff.append(getCvsTag());
237       buff.append(" date=");
238       buff.append(getBuildID());
239       buff.append(")");
240       return buff.toString();
241    }
242
243    /**
244     * Returns a property value as an int.
245     *
246     * @param name - The name of the property.
247     * @return The property value, or -1 if there was a problem converting
248     * it to an int.
249     */

250    private int getIntProperty(final String JavaDoc name)
251    {
252       try
253       {
254          return Integer.valueOf(props.getProperty(name)).intValue();
255       }
256       catch (Exception JavaDoc e)
257       {
258          return -1;
259       }
260    }
261
262    /**
263     * Load the version properties from a resource.
264     */

265    private Properties JavaDoc loadProperties()
266    {
267       props = new Properties JavaDoc();
268
269       try
270       {
271          InputStream JavaDoc in =
272             Version.class.getResourceAsStream("/org/jboss/version.properties");
273          if( in != null )
274          {
275             props.load(in);
276             in.close();
277          }
278       }
279       catch (IOException JavaDoc e)
280       {
281          throw new Error JavaDoc("Missing version.properties");
282       }
283
284       return props;
285    }
286 }
287
Popular Tags