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:ch.ivyteam.ivy.maven.util.ClasspathJar.java

public String getClasspathUrlEntries() {
    try (ZipInputStream is = new ZipInputStream(new FileInputStream(jar))) {
        Manifest manifest = new Manifest(getInputStream(is, MANIFEST_MF));
        return manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    } catch (IOException ex) {
        return null;
    }/*from   w  ww  .  j ava 2 s. c o  m*/
}

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

public void perform() {
    File[] files = getBundleConfigs();
    if (files.length > 0) {
        for (File file : files) {
            String fragmentHostBundleName = getFragmentHostBundleName(file);
            String fragmentBundleName = getFragmentBundleName(file);

            try {
                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, fragmentBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_SYMBOLIC_NAME, fragmentBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_VERSION, FRAGMENT_BUNDLE_VERSION);
                attribs.putValue(LauncherConstants.FRAGMENT_HOST, fragmentHostBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_CLASSPATH, ".");

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

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

                FileOutputStream mfos = null;
                try {
                    if (file.isDirectory()) {
                        FileUtils.copyDirectory(file, new File(tempDirPath));
                    } else { // is a single file..
                        Utils.copyFileToDir(file, new File(tempDirPath));
                    }/*from w w  w.  j av a  2  s  .c  o  m*/
                    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);
            }
        }
    }
}

From source file:org.apache.servicemix.examples.CXFNMRIntegrationTest.java

protected Manifest getManifest() {
    Manifest mf = super.getManifest();
    String importP = mf.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);
    mf.getMainAttributes().putValue(Constants.IMPORT_PACKAGE,
            importP + ",META-INF.cxf, org.apache.servicemix.jbi.jaxp");
    String exportP = mf.getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
    mf.getMainAttributes().putValue(Constants.EXPORT_PACKAGE,
            exportP + ",org.apache.handlers, " + "org.apache.springcfg.handlers, "
                    + "org.apache.handlers.types,org.apache.servicemix.examples.cxf,"
                    + "org.apache.servicemix.examples.cxf.soaphandler,"
                    + "org.apache.servicemix.examples.cxf.springcfghandler,"
                    + "org.apache.servicemix.examples.cxf.wsaddressing," + "org.apache.hello_world_soap_http,"
                    + "org.apache.cxf," + "org.apache.cxf.bus," + "org.apache.cxf.interceptor");
    return mf;/* w  w w. java2 s  .c o  m*/
}

From source file:ar.com.zauber.commons.web.version.impl.ManifestVersionProvider.java

/**
 * Creates the ManifestVersionProvider./*ww w.  ja  va2  s . co m*/
 *
 * @param manifest manifest en cuestion
 * @param attrName nombre del atributo a devolver
 */
public ManifestVersionProvider(final Manifest manifest, final String attrName) {
    Validate.notNull(manifest);
    Validate.isTrue(!StringUtils.isEmpty(attrName));

    final String v = manifest.getMainAttributes().getValue(attrName);
    this.version = v == null ? "" : v;
}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginOSGiManifest.java

private void updateManifest(String symbolicName, String classPath, String bundleActivator) throws IOException {
    try (FileInputStream manifestInputStream = new FileInputStream(manifestLocation)) {
        Manifest manifest = new Manifest(manifestInputStream);
        Attributes mainAttributes = manifest.getMainAttributes();

        if (mainAttributes.containsKey(new Attributes.Name(BUNDLE_SYMBOLICNAME))) {
            descriptor.markAsInvalid(Arrays.asList(
                    "Plugin JAR is invalid. MANIFEST.MF already contains header: " + BUNDLE_SYMBOLICNAME),
                    null);/*from  w ww .ja  va  2  s .  com*/
            return;
        }
        mainAttributes.put(new Attributes.Name(BUNDLE_SYMBOLICNAME), symbolicName);
        mainAttributes.put(new Attributes.Name(BUNDLE_CLASSPATH), classPath);
        mainAttributes.put(new Attributes.Name(BUNDLE_ACTIVATOR), bundleActivator);

        descriptor.updateBundleInformation(symbolicName, classPath, bundleActivator);

        try (FileOutputStream manifestOutputStream = new FileOutputStream(manifestLocation)) {
            manifest.write(manifestOutputStream);
        }
    }
}

From source file:org.onehippo.repository.bootstrap.Extension.java

String getModuleVersion() {
    String extensionURLString = extensionURL.toString();
    if (extensionURLString.contains(EXTENSION_FILE_NAME)) {
        String manifestUrlString = StringUtils.substringBefore(extensionURLString, EXTENSION_FILE_NAME)
                + "META-INF/MANIFEST.MF";
        try {/*from   www .  j a  va2  s  .  c om*/
            final Manifest manifest = new Manifest(new URL(manifestUrlString).openStream());
            return manifest.getMainAttributes().getValue(new Attributes.Name("Implementation-Build"));
        } catch (IOException ignore) {
        }
    }
    return null;
}

From source file:com.opensymphony.xwork2.util.fs.JarEntryRevisionTest.java

private String createJarFile(long time) throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    Path jarPath = Paths//  w  w  w .j av a2  s  .c o  m
            .get(Thread.currentThread().getContextClassLoader().getResource("xwork-jar.jar").toURI())
            .getParent();
    File jarFile = jarPath.resolve("JarEntryRevisionTest_testNeedsReloading.jar").toFile();
    FileOutputStream fos = new FileOutputStream(jarFile, false);
    JarOutputStream target = new JarOutputStream(fos, manifest);
    target.putNextEntry(new ZipEntry("com/opensymphony/xwork2/util/fs/"));
    ZipEntry entry = new ZipEntry("com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    entry.setTime(time);
    target.putNextEntry(entry);
    InputStream source = getClass()
            .getResourceAsStream("/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    IOUtils.copy(source, target);
    source.close();
    target.closeEntry();
    target.close();
    fos.close();

    return jarFile.toURI().toURL().toExternalForm();
}

From source file:de.metanome.backend.algorithm_loading.AlgorithmFinder.java

/**
 * Finds out which subclass of Algorithm is implemented by the source code in the
 * algorithmJarFile./*from  w w  w .  ja  v a2  s .co m*/
 *
 * @param algorithmJarFile the algorithm's jar file
 * @return the interfaces of the algorithm implementation in algorithmJarFile
 * @throws java.io.IOException if the algorithm jar file could not be opened
 * @throws java.lang.ClassNotFoundException if the algorithm contains a not supported interface
 */
public Set<Class<?>> getAlgorithmInterfaces(File algorithmJarFile) throws IOException, ClassNotFoundException {
    JarFile jar = new JarFile(algorithmJarFile);

    Manifest man = jar.getManifest();
    Attributes attr = man.getMainAttributes();
    String className = attr.getValue(bootstrapClassTagName);

    URL[] url = { algorithmJarFile.toURI().toURL() };
    ClassLoader loader = new URLClassLoader(url, Algorithm.class.getClassLoader());

    Class<?> algorithmClass;
    try {
        algorithmClass = Class.forName(className, false, loader);
    } catch (ClassNotFoundException e) {
        System.out.println("Could not find class " + className);
        return new HashSet<>();
    } finally {
        jar.close();
    }

    return new HashSet<>(ClassUtils.getAllInterfaces(algorithmClass));
}

From source file:de.romankreisel.faktotum.FaktotumContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    try (InputStream is = sce.getServletContext()
            .getResourceAsStream(FaktotumContextListener.RELATIVE_MANIFEST_PATH)) {
        Manifest manifest = new Manifest(is);
        Attributes attributes = manifest.getMainAttributes();
        String buildTimeString = attributes.getValue("Build-Time");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(buildTimeString)) {
            FaktotumContextListener.buildTime = simpleDateFormat.parse(buildTimeString);
        }/*from  w w  w .ja v  a 2 s.c om*/
        FaktotumContextListener.version = attributes.getValue("Implementation-Version");
        this.getLogger().info("Faktotum initialized");
        this.getLogger().info("Version: " + FaktotumContextListener.getVersion());
        this.getLogger().info("Build-Time: " + simpleDateFormat.format(FaktotumContextListener.getBuildTime()));

        this.getLogger().fine("Removing old sessions...");
        this.authenticationBean.removeOldSessions();
    } catch (IOException e) {
        this.getLogger().log(Level.WARNING, "Error reading build time from manifest", e);
    } catch (ParseException e) {
        this.getLogger().log(Level.WARNING, "Error parsing build time from manifest", e);
    } catch (Exception e) {
        this.getLogger().log(Level.SEVERE, "Error reading build time", e);
        throw e;
    }
}

From source file:org.sonar.batch.internal.PluginsManager.java

public void start() throws Exception {
    LOG.info("Starting Sonar Plugins Manager (workDir: {})", workDir);

    collection = new ClassLoadersCollection(parentClassLoader);

    for (Map.Entry<File, Manifest> entry : manifests.entrySet()) {
        File file = entry.getKey();
        Manifest manifest = entry.getValue();

        Attributes attributes = manifest.getMainAttributes();
        String childFirst = attributes.getValue("Plugin-ChildFirstClassLoader");
        String pluginKey = attributes.getValue("Plugin-Key");
        String pluginClass = attributes.getValue("Plugin-Class");
        String pluginDependencies = StringUtils.defaultString(attributes.getValue("Plugin-Dependencies"));

        File pluginDir = file.getParentFile().getParentFile();

        Collection<URL> urls = new ArrayList<URL>();
        urls.add(pluginDir.toURL());/*from   w  ww. j  av  a 2s  . c om*/
        String[] deps = StringUtils.split(pluginDependencies);
        for (String dep : deps) {
            File depFile = new File(pluginDir, dep);
            urls.add(depFile.toURL());
        }
        LOG.debug("ClassPath for plugin {} : {}", pluginKey, urls);

        collection.createClassLoader(pluginKey, urls, "true".equals(childFirst));

        plugins.put(pluginKey, pluginClass);
    }

    collection.done();
}