| 1 | package tmcsim.common; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.IOException; |
|---|
| 5 | import java.util.Properties; |
|---|
| 6 | import java.util.Scanner; |
|---|
| 7 | import java.util.logging.Level; |
|---|
| 8 | import java.util.logging.Logger; |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * RevisionNumber represents the current Subversion revision. The Ant build |
|---|
| 12 | * script generates this value and saves it in a plain text file in the 'config' |
|---|
| 13 | * folder. (See target '-pre-compile'). The file should be included into the |
|---|
| 14 | * distributed zip file by the build script so it is available at run time to be |
|---|
| 15 | * displayed in help info. |
|---|
| 16 | * |
|---|
| 17 | * @author jdalbey |
|---|
| 18 | */ |
|---|
| 19 | public final class RevisionNumber |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | private static Logger logger = Logger.getLogger("tmcsim.common"); |
|---|
| 23 | // Read the subversion revision info and return it |
|---|
| 24 | /** |
|---|
| 25 | * Read the version number from the application properties. The file |
|---|
| 26 | * 'application.properties' is generated by build.xml. |
|---|
| 27 | * |
|---|
| 28 | * @return a version string obtained from application.properties file, or |
|---|
| 29 | * "Version: unknown" if an IOerror prevents us from reading the file. |
|---|
| 30 | */ |
|---|
| 31 | public static String getAppVersion() |
|---|
| 32 | { |
|---|
| 33 | String propfilename = "/tmcsim/application.properties"; |
|---|
| 34 | String propKey = "Application.revision"; |
|---|
| 35 | String version = "unknown"; |
|---|
| 36 | try |
|---|
| 37 | { |
|---|
| 38 | Properties props = new Properties(); |
|---|
| 39 | props.load(RevisionNumber.class.getResourceAsStream(propfilename)); |
|---|
| 40 | version = (String) props.get(propKey); |
|---|
| 41 | } catch (IOException ex) |
|---|
| 42 | { |
|---|
| 43 | Logger.getLogger("tmcsim/common").log(Level.SEVERE, |
|---|
| 44 | "getAppVersion(): IOError reading " + propfilename); |
|---|
| 45 | } catch (NullPointerException npe) |
|---|
| 46 | { |
|---|
| 47 | Logger.getLogger("tmcsim/common").log(Level.SEVERE, |
|---|
| 48 | "getAppVersion().load: Missing file: " + propfilename); |
|---|
| 49 | } |
|---|
| 50 | return "revision: " + version; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Return the version number as a string. The revision number is generated |
|---|
| 55 | * by the svnversion command. |
|---|
| 56 | * |
|---|
| 57 | * @see http://svnbook.red-bean.com/en/1.7/svn.ref.svnversion.re.html |
|---|
| 58 | * Currently this string it return untouched. Future enhancements might make |
|---|
| 59 | * it pretty. |
|---|
| 60 | * @return the revision number as a string |
|---|
| 61 | */ |
|---|
| 62 | public static String getString() |
|---|
| 63 | { |
|---|
| 64 | String revision = "unknown"; // default |
|---|
| 65 | String kConfigFile = "config/svn-version.txt"; |
|---|
| 66 | // InputStream in = RevisionNumber.class.getResourceAsStream("config/svn-version.txt"); |
|---|
| 67 | try |
|---|
| 68 | { |
|---|
| 69 | Scanner scan = new Scanner(new File(kConfigFile)); |
|---|
| 70 | revision = scan.next(); |
|---|
| 71 | } catch (Exception e) |
|---|
| 72 | { |
|---|
| 73 | logger.logp(Level.SEVERE, |
|---|
| 74 | "RevisionNumber", "getString()", |
|---|
| 75 | "Exception trying to read from file: " |
|---|
| 76 | + kConfigFile, e); |
|---|
| 77 | } |
|---|
| 78 | return revision; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|