LUCENE-1666: use LucenePackage to get version from JAR's manifest, if available

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@779683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-05-28 17:27:57 +00:00
parent 6dcd9628f5
commit 54fa7be67b
1 changed files with 8 additions and 32 deletions

View File

@ -17,10 +17,7 @@ package org.apache.lucene.util;
* limitations under the License. * limitations under the License.
*/ */
import java.util.jar.Manifest; import org.apache.lucene.LucenePackage;
import java.util.jar.Attributes;
import java.io.InputStream;
import java.net.URL;
/** /**
* Some useful constants. * Some useful constants.
@ -54,38 +51,17 @@ public final class Constants {
public static final String OS_VERSION = System.getProperty("os.version"); public static final String OS_VERSION = System.getProperty("os.version");
public static final String JAVA_VENDOR = System.getProperty("java.vendor"); public static final String JAVA_VENDOR = System.getProperty("java.vendor");
public static final String LUCENE_VERSION;
public static final String LUCENE_MAIN_VERSION = "2.9"; public static final String LUCENE_MAIN_VERSION = "2.9";
public static final String LUCENE_VERSION;
static { static {
String v = LUCENE_MAIN_VERSION + "-dev"; Package pkg = LucenePackage.get();
try { String v = (pkg == null) ? null : pkg.getImplementationVersion();
// TODO: this should have worked, but doesn't seem to? if (v == null) {
// Package.getPackage("org.apache.lucene.util").getImplementationVersion(); v = LUCENE_MAIN_VERSION + "-dev";
String classContainer = Constants.class.getProtectionDomain().getCodeSource().getLocation().toString(); } else if (v.indexOf(LUCENE_MAIN_VERSION) == -1) {
URL manifestUrl = new URL("jar:" + classContainer + "!/META-INF/MANIFEST.MF"); v = v + " [" + LUCENE_MAIN_VERSION + "]";
InputStream s = manifestUrl.openStream();
try {
Manifest manifest = new Manifest(s);
Attributes attr = manifest.getMainAttributes();
String value = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
if (value != null) {
if (value.indexOf(LUCENE_MAIN_VERSION) == -1) {
v = value + " [" + LUCENE_MAIN_VERSION + "]";
} else {
v = value;
} }
}
} finally {
if (s != null) {
s.close();
}
}
} catch (Throwable t) {
// ignore
}
LUCENE_VERSION = v; LUCENE_VERSION = v;
} }
} }