tags:

views:

40

answers:

2

What would be the best way to access Manifest properties (from /META-INF/Manifest.mf) in a pure JSF2.0 application?

A: 

The following seems to work:

Create a ManagedBean:

@ManagedBean(name = "cic")
@ApplicationScoped
public class ConfigurationInformationController {
    private static final Logger logger = LoggerFactory.getLogger(ConfigurationInformationController.class.getName());
    private Manifest manifest = null;
    public Manifest getManifest() {
        return manifest;
    }
    @PostConstruct
    public void init() {
        File manifestFile = null;
        try {
            String home = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
            manifestFile = new File(home, "META-INF/MANIFEST.MF");
            manifest = new Manifest();
            manifest.read(new FileInputStream(manifestFile));
        } catch (IOException ioe) {
            logger.error("Unable to read the Manifest file from '"+manifestFile.getAbsolutePath()+"'",ioe);
        }
    }
}

In the Facelets page, call the properties from there...

    <h:outputLabel for="Build-BuildTimestamp" value="Build Timestamp:"/>
    <h:outputText id="Build-BuildTimestamp" value="#{cic.manifest.mainAttributes.getValue('Build-BuildTimestamp')}"/>
Jan
Happy that it works. Though, the roundtrip across the File object feels a bit fragile. Any better suggestions?
Jan
You cannot count on the MANIFEST.MF file being unpacked. It should be reachable through the classpath as a resource.
Thorbjørn Ravn Andersen
@Jan - use `ExternalContext.getResource*` instead of using `File`. http://download.oracle.com/javaee/5/api/javax/faces/context/ExternalContext.html @Thorbjørn Ravn Andersen - if you use `ClassLoader.getResource(...` which manifest will you get first? There may be manifests in every JAR file in WEB-INF/lib and the app server libraries.
McDowell
Interesting, I didn't realize that META-INF is on the classpath.
Jan
+1  A: 

Thx to McDowell's suggestion above, the improved init() becomes:

@PostConstruct
public void init() {
    try {
        InputStream is = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/META-INF/MANIFEST.MF");
        manifest = new Manifest();
        manifest.read(is);
    } catch (IOException ioe) {
        logger.error("Unable to read the Manifest file from classpath.", ioe);
    }
}
Jan