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;
}
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);
}
}
}
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);
}
}
}
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");
}
}
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");
}
}
Aggregations