Search in sources :

Example 11 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.

the class ResolveProcess method resolveRequired.

public Map<Resource, List<Wire>> resolveRequired(Processor properties, Project project, Registry plugins, Resolver resolver, Collection<ResolutionCallback> callbacks, LogService log) throws ResolutionException {
    required = new HashMap<Resource, List<Wire>>();
    optional = new HashMap<Resource, List<Wire>>();
    BndrunResolveContext rc = new BndrunResolveContext(properties, project, plugins, log);
    rc.addCallbacks(callbacks);
    // 1. Resolve initial requirements
    Map<Resource, List<Wire>> wirings;
    try {
        wirings = resolver.resolve(rc);
    } catch (ResolutionException re) {
        throw augment(rc, re);
    }
    // 2. Save initial requirement resolution
    Pair<Resource, List<Wire>> initialRequirement = null;
    for (Map.Entry<Resource, List<Wire>> wiring : wirings.entrySet()) {
        if (rc.getInputResource() == wiring.getKey()) {
            initialRequirement = new Pair<Resource, List<Wire>>(wiring.getKey(), wiring.getValue());
            break;
        }
    }
    // 3. Save the resolved root resources
    final List<Resource> resources = new ArrayList<Resource>();
    for (Resource r : rc.getMandatoryResources()) {
        reqs: for (Requirement req : r.getRequirements(null)) {
            for (Resource found : wirings.keySet()) {
                String filterStr = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
                try {
                    org.osgi.framework.Filter filter = filterStr != null ? org.osgi.framework.FrameworkUtil.createFilter(filterStr) : null;
                    for (Capability c : found.getCapabilities(req.getNamespace())) {
                        if (filter != null && filter.matches(c.getAttributes())) {
                            resources.add(found);
                            continue reqs;
                        }
                    }
                } catch (InvalidSyntaxException e) {
                }
            }
        }
    }
    // 4. Add any 'osgi.wiring.bundle' requirements
    List<Resource> wiredBundles = new ArrayList<Resource>();
    for (Resource resource : resources) {
        addWiredBundle(wirings, resource, wiredBundles);
    }
    for (Resource resource : wiredBundles) {
        if (!resources.contains(resource)) {
            resources.add(resource);
        }
    }
    final Map<Resource, List<Wire>> discoveredOptional = new LinkedHashMap<Resource, List<Wire>>();
    // 5. Resolve the rest
    BndrunResolveContext rc2 = new BndrunResolveContext(properties, project, plugins, log) {

        @Override
        public Collection<Resource> getMandatoryResources() {
            return resources;
        }

        @Override
        public boolean isInputResource(Resource resource) {
            for (Resource r : resources) {
                if (GenericResolveContext.resourceIdentityEquals(r, resource)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            List<Capability> toReturn = super.findProviders(requirement);
            if (toReturn.isEmpty() && isEffective(requirement) && RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(REQUIREMENT_RESOLUTION_DIRECTIVE))) {
                for (Capability cap : findProvidersFromRepositories(requirement, new LinkedHashSet<Capability>())) {
                    Resource optionalRes = cap.getResource();
                    List<Wire> list = discoveredOptional.get(optionalRes);
                    if (list == null) {
                        list = new ArrayList<>();
                        discoveredOptional.put(optionalRes, list);
                    }
                    WireImpl candidateWire = new WireImpl(cap, requirement);
                    if (!list.contains(candidateWire))
                        list.add(candidateWire);
                }
            }
            return toReturn;
        }
    };
    rc2.addCallbacks(callbacks);
    try {
        wirings = resolver.resolve(rc2);
    } catch (ResolutionException re) {
        throw augment(rc2, re);
    }
    if (initialRequirement != null) {
        wirings.put(initialRequirement.getFirst(), initialRequirement.getSecond());
    }
    Map<Resource, List<Wire>> result = invertWirings(wirings, rc2);
    removeFrameworkAndInputResources(result, rc2);
    required.putAll(result);
    optional = tidyUpOptional(wirings, discoveredOptional, log);
    return result;
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) WireImpl(aQute.bnd.osgi.resource.WireImpl) Wire(org.osgi.resource.Wire) LinkedHashMap(java.util.LinkedHashMap) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.

the class ExportMojo method export.

private void export(File runFile, FileSetRepository fileSetRepository) throws Exception {
    if (!runFile.exists()) {
        logger.error("Could not find bnd run file {}", runFile);
        errors++;
        return;
    }
    String bndrun = getNamePart(runFile);
    File temporaryDir = new File(targetDir, "tmp/export/" + bndrun);
    File cnf = new File(temporaryDir, Workspace.CNFDIR);
    IO.mkdirs(cnf);
    try (Bndrun run = Bndrun.createBndrun(null, runFile)) {
        run.setBase(temporaryDir);
        Workspace workspace = run.getWorkspace();
        workspace.setBuildDir(cnf);
        workspace.setOffline(session.getSettings().isOffline());
        workspace.addBasicPlugin(fileSetRepository);
        for (RepositoryPlugin repo : workspace.getRepositories()) {
            repo.list(null);
        }
        run.getInfo(workspace);
        report(run);
        if (!run.isOk()) {
            return;
        }
        if (resolve) {
            try {
                String runBundles = run.resolve(failOnChanges, false);
                if (!run.isOk()) {
                    return;
                }
                run.setProperty(Constants.RUNBUNDLES, runBundles);
            } catch (ResolutionException re) {
                logger.error("Unresolved requirements: {}", ResolveProcess.format(re.getUnresolvedRequirements()));
                throw re;
            } finally {
                report(run);
            }
        }
        try {
            if (bundlesOnly) {
                File runbundlesDir = new File(targetDir, "export/" + bndrun);
                IO.mkdirs(runbundlesDir);
                run.exportRunbundles(null, runbundlesDir);
            } else {
                File executableJar = new File(targetDir, bndrun + ".jar");
                run.export(null, false, executableJar);
            }
        } finally {
            report(run);
        }
    }
}
Also used : ResolutionException(org.osgi.service.resolver.ResolutionException) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) File(java.io.File) Bndrun(biz.aQute.resolve.Bndrun) Workspace(aQute.bnd.build.Workspace)

Example 13 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.

the class ResolverMojo method resolve.

private void resolve(File runFile, FileSetRepository fileSetRepository) throws Exception {
    if (!runFile.exists()) {
        logger.error("Could not find bnd run file {}", runFile);
        errors++;
        return;
    }
    String bndrun = getNamePart(runFile);
    File temporaryDir = new File(targetDir, "tmp/resolve/" + bndrun);
    File cnf = new File(temporaryDir, Workspace.CNFDIR);
    IO.mkdirs(cnf);
    try (Bndrun run = Bndrun.createBndrun(null, runFile)) {
        run.setBase(temporaryDir);
        Workspace workspace = run.getWorkspace();
        workspace.setBuildDir(cnf);
        workspace.setOffline(session.getSettings().isOffline());
        workspace.addBasicPlugin(fileSetRepository);
        for (RepositoryPlugin repo : workspace.getRepositories()) {
            repo.list(null);
        }
        run.getInfo(workspace);
        report(run);
        if (!run.isOk()) {
            return;
        }
        try {
            run.resolve(failOnChanges, true);
        } catch (ResolutionException re) {
            logger.error("Unresolved requirements: {}", ResolveProcess.format(re.getUnresolvedRequirements()));
            throw re;
        } finally {
            report(run);
        }
    }
}
Also used : ResolutionException(org.osgi.service.resolver.ResolutionException) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) File(java.io.File) Bndrun(biz.aQute.resolve.Bndrun) Workspace(aQute.bnd.build.Workspace)

Example 14 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.

the class ResolveTest method testMinimalSetup.

/**
	 * Test minimal setup
	 * 
	 * @throws URISyntaxException
	 * @throws MalformedURLException
	 */
public void testMinimalSetup() throws MalformedURLException, URISyntaxException {
    File index = IO.getFile("testdata/repo3.index.xml");
    FixedIndexedRepo fir = new FixedIndexedRepo();
    fir.setLocations(index.toURI().toString());
    Processor model = new Processor();
    model.setProperty("-runfw", "org.apache.felix.framework");
    model.setProperty("-runrequires", "osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.shell)'");
    BndrunResolveContext context = new BndrunResolveContext(model, null, model, log);
    context.setLevel(0);
    context.addRepository(fir);
    context.init();
    Resolver resolver = new BndResolver(new ResolverLogger(4));
    try {
        Map<Resource, List<Wire>> resolved = resolver.resolve(context);
        Set<Resource> resources = resolved.keySet();
        Resource shell = getResource(resources, "org.apache.felix.gogo.shell", "0.10.0");
        assertNotNull(shell);
    } catch (ResolutionException e) {
        e.printStackTrace();
        fail("Resolve failed");
    }
}
Also used : ResolutionException(org.osgi.service.resolver.ResolutionException) Processor(aQute.bnd.osgi.Processor) Resolver(org.osgi.service.resolver.Resolver) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) FixedIndexedRepo(aQute.bnd.deployer.repository.FixedIndexedRepo)

Example 15 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.

the class ResolveTest method testSimpleResolve.

/**
	 * This is a basic test of resolving. This test is paired with
	 * {@link #testResolveWithDistro()}. If you change the resources, make sure
	 * this is done in the same way. The {@link #testResolveWithDistro()} has a
	 * negative check while this one checks positive.
	 */
public void testSimpleResolve() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo3.index.xml")));
    BndEditModel model = new BndEditModel();
    model.setRunFw("org.apache.felix.framework");
    List<Requirement> requires = new ArrayList<Requirement>();
    CapReqBuilder capReq = CapReqBuilder.createBundleRequirement("org.apache.felix.gogo.shell", "[0,1)");
    requires.add(capReq.buildSyntheticRequirement());
    model.setRunRequires(requires);
    BndrunResolveContext context = new BndrunResolveContext(model, registry, log);
    Resolver resolver = new BndResolver(new ResolverLogger(4));
    try {
        Map<Resource, List<Wire>> resolved = resolver.resolve(context);
        Set<Resource> resources = resolved.keySet();
        Resource resource = getResource(resources, "org.apache.felix.gogo.runtime", "0.10");
        assertNotNull(resource);
    } catch (ResolutionException e) {
        fail("Resolve failed");
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Resolver(org.osgi.service.resolver.Resolver) MockRegistry(test.lib.MockRegistry) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) ArrayList(java.util.ArrayList) List(java.util.List) BndEditModel(aQute.bnd.build.model.BndEditModel)

Aggregations

ResolutionException (org.osgi.service.resolver.ResolutionException)19 List (java.util.List)10 Resource (org.osgi.resource.Resource)10 ArrayList (java.util.ArrayList)9 Requirement (org.osgi.resource.Requirement)9 File (java.io.File)6 Resolver (org.osgi.service.resolver.Resolver)5 Workspace (aQute.bnd.build.Workspace)4 BndEditModel (aQute.bnd.build.model.BndEditModel)4 Collection (java.util.Collection)4 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)3 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)3 BndResolver (biz.aQute.resolve.BndResolver)3 Bndrun (biz.aQute.resolve.Bndrun)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 Wire (org.osgi.resource.Wire)3 Repository (org.osgi.service.repository.Repository)3 Processor (aQute.bnd.osgi.Processor)2