| 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.Optional;
|
|---|
| 9 | import java.util.Properties;
|
|---|
| 10 | import java.util.stream.Collectors;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.tools.LanguageInfo;
|
|---|
| 13 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 14 | import org.openstreetmap.josm.tools.PlatformManager;
|
|---|
| 15 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Provides basic information about the currently used JOSM build.
|
|---|
| 19 | * @since 2358
|
|---|
| 20 | */
|
|---|
| 21 | public class Version {
|
|---|
| 22 | /** constant to indicate that the current build isn't assigned a JOSM version number */
|
|---|
| 23 | public static final int JOSM_UNKNOWN_VERSION = 0;
|
|---|
| 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 | */
|
|---|
| 33 | public static synchronized Version getInstance() {
|
|---|
| 34 | if (instance == null) {
|
|---|
| 35 | instance = new Version();
|
|---|
| 36 | instance.init();
|
|---|
| 37 | }
|
|---|
| 38 | return instance;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | private int version;
|
|---|
| 42 | private String releaseDescription;
|
|---|
| 43 | private String time;
|
|---|
| 44 | private String buildName;
|
|---|
| 45 | private boolean isLocalBuild;
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Initializes the version info from the revision resource file
|
|---|
| 49 | *
|
|---|
| 50 | * @param revisionInfo the revision info from a revision resource file as InputStream
|
|---|
| 51 | */
|
|---|
| 52 | protected void initFromRevisionInfo(InputStream revisionInfo) {
|
|---|
| 53 | if (revisionInfo == null) {
|
|---|
| 54 | this.releaseDescription = tr("UNKNOWN");
|
|---|
| 55 | this.version = JOSM_UNKNOWN_VERSION;
|
|---|
| 56 | this.time = null;
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | Properties properties = new Properties();
|
|---|
| 61 | try {
|
|---|
| 62 | properties.load(revisionInfo);
|
|---|
| 63 | } catch (IOException e) {
|
|---|
| 64 | Logging.log(Logging.LEVEL_WARN, tr("Error reading revision info from revision file: {0}", e.getMessage()), e);
|
|---|
| 65 | }
|
|---|
| 66 | String value = Optional.ofNullable(properties.getProperty("Revision")).orElse("").trim();
|
|---|
| 67 | if (!value.isEmpty()) {
|
|---|
| 68 | try {
|
|---|
| 69 | version = Integer.parseInt(value);
|
|---|
| 70 | } catch (NumberFormatException e) {
|
|---|
| 71 | version = 0;
|
|---|
| 72 | Logging.warn(tr("Unexpected JOSM version number in revision file, value is ''{0}''", value));
|
|---|
| 73 | }
|
|---|
| 74 | } else {
|
|---|
| 75 | version = JOSM_UNKNOWN_VERSION;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | // the last changed data
|
|---|
| 79 | //
|
|---|
| 80 | time = properties.getProperty("Last Changed Date");
|
|---|
| 81 | if (time == null) {
|
|---|
| 82 | time = properties.getProperty("Build-Date");
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // is this a local build ?
|
|---|
| 86 | //
|
|---|
| 87 | isLocalBuild = "true".equalsIgnoreCase(
|
|---|
| 88 | Optional.ofNullable(properties.getProperty("Is-Local-Build")).orElse("").trim());
|
|---|
| 89 |
|
|---|
| 90 | // is this a specific build ?
|
|---|
| 91 | //
|
|---|
| 92 | buildName = Optional.ofNullable(properties.getProperty("Build-Name")).orElse("").trim();
|
|---|
| 93 |
|
|---|
| 94 | // the revision info
|
|---|
| 95 | //
|
|---|
| 96 | releaseDescription = properties.entrySet().stream()
|
|---|
| 97 | .map(property -> property.getKey() + ":" + property.getValue() + "\n")
|
|---|
| 98 | .collect(Collectors.joining());
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Initializes version info
|
|---|
| 103 | */
|
|---|
| 104 | public void init() {
|
|---|
| 105 | try (InputStream stream = Utils.getResourceAsStream(getClass(), "/REVISION")) {
|
|---|
| 106 | if (stream == null) {
|
|---|
| 107 | Logging.warn(tr("The revision file ''/REVISION'' is missing."));
|
|---|
| 108 | version = 0;
|
|---|
| 109 | releaseDescription = "";
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 | initFromRevisionInfo(stream);
|
|---|
| 113 | } catch (IOException e) {
|
|---|
| 114 | Logging.warn(e);
|
|---|
| 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() {
|
|---|
| 125 | return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
|
|---|
| 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() {
|
|---|
| 134 | return releaseDescription;
|
|---|
| 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 | /**
|
|---|
| 147 | * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
|
|---|
| 148 | * @return the JOSM version
|
|---|
| 149 | */
|
|---|
| 150 | public int getVersion() {
|
|---|
| 151 | return version;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Replies true if this is a local build, i.e. an unofficial development build.
|
|---|
| 156 | *
|
|---|
| 157 | * @return true if this is a local build, i.e. an unofficial development build.
|
|---|
| 158 | */
|
|---|
| 159 | public boolean isLocalBuild() {
|
|---|
| 160 | return isLocalBuild;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns the User-Agent string
|
|---|
| 165 | * @return The User-Agent
|
|---|
| 166 | */
|
|---|
| 167 | public String getAgentString() {
|
|---|
| 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) {
|
|---|
| 178 | int v = getVersion();
|
|---|
| 179 | String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
|
|---|
| 180 | if (!Utils.isEmpty(buildName)) {
|
|---|
| 181 | s += ' ' + buildName;
|
|---|
| 182 | }
|
|---|
| 183 | if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
|
|---|
| 184 | s += " SVN";
|
|---|
| 185 | }
|
|---|
| 186 | String result = "JOSM/1.5 ("+ s+' '+LanguageInfo.getJOSMLocaleCode()+')';
|
|---|
| 187 | if (includeOsDetails) {
|
|---|
| 188 | result += ' ' + PlatformManager.getPlatform().getOSDescription();
|
|---|
| 189 | }
|
|---|
| 190 | return result;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * Returns the full User-Agent string
|
|---|
| 195 | * @return The User-Agent
|
|---|
| 196 | * @since 5868
|
|---|
| 197 | */
|
|---|
| 198 | public String getFullAgentString() {
|
|---|
| 199 | return getAgentString() + " Java/"+Utils.getSystemProperty("java.version");
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|