Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.core.util; |
| 2 | |
| 3 | import java.io.*; |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 4 | import java.util.Properties; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 5 | import java.util.regex.Pattern; |
| 6 | |
| 7 | import com.fasterxml.jackson.core.Version; |
Tatu Saloranta | 4546613 | 2013-01-11 10:20:48 -0800 | [diff] [blame] | 8 | import com.fasterxml.jackson.core.Versioned; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Functionality for supporting exposing of component {@link Version}s. |
Tatu Saloranta | a629bb9 | 2013-04-07 10:33:04 -0700 | [diff] [blame] | 12 | * Also contains other misc methods that have no other place to live in. |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 13 | *<p> |
| 14 | * Note that this class can be used in two roles: first, as a static |
| 15 | * utility class for loading purposes, and second, as a singleton |
| 16 | * loader of per-module version information. |
Tatu Saloranta | c3ef9f3 | 2013-02-04 20:35:22 -0800 | [diff] [blame] | 17 | *<p> |
| 18 | * Note that method for accessing version information changed between versions |
| 19 | * 2.1 and 2.2; earlier code used file named "VERSION.txt"; but this has serious |
| 20 | * performance issues on some platforms (Android), so a replacement system |
| 21 | * was implemented to use class generation and dynamic class loading. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 22 | */ |
| 23 | public class VersionUtil |
| 24 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 25 | private final static Pattern V_SEP = Pattern.compile("[-_./;:]"); |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 26 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 27 | private final Version _v; |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | /********************************************************** |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 31 | /* Instance life-cycle |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 32 | /********************************************************** |
| 33 | */ |
| 34 | |
| 35 | protected VersionUtil() |
| 36 | { |
| 37 | Version v = null; |
| 38 | try { |
| 39 | /* Class we pass only matters for resource-loading: can't use this Class |
| 40 | * (as it's just being loaded at this point), nor anything that depends on it. |
| 41 | */ |
| 42 | v = VersionUtil.versionFor(getClass()); |
| 43 | } catch (Exception e) { // not good to dump to stderr; but that's all we have at this low level |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 44 | System.err.println("ERROR: Failed to load Version information from "+getClass()); |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 45 | } |
| 46 | if (v == null) { |
| 47 | v = Version.unknownVersion(); |
| 48 | } |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 49 | _v = v; |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 52 | public Version version() { return _v; } |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | /********************************************************** |
| 56 | /* Static load methods |
| 57 | /********************************************************** |
| 58 | */ |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Helper method that will try to load version information for specified |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 62 | * class. Implementation is as follows: |
| 63 | * |
| 64 | * First, tries to load version info from a class named |
| 65 | * "PackageVersion" in the same package as the class. |
| 66 | * |
| 67 | * Next, if that fails, class loader that loaded specified class is |
| 68 | * asked to load resource with name "VERSION" from same location |
| 69 | * (package) as class itself had. |
| 70 | * |
| 71 | * If no version information is found, {@link Version#unknownVersion()} is returned. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 72 | */ |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 73 | @SuppressWarnings("resource") |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 74 | public static Version versionFor(Class<?> cls) |
| 75 | { |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 76 | Version packageVersion = packageVersionFor(cls); |
| 77 | if (packageVersion != null) { |
| 78 | return packageVersion; |
| 79 | } |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 80 | final InputStream in = cls.getResourceAsStream("VERSION.txt"); |
| 81 | if (in == null) { |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 82 | return Version.unknownVersion(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 83 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 84 | try { |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 85 | InputStreamReader reader = new InputStreamReader(in, "UTF-8"); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 86 | return doReadVersion(reader); |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 87 | } catch (UnsupportedEncodingException e) { |
| 88 | return Version.unknownVersion(); |
| 89 | } finally { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 90 | _close(in); |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Loads version information by introspecting a class named |
| 96 | * "PackageVersion" in the same package as the given class. |
| 97 | * |
| 98 | * If the class could not be found or does not have a public |
| 99 | * static Version field named "VERSION", returns null. |
| 100 | */ |
| 101 | public static Version packageVersionFor(Class<?> cls) |
| 102 | { |
| 103 | try { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 104 | String versionInfoClassName = cls.getPackage().getName() + ".PackageVersion"; |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 105 | Class<?> vClass = Class.forName(versionInfoClassName, true, cls.getClassLoader()); |
| 106 | // However, if class exists, it better work correctly, no swallowing exceptions |
| 107 | try { |
| 108 | return ((Versioned) vClass.newInstance()).version(); |
| 109 | } catch (Exception e) { |
| 110 | throw new IllegalArgumentException("Failed to get Versioned out of "+vClass); |
| 111 | } |
Tatu Saloranta | 4546613 | 2013-01-11 10:20:48 -0800 | [diff] [blame] | 112 | } catch (Exception e) { // ok to be missing (not good, acceptable) |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 113 | return null; |
| 114 | } |
| 115 | } |
| 116 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 117 | private static Version doReadVersion(final Reader r) |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 118 | { |
| 119 | String version = null, group = null, artifact = null; |
| 120 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 121 | final BufferedReader br = new BufferedReader(r); |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 122 | try { |
| 123 | version = br.readLine(); |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 124 | if (version != null) { |
| 125 | group = br.readLine(); |
| 126 | if (group != null) { |
| 127 | artifact = br.readLine(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 128 | } |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 129 | } |
| 130 | } catch (IOException ignored) { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 131 | } finally { |
| 132 | _close(br); |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 133 | } |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 134 | // We don't trim() version: parseVersion() takes care ot that |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 135 | if (group != null) { |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 136 | group = group.trim(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 137 | } |
| 138 | if (artifact != null) { |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 139 | artifact = artifact.trim(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 140 | } |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 141 | return parseVersion(version, group, artifact); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 144 | /** |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 145 | * Will attempt to load the maven version for the given groupId and |
| 146 | * artifactId. Maven puts a pom.properties file in |
| 147 | * META-INF/maven/groupId/artifactId, containing the groupId, |
| 148 | * artifactId and version of the library. |
| 149 | * |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 150 | * @param cl the ClassLoader to load the pom.properties file from |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 151 | * @param groupId the groupId of the library |
| 152 | * @param artifactId the artifactId of the library |
| 153 | * @return The version |
| 154 | */ |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 155 | @SuppressWarnings("resource") |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 156 | public static Version mavenVersionFor(ClassLoader cl, String groupId, String artifactId) |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 157 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 158 | InputStream pomProperties = cl.getResourceAsStream("META-INF/maven/" |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 159 | + groupId.replaceAll("\\.", "/")+ "/" + artifactId + "/pom.properties"); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 160 | if (pomProperties != null) { |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 161 | try { |
| 162 | Properties props = new Properties(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 163 | props.load(pomProperties); |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 164 | String versionStr = props.getProperty("version"); |
| 165 | String pomPropertiesArtifactId = props.getProperty("artifactId"); |
| 166 | String pomPropertiesGroupId = props.getProperty("groupId"); |
| 167 | return parseVersion(versionStr, pomPropertiesGroupId, pomPropertiesArtifactId); |
| 168 | } catch (IOException e) { |
| 169 | // Ignore |
| 170 | } finally { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 171 | _close(pomProperties); |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | return Version.unknownVersion(); |
| 175 | } |
| 176 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 177 | public static Version parseVersion(String s, String groupId, String artifactId) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 178 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 179 | if (s != null && (s = s.trim()).length() > 0) { |
| 180 | String[] parts = V_SEP.split(s); |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 181 | return new Version(parseVersionPart(parts[0]), |
| 182 | (parts.length > 1) ? parseVersionPart(parts[1]) : 0, |
| 183 | (parts.length > 2) ? parseVersionPart(parts[2]) : 0, |
| 184 | (parts.length > 3) ? parts[3] : null, |
| 185 | groupId, artifactId); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 186 | } |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 187 | return null; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 190 | protected static int parseVersionPart(String s) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 191 | int number = 0; |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame^] | 192 | for (int i = 0, len = s.length(); i < len; ++i) { |
| 193 | char c = s.charAt(i); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 194 | if (c > '9' || c < '0') break; |
| 195 | number = (number * 10) + (c - '0'); |
| 196 | } |
| 197 | return number; |
| 198 | } |
Tatu Saloranta | a629bb9 | 2013-04-07 10:33:04 -0700 | [diff] [blame] | 199 | |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 200 | private final static void _close(Closeable c) { |
| 201 | try { |
| 202 | c.close(); |
| 203 | } catch (IOException e) { } |
| 204 | } |
| 205 | |
Tatu Saloranta | a629bb9 | 2013-04-07 10:33:04 -0700 | [diff] [blame] | 206 | /* |
| 207 | /********************************************************** |
| 208 | /* Orphan utility methods |
| 209 | /********************************************************** |
| 210 | */ |
| 211 | |
| 212 | public final static void throwInternal() { |
| 213 | throw new RuntimeException("Internal error: this code path should never get executed"); |
| 214 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 215 | } |