[2512] | 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;
|
---|
[9019] | 7 | import java.io.InputStream;
|
---|
[2512] | 8 | import java.util.Map.Entry;
|
---|
[11889] | 9 | import java.util.Optional;
|
---|
[9019] | 10 | import java.util.Properties;
|
---|
[2512] | 11 |
|
---|
| 12 | import org.openstreetmap.josm.Main;
|
---|
| 13 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
[12620] | 14 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2512] | 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Provides basic information about the currently used JOSM build.
|
---|
[11889] | 18 | * @since 2358
|
---|
[2512] | 19 | */
|
---|
| 20 | public class Version {
|
---|
[4237] | 21 | /** constant to indicate that the current build isn't assigned a JOSM version number */
|
---|
[6889] | 22 | public static final int JOSM_UNKNOWN_VERSION = 0;
|
---|
[2512] | 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 | */
|
---|
[8126] | 32 | public static synchronized Version getInstance() {
|
---|
[2512] | 33 | if (instance == null) {
|
---|
| 34 | instance = new Version();
|
---|
| 35 | instance.init();
|
---|
| 36 | }
|
---|
| 37 | return instance;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private int version;
|
---|
[4259] | 41 | private String releaseDescription;
|
---|
[2512] | 42 | private String time;
|
---|
[5362] | 43 | private String buildName;
|
---|
[2512] | 44 | private boolean isLocalBuild;
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Initializes the version infos from the revision resource file
|
---|
| 48 | *
|
---|
[9019] | 49 | * @param revisionInfo the revision info from a revision resource file as InputStream
|
---|
[2512] | 50 | */
|
---|
[9019] | 51 | protected void initFromRevisionInfo(InputStream revisionInfo) {
|
---|
[2512] | 52 | if (revisionInfo == null) {
|
---|
[4259] | 53 | this.releaseDescription = tr("UNKNOWN");
|
---|
[2512] | 54 | this.version = JOSM_UNKNOWN_VERSION;
|
---|
| 55 | this.time = null;
|
---|
| 56 | return;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[9019] | 59 | Properties properties = new Properties();
|
---|
| 60 | try {
|
---|
| 61 | properties.load(revisionInfo);
|
---|
| 62 | } catch (IOException e) {
|
---|
[12620] | 63 | Logging.log(Logging.LEVEL_WARN, tr("Error reading revision info from revision file: {0}", e.getMessage()), e);
|
---|
[9019] | 64 | }
|
---|
[11889] | 65 | String value = Optional.ofNullable(properties.getProperty("Revision")).orElse("").trim();
|
---|
| 66 | if (!value.isEmpty()) {
|
---|
[2512] | 67 | try {
|
---|
| 68 | version = Integer.parseInt(value);
|
---|
[8510] | 69 | } catch (NumberFormatException e) {
|
---|
[2512] | 70 | version = 0;
|
---|
[12620] | 71 | Logging.warn(tr("Unexpected JOSM version number in revision file, value is ''{0}''", value));
|
---|
[2512] | 72 | }
|
---|
| 73 | } else {
|
---|
| 74 | version = JOSM_UNKNOWN_VERSION;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | // the last changed data
|
---|
| 78 | //
|
---|
[9019] | 79 | time = properties.getProperty("Last Changed Date");
|
---|
[2512] | 80 | if (time == null) {
|
---|
[9019] | 81 | time = properties.getProperty("Build-Date");
|
---|
[2512] | 82 | }
|
---|
| 83 |
|
---|
| 84 | // is this a local build ?
|
---|
| 85 | //
|
---|
[11889] | 86 | isLocalBuild = "true".equalsIgnoreCase(
|
---|
| 87 | Optional.ofNullable(properties.getProperty("Is-Local-Build")).orElse("").trim());
|
---|
[2512] | 88 |
|
---|
[5362] | 89 | // is this a specific build ?
|
---|
| 90 | //
|
---|
[11889] | 91 | buildName = Optional.ofNullable(properties.getProperty("Build-Name")).orElse("").trim();
|
---|
[5362] | 92 |
|
---|
[2512] | 93 | // the revision info
|
---|
| 94 | //
|
---|
[6822] | 95 | StringBuilder sb = new StringBuilder();
|
---|
[9019] | 96 | for (Entry<Object, Object> property: properties.entrySet()) {
|
---|
[8390] | 97 | sb.append(property.getKey()).append(':').append(property.getValue()).append('\n');
|
---|
[2512] | 98 | }
|
---|
[4259] | 99 | releaseDescription = sb.toString();
|
---|
[2512] | 100 | }
|
---|
| 101 |
|
---|
[5850] | 102 | /**
|
---|
| 103 | * Initializes version info
|
---|
| 104 | */
|
---|
[2512] | 105 | public void init() {
|
---|
[12627] | 106 | try (InputStream stream = Version.class.getResourceAsStream("/REVISION")) {
|
---|
[9407] | 107 | if (stream == null) {
|
---|
[12620] | 108 | Logging.warn(tr("The revision file ''/REVISION'' is missing."));
|
---|
[9407] | 109 | version = 0;
|
---|
| 110 | releaseDescription = "";
|
---|
| 111 | return;
|
---|
| 112 | }
|
---|
| 113 | initFromRevisionInfo(stream);
|
---|
| 114 | } catch (IOException e) {
|
---|
[12620] | 115 | Logging.warn(e);
|
---|
[2512] | 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() {
|
---|
[10378] | 126 | return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
|
---|
[2512] | 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() {
|
---|
[4259] | 135 | return releaseDescription;
|
---|
[2512] | 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 | /**
|
---|
[5266] | 148 | * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
|
---|
[2512] | 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 |
|
---|
[5850] | 164 | /**
|
---|
| 165 | * Returns the User-Agent string
|
---|
| 166 | * @return The User-Agent
|
---|
| 167 | */
|
---|
[2512] | 168 | public String getAgentString() {
|
---|
[5956] | 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) {
|
---|
[2512] | 179 | int v = getVersion();
|
---|
| 180 | String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
|
---|
[11899] | 181 | if (buildName != null && !buildName.isEmpty()) {
|
---|
[8846] | 182 | s += ' ' + buildName;
|
---|
[5362] | 183 | }
|
---|
[2512] | 184 | if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
|
---|
| 185 | s += " SVN";
|
---|
| 186 | }
|
---|
[10300] | 187 | String result = "JOSM/1.5 ("+ s+' '+LanguageInfo.getJOSMLocaleCode()+')';
|
---|
[6421] | 188 | if (includeOsDetails && Main.platform != null) {
|
---|
[8846] | 189 | result += ' ' + Main.platform.getOSDescription();
|
---|
[5956] | 190 | }
|
---|
| 191 | return result;
|
---|
[2512] | 192 | }
|
---|
[5868] | 193 |
|
---|
| 194 | /**
|
---|
| 195 | * Returns the full User-Agent string
|
---|
| 196 | * @return The User-Agent
|
---|
[5881] | 197 | * @since 5868
|
---|
[5868] | 198 | */
|
---|
| 199 | public String getFullAgentString() {
|
---|
| 200 | return getAgentString() + " Java/"+System.getProperty("java.version");
|
---|
| 201 | }
|
---|
[2512] | 202 | }
|
---|