001: package dalma.container;
002:
003: import java.io.File;
004: import java.io.BufferedInputStream;
005: import java.io.InputStream;
006: import java.io.FileInputStream;
007: import java.io.IOException;
008: import java.util.jar.Manifest;
009: import java.util.jar.Attributes;
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012:
013: /**
014: * Represents a library module made available to all workflow applications.
015: *
016: * @author Kohsuke Kawaguchi
017: */
018: public final class Module {
019: /**
020: * Container that owns this module.
021: */
022: public final Container owner;
023:
024: /**
025: * Home directory of this module.
026: *
027: * <tt>$DALMA_HOME/modules/xxx</tt>.
028: */
029: public final File dir;
030:
031: /**
032: * Module manifest.
033: */
034: public final Manifest manifest;
035:
036: public Module(Container owner, File dir) {
037: this .owner = owner;
038: this .dir = dir;
039:
040: this .manifest = new Manifest();
041:
042: File manifestFile = new File(dir, "META-INF/MANIFEST.MF");
043: if (manifestFile.exists()) {
044: // read the manifest file
045:
046: try {
047: InputStream is = new BufferedInputStream(
048: new FileInputStream(manifestFile));
049: try {
050: manifest.read(is);
051: } finally {
052: is.close();
053: }
054: } catch (IOException e) {
055: logger.log(Level.WARNING,
056: "Failed to read manifest file " + manifestFile,
057: e);
058: // this isn't considered a fatal error
059: }
060: }
061: }
062:
063: /**
064: * Gets the title of this module.
065: *
066: * <p>
067: * The <tt>Specification-Title</tt> (or if it's absent, <tt>Implementation-Title</tt>)
068: * of the manifest is used.
069: *
070: * @return
071: * null if this information is not available.
072: */
073: public String getTitle() {
074: Attributes atts = manifest.getMainAttributes();
075: String value;
076:
077: value = atts.getValue(Attributes.Name.SPECIFICATION_TITLE);
078: if (value != null)
079: return value;
080:
081: value = atts.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
082: if (value != null)
083: return value;
084:
085: return null;
086: }
087:
088: /**
089: * Returns the module name.
090: *
091: * <p>
092: * Module name uniquely identifies a {@link Module}. This is the <tt>xxx</tt>
093: * portion of <tt>$DALMA_HOME/modules/xxx</tt>
094: *
095: * @return
096: * always non-null.
097: */
098: public String getName() {
099: return dir.getName();
100: }
101:
102: private static final Logger logger = Logger.getLogger(Module.class
103: .getName());
104: }
|