package org.peerbox.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import net.tomp2p.p2p.Peer; import org.hive2hive.core.api.H2HNode; import org.peerbox.App; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * VersionUtils allows extracting version information of some important dependencies. * It reads the pom.properties file generated by maven in the jar package. * A consequence of this is that it only works if jar packages are executed * (e.g. it does not work in eclipse projects when a regular run configuration is run). * * @author albrecht * */ public class VersionUtils { private static final Logger logger = LoggerFactory.getLogger(VersionUtils.class); private VersionUtils() { // prevent instances } /** * reads the version property from the property file. * * @param clazz a class in the package (note: must be part of the artifact). * @param pomProperties resource path to the pom.properties file inside the jar. * @return the version string or null if it was not possible to read. */ private static synchronized String readVersionFromPom(Class<?> clazz, String pomProperties) { String version = null; // try reading the pom.properties file from the jar try { Properties p = new Properties(); try (InputStream is = clazz.getResourceAsStream(pomProperties)) { if (is != null) { p.load(is); version = p.getProperty("version", null); } } } catch (IOException e) { version = null; logger.warn("Could not read version for '{}' from file '{}'", clazz.getName(), pomProperties); } // using Java API if (version == null) { Package p = clazz.getPackage(); if (p != null) { version = p.getImplementationVersion(); if (version == null) { version = p.getSpecificationVersion(); } } } return version; } /** * Get version of PeerWasp. * * @return peerwasp version string or null. */ public static String getPeerWaspVersion() { String pomProperties = "/META-INF/maven/com.peerwasp/peerwasp/pom.properties"; Class<App> clazz = App.class; String version = readVersionFromPom(clazz, pomProperties); return version; } /** * Get version of Hive2Hive. * * @return h2h version string (core) or null. */ public static String getH2HVersion() { String pomProperties = "/META-INF/maven/org.hive2hive/org.hive2hive.core/pom.properties"; Class<H2HNode> clazz = H2HNode.class; String version = readVersionFromPom(clazz, pomProperties); return version; } /** * Get version of TomP2P. * * @return tomp2p version string (core) or null. */ public static String getTomP2PVersion() { String pomProperties = "/META-INF/maven/net.tomp2p/tomp2p-core/pom.properties"; Class<Peer> clazz = Peer.class; String version = readVersionFromPom(clazz, pomProperties); return version; } }