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. |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 22 | *<p> |
| 23 | * Note that functionality for reading "VERSION.txt" was removed completely |
| 24 | * from Jackson 2.6. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 25 | */ |
| 26 | public class VersionUtil |
| 27 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 28 | private final static Pattern V_SEP = Pattern.compile("[-_./;:]"); |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 29 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 30 | private final Version _v; |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | /********************************************************** |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 34 | /* Instance life-cycle |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 35 | /********************************************************** |
| 36 | */ |
| 37 | |
| 38 | protected VersionUtil() |
| 39 | { |
| 40 | Version v = null; |
| 41 | try { |
| 42 | /* Class we pass only matters for resource-loading: can't use this Class |
| 43 | * (as it's just being loaded at this point), nor anything that depends on it. |
| 44 | */ |
| 45 | v = VersionUtil.versionFor(getClass()); |
| 46 | } 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] | 47 | System.err.println("ERROR: Failed to load Version information from "+getClass()); |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 48 | } |
| 49 | if (v == null) { |
| 50 | v = Version.unknownVersion(); |
| 51 | } |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 52 | _v = v; |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 55 | public Version version() { return _v; } |
Tatu Saloranta | 48d2609 | 2011-12-28 23:11:49 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | /********************************************************** |
| 59 | /* Static load methods |
| 60 | /********************************************************** |
| 61 | */ |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Helper method that will try to load version information for specified |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 65 | * class. Implementation is as follows: |
| 66 | * |
| 67 | * First, tries to load version info from a class named |
| 68 | * "PackageVersion" in the same package as the class. |
| 69 | * |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 70 | * If no version information is found, {@link Version#unknownVersion()} is returned. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 71 | */ |
| 72 | public static Version versionFor(Class<?> cls) |
| 73 | { |
Sammy Chu | 18ad3bc | 2016-01-26 11:58:58 +0800 | [diff] [blame] | 74 | Version version = packageVersionFor(cls); |
| 75 | return version == null ? Version.unknownVersion() : version; |
Francis Galiegue | 6ebc759 | 2012-11-13 17:40:58 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Loads version information by introspecting a class named |
| 80 | * "PackageVersion" in the same package as the given class. |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 81 | *<p> |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 82 | * If the class could not be found or does not have a public |
| 83 | * static Version field named "VERSION", returns null. |
| 84 | */ |
| 85 | public static Version packageVersionFor(Class<?> cls) |
| 86 | { |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 87 | Version v = null; |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 88 | try { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 89 | String versionInfoClassName = cls.getPackage().getName() + ".PackageVersion"; |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 90 | Class<?> vClass = Class.forName(versionInfoClassName, true, cls.getClassLoader()); |
| 91 | // However, if class exists, it better work correctly, no swallowing exceptions |
| 92 | try { |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 93 | v = ((Versioned) vClass.newInstance()).version(); |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 94 | } catch (Exception e) { |
| 95 | throw new IllegalArgumentException("Failed to get Versioned out of "+vClass); |
| 96 | } |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 97 | } catch (Exception e) { // ok to be missing (not good but acceptable) |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 98 | ; |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 99 | } |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 100 | return (v == null) ? Version.unknownVersion() : v; |
Ben Gertzfield | f440fa0 | 2013-01-03 16:38:28 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 103 | /** |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 104 | * Will attempt to load the maven version for the given groupId and |
| 105 | * artifactId. Maven puts a pom.properties file in |
| 106 | * META-INF/maven/groupId/artifactId, containing the groupId, |
| 107 | * artifactId and version of the library. |
| 108 | * |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 109 | * @param cl the ClassLoader to load the pom.properties file from |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 110 | * @param groupId the groupId of the library |
| 111 | * @param artifactId the artifactId of the library |
| 112 | * @return The version |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 113 | * |
| 114 | * @deprecated Since 2.6: functionality not used by any official Jackson component, should be |
| 115 | * moved out if anyone needs it |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 116 | */ |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 117 | @SuppressWarnings("resource") |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 118 | @Deprecated // since 2.6 |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 119 | public static Version mavenVersionFor(ClassLoader cl, String groupId, String artifactId) |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 120 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 121 | InputStream pomProperties = cl.getResourceAsStream("META-INF/maven/" |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 122 | + groupId.replaceAll("\\.", "/")+ "/" + artifactId + "/pom.properties"); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 123 | if (pomProperties != null) { |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 124 | try { |
| 125 | Properties props = new Properties(); |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 126 | props.load(pomProperties); |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 127 | String versionStr = props.getProperty("version"); |
| 128 | String pomPropertiesArtifactId = props.getProperty("artifactId"); |
| 129 | String pomPropertiesGroupId = props.getProperty("groupId"); |
| 130 | return parseVersion(versionStr, pomPropertiesGroupId, pomPropertiesArtifactId); |
| 131 | } catch (IOException e) { |
| 132 | // Ignore |
| 133 | } finally { |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 134 | _close(pomProperties); |
James Roper | 7877499 | 2012-01-03 20:43:57 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | return Version.unknownVersion(); |
| 138 | } |
| 139 | |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 140 | /** |
Cowtowncoder | 2e262b9 | 2015-05-27 17:32:17 -0700 | [diff] [blame] | 141 | * Method used by <code>PackageVersion</code> classes to decode version injected by Maven build. |
Cowtowncoder | 88c296c | 2015-05-27 12:08:24 -0700 | [diff] [blame] | 142 | */ |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 143 | public static Version parseVersion(String s, String groupId, String artifactId) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 144 | { |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 145 | if (s != null && (s = s.trim()).length() > 0) { |
| 146 | String[] parts = V_SEP.split(s); |
Tatu Saloranta | 838dc4c | 2013-09-06 21:41:44 -0700 | [diff] [blame] | 147 | return new Version(parseVersionPart(parts[0]), |
| 148 | (parts.length > 1) ? parseVersionPart(parts[1]) : 0, |
| 149 | (parts.length > 2) ? parseVersionPart(parts[2]) : 0, |
| 150 | (parts.length > 3) ? parts[3] : null, |
| 151 | groupId, artifactId); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 152 | } |
Tatu Saloranta | 1fb1e27 | 2016-01-25 20:00:10 -0800 | [diff] [blame] | 153 | return Version.unknownVersion(); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 156 | protected static int parseVersionPart(String s) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 157 | int number = 0; |
Tatu Saloranta | fedd795 | 2014-01-14 20:41:04 -0800 | [diff] [blame] | 158 | for (int i = 0, len = s.length(); i < len; ++i) { |
| 159 | char c = s.charAt(i); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 160 | if (c > '9' || c < '0') break; |
| 161 | number = (number * 10) + (c - '0'); |
| 162 | } |
| 163 | return number; |
| 164 | } |
Tatu Saloranta | a629bb9 | 2013-04-07 10:33:04 -0700 | [diff] [blame] | 165 | |
Tatu Saloranta | 1313ea3 | 2013-09-06 21:36:34 -0700 | [diff] [blame] | 166 | private final static void _close(Closeable c) { |
| 167 | try { |
| 168 | c.close(); |
| 169 | } catch (IOException e) { } |
| 170 | } |
| 171 | |
Tatu Saloranta | a629bb9 | 2013-04-07 10:33:04 -0700 | [diff] [blame] | 172 | /* |
| 173 | /********************************************************** |
| 174 | /* Orphan utility methods |
| 175 | /********************************************************** |
| 176 | */ |
| 177 | |
| 178 | public final static void throwInternal() { |
| 179 | throw new RuntimeException("Internal error: this code path should never get executed"); |
| 180 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 181 | } |