diff --git a/hazelcast-hibernate53/src/main/java/com/hazelcast/hibernate/PhoneHomeInfo.java b/hazelcast-hibernate53/src/main/java/com/hazelcast/hibernate/PhoneHomeInfo.java index 58ce53f1..88674a4d 100644 --- a/hazelcast-hibernate53/src/main/java/com/hazelcast/hibernate/PhoneHomeInfo.java +++ b/hazelcast-hibernate53/src/main/java/com/hazelcast/hibernate/PhoneHomeInfo.java @@ -22,9 +22,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLClassLoader; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.Manifest; /** * Creates query string according to plugin properties to be sent to phone home @@ -73,11 +77,24 @@ private String buildQueryString(boolean isLocalRegion) { // especially for the parameter keys. return new QueryStringBuilder() .addParam("version", version) - .addParam("hibernate-version", Hibernate.class.getPackage().getImplementationVersion()) + .addParam("hibernate-version", getHibernateVersion()) .addParam("region-type", isLocalRegion ? "local" : "distributed") .build(); } + private static String getHibernateVersion() { + URL hibernateJar = Hibernate.class.getProtectionDomain().getCodeSource().getLocation(); + try (URLClassLoader classLoader = new URLClassLoader(new URL[]{hibernateJar})) { + URL url = classLoader.findResource("META-INF/MANIFEST.MF"); + Manifest manifest = new Manifest(url.openStream()); + Attributes mainAttributes = manifest.getMainAttributes(); + return mainAttributes.getValue("Implementation-Version"); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /** * Constructs query string using the added parameters. */ diff --git a/hazelcast-hibernate53/src/test/java/com/hazelcast/hibernate/PhoneHomeTest.java b/hazelcast-hibernate53/src/test/java/com/hazelcast/hibernate/PhoneHomeTest.java index a9a28d98..2cce8bb9 100644 --- a/hazelcast-hibernate53/src/test/java/com/hazelcast/hibernate/PhoneHomeTest.java +++ b/hazelcast-hibernate53/src/test/java/com/hazelcast/hibernate/PhoneHomeTest.java @@ -14,6 +14,7 @@ import static java.util.stream.Collectors.toMap; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -63,6 +64,7 @@ private void verifyQueryString(boolean isLocalRegion, String queryString) { assertEquals(3, parameters.size()); assertEquals(isLocalRegion ? "local" : "distributed", parameters.get("region-type")); assertEquals(resolvedPluginVersion, parameters.get("version")); + assertNotNull(Hibernate.class.getPackage().getImplementationVersion()); assertEquals(Hibernate.class.getPackage().getImplementationVersion(), parameters.get("hibernate-version")); assertNotEquals("N/A", resolvedPluginVersion); }