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