source: josm/trunk/src/org/openstreetmap/josm/data/Version.java@ 10063

Last change on this file since 10063 was 9407, checked in by Don-vip, 8 years ago

findbugs - fix warning OBL_UNSATISFIED_OBLIGATION: "Version.init() may fail to clean up java.io.InputStream" by using try-with-resources

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.Map.Entry;
9import java.util.Properties;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.tools.LanguageInfo;
13
14/**
15 * Provides basic information about the currently used JOSM build.
16 *
17 */
18public class Version {
19 /** constant to indicate that the current build isn't assigned a JOSM version number */
20 public static final int JOSM_UNKNOWN_VERSION = 0;
21
22 /** the unique instance */
23 private static Version instance;
24
25 /**
26 * Replies the unique instance of the version information
27 *
28 * @return the unique instance of the version information
29 */
30 public static synchronized Version getInstance() {
31 if (instance == null) {
32 instance = new Version();
33 instance.init();
34 }
35 return instance;
36 }
37
38 private int version;
39 private String releaseDescription;
40 private String time;
41 private String buildName;
42 private boolean isLocalBuild;
43
44 /**
45 * Initializes the version infos from the revision resource file
46 *
47 * @param revisionInfo the revision info from a revision resource file as InputStream
48 */
49 protected void initFromRevisionInfo(InputStream revisionInfo) {
50 if (revisionInfo == null) {
51 this.releaseDescription = tr("UNKNOWN");
52 this.version = JOSM_UNKNOWN_VERSION;
53 this.time = null;
54 return;
55 }
56
57 Properties properties = new Properties();
58 try {
59 properties.load(revisionInfo);
60 } catch (IOException e) {
61 Main.warn(tr("Error reading revision info from revision file: {0}", e.getMessage()));
62 }
63 String value = properties.getProperty("Revision");
64 if (value != null) {
65 value = value.trim();
66 try {
67 version = Integer.parseInt(value);
68 } catch (NumberFormatException e) {
69 version = 0;
70 Main.warn(tr("Unexpected JOSM version number in revision file, value is ''{0}''", value));
71 }
72 } else {
73 version = JOSM_UNKNOWN_VERSION;
74 }
75
76 // the last changed data
77 //
78 time = properties.getProperty("Last Changed Date");
79 if (time == null) {
80 time = properties.getProperty("Build-Date");
81 }
82
83 // is this a local build ?
84 //
85 isLocalBuild = false;
86 value = properties.getProperty("Is-Local-Build");
87 if (value != null && "true".equalsIgnoreCase(value.trim())) {
88 isLocalBuild = true;
89 }
90
91 // is this a specific build ?
92 //
93 buildName = null;
94 value = properties.getProperty("Build-Name");
95 if (value != null && !value.trim().isEmpty()) {
96 buildName = value.trim();
97 }
98
99 // the revision info
100 //
101 StringBuilder sb = new StringBuilder();
102 for (Entry<Object, Object> property: properties.entrySet()) {
103 sb.append(property.getKey()).append(':').append(property.getValue()).append('\n');
104 }
105 releaseDescription = sb.toString();
106 }
107
108 /**
109 * Initializes version info
110 */
111 public void init() {
112 try (InputStream stream = Main.class.getResourceAsStream("/REVISION")) {
113 if (stream == null) {
114 Main.warn(tr("The revision file ''/REVISION'' is missing."));
115 version = 0;
116 releaseDescription = "";
117 return;
118 }
119 initFromRevisionInfo(stream);
120 } catch (IOException e) {
121 Main.warn(e);
122 }
123 }
124
125 /**
126 * Replies the version string. Either the SVN revision "1234" (as string) or the
127 * the I18n equivalent of "UNKNOWN".
128 *
129 * @return the JOSM version
130 */
131 public String getVersionString() {
132 return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
133 }
134
135 /**
136 * Replies a text with the release attributes
137 *
138 * @return a text with the release attributes
139 */
140 public String getReleaseAttributes() {
141 return releaseDescription;
142 }
143
144 /**
145 * Replies the build date as string
146 *
147 * @return the build date as string
148 */
149 public String getTime() {
150 return time;
151 }
152
153 /**
154 * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
155 * @return the JOSM version
156 */
157 public int getVersion() {
158 return version;
159 }
160
161 /**
162 * Replies true if this is a local build, i.e. an inofficial development build.
163 *
164 * @return true if this is a local build, i.e. an inofficial development build.
165 */
166 public boolean isLocalBuild() {
167 return isLocalBuild;
168 }
169
170 /**
171 * Returns the User-Agent string
172 * @return The User-Agent
173 */
174 public String getAgentString() {
175 return getAgentString(true);
176 }
177
178 /**
179 * Returns the User-Agent string, with or without OS details
180 * @param includeOsDetails Append Operating System details at the end of the User-Agent
181 * @return The User-Agent
182 * @since 5956
183 */
184 public String getAgentString(boolean includeOsDetails) {
185 int v = getVersion();
186 String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
187 if (buildName != null) {
188 s += ' ' + buildName;
189 }
190 if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
191 s += " SVN";
192 }
193 String result = "JOSM/1.5 ("+ s+' '+LanguageInfo.getJOSMLocaleCode()+")";
194 if (includeOsDetails && Main.platform != null) {
195 result += ' ' + Main.platform.getOSDescription();
196 }
197 return result;
198 }
199
200 /**
201 * Returns the full User-Agent string
202 * @return The User-Agent
203 * @since 5868
204 */
205 public String getFullAgentString() {
206 return getAgentString() + " Java/"+System.getProperty("java.version");
207 }
208}
Note: See TracBrowser for help on using the repository browser.