|
| 1 | +package com.ericsson.eiffel.remrem.shared; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.Enumeration; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.jar.Attributes; |
| 10 | +import java.util.jar.JarFile; |
| 11 | +import java.util.jar.Manifest; |
| 12 | + |
| 13 | +/** |
| 14 | + * This class will search in all registered jars and their manifest file for |
| 15 | + * attribute Remrem-Version-Key. It will return a list with all versions found. |
| 16 | + * |
| 17 | + * @author evasiba |
| 18 | + * |
| 19 | + */ |
| 20 | +public class VersionService { |
| 21 | + |
| 22 | + private static final String REMREM_VERSION_KEY = "remremVersionKey"; |
| 23 | + private static final String IS_ENDPOINT_VERSION = "isEndpointVersion"; |
| 24 | + private static final String ENDPOINT_VERSION = "endpointVersions"; |
| 25 | + private static final String SERVICE_VERSION = "serviceVersion"; |
| 26 | + |
| 27 | + /** |
| 28 | + * This method will load and parse the MINIFEST files to get the version of |
| 29 | + * the loaded messaging protocols. It is required to define the versions as |
| 30 | + * mainifest attributes in the build.gradle or pom.xml files using |
| 31 | + * attributes "remremVersionKey" and "isEndpointVersion" to specify the type |
| 32 | + * of the protocol or service and if it is endpoint or not respectively. |
| 33 | + * Example for build.gradle: manifest { attributes('remremVersionKey': |
| 34 | + * 'semanticsVersion') attributes('semanticsVersion': version) |
| 35 | + * attributes('isEndpointVersion': 'true') } |
| 36 | + * |
| 37 | + * @return a map containing the protocol and service types with their |
| 38 | + * versions {"endpointVersions" : {"semanticsVersion" : "1.1.1"}, |
| 39 | + * "serviceVersion": {"remremGenerateVersion": "0.1.1"}} |
| 40 | + */ |
| 41 | + public static Map<String, Map<String, String>> getMessagingVersions() { |
| 42 | + Enumeration<?> resEnum; |
| 43 | + Map<String, Map<String, String>> versions = new HashMap<>(); |
| 44 | + Map<String, String> endpointVersions = new HashMap<String, String>(); |
| 45 | + Map<String, String> serviceVersion = new HashMap<String, String>(); |
| 46 | + |
| 47 | + try { |
| 48 | + resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME); |
| 49 | + while (resEnum.hasMoreElements()) { |
| 50 | + try { |
| 51 | + URL url = (URL) resEnum.nextElement(); |
| 52 | + InputStream is = url.openStream(); |
| 53 | + if (is != null) { |
| 54 | + Manifest manifest = new Manifest(is); |
| 55 | + Attributes mainAttribs = manifest.getMainAttributes(); |
| 56 | + String versionKey = mainAttribs.getValue(REMREM_VERSION_KEY); |
| 57 | + |
| 58 | + if (versionKey != null) { |
| 59 | + String version = mainAttribs.getValue(versionKey); |
| 60 | + if (version != null) { |
| 61 | + if (mainAttribs.getValue(IS_ENDPOINT_VERSION) != null) { |
| 62 | + endpointVersions.put(versionKey, version); |
| 63 | + } else { |
| 64 | + serviceVersion.put(versionKey, version); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + // Silently ignore wrong manifests on classpath? |
| 71 | + } |
| 72 | + } |
| 73 | + versions.put(ENDPOINT_VERSION, endpointVersions); |
| 74 | + versions.put(SERVICE_VERSION, serviceVersion); |
| 75 | + } catch (IOException e1) { |
| 76 | + // Silently ignore wrong manifests on classpath? |
| 77 | + } |
| 78 | + return versions; |
| 79 | + } |
| 80 | +} |
0 commit comments