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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/VersionInfo.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,309 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.util;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.io.InputStream;
    1.35 +import java.util.Map;
    1.36 +import java.util.Properties;
    1.37 +import java.util.ArrayList;
    1.38 +
    1.39 +
    1.40 +/**
    1.41 + * Provides access to version information for HTTP components.
    1.42 + * Static methods are used to extract version information from property
    1.43 + * files that are automatically packaged with HTTP component release JARs.
    1.44 + * <br/>
    1.45 + * All available version information is provided in strings, where
    1.46 + * the string format is informal and subject to change without notice.
    1.47 + * Version information is provided for debugging output and interpretation
    1.48 + * by humans, not for automated processing in applications.
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +public class VersionInfo {
    1.53 +
    1.54 +    /** A string constant for unavailable information. */
    1.55 +    public final static String UNAVAILABLE = "UNAVAILABLE";
    1.56 +
    1.57 +    /** The filename of the version information files. */
    1.58 +    public final static String VERSION_PROPERTY_FILE = "version.properties";
    1.59 +
    1.60 +    // the property names
    1.61 +    public final static String PROPERTY_MODULE    = "info.module";
    1.62 +    public final static String PROPERTY_RELEASE   = "info.release";
    1.63 +    public final static String PROPERTY_TIMESTAMP = "info.timestamp";
    1.64 +
    1.65 +
    1.66 +    /** The package that contains the version information. */
    1.67 +    private final String infoPackage;
    1.68 +
    1.69 +    /** The module from the version info. */
    1.70 +    private final String infoModule;
    1.71 +
    1.72 +    /** The release from the version info. */
    1.73 +    private final String infoRelease;
    1.74 +
    1.75 +    /** The timestamp from the version info. */
    1.76 +    private final String infoTimestamp;
    1.77 +
    1.78 +    /** The classloader from which the version info was obtained. */
    1.79 +    private final String infoClassloader;
    1.80 +
    1.81 +
    1.82 +    /**
    1.83 +     * Instantiates version information.
    1.84 +     *
    1.85 +     * @param pckg      the package
    1.86 +     * @param module    the module, or <code>null</code>
    1.87 +     * @param release   the release, or <code>null</code>
    1.88 +     * @param time      the build time, or <code>null</code>
    1.89 +     * @param clsldr    the class loader, or <code>null</code>
    1.90 +     */
    1.91 +    protected VersionInfo(String pckg, String module,
    1.92 +                          String release, String time, String clsldr) {
    1.93 +        if (pckg == null) {
    1.94 +            throw new IllegalArgumentException
    1.95 +                ("Package identifier must not be null.");
    1.96 +        }
    1.97 +
    1.98 +        infoPackage     = pckg;
    1.99 +        infoModule      = (module  != null) ? module  : UNAVAILABLE;
   1.100 +        infoRelease     = (release != null) ? release : UNAVAILABLE;
   1.101 +        infoTimestamp   = (time    != null) ? time    : UNAVAILABLE;
   1.102 +        infoClassloader = (clsldr  != null) ? clsldr  : UNAVAILABLE;
   1.103 +    }
   1.104 +
   1.105 +
   1.106 +    /**
   1.107 +     * Obtains the package name.
   1.108 +     * The package name identifies the module or informal unit.
   1.109 +     *
   1.110 +     * @return  the package name, never <code>null</code>
   1.111 +     */
   1.112 +    public final String getPackage() {
   1.113 +        return infoPackage;
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Obtains the name of the versioned module or informal unit.
   1.118 +     * This data is read from the version information for the package.
   1.119 +     *
   1.120 +     * @return  the module name, never <code>null</code>
   1.121 +     */
   1.122 +    public final String getModule() {
   1.123 +        return infoModule;
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Obtains the release of the versioned module or informal unit.
   1.128 +     * This data is read from the version information for the package.
   1.129 +     *
   1.130 +     * @return  the release version, never <code>null</code>
   1.131 +     */
   1.132 +    public final String getRelease() {
   1.133 +        return infoRelease;
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * Obtains the timestamp of the versioned module or informal unit.
   1.138 +     * This data is read from the version information for the package.
   1.139 +     *
   1.140 +     * @return  the timestamp, never <code>null</code>
   1.141 +     */
   1.142 +    public final String getTimestamp() {
   1.143 +        return infoTimestamp;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Obtains the classloader used to read the version information.
   1.148 +     * This is just the <code>toString</code> output of the classloader,
   1.149 +     * since the version information should not keep a reference to
   1.150 +     * the classloader itself. That could prevent garbage collection.
   1.151 +     *
   1.152 +     * @return  the classloader description, never <code>null</code>
   1.153 +     */
   1.154 +    public final String getClassloader() {
   1.155 +        return infoClassloader;
   1.156 +    }
   1.157 +
   1.158 +
   1.159 +    /**
   1.160 +     * Provides the version information in human-readable format.
   1.161 +     *
   1.162 +     * @return  a string holding this version information
   1.163 +     */
   1.164 +    public String toString() {
   1.165 +        StringBuffer sb = new StringBuffer
   1.166 +            (20 + infoPackage.length() + infoModule.length() +
   1.167 +             infoRelease.length() + infoTimestamp.length() +
   1.168 +             infoClassloader.length());
   1.169 +
   1.170 +        sb.append("VersionInfo(")
   1.171 +            .append(infoPackage).append(':').append(infoModule);
   1.172 +
   1.173 +        // If version info is missing, a single "UNAVAILABLE" for the module
   1.174 +        // is sufficient. Everything else just clutters the output.
   1.175 +        if (!UNAVAILABLE.equals(infoRelease))
   1.176 +            sb.append(':').append(infoRelease);
   1.177 +        if (!UNAVAILABLE.equals(infoTimestamp))
   1.178 +            sb.append(':').append(infoTimestamp);
   1.179 +
   1.180 +        sb.append(')');
   1.181 +
   1.182 +        if (!UNAVAILABLE.equals(infoClassloader))
   1.183 +            sb.append('@').append(infoClassloader);
   1.184 +
   1.185 +        return sb.toString();
   1.186 +    }
   1.187 +
   1.188 +
   1.189 +    /**
   1.190 +     * Loads version information for a list of packages.
   1.191 +     *
   1.192 +     * @param pckgs     the packages for which to load version info
   1.193 +     * @param clsldr    the classloader to load from, or
   1.194 +     *                  <code>null</code> for the thread context classloader
   1.195 +     *
   1.196 +     * @return  the version information for all packages found,
   1.197 +     *          never <code>null</code>
   1.198 +     */
   1.199 +    public final static VersionInfo[] loadVersionInfo(String[] pckgs,
   1.200 +                                                      ClassLoader clsldr) {
   1.201 +        if (pckgs == null) {
   1.202 +            throw new IllegalArgumentException
   1.203 +                ("Package identifier list must not be null.");
   1.204 +        }
   1.205 +
   1.206 +        ArrayList vil = new ArrayList(pckgs.length);
   1.207 +        for (int i=0; i<pckgs.length; i++) {
   1.208 +            VersionInfo vi = loadVersionInfo(pckgs[i], clsldr);
   1.209 +            if (vi != null)
   1.210 +                vil.add(vi);
   1.211 +        }
   1.212 +
   1.213 +        return (VersionInfo[]) vil.toArray(new VersionInfo[vil.size()]);
   1.214 +    }
   1.215 +
   1.216 +
   1.217 +    /**
   1.218 +     * Loads version information for a package.
   1.219 +     *
   1.220 +     * @param pckg      the package for which to load version information,
   1.221 +     *                  for example "ch.boye.httpclientandroidlib".
   1.222 +     *                  The package name should NOT end with a dot.
   1.223 +     * @param clsldr    the classloader to load from, or
   1.224 +     *                  <code>null</code> for the thread context classloader
   1.225 +     *
   1.226 +     * @return  the version information for the argument package, or
   1.227 +     *          <code>null</code> if not available
   1.228 +     */
   1.229 +    public final static VersionInfo loadVersionInfo(final String pckg,
   1.230 +                                                    ClassLoader clsldr) {
   1.231 +        if (pckg == null) {
   1.232 +            throw new IllegalArgumentException
   1.233 +                ("Package identifier must not be null.");
   1.234 +        }
   1.235 +
   1.236 +        if (clsldr == null)
   1.237 +            clsldr = Thread.currentThread().getContextClassLoader();
   1.238 +
   1.239 +        Properties vip = null; // version info properties, if available
   1.240 +        try {
   1.241 +            // ch.boye.httpclientandroidlib      becomes
   1.242 +            // org/apache/http/version.properties
   1.243 +            InputStream is = clsldr.getResourceAsStream
   1.244 +                (pckg.replace('.', '/') + "/" + VERSION_PROPERTY_FILE);
   1.245 +            if (is != null) {
   1.246 +                try {
   1.247 +                    Properties props = new Properties();
   1.248 +                    props.load(is);
   1.249 +                    vip = props;
   1.250 +                } finally {
   1.251 +                    is.close();
   1.252 +                }
   1.253 +            }
   1.254 +        } catch (IOException ex) {
   1.255 +            // shamelessly munch this exception
   1.256 +        }
   1.257 +
   1.258 +        VersionInfo result = null;
   1.259 +        if (vip != null)
   1.260 +            result = fromMap(pckg, vip, clsldr);
   1.261 +
   1.262 +        return result;
   1.263 +    }
   1.264 +
   1.265 +
   1.266 +    /**
   1.267 +     * Instantiates version information from properties.
   1.268 +     *
   1.269 +     * @param pckg      the package for the version information
   1.270 +     * @param info      the map from string keys to string values,
   1.271 +     *                  for example {@link java.util.Properties}
   1.272 +     * @param clsldr    the classloader, or <code>null</code>
   1.273 +     *
   1.274 +     * @return  the version information
   1.275 +     */
   1.276 +    protected final static VersionInfo fromMap(String pckg, Map info,
   1.277 +                                               ClassLoader clsldr) {
   1.278 +        if (pckg == null) {
   1.279 +            throw new IllegalArgumentException
   1.280 +                ("Package identifier must not be null.");
   1.281 +        }
   1.282 +
   1.283 +        String module = null;
   1.284 +        String release = null;
   1.285 +        String timestamp = null;
   1.286 +
   1.287 +        if (info != null) {
   1.288 +            module = (String) info.get(PROPERTY_MODULE);
   1.289 +            if ((module != null) && (module.length() < 1))
   1.290 +                module = null;
   1.291 +
   1.292 +            release = (String) info.get(PROPERTY_RELEASE);
   1.293 +            if ((release != null) && ((release.length() < 1) ||
   1.294 +                                      (release.equals("${pom.version}"))))
   1.295 +                release = null;
   1.296 +
   1.297 +            timestamp = (String) info.get(PROPERTY_TIMESTAMP);
   1.298 +            if ((timestamp != null) &&
   1.299 +                ((timestamp.length() < 1) ||
   1.300 +                 (timestamp.equals("${mvn.timestamp}")))
   1.301 +                )
   1.302 +                timestamp = null;
   1.303 +        } // if info
   1.304 +
   1.305 +        String clsldrstr = null;
   1.306 +        if (clsldr != null)
   1.307 +            clsldrstr = clsldr.toString();
   1.308 +
   1.309 +        return new VersionInfo(pckg, module, release, timestamp, clsldrstr);
   1.310 +    }
   1.311 +
   1.312 +} // class VersionInfo

mercurial