replace VersionUtils with a platform-agnostic impl

This commit is contained in:
Costin Leau
2012-07-11 14:25:12 +03:00
parent c8a5582ddb
commit 4ace694265

View File

@@ -15,52 +15,21 @@
*/ */
package org.springframework.roo.support.util; package org.springframework.roo.support.util;
import java.io.File; import org.springframework.core.SpringVersion;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
/** /**
* @author Jarred Li * @author Jarred Li
*
*/ */
public class VersionUtils { public class VersionUtils {
public static String versionInfo() { /**
// Try to determine the bundle version * Returns the full version string of the present Spring codebase,
String bundleVersion = null; * or <code>null</code> if it cannot be determined.
JarFile jarFile = null; * @see java.lang.Package#getImplementationVersion()
try { */
URL classContainer = VersionUtils.class.getProtectionDomain().getCodeSource().getLocation(); public static String getVersion() {
if (classContainer.toString().endsWith(".jar")) { Package pkg = SpringVersion.class.getPackage();
// Attempt to obtain the "Bundle-Version" version from the manifest return (pkg != null ? pkg.getImplementationVersion() : "Unknown Version");
jarFile = new JarFile(new File(classContainer.toURI()), false);
ZipEntry manifestEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(jarFile.getInputStream(manifestEntry));
bundleVersion = manifest.getMainAttributes().getValue("version");
}
} catch (IOException ignoreAndMoveOn) {
} catch (URISyntaxException ignoreAndMoveOn) {
} finally {
IOUtils.closeQuietly(jarFile);
}
StringBuilder sb = new StringBuilder();
if (bundleVersion != null) {
sb.append(bundleVersion);
}
if (sb.length() == 0) {
sb.append("UNKNOWN VERSION");
}
return sb.toString();
} }
} }