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.theoryinpractise.clojure.AbstractClojureCompilerMojo.java

private File createJar(final String cp, final String mainClass) {
    try {//from w w  w.  ja va 2s . com
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, cp);
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
        File tempFile = File.createTempFile("clojuremavenplugin", "jar");
        tempFile.deleteOnExit();
        JarOutputStream target = new JarOutputStream(new FileOutputStream(tempFile), manifest);
        target.close();
        return tempFile;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.taobao.android.apatch.ApkPatch.java

@SuppressWarnings("deprecation")
protected Manifest getMeta() {
    Manifest manifest = new Manifest();
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("Created-By", "1.0 (ApkPatch)");
    main.putValue("Created-Time", new Date(System.currentTimeMillis()).toGMTString());
    main.putValue("From-File", baseFiles.get(0).getName());
    main.putValue("To-File", newFiles.get(0).getName());
    main.putValue("Patch-Name", name);
    main.putValue(name + "-Patch-Classes", Formater.dotStringList(classes));
    main.putValue(name + "-Prepare-Classes", Formater.dotStringList(prepareClasses));
    main.putValue(name + "-Used-Methods", Formater.dotStringList(usedMethods));
    main.putValue(name + "-Modified-Classes", Formater.dotStringList(modifiedClasses));
    main.putValue(name + "-Used-Classes", Formater.dotStringList(usedClasses));
    main.putValue(name + "-add-classes", Formater.dotStringList(addClasses));
    return manifest;
}

From source file:hudson.PluginWrapper.java

private String computeShortName(Manifest manifest, File archive) {
    // use the name captured in the manifest, as often plugins
    // depend on the specific short name in its URLs.
    String n = manifest.getMainAttributes().getValue("Short-Name");
    if (n != null)
        return n;

    // maven seems to put this automatically, so good fallback to check.
    n = manifest.getMainAttributes().getValue("Extension-Name");
    if (n != null)
        return n;

    // otherwise infer from the file name, since older plugins don't have
    // this entry.
    return getBaseName(archive);
}

From source file:co.cask.cdap.internal.app.runtime.spark.SparkRuntimeService.java

/**
 * Updates the dependency jar packaged by the {@link ApplicationBundler#createBundle(Location, Iterable,
 * Iterable)} by moving the things inside classes, lib, resources a level up as expected by spark.
 *
 * @param dependencyJar {@link Location} of the job jar to be updated
 * @param context       {@link BasicSparkContext} of this job
 *///from w  w  w.  j  a v  a2 s .  c o  m
private Location updateDependencyJar(Location dependencyJar, BasicSparkContext context) throws IOException {

    final String[] prefixToStrip = { ApplicationBundler.SUBDIR_CLASSES, ApplicationBundler.SUBDIR_LIB,
            ApplicationBundler.SUBDIR_RESOURCES };

    Id.Program programId = context.getProgram().getId();

    Location updatedJar = locationFactory.create(String.format("%s.%s.%s.%s.%s.jar",
            ProgramType.SPARK.name().toLowerCase(), programId.getAccountId(), programId.getApplicationId(),
            programId.getId(), context.getRunId().getId()));

    // Creates Manifest
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.MANIFEST_VERSION, "1.0");
    JarOutputStream jarOutput = new JarOutputStream(updatedJar.getOutputStream(), manifest);

    try {
        JarInputStream jarInput = new JarInputStream(dependencyJar.getInputStream());

        try {
            JarEntry jarEntry = jarInput.getNextJarEntry();

            while (jarEntry != null) {
                boolean isDir = jarEntry.isDirectory();
                String entryName = jarEntry.getName();
                String newEntryName = entryName;

                for (String prefix : prefixToStrip) {
                    if (entryName.startsWith(prefix) && !entryName.equals(prefix)) {
                        newEntryName = entryName.substring(prefix.length());
                    }
                }

                jarEntry = new JarEntry(newEntryName);
                jarOutput.putNextEntry(jarEntry);
                if (!isDir) {
                    ByteStreams.copy(jarInput, jarOutput);
                }
                jarEntry = jarInput.getNextJarEntry();
            }
        } finally {
            jarInput.close();
            Locations.deleteQuietly(dependencyJar);
        }
    } finally {
        jarOutput.close();
    }
    return updatedJar;
}

From source file:com.opengamma.web.WebAbout.java

/**
 * Gets the OpenGamma version./*from w w  w .j av a2  s  .  c o m*/
 * @return the version, not null
 */
public String getOpenGammaBuild() {
    URL url = ClasspathHelper.forClass(getClass(), getClass().getClassLoader());
    if (url != null && url.toString().contains(".jar")) {
        try {
            final String part = ClasspathHelper.cleanPath(url);
            try (JarFile myJar = new JarFile(part)) {
                final Manifest manifest = myJar.getManifest();
                if (manifest != null) {
                    Attributes attributes = manifest.getMainAttributes();
                    if (attributes != null) {
                        if (attributes.getValue(IMPLEMENTATION_BUILD) != null) {
                            return attributes.getValue(IMPLEMENTATION_BUILD);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            s_logger.warn(ex.getMessage(), ex);
        }
    }
    return "?";
}

From source file:com.orange.mmp.dao.flf.ModuleDaoFlfImpl.java

@SuppressWarnings("unchecked")
public Module createOrUdpdate(Module module) throws MMPDaoException {
    if (module == null || module.getLocation() == null) {
        throw new MMPDaoException("missing or bad data access object");
    }// w ww. j  a v a  2  s. c  o  m

    JarFile jarFile = null;
    try {
        this.lock.lock();
        jarFile = new JarFile(new File(module.getLocation()));
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            throw new MMPDaoException("invalid module archive, MANIFEST file not found");
        }
        String symbolicName = manifest.getMainAttributes().getValue(MODULE_ID_HEADER);
        if (manifest.getMainAttributes().getValue(MODULE_ID_HEADER) != null) {
            if (module instanceof Widget) {
                String[] widgetId = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN);
                module.setId(widgetId[0]);
            } else {
                module.setId(symbolicName);
            }
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_ID_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_NAME_HEADER) != null) {
            module.setName(manifest.getMainAttributes().getValue(MODULE_NAME_HEADER));
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_NAME_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER) != null) {
            module.setVersion(new Version(manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER)));
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_VERSION_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER) != null) {
            module.setCategory(manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER));
        } else
            module.setCategory(Constants.MODULE_CATEGORY_LIBRARY);

        File moduleFile = new File(module.getLocation());
        File dstFile;
        if (module instanceof Widget) {
            String[] nameAndBranch = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN);
            if (nameAndBranch.length > 1)
                dstFile = new File(this.path,
                        nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN)
                                .concat(nameAndBranch[1]).concat(".jar"));
            else {
                String defaultBranchId = null;
                Branch defaultBranch = new Branch();
                defaultBranch.setDefault(true);
                Branch defaultBranchesResult[] = (Branch[]) DaoManagerFactory.getInstance().getDaoManager()
                        .getDao("branch").find(defaultBranch);
                if (defaultBranchesResult.length > 0) {
                    defaultBranchId = defaultBranchesResult[0].getId();
                }
                defaultBranch = defaultBranchesResult[0];
                dstFile = new File(this.path,
                        nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN)
                                .concat(defaultBranchId).concat(".jar"));
            }
        } else {
            dstFile = new File(this.path, symbolicName.concat(".jar"));
        }
        FileUtils.copyFile(moduleFile, dstFile);
        module.setLocation(dstFile.toURI());
        module.setLastModified(dstFile.lastModified());
        jarFile.close();

        FileUtils.touch(new File(this.path));

        return module;
    } catch (IOException ioe) {
        throw new MMPDaoException("failed to add module : " + ioe.getMessage());
    } finally {
        try {
            if (jarFile != null)
                jarFile.close();
        } catch (IOException ioe) {
            //Nop just log
        }
        this.lock.unlock();
    }
}

From source file:org.springframework.boot.loader.tools.Repackager.java

private Manifest buildManifest(JarFile source) throws IOException {
    Manifest manifest = source.getManifest();
    if (manifest == null) {
        manifest = new Manifest();
        manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    }/*from   w  w  w.  jav  a 2s. c om*/
    manifest = new Manifest(manifest);
    String startClass = this.mainClass;
    if (startClass == null) {
        startClass = manifest.getMainAttributes().getValue(MAIN_CLASS_ATTRIBUTE);
    }
    if (startClass == null) {
        startClass = findMainMethodWithTimeoutWarning(source);
    }
    String launcherClassName = this.layout.getLauncherClassName();
    if (launcherClassName != null) {
        manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
        if (startClass == null) {
            throw new IllegalStateException("Unable to find main class");
        }
        manifest.getMainAttributes().putValue(START_CLASS_ATTRIBUTE, startClass);
    } else if (startClass != null) {
        manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, startClass);
    }
    String bootVersion = getClass().getPackage().getImplementationVersion();
    manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion);
    manifest.getMainAttributes().putValue(BOOT_CLASSES_ATTRIBUTE,
            (this.layout instanceof RepackagingLayout)
                    ? ((RepackagingLayout) this.layout).getRepackagedClassesLocation()
                    : this.layout.getClassesLocation());
    String lib = this.layout.getLibraryDestination("", LibraryScope.COMPILE);
    if (StringUtils.hasLength(lib)) {
        manifest.getMainAttributes().putValue(BOOT_LIB_ATTRIBUTE, lib);
    }
    return manifest;
}

From source file:com.opengamma.web.WebAbout.java

/**
 * Gets the OpenGamma version.//from  w w w  .  j a va  2s.  c  om
 * @return the version, not null
 */
public String getOpenGammaVersion() {
    URL url = ClasspathHelper.forClass(getClass(), getClass().getClassLoader());
    if (url != null && url.toString().contains(".jar")) {
        try {
            final String part = ClasspathHelper.cleanPath(url);
            try (JarFile myJar = new JarFile(part)) {
                final Manifest manifest = myJar.getManifest();
                if (manifest != null) {
                    Attributes attributes = manifest.getMainAttributes();
                    if (attributes != null) {
                        if (attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION) != null) {
                            return attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
                        }
                        if (attributes.getValue(Attributes.Name.SPECIFICATION_VERSION) != null) {
                            return attributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            s_logger.warn(ex.getMessage(), ex);
        }
    } else {
        List<AboutDependency> classpath = getClasspath();
        for (AboutDependency depend : classpath) {
            if ("og-web".equals(depend.getArtifactId())) {
                return depend.getVersion();
            }
        }
    }
    return "?";
}

From source file:org.zanata.ZanataInit.java

public void initZanata(ServletContext context) throws Exception {
    checkAppServerVersion();/*from w ww .ja v  a2 s . co  m*/
    String appServerHome = context.getRealPath("/");
    File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");
    VersionInfo zanataVersion;
    Attributes atts = null;
    if (manifestFile.canRead()) {
        Manifest mf = new Manifest();
        try (FileInputStream fis = new FileInputStream(manifestFile)) {
            mf.read(fis);
        }
        atts = mf.getMainAttributes();
    }
    zanataVersion = VersionUtility.getVersionInfo(atts, ZanataInit.class);
    this.applicationConfiguration.setVersion(zanataVersion.getVersionNo());
    this.applicationConfiguration.setBuildTimestamp(zanataVersion.getBuildTimeStamp());
    this.applicationConfiguration.setScmDescribe(zanataVersion.getScmDescribe());
    this.applicationConfiguration.applyLoggingConfiguration();
    logBanner(zanataVersion);
    boolean authlogged = false;
    if (applicationConfiguration.isInternalAuth()) {
        log.info("Internal authentication: enabled");
        authlogged = true;
    }
    if (applicationConfiguration.isOpenIdAuth()) {
        log.info("OpenID authentication: enabled");
        authlogged = true;
    }
    if (applicationConfiguration.isKerberosAuth()) {
        log.info("SPNEGO/Kerberos authentication: enabled");
        authlogged = true;
    }
    if (!authlogged) {
        log.info("Using JAAS authentication");
    }
    log.info("Enable copyTrans: {}", this.applicationConfiguration.isCopyTransEnabled());
    String javamelodyDir = System.getProperty("javamelody.storage-directory");
    log.info("JavaMelody stats directory: " + javamelodyDir);
    String indexBase = applicationConfiguration.getHibernateSearchIndexBase();
    log.info("Lucene index directory: " + indexBase);
    if (indexBase != null) {
        checkLuceneLocks(new File(indexBase));
    }
    // Email server information
    log.info("Mail Session (JNDI): {}", EmailBuilder.MAIL_SESSION_JNDI);
    startupEvent.fire(new ServerStarted());
    log.info("Started Zanata...");
}