Search in sources :

Example 1 with ResourceBuilder

use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.

the class ResolveCommand method _validate.

public void _validate(ValidateOptions options) throws Exception {
    ResourceBuilder system = new ResourceBuilder();
    system.addEE(options.ee(EE.JavaSE_1_8));
    if (options.core() != null)
        system.addManifest(options.core().getManifest());
    if (options.packages() != null)
        system.addExportPackages(options.packages());
    if (options.capabilities() != null)
        system.addProvideCapabilities(options.capabilities());
    if (options.system() != null) {
        File f = IO.getFile(options.system());
        if (!f.isFile()) {
            error("Specified system file but not found: %s", f);
            return;
        }
        Domain domain = Domain.domain(f);
        system.addManifest(domain);
    }
    List<String> args = options._arguments();
    File index = getFile(args.remove(0));
    logger.debug("validating {}", index);
    ResolverValidator validator = new ResolverValidator(bnd);
    validator.use(bnd);
    validator.addRepository(index.toURI());
    validator.setSystem(system.build());
    List<Resolution> result = validator.validate();
    Set<Requirement> done = new HashSet<>();
    for (Resolution res : result) {
        if (options.all()) {
            bnd.out.format("%s %-60s%n", res.succeeded ? "OK" : "**", res.resource, res.message == null ? "" : res.message);
        }
        if (!res.succeeded) {
            for (Requirement req : res.missing) {
                if (done.contains(req))
                    continue;
                bnd.out.format("    missing   %s%n", req);
                done.add(req);
            }
            if (options.all()) {
                for (Requirement req : res.repos) {
                    bnd.out.format("    repos     %s%n", req);
                }
                for (Requirement req : res.system) {
                    bnd.out.format("    system    %s%n", req);
                }
                for (Requirement req : res.optionals) {
                    bnd.out.format("    optional  %s%n", req);
                }
            }
        }
    }
    bnd.getInfo(validator);
}
Also used : Requirement(org.osgi.resource.Requirement) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) ResolverValidator(biz.aQute.resolve.ResolverValidator) Domain(aQute.bnd.osgi.Domain) File(java.io.File) Resolution(biz.aQute.resolve.ResolverValidator.Resolution) HashSet(java.util.HashSet)

Example 2 with ResourceBuilder

use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.

the class Profiles method _create.

public void _create(CreateOptions options) throws Exception {
    Builder b = new Builder();
    bnd.addClose(b);
    b.setBase(bnd.getBase());
    if (options.properties() != null) {
        for (String propertyFile : options.properties()) {
            File pf = bnd.getFile(propertyFile);
            b.addProperties(pf);
        }
    }
    if (options.bsn() != null)
        b.setProperty(Constants.BUNDLE_SYMBOLICNAME, options.bsn());
    if (options.version() != null)
        b.setProperty(Constants.BUNDLE_VERSION, options.version().toString());
    Instructions match = options.match();
    Parameters packages = new Parameters();
    Parameters capabilities = new Parameters();
    Collection<String> paths = new ArrayList<String>(new Parameters(b.getProperty("-paths"), bnd).keySet());
    if (paths.isEmpty())
        paths = options._arguments();
    logger.debug("input {}", paths);
    ResourceBuilder pb = new ResourceBuilder();
    for (String root : paths) {
        File f = bnd.getFile(root);
        if (!f.exists()) {
            error("could not find %s", f);
        } else {
            Glob g = options.extension();
            if (g == null)
                g = new Glob("*.jar");
            Collection<File> files = IO.tree(f, "*.jar");
            logger.debug("will profile {}", files);
            for (File file : files) {
                Domain domain = Domain.domain(file);
                if (domain == null) {
                    error("Not a bundle because no manifest %s", file);
                    continue;
                }
                String bsn = domain.getBundleSymbolicName().getKey();
                if (bsn == null) {
                    error("Not a bundle because no manifest %s", file);
                    continue;
                }
                if (match != null) {
                    Instruction instr = match.finder(bsn);
                    if (instr == null || instr.isNegated()) {
                        logger.debug("skipped {} because of non matching bsn {}", file, bsn);
                        continue;
                    }
                }
                Parameters eps = domain.getExportPackage();
                Parameters pcs = domain.getProvideCapability();
                logger.debug("parse {}:\ncaps: {}\npac: {}\n", file, pcs, eps);
                packages.mergeWith(eps, false);
                capabilities.mergeWith(pcs, false);
            }
        }
    }
    b.setProperty(Constants.PROVIDE_CAPABILITY, capabilities.toString());
    b.setProperty(Constants.EXPORT_PACKAGE, packages.toString());
    logger.debug("Found {} packages and {} capabilities", packages.size(), capabilities.size());
    Jar jar = b.build();
    File f = b.getOutputFile(options.output());
    logger.debug("Saving as {}", f);
    jar.write(f);
}
Also used : ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Parameters(aQute.bnd.header.Parameters) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Builder(aQute.bnd.osgi.Builder) ArrayList(java.util.ArrayList) Instructions(aQute.bnd.osgi.Instructions) Instruction(aQute.bnd.osgi.Instruction) Glob(aQute.libg.glob.Glob) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Example 3 with ResourceBuilder

use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.

the class InfoRepositoryWrapper method init.

boolean init() {
    try {
        if (System.currentTimeMillis() < lastTime + 10000)
            return true;
    } finally {
        lastTime = System.currentTimeMillis();
    }
    Set<String> errors = new LinkedHashSet<String>();
    try {
        //
        // Get the current repo contents
        //
        Set<String> toBeDeleted = new HashSet<String>(persistent.keySet());
        Map<String, DownloadBlocker> blockers = new HashMap<String, DownloadBlocker>();
        for (InfoRepository repo : repos) {
            Map<String, ResourceDescriptor> map = collectKeys(repo);
            for (final Map.Entry<String, ResourceDescriptor> entry : map.entrySet()) {
                final String id = entry.getKey();
                toBeDeleted.remove(id);
                if (persistent.containsKey(id))
                    continue;
                final ResourceDescriptor rd = entry.getValue();
                DownloadBlocker blocker = new DownloadBlocker(null) {

                    //
                    // We steal the thread of the downloader to index
                    //
                    @Override
                    public void success(File file) throws Exception {
                        IndexResult index = null;
                        try {
                            index = repoIndexer.indexFile(file);
                            ResourceBuilder rb = new ResourceBuilder();
                            for (org.osgi.service.indexer.Capability capability : index.capabilities) {
                                CapReqBuilder cb = new CapReqBuilder(capability.getNamespace());
                                cb.addAttributes(capability.getAttributes());
                                cb.addDirectives(capability.getDirectives());
                                rb.addCapability(cb.buildSyntheticCapability());
                            }
                            for (org.osgi.service.indexer.Requirement requirement : index.requirements) {
                                CapReqBuilder cb = new CapReqBuilder(requirement.getNamespace());
                                cb.addAttributes(requirement.getAttributes());
                                cb.addDirectives(requirement.getDirectives());
                                rb.addRequirement(cb.buildSyntheticRequirement());
                            }
                            Resource resource = rb.build();
                            PersistentResource pr = new PersistentResource(resource);
                            persistent.put(id, pr);
                        } finally {
                            super.success(file);
                            if (index != null) {
                                index.resource.close();
                            }
                        }
                    }
                };
                blockers.put(entry.getKey(), blocker);
                repo.get(rd.bsn, rd.version, null, blocker);
            }
        }
        for (Entry<String, DownloadBlocker> entry : blockers.entrySet()) {
            String key = entry.getKey();
            DownloadBlocker blocker = entry.getValue();
            String reason = blocker.getReason();
            if (reason != null) {
                errors.add(key + ": " + reason);
            }
        }
        persistent.keySet().removeAll(toBeDeleted);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!errors.isEmpty())
        throw new IllegalStateException("Cannot index " + repos + " due to " + errors);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) IndexResult(org.osgi.service.indexer.ResourceIndexer.IndexResult) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) Resource(org.osgi.resource.Resource) DownloadBlocker(aQute.bnd.build.DownloadBlocker) InfoRepository(aQute.bnd.service.repository.InfoRepository) IOException(java.io.IOException) HashMap(java.util.HashMap) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) PersistentMap(aQute.lib.persistentmap.PersistentMap) File(java.io.File) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 4 with ResourceBuilder

use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.

the class ResolveTest method testenRouteGuard.

/**
	 * The enRoute base guard resolved but is missing bundles, the runbundles do
	 * not run
	 */
public void testenRouteGuard() throws Exception {
    MockRegistry registry = new MockRegistry();
    Repository repo = createRepo(IO.getFile("testdata/enroute/index.xml"));
    registry.addPlugin(repo);
    List<Requirement> reqs = CapReqBuilder.getRequirementsFrom(new Parameters("osgi.wiring.package;filter:='(osgi.wiring.package=org.osgi.service.async)'"));
    Collection<Capability> pack = repo.findProviders(reqs).get(reqs.get(0));
    assertEquals(2, pack.size());
    ResourceBuilder b = new ResourceBuilder();
    File guard = IO.getFile("testdata/enroute/osgi.enroute.base.guard.jar");
    Domain manifest = Domain.domain(guard);
    b.addManifest(manifest);
    Repository resourceRepository = new ResourcesRepository(b.build());
    registry.addPlugin(resourceRepository);
    Processor model = new Processor();
    model.setRunfw("org.eclipse.osgi");
    model.setRunblacklist("osgi.identity;filter:='(osgi.identity=osgi.enroute.base.api)',osgi.identity;filter:='(osgi.identity=osgi.cmpn)',osgi.identity;filter:='(osgi.identity=osgi.core)");
    model.setRunRequires("osgi.identity;filter:='(osgi.identity=osgi.enroute.base.guard)'");
    model.setRunee("JavaSE-1.8");
    try {
        BndrunResolveContext context = new BndrunResolveContext(model, null, registry, log);
        Resolver resolver = new BndResolver(new ResolverLogger(4));
        Map<Resource, List<Wire>> resolved = resolver.resolve(context);
        Set<Resource> resources = resolved.keySet();
    } catch (ResolutionException e) {
        String msg = e.getMessage().replaceAll("\\[caused by:", "\n->");
        System.out.println(msg);
        fail(msg);
    }
}
Also used : ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Parameters(aQute.bnd.header.Parameters) Processor(aQute.bnd.osgi.Processor) Capability(org.osgi.resource.Capability) Resolver(org.osgi.service.resolver.Resolver) MockRegistry(test.lib.MockRegistry) Resource(org.osgi.resource.Resource) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository) ArrayList(java.util.ArrayList) List(java.util.List) Domain(aQute.bnd.osgi.Domain) File(java.io.File) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository)

Example 5 with ResourceBuilder

use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.

the class ValidatorTest method testDelibarateFailWithSpecificResources.

public void testDelibarateFailWithSpecificResources() throws Exception {
    try (ResolverValidator validator = new ResolverValidator()) {
        ResourceBuilder system = new ResourceBuilder();
        system.addEE(EE.JavaSE_1_8);
        system.addManifest(OSGI_CORE.R6_0_0.getManifest());
        validator.setSystem(system.build());
        validator.setTrace(true);
        List<Resource> resources = XMLResourceParser.getResources(IO.getFile("testdata/repo5-broken.index.xml").toURI());
        List<Resolution> resolutions = validator.validate(resources);
        assertFalse(validator.check());
        assertEquals(1, resolutions.size());
        String message = resolutions.get(0).message;
        String expectedToContain = "missing requirement org.apache.felix.gogo.api";
        assertTrue(String.format("expected to contain <%s> but was <%s>", expectedToContain, message), message.contains(expectedToContain));
    }
}
Also used : ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Resource(org.osgi.resource.Resource) Resolution(biz.aQute.resolve.ResolverValidator.Resolution)

Aggregations

ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)41 Resource (org.osgi.resource.Resource)22 File (java.io.File)13 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)8 Capability (org.osgi.resource.Capability)8 Requirement (org.osgi.resource.Requirement)8 Parameters (aQute.bnd.header.Parameters)6 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)6 Version (org.osgi.framework.Version)6 Domain (aQute.bnd.osgi.Domain)5 Resolution (biz.aQute.resolve.ResolverValidator.Resolution)5 HashSet (java.util.HashSet)5 IOException (java.io.IOException)4 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Processor (aQute.bnd.osgi.Processor)3 HashMap (java.util.HashMap)3 List (java.util.List)3 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)2 Referral (aQute.bnd.deployer.repository.api.Referral)2