01: package org.pentaho.util;
02:
03: import java.net.URL;
04: import java.util.jar.JarInputStream;
05: import java.util.jar.Manifest;
06:
07: /**
08: * Set of utility methods related to the manifest file.
09: * <br/>
10: * NOTE: if the manifest file can not be retrieved, these methods will not work
11: * and will return <code>null</code>. The most common case for this is the the
12: * code is being run outside of a jar file.
13: * @author dkincade
14: */
15: public class ManifestUtil {
16: /**
17: * Retrieves the manifest information for the jar file which contains
18: * this utility class.
19: * @return The Manifest file for the jar file which contains this utility class,
20: * or <code>null</code> if the code is not in a jar file.
21: */
22: public static Manifest getManifest() {
23: return getManifest(ManifestUtil.class);
24: }
25:
26: /**
27: * Retrieves the manifest information for the jar file which contains
28: * the specified class.
29: * @return The Manifest file for the jar file which contains the specified class,
30: * or <code>null</code> if the code is not in a jar file.
31: */
32: public static Manifest getManifest(Class clazz) {
33: try {
34: final URL codeBase = clazz.getProtectionDomain()
35: .getCodeSource().getLocation();
36: if (codeBase.getPath().endsWith(".jar")) { //$NON-NLS-1$
37: final JarInputStream jin = new JarInputStream(codeBase
38: .openStream());
39: return jin.getManifest();
40: }
41: } catch (Exception e) {
42: // TODO handle this exception
43: }
44: return null;
45: }
46: }
|