Search in sources :

Example 6 with JarMetaLoader

use of org.graalvm.component.installer.jar.JarMetaLoader in project graal by oracle.

the class CommandTestBase method initRemoteComponent.

protected void initRemoteComponent(String relativeJar, String u, String disp, String spec) throws IOException {
    clu = getClass().getResource(relativeJar);
    url = new URL(u);
    Handler.bind(url.toString(), clu);
    File f = dataFile(relativeJar).toFile();
    JarFile jf = new JarFile(f, false);
    ComponentPackageLoader cpl = new JarMetaLoader(jf, this);
    info = cpl.getComponentInfo();
    // unknown in catalog metadata
    info.setLicensePath(null);
    info.setRemoteURL(url);
    param = rparam = new CatalogItemParam(this, info, disp, spec, this, false);
}
Also used : CatalogItemParam(org.graalvm.component.installer.remote.CatalogIterable.CatalogItemParam) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) ComponentPackageLoader(org.graalvm.component.installer.persist.ComponentPackageLoader) JarMetaLoader(org.graalvm.component.installer.jar.JarMetaLoader)

Example 7 with JarMetaLoader

use of org.graalvm.component.installer.jar.JarMetaLoader in project graal by oracle.

the class JarPackageProvider method createLoader.

@Override
public MetadataLoader createLoader(Path p, byte[] magic, String serial, Feedback feedback, boolean verify) throws IOException {
    if ((magic[0] == 0x50) && (magic[1] == 0x4b) && (magic[2] == 0x03) && (magic[3] == 0x04)) {
        JarFile jf = new JarFile(p.toFile(), verify);
        boolean ok = false;
        try {
            JarMetaLoader ldr = new JarMetaLoader(jf, serial, feedback);
            ok = true;
            return ldr;
        } finally {
            if (!ok) {
                try {
                    jf.close();
                } catch (IOException ex) {
                    feedback.error("ERR_ClosingJarFile", ex, ex.getLocalizedMessage());
                }
            }
        }
    } else {
        return null;
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarMetaLoader(org.graalvm.component.installer.jar.JarMetaLoader)

Example 8 with JarMetaLoader

use of org.graalvm.component.installer.jar.JarMetaLoader in project graal by oracle.

the class GenerateCatalog method generateCatalog.

private void generateCatalog() throws IOException {
    for (Spec spec : componentSpecs) {
        File f = spec.f;
        byte[] hash = computeHash(f);
        String hashString = digest2String(hash);
        try (JarFile jf = new JarFile(f)) {
            ComponentPackageLoader ldr = new JarMetaLoader(jf, hashString, env);
            ComponentInfo info = ldr.createComponentInfo();
            String prefix = findComponentPrefix(info);
            if (!graalVMReleases.containsKey(prefix)) {
                graalVMReleases.put(prefix, new GraalVersion(version, os, arch));
            }
            Manifest mf = jf.getManifest();
            if (mf == null) {
                throw new IOException("No manifest in " + spec);
            }
            String tagString;
            if (formatVer < 2 || info.getTag() == null || info.getTag().isEmpty()) {
                tagString = "";
            } else {
                // include hash of the file in property prefix.
                // NOI18N
                tagString = "/" + hashString;
            }
            Attributes atts = mf.getMainAttributes();
            String bid = atts.getValue(BundleConstants.BUNDLE_ID).toLowerCase().replace("-", "_");
            String bl = atts.getValue(BundleConstants.BUNDLE_NAME);
            if (bid == null) {
                throw new IOException("Missing bundle id in " + spec);
            }
            if (bl == null) {
                throw new IOException("Missing bundle name in " + spec);
            }
            String name;
            prefix += tagString;
            if (spec.u != null) {
                name = spec.u.toString();
            } else {
                name = spec.relativePath != null ? spec.relativePath : f.getName();
            }
            int pos;
            while ((pos = name.indexOf("${")) != -1) {
                int endPos = name.indexOf("}", pos + 1);
                if (endPos == -1) {
                    break;
                }
                String key = name.substring(pos + 2, endPos);
                String repl = info.getRequiredGraalValues().get(key);
                if (repl == null) {
                    switch(key) {
                        case "version":
                            repl = version;
                            break;
                        case "os":
                            repl = os;
                            break;
                        case "arch":
                            repl = arch;
                            break;
                        case "comp_version":
                            repl = info.getVersionString();
                            break;
                        default:
                            throw new IllegalArgumentException(key);
                    }
                }
                if (repl == null) {
                    throw new IllegalArgumentException(key);
                }
                String toReplace = "${" + key + "}";
                name = name.replace(toReplace, repl);
            }
            String url = (urlPrefix == null || urlPrefix.isEmpty()) ? name : urlPrefix + "/" + name;
            String sel;
            String hashSuffix;
            switch(formatVer) {
                case 1:
                    sel = "Component.{0}.{1}";
                    hashSuffix = "-hash";
                    break;
                case 2:
                    sel = "Component.{0}/{1}";
                    hashSuffix = "-hash";
                    break;
                default:
                    throw new IllegalStateException();
            }
            catalogContents.append(MessageFormat.format(sel + "={2}\n", prefix, bid, url));
            catalogContents.append(MessageFormat.format(sel + hashSuffix + "={2}\n", prefix, bid, hashString));
            for (Object a : atts.keySet()) {
                String key = a.toString();
                String val = atts.getValue(key);
                if (key.startsWith("x-GraalVM-Message-")) {
                    // NOI18N
                    continue;
                }
                catalogContents.append(MessageFormat.format(sel + "-{2}={3}\n", prefix, bid, key, val));
            }
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) ComponentPackageLoader(org.graalvm.component.installer.persist.ComponentPackageLoader) JarMetaLoader(org.graalvm.component.installer.jar.JarMetaLoader) ComponentInfo(org.graalvm.component.installer.model.ComponentInfo) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 9 with JarMetaLoader

use of org.graalvm.component.installer.jar.JarMetaLoader in project graal by oracle.

the class VerifierTest method testGraalCapabilitiesCaseInsensitive.

@Test
public void testGraalCapabilitiesCaseInsensitive() throws Exception {
    try (JarFile jf = new JarFile(dataFile("truffleruby2.jar").toFile())) {
        ComponentPackageLoader ldr = new JarMetaLoader(jf, this);
        rubyInfo = ldr.createComponentInfo();
        ldr.loadPaths();
        ldr.loadSymlinks();
    }
    mockStorage.graalInfo.put(CAP_OS_NAME, "LiNuX");
    Verifier vfy = new Verifier(this, registry, registry);
    vfy.validateRequirements(rubyInfo);
}
Also used : JarFile(java.util.jar.JarFile) ComponentPackageLoader(org.graalvm.component.installer.persist.ComponentPackageLoader) JarMetaLoader(org.graalvm.component.installer.jar.JarMetaLoader) Test(org.junit.Test)

Example 10 with JarMetaLoader

use of org.graalvm.component.installer.jar.JarMetaLoader in project graal by oracle.

the class ComponentPackageLoaderTest method testCollectErrors.

@Test
public void testCollectErrors() throws Exception {
    File f = dataFile("broken1.zip").toFile();
    jf = new JarFile(f);
    loader = new JarMetaLoader(jf, this).infoOnly(true);
    info = loader.createComponentInfo();
    List<String> errs = new ArrayList<>();
    loader.getErrors().forEach((e) -> errs.add(((MetadataException) e).getOffendingHeader()));
    Collections.sort(errs);
    assertEquals("org.graalvm.ruby", info.getId());
    assertEquals(Arrays.asList(BundleConstants.BUNDLE_NAME, BundleConstants.BUNDLE_REQUIRED, BundleConstants.BUNDLE_VERSION), errs);
}
Also used : ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) MetadataException(org.graalvm.component.installer.MetadataException) JarMetaLoader(org.graalvm.component.installer.jar.JarMetaLoader) Test(org.junit.Test)

Aggregations

JarFile (java.util.jar.JarFile)11 JarMetaLoader (org.graalvm.component.installer.jar.JarMetaLoader)11 File (java.io.File)6 ComponentPackageLoader (org.graalvm.component.installer.persist.ComponentPackageLoader)6 Test (org.junit.Test)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 DependencyException (org.graalvm.component.installer.DependencyException)1 DownloadURLParam (org.graalvm.component.installer.DownloadURLIterable.DownloadURLParam)1 MetadataException (org.graalvm.component.installer.MetadataException)1 JarArchive (org.graalvm.component.installer.jar.JarArchive)1 ComponentInfo (org.graalvm.component.installer.model.ComponentInfo)1 CatalogItemParam (org.graalvm.component.installer.remote.CatalogIterable.CatalogItemParam)1 Before (org.junit.Before)1