use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.
the class JpmRepoTest method testSimpleResolve.
// public void testResolveProviderWithRunpath() throws Exception {
// try {
// Project provider = ws.getProject("provider");
// provider.build();
// assertTrue(provider.check());
//
// Project requirer = ws.getProject("requirer");
// requirer.build();
// assertTrue(requirer.check());
//
// BndEditModel model = new BndEditModel();
// model.setProject(requirer);
// BndrunResolveContext context = new BndrunResolveContext(model, ws, log);
//
// Resolver resolver = new ResolverImpl(new
// org.apache.felix.resolver.Logger(4), null);
//
// Map<Resource,List<Wire>> resolved = resolver.resolve(context);
// Set<Resource> resources = resolved.keySet();
// Resource resource = getResource(resources, "requirer", "0");
// assertNotNull(resource);
// }
// catch (ResolutionException e) {
// fail("Resolve failed " + e);
// }
// }
public void testSimpleResolve() {
Repository repo = ws.getPlugin(Repository.class);
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());
Map<Requirement, Collection<Capability>> shell = repo.findProviders(requires);
assertNotNull(shell);
assertEquals(1, shell.size());
model.setRunRequires(requires);
BndrunResolveContext context = new BndrunResolveContext(model, ws, log);
Resolver resolver = new BndResolver(new org.apache.felix.resolver.Logger(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.12");
assertNotNull(resource);
} catch (ResolutionException e) {
fail("Resolve failed");
}
}
use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.
the class TestingMojo method testing.
private void testing(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 workingDir = new File(cwd, bndrun);
File cnf = new File(workingDir, Workspace.CNFDIR);
IO.mkdirs(cnf);
try (Bndrun run = Bndrun.createBndrun(null, runFile)) {
run.setBase(workingDir);
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 {
run.test(new File(reportsDir, bndrun), null);
} finally {
report(run);
}
}
}
use of org.osgi.service.resolver.ResolutionException in project intellij-plugins by JetBrains.
the class ResolveAction method actionPerformed.
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
Project project = event.getProject();
if (virtualFile == null || project == null)
return;
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null)
return;
FileDocumentManager.getInstance().saveAllDocuments();
new Task.Backgroundable(project, message("bnd.resolve.requirements.title"), true) {
private Map<Resource, List<Wire>> resolveResult;
private String updatedText;
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
File file = new File(virtualFile.getPath());
try (Workspace workspace = Workspace.findWorkspace(file);
Run run = Run.createRun(workspace, file);
ProjectResolver projectResolver = new ProjectResolver(run)) {
resolveResult = projectResolver.resolve();
List<VersionedClause> versionedClauses = projectResolver.getRunBundles().stream().map(c -> {
Attrs attrs = new Attrs();
attrs.put(Constants.VERSION_ATTRIBUTE, c.getVersion());
return new VersionedClause(c.getBundleSymbolicName(), attrs);
}).sorted(Comparator.comparing(VersionedClause::getName)).collect(Collectors.toList());
BndEditModel editModel = new BndEditModel();
IDocument bndDocument = new aQute.bnd.properties.Document(document.getImmutableCharSequence().toString());
editModel.loadFrom(bndDocument);
editModel.setRunBundles(versionedClauses);
editModel.saveChangesTo(bndDocument);
updatedText = bndDocument.get();
} catch (ProcessCanceledException e) {
throw e;
} catch (Exception e) {
throw new WrappingException(e);
}
indicator.checkCanceled();
}
@Override
public void onSuccess() {
if (new ResolutionSucceedDialog(project, resolveResult).showAndGet() && FileModificationService.getInstance().prepareVirtualFilesForWrite(project, Collections.singleton(virtualFile))) {
writeCommandAction(project).withName("Bndrun Resolve").run(() -> document.setText(updatedText));
}
}
@Override
public void onThrowable(@NotNull Throwable t) {
Throwable cause = t instanceof WrappingException ? t.getCause() : t;
LOG.warn("Resolution failed", cause);
if (cause instanceof ResolutionException) {
new ResolutionFailedDialog(project, (ResolutionException) cause).show();
} else {
OsmorcBundle.notification(message("bnd.resolve.failed.title"), cause.getMessage(), NotificationType.ERROR).notify(project);
}
}
}.queue();
}
use of org.osgi.service.resolver.ResolutionException in project karaf by apache.
the class VerifyMojo method doExecute.
protected void doExecute() throws MojoExecutionException, MojoFailureException {
System.setProperty("karaf.home", "target/karaf");
System.setProperty("karaf.data", "target/karaf/data");
Hashtable<String, String> properties = new Hashtable<>();
if (additionalMetadata != null) {
try (Reader reader = new FileReader(additionalMetadata)) {
Properties metadata = new Properties();
metadata.load(reader);
for (Enumeration<?> e = metadata.propertyNames(); e.hasMoreElements(); ) {
Object key = e.nextElement();
Object val = metadata.get(key);
properties.put(key.toString(), val.toString());
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to load additional metadata from " + additionalMetadata, e);
}
}
Set<String> allDescriptors = new LinkedHashSet<>();
if (descriptors == null) {
if (framework == null) {
framework = Collections.singleton("framework");
}
descriptors = new LinkedHashSet<>();
if (framework.contains("framework")) {
allDescriptors.add("mvn:org.apache.karaf.features/framework/" + getVersion("org.apache.karaf.features:framework") + "/xml/features");
}
allDescriptors.add("file:" + project.getBuild().getDirectory() + "/feature/feature.xml");
} else {
allDescriptors.addAll(descriptors);
if (framework != null && framework.contains("framework")) {
allDescriptors.add("mvn:org.apache.karaf.features/framework/" + getVersion("org.apache.karaf.features:framework") + "/xml/features");
}
}
// TODO: allow using external configuration ?
ScheduledExecutorService executor = Executors.newScheduledThreadPool(8);
DownloadManager manager = new CustomDownloadManager(resolver, executor);
final Map<String, Features> repositories;
Map<String, List<Feature>> allFeatures = new HashMap<>();
try {
repositories = loadRepositories(manager, allDescriptors);
for (String repoUri : repositories.keySet()) {
List<Feature> features = repositories.get(repoUri).getFeature();
// Ack features to inline configuration files urls
for (Feature feature : features) {
for (org.apache.karaf.features.internal.model.Bundle bi : feature.getBundle()) {
String loc = bi.getLocation();
String nloc = null;
if (loc.contains("file:")) {
for (ConfigFile cfi : feature.getConfigfile()) {
if (cfi.getFinalname().substring(1).equals(loc.substring(loc.indexOf("file:") + "file:".length()))) {
nloc = cfi.getLocation();
}
}
}
if (nloc != null) {
Field field = bi.getClass().getDeclaredField("location");
field.setAccessible(true);
field.set(bi, loc.substring(0, loc.indexOf("file:")) + nloc);
}
}
}
allFeatures.put(repoUri, features);
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to load features descriptors", e);
}
List<Feature> featuresToTest = new ArrayList<>();
if (verifyTransitive) {
for (List<Feature> features : allFeatures.values()) {
featuresToTest.addAll(features);
}
} else {
for (String uri : descriptors) {
featuresToTest.addAll(allFeatures.get(uri));
}
}
if (features != null && !features.isEmpty()) {
Pattern pattern = getPattern(features);
for (Iterator<Feature> iterator = featuresToTest.iterator(); iterator.hasNext(); ) {
Feature feature = iterator.next();
String id = feature.getName() + "/" + feature.getVersion();
if (!pattern.matcher(id).matches()) {
iterator.remove();
}
}
}
for (String fmk : framework) {
properties.put("feature.framework." + fmk, fmk);
}
List<Exception> failures = new ArrayList<>();
for (Feature feature : featuresToTest) {
try {
String id = feature.getName() + "/" + feature.getVersion();
verifyResolution(new CustomDownloadManager(resolver, executor), repositories, Collections.singleton(id), properties);
getLog().info("Verification of feature " + id + " succeeded");
} catch (Exception e) {
if (e.getCause() instanceof ResolutionException) {
getLog().warn(e.getMessage());
} else {
getLog().warn(e);
}
failures.add(e);
if ("first".equals(fail)) {
throw e;
}
}
for (Conditional cond : feature.getConditional()) {
Set<String> ids = new LinkedHashSet<>();
ids.add(feature.getId());
ids.addAll(cond.getCondition());
try {
verifyResolution(manager, repositories, ids, properties);
getLog().info("Verification of feature " + ids + " succeeded");
} catch (Exception e) {
if (ignoreMissingConditions && e.getCause() instanceof ResolutionException) {
boolean ignore = true;
Collection<Requirement> requirements = ((ResolutionException) e.getCause()).getUnresolvedRequirements();
for (Requirement req : requirements) {
ignore &= (IdentityNamespace.IDENTITY_NAMESPACE.equals(req.getNamespace()) && ResourceUtils.TYPE_FEATURE.equals(req.getAttributes().get("type")) && cond.getCondition().contains(req.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE).toString()));
}
if (ignore) {
getLog().warn("Feature resolution failed for " + ids + "\nMessage: " + e.getCause().getMessage());
continue;
}
}
if (e.getCause() instanceof ResolutionException) {
getLog().warn(e.getMessage());
} else {
getLog().warn(e);
}
failures.add(e);
if ("first".equals(fail)) {
throw e;
}
}
}
}
if ("end".equals(fail) && !failures.isEmpty()) {
throw new MojoExecutionException("Verification failures", new MultiException("Verification failures", failures));
}
}
use of org.osgi.service.resolver.ResolutionException in project bnd by bndtools.
the class ResolverValidator method resolve.
public Resolution resolve(Repository repository, Resource resource) throws Exception {
Resolution resolution = new Resolution();
Requirement identity = getIdentity(resource);
setProperty("-runrequires", ResourceUtils.toRequireCapability(identity));
BndrunResolveContext context = getResolveContext();
context.addRepository(repository);
context.init();
resolution.resource = resource;
try {
Map<Resource, List<Wire>> resolve2 = resolver.resolve(context);
resolution.succeeded = true;
resolution.resolved = resolve2.keySet();
logger.debug("resolving {} succeeded", resource);
} catch (ResolutionException e) {
logger.debug("resolving {} failed", resource);
resolution.succeeded = false;
resolution.message = e.getMessage();
for (Requirement req : e.getUnresolvedRequirements()) {
logger.debug(" missing {}", req);
resolution.unresolved.add(req);
}
ResourcesRepository systemRepository = new ResourcesRepository(system);
for (Requirement r : resource.getRequirements(null)) {
Collection<Capability> caps = systemRepository.findProvider(r);
boolean missing = caps.isEmpty();
if (missing) {
Set<Requirement> requirements = singleton(r);
caps = repository.findProviders(requirements).get(r);
missing = caps.isEmpty();
if (missing) {
if (ResourceUtils.getResolution(r) == ResolutionDirective.optional)
resolution.optionals.add(r);
else
resolution.missing.add(r);
} else {
logger.debug(" found {} in repo", r);
resolution.repos.add(r);
}
} else {
logger.debug(" found {} in system", r);
resolution.system.add(r);
}
}
error("resolving %s failed with %s", resource, resolution.message);
} catch (Exception e) {
e.printStackTrace();
error("resolving %s failed with %s", context.getInputResource().getRequirements(null), e);
resolution.message = e.getMessage();
}
return resolution;
}
Aggregations