mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/VersionInfo.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.util;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.io.InputStream;
michael@0 32 import java.util.Map;
michael@0 33 import java.util.Properties;
michael@0 34 import java.util.ArrayList;
michael@0 35
michael@0 36
michael@0 37 /**
michael@0 38 * Provides access to version information for HTTP components.
michael@0 39 * Static methods are used to extract version information from property
michael@0 40 * files that are automatically packaged with HTTP component release JARs.
michael@0 41 * <br/>
michael@0 42 * All available version information is provided in strings, where
michael@0 43 * the string format is informal and subject to change without notice.
michael@0 44 * Version information is provided for debugging output and interpretation
michael@0 45 * by humans, not for automated processing in applications.
michael@0 46 *
michael@0 47 * @since 4.0
michael@0 48 */
michael@0 49 public class VersionInfo {
michael@0 50
michael@0 51 /** A string constant for unavailable information. */
michael@0 52 public final static String UNAVAILABLE = "UNAVAILABLE";
michael@0 53
michael@0 54 /** The filename of the version information files. */
michael@0 55 public final static String VERSION_PROPERTY_FILE = "version.properties";
michael@0 56
michael@0 57 // the property names
michael@0 58 public final static String PROPERTY_MODULE = "info.module";
michael@0 59 public final static String PROPERTY_RELEASE = "info.release";
michael@0 60 public final static String PROPERTY_TIMESTAMP = "info.timestamp";
michael@0 61
michael@0 62
michael@0 63 /** The package that contains the version information. */
michael@0 64 private final String infoPackage;
michael@0 65
michael@0 66 /** The module from the version info. */
michael@0 67 private final String infoModule;
michael@0 68
michael@0 69 /** The release from the version info. */
michael@0 70 private final String infoRelease;
michael@0 71
michael@0 72 /** The timestamp from the version info. */
michael@0 73 private final String infoTimestamp;
michael@0 74
michael@0 75 /** The classloader from which the version info was obtained. */
michael@0 76 private final String infoClassloader;
michael@0 77
michael@0 78
michael@0 79 /**
michael@0 80 * Instantiates version information.
michael@0 81 *
michael@0 82 * @param pckg the package
michael@0 83 * @param module the module, or <code>null</code>
michael@0 84 * @param release the release, or <code>null</code>
michael@0 85 * @param time the build time, or <code>null</code>
michael@0 86 * @param clsldr the class loader, or <code>null</code>
michael@0 87 */
michael@0 88 protected VersionInfo(String pckg, String module,
michael@0 89 String release, String time, String clsldr) {
michael@0 90 if (pckg == null) {
michael@0 91 throw new IllegalArgumentException
michael@0 92 ("Package identifier must not be null.");
michael@0 93 }
michael@0 94
michael@0 95 infoPackage = pckg;
michael@0 96 infoModule = (module != null) ? module : UNAVAILABLE;
michael@0 97 infoRelease = (release != null) ? release : UNAVAILABLE;
michael@0 98 infoTimestamp = (time != null) ? time : UNAVAILABLE;
michael@0 99 infoClassloader = (clsldr != null) ? clsldr : UNAVAILABLE;
michael@0 100 }
michael@0 101
michael@0 102
michael@0 103 /**
michael@0 104 * Obtains the package name.
michael@0 105 * The package name identifies the module or informal unit.
michael@0 106 *
michael@0 107 * @return the package name, never <code>null</code>
michael@0 108 */
michael@0 109 public final String getPackage() {
michael@0 110 return infoPackage;
michael@0 111 }
michael@0 112
michael@0 113 /**
michael@0 114 * Obtains the name of the versioned module or informal unit.
michael@0 115 * This data is read from the version information for the package.
michael@0 116 *
michael@0 117 * @return the module name, never <code>null</code>
michael@0 118 */
michael@0 119 public final String getModule() {
michael@0 120 return infoModule;
michael@0 121 }
michael@0 122
michael@0 123 /**
michael@0 124 * Obtains the release of the versioned module or informal unit.
michael@0 125 * This data is read from the version information for the package.
michael@0 126 *
michael@0 127 * @return the release version, never <code>null</code>
michael@0 128 */
michael@0 129 public final String getRelease() {
michael@0 130 return infoRelease;
michael@0 131 }
michael@0 132
michael@0 133 /**
michael@0 134 * Obtains the timestamp of the versioned module or informal unit.
michael@0 135 * This data is read from the version information for the package.
michael@0 136 *
michael@0 137 * @return the timestamp, never <code>null</code>
michael@0 138 */
michael@0 139 public final String getTimestamp() {
michael@0 140 return infoTimestamp;
michael@0 141 }
michael@0 142
michael@0 143 /**
michael@0 144 * Obtains the classloader used to read the version information.
michael@0 145 * This is just the <code>toString</code> output of the classloader,
michael@0 146 * since the version information should not keep a reference to
michael@0 147 * the classloader itself. That could prevent garbage collection.
michael@0 148 *
michael@0 149 * @return the classloader description, never <code>null</code>
michael@0 150 */
michael@0 151 public final String getClassloader() {
michael@0 152 return infoClassloader;
michael@0 153 }
michael@0 154
michael@0 155
michael@0 156 /**
michael@0 157 * Provides the version information in human-readable format.
michael@0 158 *
michael@0 159 * @return a string holding this version information
michael@0 160 */
michael@0 161 public String toString() {
michael@0 162 StringBuffer sb = new StringBuffer
michael@0 163 (20 + infoPackage.length() + infoModule.length() +
michael@0 164 infoRelease.length() + infoTimestamp.length() +
michael@0 165 infoClassloader.length());
michael@0 166
michael@0 167 sb.append("VersionInfo(")
michael@0 168 .append(infoPackage).append(':').append(infoModule);
michael@0 169
michael@0 170 // If version info is missing, a single "UNAVAILABLE" for the module
michael@0 171 // is sufficient. Everything else just clutters the output.
michael@0 172 if (!UNAVAILABLE.equals(infoRelease))
michael@0 173 sb.append(':').append(infoRelease);
michael@0 174 if (!UNAVAILABLE.equals(infoTimestamp))
michael@0 175 sb.append(':').append(infoTimestamp);
michael@0 176
michael@0 177 sb.append(')');
michael@0 178
michael@0 179 if (!UNAVAILABLE.equals(infoClassloader))
michael@0 180 sb.append('@').append(infoClassloader);
michael@0 181
michael@0 182 return sb.toString();
michael@0 183 }
michael@0 184
michael@0 185
michael@0 186 /**
michael@0 187 * Loads version information for a list of packages.
michael@0 188 *
michael@0 189 * @param pckgs the packages for which to load version info
michael@0 190 * @param clsldr the classloader to load from, or
michael@0 191 * <code>null</code> for the thread context classloader
michael@0 192 *
michael@0 193 * @return the version information for all packages found,
michael@0 194 * never <code>null</code>
michael@0 195 */
michael@0 196 public final static VersionInfo[] loadVersionInfo(String[] pckgs,
michael@0 197 ClassLoader clsldr) {
michael@0 198 if (pckgs == null) {
michael@0 199 throw new IllegalArgumentException
michael@0 200 ("Package identifier list must not be null.");
michael@0 201 }
michael@0 202
michael@0 203 ArrayList vil = new ArrayList(pckgs.length);
michael@0 204 for (int i=0; i<pckgs.length; i++) {
michael@0 205 VersionInfo vi = loadVersionInfo(pckgs[i], clsldr);
michael@0 206 if (vi != null)
michael@0 207 vil.add(vi);
michael@0 208 }
michael@0 209
michael@0 210 return (VersionInfo[]) vil.toArray(new VersionInfo[vil.size()]);
michael@0 211 }
michael@0 212
michael@0 213
michael@0 214 /**
michael@0 215 * Loads version information for a package.
michael@0 216 *
michael@0 217 * @param pckg the package for which to load version information,
michael@0 218 * for example "ch.boye.httpclientandroidlib".
michael@0 219 * The package name should NOT end with a dot.
michael@0 220 * @param clsldr the classloader to load from, or
michael@0 221 * <code>null</code> for the thread context classloader
michael@0 222 *
michael@0 223 * @return the version information for the argument package, or
michael@0 224 * <code>null</code> if not available
michael@0 225 */
michael@0 226 public final static VersionInfo loadVersionInfo(final String pckg,
michael@0 227 ClassLoader clsldr) {
michael@0 228 if (pckg == null) {
michael@0 229 throw new IllegalArgumentException
michael@0 230 ("Package identifier must not be null.");
michael@0 231 }
michael@0 232
michael@0 233 if (clsldr == null)
michael@0 234 clsldr = Thread.currentThread().getContextClassLoader();
michael@0 235
michael@0 236 Properties vip = null; // version info properties, if available
michael@0 237 try {
michael@0 238 // ch.boye.httpclientandroidlib becomes
michael@0 239 // org/apache/http/version.properties
michael@0 240 InputStream is = clsldr.getResourceAsStream
michael@0 241 (pckg.replace('.', '/') + "/" + VERSION_PROPERTY_FILE);
michael@0 242 if (is != null) {
michael@0 243 try {
michael@0 244 Properties props = new Properties();
michael@0 245 props.load(is);
michael@0 246 vip = props;
michael@0 247 } finally {
michael@0 248 is.close();
michael@0 249 }
michael@0 250 }
michael@0 251 } catch (IOException ex) {
michael@0 252 // shamelessly munch this exception
michael@0 253 }
michael@0 254
michael@0 255 VersionInfo result = null;
michael@0 256 if (vip != null)
michael@0 257 result = fromMap(pckg, vip, clsldr);
michael@0 258
michael@0 259 return result;
michael@0 260 }
michael@0 261
michael@0 262
michael@0 263 /**
michael@0 264 * Instantiates version information from properties.
michael@0 265 *
michael@0 266 * @param pckg the package for the version information
michael@0 267 * @param info the map from string keys to string values,
michael@0 268 * for example {@link java.util.Properties}
michael@0 269 * @param clsldr the classloader, or <code>null</code>
michael@0 270 *
michael@0 271 * @return the version information
michael@0 272 */
michael@0 273 protected final static VersionInfo fromMap(String pckg, Map info,
michael@0 274 ClassLoader clsldr) {
michael@0 275 if (pckg == null) {
michael@0 276 throw new IllegalArgumentException
michael@0 277 ("Package identifier must not be null.");
michael@0 278 }
michael@0 279
michael@0 280 String module = null;
michael@0 281 String release = null;
michael@0 282 String timestamp = null;
michael@0 283
michael@0 284 if (info != null) {
michael@0 285 module = (String) info.get(PROPERTY_MODULE);
michael@0 286 if ((module != null) && (module.length() < 1))
michael@0 287 module = null;
michael@0 288
michael@0 289 release = (String) info.get(PROPERTY_RELEASE);
michael@0 290 if ((release != null) && ((release.length() < 1) ||
michael@0 291 (release.equals("${pom.version}"))))
michael@0 292 release = null;
michael@0 293
michael@0 294 timestamp = (String) info.get(PROPERTY_TIMESTAMP);
michael@0 295 if ((timestamp != null) &&
michael@0 296 ((timestamp.length() < 1) ||
michael@0 297 (timestamp.equals("${mvn.timestamp}")))
michael@0 298 )
michael@0 299 timestamp = null;
michael@0 300 } // if info
michael@0 301
michael@0 302 String clsldrstr = null;
michael@0 303 if (clsldr != null)
michael@0 304 clsldrstr = clsldr.toString();
michael@0 305
michael@0 306 return new VersionInfo(pckg, module, release, timestamp, clsldrstr);
michael@0 307 }
michael@0 308
michael@0 309 } // class VersionInfo

mercurial