Example usage for java.util.jar Attributes getValue

List of usage examples for java.util.jar Attributes getValue

Introduction

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

Prototype

public String getValue(Name name) 

Source Link

Document

Returns the value of the specified Attributes.Name, or null if the attribute was not found.

Usage

From source file:com.l2jfree.versionning.Version.java

/**
 * @param attrs/*from w  w  w.  jav  a2s.  c o m*/
 */
private void setBuildTime(Attributes attrs) {
    String buildTime = attrs.getValue("Implementation-Time");
    if (buildTime != null) {
        _buildTime = buildTime;
    } else {
        _buildTime = "1900-01-01";
    }
}

From source file:com.l2jfree.versionning.Version.java

/**
 * @param attrs//from  w w  w .j  ava2s  .  com
 */
private void setVersionNumber(Attributes attrs) {
    String versionNumber = attrs.getValue("Implementation-Version");
    if (versionNumber != null) {
        _versionNumber = versionNumber;
    } else {
        _versionNumber = "-1";
    }
}

From source file:com.l2jfree.versionning.Version.java

/**
 * @param attrs//from   ww w  .  j ava2 s  .  co  m
 */
private void setBuildJdk(Attributes attrs) {
    String buildJdk = attrs.getValue("Build-Jdk");
    if (buildJdk != null) {
        _buildJdk = buildJdk;
    } else {
        buildJdk = attrs.getValue("Created-By");
        if (buildJdk != null) {
            _buildJdk = buildJdk;
        } else {
            _buildJdk = "-1";
        }
    }
}

From source file:com.l2jfree.versionning.Version.java

/**
 * @param attrs//from  ww w  .ja v a  2  s  . c  o m
 */
private void setRevisionNumber(Attributes attrs) {
    String revisionNumber = attrs.getValue("Implementation-Build");
    if (revisionNumber != null) {
        _revisionNumber = revisionNumber;
    } else {
        _revisionNumber = "-1";
    }
}

From source file:net.minecraftforge.fml.relauncher.libraries.LibraryManager.java

private static Pair<Artifact, byte[]> extractPacked(JarFile jar, ModList modlist, File... modDirs)
        throws IOException {
    Attributes attrs;
    if (jar.getManifest() == null)
        return null;

    JarEntry manifest_entry = jar.getJarEntry(JarFile.MANIFEST_NAME);
    if (manifest_entry == null)
        manifest_entry = jar.stream()//from w  w w .j av  a  2  s  .c  o m
                .filter(e -> JarFile.MANIFEST_NAME.equals(e.getName().toUpperCase(Locale.ENGLISH))).findFirst()
                .get(); //We know that getManifest returned non-null so we know there is *some* entry that matches the manifest file. So we dont need to empty check.

    attrs = jar.getManifest().getMainAttributes();

    String modSide = attrs.getValue(LibraryManager.MODSIDE);
    if (modSide != null && !"BOTH".equals(modSide) && !FMLLaunchHandler.side().name().equals(modSide))
        return null;

    if (attrs.containsKey(MODCONTAINSDEPS)) {
        for (String dep : attrs.getValue(MODCONTAINSDEPS).split(" ")) {
            if (!dep.endsWith(".jar")) {
                FMLLog.log.error("Contained Dep is not a jar file: {}", dep);
                throw new IllegalStateException("Invalid contained dep, Must be jar: " + dep);
            }

            if (jar.getJarEntry(dep) == null && jar.getJarEntry("META-INF/libraries/" + dep) != null)
                dep = "META-INF/libraries/" + dep;

            JarEntry depEntry = jar.getJarEntry(dep);
            if (depEntry == null) {
                FMLLog.log.error("Contained Dep is not in the jar: {}", dep);
                throw new IllegalStateException("Invalid contained dep, Missing from jar: " + dep);
            }

            String depEndName = new File(dep).getName(); // extract last part of name
            if (skipContainedDeps.contains(dep) || skipContainedDeps.contains(depEndName)) {
                FMLLog.log.error("Skipping dep at request: {}", dep);
                continue;
            }

            Attributes meta = null;
            byte[] data = null;
            byte[] manifest_data = null;

            JarEntry metaEntry = jar.getJarEntry(dep + ".meta");
            if (metaEntry != null) {
                manifest_data = readAll(jar.getInputStream(metaEntry));
                meta = new Manifest(new ByteArrayInputStream(manifest_data)).getMainAttributes();
            } else {
                data = readAll(jar.getInputStream(depEntry));
                try (ZipInputStream zi = new ZipInputStream(new ByteArrayInputStream(data))) //We use zip input stream directly, as the current Oracle implementation of JarInputStream only works when the manifest is the First/Second entry in the jar...
                {
                    ZipEntry ze = null;
                    while ((ze = zi.getNextEntry()) != null) {
                        if (ze.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
                            manifest_data = readAll(zi);
                            meta = new Manifest(new ByteArrayInputStream(manifest_data)).getMainAttributes();
                            break;
                        }
                    }
                }
            }

            if (meta == null || !meta.containsKey(MAVEN_ARTIFACT)) //Ugh I really don't want to do backwards compatibility here, I want to force modders to provide information... TODO: Remove in 1.13?
            {
                boolean found = false;
                for (File dir : modDirs) {
                    File target = new File(dir, depEndName);
                    if (target.exists()) {
                        FMLLog.log.debug("Found existing ContainDep extracted to {}, skipping extraction",
                                target.getCanonicalPath());
                        found = true;
                    }
                }
                if (!found) {
                    File target = new File(modDirs[0], depEndName);
                    FMLLog.log.debug("Extracting ContainedDep {} from {} to {}", dep, jar.getName(),
                            target.getCanonicalPath());
                    try {
                        Files.createParentDirs(target);
                        try (FileOutputStream out = new FileOutputStream(target);
                                InputStream in = data == null ? jar.getInputStream(depEntry)
                                        : new ByteArrayInputStream(data)) {
                            ByteStreams.copy(in, out);
                        }
                        FMLLog.log.debug("Extracted ContainedDep {} from {} to {}", dep, jar.getName(),
                                target.getCanonicalPath());
                        extractPacked(target, modlist, modDirs);
                    } catch (IOException e) {
                        FMLLog.log.error("An error occurred extracting dependency", e);
                    }
                }
            } else {
                try {
                    Artifact artifact = readArtifact(modlist.getRepository(), meta);
                    File target = artifact.getFile();
                    if (target.exists()) {
                        FMLLog.log.debug(
                                "Found existing ContainedDep {}({}) from {} extracted to {}, skipping extraction",
                                dep, artifact.toString(), target.getCanonicalPath(), jar.getName());
                        if (!ENABLE_AUTO_MOD_MOVEMENT) {
                            Pair<?, ?> child = extractPacked(target, modlist, modDirs); //If we're not building a real list we have to re-build the dep list every run. So search down.
                            if (child == null && metaEntry != null) //External meta with no internal name... If there is a internal name, we trust that that name is the correct one.
                            {
                                modlist.add(artifact);
                            }
                        }
                    } else {
                        FMLLog.log.debug("Extracting ContainedDep {}({}) from {} to {}", dep,
                                artifact.toString(), jar.getName(), target.getCanonicalPath());
                        Files.createParentDirs(target);
                        try (FileOutputStream out = new FileOutputStream(target);
                                InputStream in = data == null ? jar.getInputStream(depEntry)
                                        : new ByteArrayInputStream(data)) {
                            ByteStreams.copy(in, out);
                        }
                        FMLLog.log.debug("Extracted ContainedDep {}({}) from {} to {}", dep,
                                artifact.toString(), jar.getName(), target.getCanonicalPath());

                        if (artifact.isSnapshot()) {
                            SnapshotJson json = SnapshotJson.create(artifact.getSnapshotMeta());
                            json.add(new SnapshotJson.Entry(artifact.getTimestamp(), meta.getValue(MD5)));
                            json.write(artifact.getSnapshotMeta());
                        }

                        if (!DISABLE_EXTERNAL_MANIFEST) {
                            File meta_target = new File(target.getAbsolutePath() + ".meta");
                            Files.write(manifest_data, meta_target);
                        }
                        Pair<?, ?> child = extractPacked(target, modlist, modDirs);
                        if (child == null && metaEntry != null) //External meta with no internal name... If there is a internal name, we trust that that name is the correct one.
                        {
                            modlist.add(artifact);
                        }
                    }
                } catch (NumberFormatException nfe) {
                    FMLLog.log.error(FMLLog.log.getMessageFactory().newMessage(
                            "An error occurred extracting dependency. Invalid Timestamp: {}",
                            meta.getValue(TIMESTAMP)), nfe);
                } catch (IOException e) {
                    FMLLog.log.error("An error occurred extracting dependency", e);
                }
            }
        }
    }

    if (attrs.containsKey(MAVEN_ARTIFACT)) {
        Artifact artifact = readArtifact(modlist.getRepository(), attrs);
        modlist.add(artifact);
        return Pair.of(artifact, readAll(jar.getInputStream(manifest_entry)));
    }
    return null;
}

From source file:com.kotcrab.vis.editor.plugin.PluginDescriptor.java

public PluginDescriptor(FileHandle file, Manifest manifest) throws EditorException {
    this.file = file;
    folderName = file.parent().name();/*from  www  . j  av  a2s .c  o  m*/
    libs.addAll(file.parent().child("lib").list());

    Attributes attributes = manifest.getMainAttributes();

    id = attributes.getValue(PLUGIN_ID);
    name = attributes.getValue(PLUGIN_NAME);
    description = attributes.getValue(PLUGIN_DESCRIPTION);
    provider = attributes.getValue(PLUGIN_PROVIDER);
    version = attributes.getValue(PLUGIN_VERSION);
    String comp = attributes.getValue(PLUGIN_COMPATIBILITY);
    String licenseFile = attributes.getValue(PLUGIN_LICENSE);

    if (id == null || name == null || description == null || provider == null || version == null
            || comp == null)
        throw new EditorException("Missing one of required field in plugin manifest, plugin: " + file.name());

    try {
        compatibility = Integer.valueOf(comp);
    } catch (NumberFormatException e) {
        throw new EditorException("Failed to parse compatibility code, value must be integer!", e);
    }

    if (licenseFile != null && !licenseFile.isEmpty()) {
        JarFile jar = null;
        try {
            jar = new JarFile(file.file());
            JarEntry licenseEntry = jar.getJarEntry(licenseFile);
            license = StreamUtils.copyStreamToString(jar.getInputStream(licenseEntry));
        } catch (Exception e) {
            throw new EditorException("Failed to read license file for plugin: " + file.name(), e);
        } finally {
            IOUtils.closeQuietly(jar);
        }
    }
}

From source file:com.sketchy.server.action.GetCurrentVersion.java

@Override
public JSONServletResult execute(HttpServletRequest request) throws Exception {
    JSONServletResult jsonServletResult = new JSONServletResult(Status.SUCCESS);
    String version = "";
    try {//  ww w.  j  av  a2  s  . c o  m
        File sketchyFile = new File("Sketchy.jar");
        if (!sketchyFile.exists()) {
            throw new Exception("Can't find Sketchy.jar file!");
        }
        JarInputStream jarInputStream = null;
        try {
            // check to make sure it's a Sketchy File with a Manifest File
            jarInputStream = new JarInputStream(new FileInputStream(sketchyFile), true);
            Manifest manifest = jarInputStream.getManifest();
            if (manifest == null) {
                throw new Exception("Manifest file not found.");
            }
            Attributes titleAttributes = manifest.getMainAttributes();
            version = titleAttributes.getValue("Implementation-Version");
        } catch (Exception e) {
            throw new Exception("Invalid Upgrade File!");
        } finally {
            IOUtils.closeQuietly(jarInputStream);
        }

        jsonServletResult.put("currentVersion", version);
    } catch (Throwable t) {
        jsonServletResult = new JSONServletResult(Status.ERROR,
                "Error getting Current Version! " + t.getMessage());
    }
    return jsonServletResult;
}

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

public Version(ServletContext servletContext) throws IOException {
    InputStream is = null;/*ww  w .j av  a2  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.apache.sling.scripting.sightly.impl.engine.SightlyEngineConfiguration.java

protected void activate(ComponentContext componentContext) {
    InputStream ins = null;/*from w w  w  . ja  v a 2s  .c  o m*/
    try {
        ins = getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
        if (ins != null) {
            Manifest manifest = new Manifest(ins);
            Attributes attrs = manifest.getMainAttributes();
            String version = attrs.getValue("ScriptEngine-Version");
            if (version != null) {
                engineVersion = version;
            }
            String symbolicName = attrs.getValue("Bundle-SymbolicName");
            if (StringUtils.isNotEmpty(symbolicName)) {
                bundleSymbolicName = symbolicName;
            }
        }
    } catch (IOException ioe) {
    } finally {
        if (ins != null) {
            try {
                ins.close();
            } catch (IOException ignore) {
            }
        }
    }
    Dictionary properties = componentContext.getProperties();
    keepGenerated = PropertiesUtil.toBoolean(properties.get(SCR_PROP_NAME_KEEPGENERATED),
            SCR_PROP_DEFAULT_KEEPGENERATED);
}

From source file:org.mc4j.ems.connection.support.metadata.AbstractConnectionTypeDescriptor.java

public String getServerVersion(File recognitionFile) {
    try {//from   ww  w .j av  a  2s  . c om
        String version;
        JarFile recJarFile = new JarFile(recognitionFile);

        version = recJarFile.getManifest().getMainAttributes().getValue("Implementation-Version");

        if (version == null) {
            Map attrMap = recJarFile.getManifest().getEntries();
            for (Iterator iterator = attrMap.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String name = (String) entry.getKey();
                Attributes attr = (Attributes) entry.getValue();
                version = attr.getValue("Implementation-Version");
            }
        }
        return version;
    } catch (MalformedURLException e) {
        log.warn("Could not determine server version from matched file " + recognitionFile.getAbsolutePath(),
                e);
    } catch (IOException e) {
        log.warn("Could not determine server version from matched file " + recognitionFile.getAbsolutePath(),
                e);
    }
    return null;
}