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