Example usage for java.util.jar Manifest Manifest

List of usage examples for java.util.jar Manifest Manifest

Introduction

In this page you can find the example usage for java.util.jar Manifest Manifest.

Prototype

public Manifest(Manifest man) 

Source Link

Document

Constructs a new Manifest that is a copy of the specified Manifest.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a manifest from a file
    InputStream fis = new FileInputStream("manifestfile");
    Manifest manifest = new Manifest(fis);

    // Construct a string version of a manifest
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("Manifest-Version: 1.0\n");
    sbuf.append("\n");
    sbuf.append("Name: javax/swing/JScrollPane.class\n");
    sbuf.append("Java-Bean: True\n");

    // Convert the string to a input stream
    InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));

    // Create the manifest
    manifest = new Manifest(is);
}

From source file:org.northrop.leanne.publisher.Main.java

public static void main(String[] args) {
    CommandLineParser parser = new GnuParser();
    try {//from w w w  .  j a v  a 2s .co m
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (args.length == 0 || line.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("pub", options);
        } else if (line.hasOption("version")) {
            URLClassLoader cl = (URLClassLoader) Main.class.getClassLoader();
            String title = "";
            String version = "";
            String date = "";
            try {
                URL url = cl.findResource("META-INF/MANIFEST.MF");
                Manifest manifest = new Manifest(url.openStream());
                Attributes attr = manifest.getMainAttributes();
                title = attr.getValue("Implementation-Title");
                version = attr.getValue("Implementation-Version");
                date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM)
                        .format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(attr.getValue("Built-Date")));
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("------------------------------------------------------------");
            System.out.println("Publisher Pipeline " + version);
            System.out.println("------------------------------------------------------------");
            System.out.println("");
            System.out.println(title + " build time " + date);
            System.out.println("Java: " + System.getProperty("java.version"));
            System.out.println("JVM:  " + System.getProperty("java.vendor"));
            System.out.println("OS:   " + System.getProperty("os.name") + " " + System.getProperty("os.version")
                    + " " + System.getProperty("os.arch"));
        } else {
            Option[] options = line.getOptions();
            Binding binding = new Binding();
            binding.setVariable("home", System.getProperty("pub.home"));
            binding.setVariable("inputDir", System.getProperty("pub.home") + "/input");
            binding.setVariable("outputDir", System.getProperty("pub.home") + "/output");
            binding.setVariable("appName", System.getProperty("program.name"));
            binding.setVariable("isdebug", false);

            for (int i = 0; i < options.length; i++) {
                Option opt = options[i];
                binding.setVariable("is" + opt.getOpt(), true);
            }

            String[] roots = new String[] { System.getProperty("pub.home") + "/resources/scripts",
                    System.getProperty("pub.home") + "/resources/scripts/formats" };
            ClassLoader parent = Main.class.getClassLoader();
            GroovyScriptEngine gse = new GroovyScriptEngine(roots, parent);

            if (!line.hasOption("rerun")) {
                gse.run("prep.groovy", binding);
            }

            for (String name : getFormats()) {
                if (line.hasOption(name.toLowerCase())) {
                    String file = ("" + name.charAt(0)).toLowerCase() + name.substring(1) + ".groovy";
                    gse.run(file, binding);
                }
            }
        }
    } catch (ParseException exp) {
        System.err.println("Command line parsing failed.  Reason: " + exp.getMessage());
    } catch (ResourceException resourceError) {
        System.err.println("Groovy script failed.  Reason: " + resourceError.getMessage());
    } catch (IOException ioError) {
        System.err.println("Groovy script failed.  Reason: " + ioError.getMessage());
    } catch (ScriptException error) {
        System.err.println("Groovy script failed.  Reason: " + error.getMessage());
        error.printStackTrace();
    }
}

From source file:edu.cornell.med.icb.util.VersionUtils.java

/**
 * Gets the Implementation-Version attribute from the manifest of the jar file a class
 * is loaded from.//from  w w w. j  av a 2s.  c  o  m
 * @param clazz The class to get the version for
 * @return The value of the Implementation-Version attribute or "UNKNOWN" if the
 * jar file cannot be read.
 */
public static String getImplementationVersion(final Class<?> clazz) {
    String version;
    try {
        final String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
        final URL manifestUrl = new URL("jar:" + classContainer + "!/" + JarFile.MANIFEST_NAME);
        final Manifest manifest = new Manifest(manifestUrl.openStream());
        final Attributes attributes = manifest.getMainAttributes();
        version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    } catch (Exception e) {
        // pretty much any error here is ok since we may not even have a jar to read from
        version = "UNKNOWN";
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(Attributes.Name.IMPLEMENTATION_VERSION + ": " + version);
    }
    return StringUtils.defaultString(version);
}

From source file:com.arrow.acs.ManifestUtils.java

public static Manifest readManifest(Class<?> clazz) {
    String method = "readManifest";
    String jarFile = null;/*from  ww w  .ja v  a  2 s . c  o  m*/
    String path = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
    for (String token : path.split("/")) {
        token = token.replace("!", "").toLowerCase().trim();
        if (token.endsWith(".jar")) {
            jarFile = token;
            break;
        }
    }
    LOGGER.logInfo(method, "className: %s, path: %s, jarFile: %s", clazz.getName(), path, jarFile);
    InputStream is = null;
    try {
        if (!StringUtils.isEmpty(jarFile)) {
            Enumeration<URL> enumeration = Thread.currentThread().getContextClassLoader()
                    .getResources(JarFile.MANIFEST_NAME);
            while (enumeration.hasMoreElements()) {
                URL url = enumeration.nextElement();
                for (String token : url.toString().split("/")) {
                    token = token.replace("!", "").toLowerCase();
                    if (token.equals(jarFile)) {
                        LOGGER.logInfo(method, "loading manifest from: %s", url.toString());
                        return new Manifest(is = url.openStream());
                    }
                }
            }
        } else {
            URL url = new URL(path + "/META-INF/MANIFEST.MF");
            LOGGER.logInfo(method, "loading manifest from: %s", url.toString());
            return new Manifest(is = url.openStream());
        }
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(is);
    }
    LOGGER.logError(method, "manifest file not found for: %s", clazz.getName());
    return null;
}

From source file:org.jboss.windup.metadata.type.ManifestMetadata.java

public Manifest getManifest() {
    if (manifest == null) {
        // create it.
        try {/*w  w w  . ja va  2  s  . c  o  m*/
            if (LOG.isDebugEnabled()) {
                LOG.debug("Reading manifest: " + filePointer.getAbsolutePath());
            }
            Manifest mTmp = new Manifest(new FileInputStream(filePointer));
            manifest = new SoftReference<Manifest>(mTmp);
            LOG.debug("Reading manifest complete.");
        } catch (Exception e) {
            LOG.error("Bad Manifest? " + filePointer.getAbsolutePath());
            LOG.info("Skipping file. Continuing Windup Processing...");

            Summary sr = new Summary();
            sr.setDescription("Bad Manifest? Unable to parse.");
            sr.setLevel(NotificationLevel.CRITICAL);
            sr.setEffort(new UnknownEffort());
            this.getDecorations().add(sr);
            this.manifest = null;
        }
    }
    if (manifest == null) {
        return null;
    }
    return manifest.get();
}

From source file:com.jaeksoft.searchlib.web.Version.java

public Version(ServletContext servletContext) throws IOException {
    InputStream is = null;/*from   w ww.  j  av a 2  s .c o m*/
    try {
        is = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
        if (is != null) {
            Manifest manifest = new Manifest(is);
            Attributes attributes = manifest.getMainAttributes();
            title = attributes.getValue("Implementation-Title");
            version = attributes.getValue("Implementation-Version");
            build = attributes.getValue("Implementation-Build");
        } else {
            title = null;
            version = null;
            build = null;
        }
        updateUrl = toUpdateUrl();
        versionString = toVersionString();
    } finally {
        if (is != null)
            is.close();
    }
}

From source file:org.energyos.espi.datacustodian.web.VersionRESTController.java

/**
 * Handling GET request to retrieve details from MANIFEST.MF file
 * /*from w  ww.ja  v a 2 s . co  m*/
 * @return implementation details
 */
@RequestMapping(value = "/about-version", method = RequestMethod.GET)
public String getBuildNumber(HttpServletRequest request, ModelMap model) throws IOException {

    ServletContext context = request.getSession().getServletContext();
    InputStream manifestStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");
    Manifest manifest = new Manifest(manifestStream);

    Map<String, String> aboutInfo = new HashMap<>();
    aboutInfo.put("Implementation-Vendor", manifest.getMainAttributes().getValue("Implementation-Vendor"));
    aboutInfo.put("Implementation-Title", manifest.getMainAttributes().getValue("Implementation-Title"));
    aboutInfo.put("Implementation-Version", manifest.getMainAttributes().getValue("Implementation-Version"));
    aboutInfo.put("Implementation-Jdk", manifest.getMainAttributes().getValue("Build-Jdk"));
    aboutInfo.put("Implementation-Build", manifest.getMainAttributes().getValue("Implementation-Build"));
    aboutInfo.put("Implementation-Build-Time",
            manifest.getMainAttributes().getValue("Implementation-Build-Time"));

    model.put("aboutInfo", aboutInfo);
    return "/about";

}

From source file:jp.gr.java_conf.schkit.utils.ApplicationInfo.java

private void loadManifest() throws Exception {
    if (classLoader == null)
        return;/* w  ww.  j a  v  a 2s.  c  o  m*/

    URL resource = classLoader.findResource(MANIFEST_PATH);
    if (resource == null)
        return;

    Manifest manifest = new Manifest(resource.openStream());

    Attributes attrs = manifest.getMainAttributes();

    String value;
    value = attrs.getValue("Implementation-Title");
    if (value != null)
        name = value;
    value = attrs.getValue("Implementation-Version");
    if (value != null)
        version = value;
    value = attrs.getValue("Create-By");
    if (value != null)
        author = value;
    value = attrs.getValue("License-By");
    if (value != null)
        license = value;

    if (classLotation != null) {
        Date date = new Date(classLotation.openConnection().getLastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        modified = sdf.format(date);
    }
}

From source file:com.vmware.vfabric.hyperic.plugin.vfws.VfwsCollector.java

private void parse(HttpResponse response) throws IOException {
    InputStream is = response.getEntity().getContent();

    if (is == null) {
        log.error("Unable to retrieve results. InputStream is null");
        return;/*from w  ww . j a v  a2 s  . c o  m*/
    }

    Manifest manifest = new Manifest(is);
    Attributes attributes = manifest.getMainAttributes();
    if (null == attributes) {
        log.error("Unable to parse results. No attributes found");
        return;
    }

    Iterator it = attributes.keySet().iterator();
    while (it.hasNext()) {
        Name key = (Name) it.next();
        if (key == null) {
            log.debug("Skipping null key");
            continue;
        }
        Object value = attributes.get(key);
        if (value.getClass() != String.class) {
            log.error("Attribute value not of class String");
            continue;
        }
        String keyName = key.toString();
        String val = (String) value;
        // BusyWorkers and IdleWorkers have u in the values
        if (keyName.contains("Workers")) {
            setWorkers(keyName, val);
        } else if (keyName.contains("StartTime")) {
            setStartTime(keyName, val);
        } else {
            setValue(keyName, val);
        }
    }
}

From source file:fm.last.citrine.web.AdminController.java

@Override
protected void initServletContext(ServletContext servletContext) {
    super.initServletContext(servletContext);
    try {//from w w  w. j a va 2  s  . c  o  m
        InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(inputStream);
        Attributes attributes = manifest.getMainAttributes();
        buildVersion = attributes.getValue("Build-Version");
        buildDateTime = attributes.getValue("Build-DateTime");
        log.info("Citrine Build-Version: " + attributes.getValue("Build-Version"));
        log.info("Citrine Build-DateTime: " + attributes.getValue("Build-DateTime"));
    } catch (Exception e) {
        log.error("Error determining build version", e);
    }
}