Search in sources :

Example 86 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class XMLType method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    Jar jar = analyzer.getJar();
    Map<String, Resource> dir = jar.getDirectories().get(root);
    if (dir == null || dir.isEmpty()) {
        Resource resource = jar.getResource(root);
        if (resource != null)
            process(analyzer, root, resource);
        return false;
    }
    for (Iterator<Map.Entry<String, Resource>> i = dir.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry<String, Resource> entry = i.next();
        String path = entry.getKey();
        Resource resource = entry.getValue();
        if (paths.matcher(path).matches()) {
            process(analyzer, path, resource);
        }
    }
    return false;
}
Also used : Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Map(java.util.Map)

Example 87 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class Repository method analyze.

/**
	 * We have a URI that potentially could be a JAR. We download it and analyze
	 * it. If it looks like a bndle or JAR, we try to guess the different parts
	 * from it and return a ReveisionRef.
	 * 
	 * @param uri the potential URI to a bundle/jar
	 * @return null or a RevisionRef describing the bundle/jar
	 * @throws IOException
	 * @throws IllegalArgumentException
	 */
private RevisionRef analyze(File file, URI uri) throws IllegalArgumentException, IOException {
    try {
        try (Jar jar = new Jar(file)) {
            Manifest manifest = jar.getManifest();
            if (manifest == null) {
                logger.debug("Jar {} has no manifest", uri);
                return null;
            }
            Domain domain = Domain.domain(manifest);
            RevisionRef ref = new RevisionRef();
            ref.created = System.currentTimeMillis();
            ref.md5 = MD5.digest(file).digest();
            ref.revision = SHA1.digest(file).digest();
            ref.phase = Library.Phase.MASTER;
            ref.size = file.length();
            ref.urls.add(uri);
            Entry<String, Attrs> bsn = domain.getBundleSymbolicName();
            if (bsn != null) {
                ref.bsn = bsn.getKey();
                ref.name = domain.get(Constants.BUNDLE_SYMBOLICNAME);
                ref.version = domain.getBundleVersion();
                ref.description = domain.get(Constants.BUNDLE_DESCRIPTION);
                ref.groupId = "osgi";
                ref.artifactId = ref.bsn;
            }
            try {
                Map<String, Resource> map = jar.getDirectories().get("META-INF/maven");
                if (map.size() != 1) {
                    return ref;
                }
                ref.groupId = map.keySet().iterator().next();
                map = jar.getDirectories().get("META-INF/maven/" + ref.groupId);
                if (map.size() != 1) {
                    return ref;
                }
                ref.artifactId = map.keySet().iterator().next();
                if (ref.bsn == null) {
                    ref.bsn = ref.groupId + "__" + ref.artifactId;
                }
                Resource r = jar.getResource("META-INF/maven/" + ref.groupId + "/" + ref.artifactId + "/pom.xml");
                if (r != null) {
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(r.openInputStream());
                    XPath xp = xpf.newXPath();
                    if (ref.description == null) {
                        ref.description = xp.evaluate("//description", doc);
                    }
                    if (ref.version == null) {
                        ref.version = xp.evaluate("//version", doc);
                    }
                    if (ref.name == null) {
                        ref.name = xp.evaluate("//name", doc);
                    }
                    ref.packaging = xp.evaluate("//packaging", doc);
                    ref.classifier = xp.evaluate("//classifier", doc);
                }
            } catch (Exception e) {
                logger.debug("parsing maven failed for {}: {}", uri, e);
            }
            if (ref.version == null)
                ref.version = "0";
            if (Verifier.isVersion(ref.version)) {
                Version version = new Version(ref.version);
                ref.baseline = version.getWithoutQualifier().toString();
                ref.qualifier = version.getQualifier();
            }
            if (ref.bsn == null) {
                Pattern JAR_URI_P = Pattern.compile(".*/([^/]+)(?:\\.jar)?", Pattern.CASE_INSENSITIVE);
                Matcher m = JAR_URI_P.matcher(uri.toString());
                if (m.matches()) {
                    ref.bsn = m.group(1);
                } else
                    ref.bsn = "unknown";
            }
            return ref;
        }
    } catch (Exception e) {
        logger.debug("Could not parse JAR {}: {}", uri, e);
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Attrs(aQute.bnd.header.Attrs) Resource(aQute.bnd.osgi.Resource) Manifest(java.util.jar.Manifest) Document(org.w3c.dom.Document) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) SocketException(java.net.SocketException) IOException(java.io.IOException) RevisionRef(aQute.service.library.Library.RevisionRef) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Version(aQute.bnd.version.Version) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain)

Example 88 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class EnrouteCommand method copy.

private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite) throws Exception {
    try (Jar jar = new Jar("dot", in)) {
        for (Entry<String, Resource> e : jar.getResources().entrySet()) {
            String path = e.getKey();
            logger.debug("path {}", path);
            if (glob != null && !glob.matcher(path).matches())
                continue;
            Resource r = e.getValue();
            File dest = Processor.getFile(workspaceDir, path);
            if (overwrite || !dest.isFile() || dest.lastModified() < r.lastModified() || r.lastModified() <= 0) {
                logger.debug("copy {} to {}", path, dest);
                File dp = dest.getParentFile();
                IO.mkdirs(dp);
                IO.copy(r.openInputStream(), dest);
            }
        }
    }
}
Also used : Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Example 89 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class SubsystemExporter method export.

@Override
public Map.Entry<String, Resource> export(String type, final Project project, Map<String, String> options) throws Exception {
    Jar jar = new Jar(".");
    project.addClose(jar);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue("Subsystem-ManifestVersion", "1");
    List<Container> distro = project.getBundles(Strategy.LOWEST, project.getProperty(Constants.DISTRO), Constants.DISTRO);
    List<File> distroFiles = getBundles(distro, project);
    List<File> files = getBundles(project.getRunbundles(), project);
    MultiMap<String, Attrs> imports = new MultiMap<String, Attrs>();
    MultiMap<String, Attrs> exports = new MultiMap<String, Attrs>();
    Parameters requirements = new Parameters();
    Parameters capabilities = new Parameters();
    for (File file : files) {
        Domain domain = Domain.domain(file);
        String bsn = domain.getBundleSymbolicName().getKey();
        String version = domain.getBundleVersion();
        for (Entry<String, Attrs> e : domain.getImportPackage().entrySet()) {
            imports.add(e.getKey(), e.getValue());
        }
        for (Entry<String, Attrs> e : domain.getExportPackage().entrySet()) {
            exports.add(e.getKey(), e.getValue());
        }
        String path = bsn + "-" + version + ".jar";
        jar.putResource(path, new FileResource(file));
    }
    headers(project, manifest.getMainAttributes());
    set(manifest.getMainAttributes(), SUBSYSTEM_TYPE, OSGI_SUBSYSTEM_FEATURE);
    String ssn = project.getName();
    Collection<String> bsns = project.getBsns();
    if (bsns.size() > 0) {
        ssn = bsns.iterator().next();
    }
    set(manifest.getMainAttributes(), SUBSYSTEM_SYMBOLIC_NAME, ssn);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    manifest.write(bout);
    jar.putResource(OSGI_INF_SUBSYSTEM_MF, new EmbeddedResource(bout.toByteArray(), 0));
    final JarResource jarResource = new JarResource(jar);
    final String name = ssn + ".esa";
    return new Map.Entry<String, Resource>() {

        @Override
        public String getKey() {
            return name;
        }

        @Override
        public Resource getValue() {
            return jarResource;
        }

        @Override
        public Resource setValue(Resource arg0) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : Parameters(aQute.bnd.header.Parameters) JarResource(aQute.bnd.osgi.JarResource) Attrs(aQute.bnd.header.Attrs) FileResource(aQute.bnd.osgi.FileResource) JarResource(aQute.bnd.osgi.JarResource) FileResource(aQute.bnd.osgi.FileResource) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Resource(aQute.bnd.osgi.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) MultiMap(aQute.lib.collections.MultiMap) Container(aQute.bnd.build.Container) Entry(java.util.Map.Entry) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Example 90 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class bnd method _flatten.

@Description("Flatten a bundle by expanding all entries on the Bundle-ClassPath")
public void _flatten(FlattenOptions opts) throws Exception {
    List<String> inputs = opts._arguments();
    String inputPath = inputs.remove(0);
    String outputPath = inputs.remove(0);
    File source = getFile(inputPath);
    if (!source.isFile()) {
        error("Not a source file %s", source);
        return;
    }
    File destination = getFile(outputPath);
    IO.mkdirs(destination.getParentFile());
    if (!destination.getParentFile().isDirectory()) {
        error("Could not create directory for output file %s", outputPath);
    }
    Jar input = new Jar(source);
    addClose(input);
    Manifest manifest = input.getManifest();
    Domain domain = Domain.domain(manifest);
    List<String> bundleClassPath = new ArrayList<>(domain.getBundleClasspath().keySet());
    if (bundleClassPath.isEmpty()) {
        warning("%s has no bundle class path", source);
        return;
    }
    Collections.reverse(bundleClassPath);
    Jar output = new Jar(source.getName());
    for (String path : bundleClassPath) {
        logger.debug("bcp entry {}", path);
        Resource r = input.getResource(path);
        if (r == null) {
            logger.debug("Is directory {}", path);
            if (path.equals(".")) {
                addAll(output, input, "", bundleClassPath);
            } else
                addAll(output, input, path, null);
        } else {
            logger.debug("Is jar {}", path);
            Jar sub = new Jar(path, r.openInputStream());
            addClose(sub);
            addAll(output, sub, "", null);
        }
    }
    domain.setBundleClasspath(".");
    output.setManifest(manifest);
    output.stripSignatures();
    output.write(destination);
}
Also used : ArrayList(java.util.ArrayList) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Domain(aQute.bnd.osgi.Domain) File(java.io.File) Description(aQute.lib.getopt.Description)

Aggregations

Resource (aQute.bnd.osgi.Resource)147 Jar (aQute.bnd.osgi.Jar)87 Builder (aQute.bnd.osgi.Builder)83 File (java.io.File)76 XmlTester (aQute.bnd.test.XmlTester)48 JarResource (aQute.bnd.osgi.JarResource)20 Attributes (java.util.jar.Attributes)20 Map (java.util.Map)19 Manifest (java.util.jar.Manifest)19 FileResource (aQute.bnd.osgi.FileResource)17 LogService (org.osgi.service.log.LogService)15 HashMap (java.util.HashMap)14 Document (org.w3c.dom.Document)14 Properties (java.util.Properties)12 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 InputStream (java.io.InputStream)9 Attrs (aQute.bnd.header.Attrs)8 ArrayList (java.util.ArrayList)8 TreeMap (java.util.TreeMap)8