Example usage for java.util.jar Manifest getMainAttributes

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

Introduction

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

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

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

public Version(ServletContext servletContext) throws IOException {
    InputStream is = null;//from  w  w w  .  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:com.github.dirkraft.jash.ShExecutableJarBundler.java

@Override
public List<String> validateAndInit() throws Exception {
    List<String> errors = super.validateAndInit();

    if (!jarFile.isFile()) {
        errors.add(jarFile + " is not a file.");
        return errors;
    }//from   w w  w .j  ava 2  s .  c o m

    JarFile jar = new JarFile(jarFile);
    Manifest manifest = jar.getManifest();
    String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
    ClassLoader jarClassLoader = URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() });
    try {
        ClassUtils.getClass(jarClassLoader, mainClass, false);
    } catch (ClassNotFoundException e) {
        errors.add("Could not find Main-Class: " + mainClass + " in jar " + jarFile.getAbsolutePath());
        return errors;
    }

    _targetBinary = outputDir.toPath().resolve(commandName).toFile();
    if (_targetBinary.exists()) {
        errors.add("Target binary path " + _targetBinary.getAbsolutePath()
                + " is occupied. Will not overwrite, so aborting.");
    }

    return errors;
}

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;/* w w w.j  a  v a2  s .  c om*/
    }

    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:org.eclipse.licensing.eclipse_licensing_plugin.InjectLicensingMojo.java

private void updateManifest() throws IOException {
    File manifestFile = new File(project.getBasedir(), "META-INF/MANIFEST.MF");
    Manifest manifest = new Manifest(new FileInputStream(manifestFile));
    String[] requiredBundles = manifest.getMainAttributes().getValue("Require-Bundle").split(",");
    List<String> requiredList = new ArrayList<String>(Arrays.asList(requiredBundles));
    requiredList.remove("org.eclipse.licensing.base");
    requiredList.add("org.apache.commons.codec");
    requiredList.add("org.eclipse.osgi");
    String newRequiredBundles = StringUtils.join(requiredList.toArray(), ",");
    manifest.getMainAttributes().putValue("Require-Bundle", newRequiredBundles);
    manifest.write(new FileOutputStream(manifestFile));
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.TestContainerLaunch.java

private static List<String> getJarManifestClasspath(String path) throws Exception {
    List<String> classpath = new ArrayList<String>();
    JarFile jarFile = new JarFile(path);
    Manifest manifest = jarFile.getManifest();
    String cps = manifest.getMainAttributes().getValue("Class-Path");
    StringTokenizer cptok = new StringTokenizer(cps);
    while (cptok.hasMoreTokens()) {
        String cpentry = cptok.nextToken();
        classpath.add(cpentry);/*ww  w .j av  a  2s . c  om*/
    }
    return classpath;
}

From source file:org.pentaho.webpackage.deployer.archive.impl.WebPackageURLConnectionTest.java

private void verifyManifest(Manifest manifest, String moduleName, String moduleVersion) {
    assertTrue(manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME)
            .startsWith("pentaho-webpackage-"));
    assertTrue(manifest.getMainAttributes().getValue(Constants.PROVIDE_CAPABILITY)
            .startsWith(PentahoWebPackageService.CAPABILITY_NAMESPACE + ";name=\"" + moduleName
                    + "\";version:Version=\"" + moduleVersion + "\";root=\"/pwp-"));
}

From source file:com.ikon.util.cl.FilesystemClassLoader.java

/**
 * Get main class name//  w  w  w  .ja v a2s .c  o  m
 */
@Override
public String getMainClassName() throws IOException {
    log.debug("getMainClassName()");
    File mf = new File(file, "META-INF/MANIFEST.MF");
    FileInputStream fis = null;

    try {
        if (mf.exists() && mf.canRead()) {
            fis = new FileInputStream(mf);
            Manifest manif = new Manifest(fis);
            Attributes attr = manif.getMainAttributes();
            return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
        }
    } finally {
        IOUtils.closeQuietly(fis);
    }

    return null;
}

From source file:org.owasp.webgoat.application.WebGoatServletListener.java

private void setApplicationVariables(ServletContext context) {
    Application app = Application.getInstance();
    try {//from   w ww. j a  v  a2s .  c o  m
        InputStream inputStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(inputStream);
        Attributes attr = manifest.getMainAttributes();
        String name = attr.getValue("Specification-Title");
        String version = attr.getValue("Specification-Version");
        String build = attr.getValue("Implementation-Version");
        app.setName(name);
        app.setVersion(version);
        app.setBuild(build);
    } catch (IOException ioe) {
        context.log("Error setting application variables", ioe);
    }
}

From source file:org.wso2.carbon.server.extensions.Log4jPropFileFragmentBundleCreator.java

public void perform() {

    //Get the log4j.properties file path.
    //Calculate the target fragment bundle file name with required manifest headers.
    //org.wso2.carbon.logging.propfile_1.0.0.jar

    try {//from ww w.j  av a2s . com

        Manifest mf = new Manifest();
        Attributes attribs = mf.getMainAttributes();
        attribs.putValue(LauncherConstants.MANIFEST_VERSION, "1.0");
        attribs.putValue(LauncherConstants.BUNDLE_MANIFEST_VERSION, "2");
        attribs.putValue(LauncherConstants.BUNDLE_NAME, FRAGMENT_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_SYMBOLIC_NAME, FRAGMENT_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_VERSION, FRAGMENT_BUNDLE_VERSION);
        attribs.putValue(LauncherConstants.FRAGMENT_HOST, FRAGMENT_HOST_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_CLASSPATH, ".");

        File confFolder = new File(Utils.getCarbonComponentRepo(), "../conf");
        String loggingPropFilePath = confFolder.getAbsolutePath() + File.separator + LOG4J_PROP_FILE_NAME;

        File dropinsFolder = new File(Utils.getCarbonComponentRepo(), "dropins");
        String targetFilePath = dropinsFolder.getAbsolutePath() + File.separator + FRAGMENT_BUNDLE_NAME + "_"
                + FRAGMENT_BUNDLE_VERSION + ".jar";

        String tempDirPath = Utils.JAR_TO_BUNDLE_DIR + File.separator + System.currentTimeMillis()
                + Math.random();

        FileOutputStream mfos = null;
        try {

            Utils.copyFileToDir(new File(loggingPropFilePath), new File(tempDirPath));
            String metaInfPath = tempDirPath + File.separator + "META-INF";
            if (!new File(metaInfPath).mkdirs()) {
                throw new IOException("Failed to create the directory: " + metaInfPath);
            }
            mfos = new FileOutputStream(metaInfPath + File.separator + "MANIFEST.MF");
            mf.write(mfos);

            Utils.archiveDir(targetFilePath, tempDirPath);
            Utils.deleteDir(new File(tempDirPath));
        } finally {
            try {
                if (mfos != null) {
                    mfos.close();
                }
            } catch (IOException e) {
                log.error("Unable to close the OutputStream " + e.getMessage(), e);
            }
        }

    } catch (IOException e) {
        log.error("Error occured while creating the log4j prop fragment bundle.", e);
    }

    //Utils.createBundle(log4jPropFile, targetFragmentBundle, mf);
    //To change body of implemented methods use File | Settings | File Templates.
}

From source file:org.nosphere.honker.deptree.DepTreeManifestLoader.java

private DepTreeData.Manifest loadData(java.util.jar.Manifest mf) {
    Attributes attr = mf.getMainAttributes();
    String name = attr.getValue("Bundle-Name");
    if (name == null) {
        name = attr.getValue("Implementation-Title");
    }/*  w  w  w. j a  v  a2 s.c  o  m*/
    if (name == null) {
        name = attr.getValue("Bundle-SymbolicName");
    }
    String version = attr.getValue("Bundle-Version");
    if (version == null) {
        version = attr.getValue("Implementation-Version");
    }
    if (version == null) {
        version = attr.getValue("Specification-Version");
    }
    String vendor = attr.getValue("Bundle-Vendor");
    if (vendor == null) {
        vendor = attr.getValue("Implementation-Vendor");
    }
    String url = attr.getValue("Bundle-DocURL");
    String license = attr.getValue("Bundle-License");
    if (license != null && license.contains(";")) {
        String[] licenseItems = license.split(";");
        for (String licenseItem : licenseItems) {
            String trimmed = licenseItem.trim();
            if (!trimmed.contains("=")) {
                license = trimmed;
                break;
            }
            if (trimmed.startsWith("link=")) {
                trimmed = trimmed.substring(5, trimmed.length());
                if (trimmed.startsWith("\"") || trimmed.startsWith("'")) {
                    trimmed = trimmed.substring(1, trimmed.length() - 1);
                }
                license = trimmed;
                break;
            }
        }
    }
    return new DepTreeData.Manifest(name, version, vendor, url, license);
}