views:

32

answers:

2

I have a simple service called BuildNumberService which would be instantiated by spring.

I'm trying to find the cleanest code for the class to find out the MANIFEST.MF file from the jar file it has been packaged into.

The code has to run inside a servlet container.

@Service
public class BuildNumberService {

    private static final String IMPLEMENTATION_BUILD = "Implementation-Build";

    private String version = null;

    public BuildNumberService() {

      // find correct manifest.mf
      // ?????


      // then read IMPLEMENTATION_BUILD attributes and caches it.
       Attributes mainAttributes = mf.getMainAttributes();
       version = mainAttributes.getValue(IMPLEMENTATION_BUILD);
    }

    public String getVersion() {
        return this.version;
    }
}

How would you do that ?

Edit: Actually, what I'm trying to do, is, find a resource by name which sits in the same package as the actual class.

+1  A: 

I guess you can see the answer to that question.

Riduidel
Yup, I guess that's simpler than my solution :-)
seanizer
Hi, I've already looked into that question. I don't like the accepted answer (using new JarInputStream(stream);) since the problem is actually how to find the stream.
David
A: 

Well if you know the jar is on the file system (e.g. not inside a war) and you also know that the security manager gives you the permission to access a class' protection domain, you can do it like this:

public static InputStream findManifest(final Class<?> clazz) throws IOException,
    URISyntaxException{
    final URL jarUrl =
        clazz.getProtectionDomain().getCodeSource().getLocation();
    final JarFile jf = new JarFile(new File(jarUrl.toURI()));
    final ZipEntry entry = jf.getEntry("META-INF/MANIFEST.MF");
    return entry == null ? null : jf.getInputStream(entry);
}
seanizer
Psshh.. [`JarFile#getManifest()`](http://download.oracle.com/javase/6/docs/api/java/util/jar/JarFile.html#getManifest%28%29).
BalusC
@BalusC that would be too easy. Let me do the leg work myself :-) (To be honest, didn't know about that)
seanizer
Is that the only way for a class to find the jar in which it is packaged ? It seems that the jdk makes it difficult to read these metadata at runtime. As if it was not supposed to be done.
David
*not supposed to be done*. I'd rephrase that to: only supposed to be done by folks who really know what they are doing (e.g. frameworks that have component scanning like hibernate, spring, seam etc. must use functionality like this). But yes, I'd guess it's the only way
seanizer