Search in sources :

Example 31 with ResolutionException

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

the class ResolveOperation method run.

@Override
public void run(IProgressMonitor monitor) {
    MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, Messages.ResolveOperation_errorOverview, null);
    // Start a coordination
    BundleContext bc = Plugin.getDefault().getBundleContext();
    ServiceReference<Coordinator> coordSvcRef = bc.getServiceReference(Coordinator.class);
    Coordinator coordinator = coordSvcRef != null ? (Coordinator) bc.getService(coordSvcRef) : null;
    Coordination coordination = coordinator != null ? coordinator.begin(ResolveOperation.class.getName(), 0) : null;
    // Begin resolve
    try (ResolverLogger logger = new ResolverLogger()) {
        try {
            ResolveProcess resolve = new ResolveProcess();
            BndResolver bndResolver = new BndResolver(logger);
            ReporterLogService log = new ReporterLogService(model.getWorkspace());
            Map<Resource, List<Wire>> wirings = resolve.resolveRequired(model, model.getWorkspace(), bndResolver, callbacks, log);
            Map<Resource, List<Wire>> optionalResources = new HashMap<Resource, List<Wire>>(resolve.getOptionalResources().size());
            for (Resource optional : resolve.getOptionalResources()) {
                optionalResources.put(optional, new ArrayList<Wire>(resolve.getOptionalReasons(optional)));
            }
            result = new ResolutionResult(Outcome.Resolved, wirings, optionalResources, null, status, logger.getLog());
            if (coordination != null)
                coordination.end();
        } catch (ResolveCancelledException e) {
            result = new ResolutionResult(Outcome.Cancelled, null, null, null, status, logger.getLog());
            if (coordination != null)
                coordination.fail(e);
        } catch (ResolutionException e) {
            status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, e.getLocalizedMessage(), e));
            result = new ResolutionResult(Outcome.Unresolved, null, null, e, status, logger.getLog());
            if (coordination != null)
                coordination.fail(e);
        } catch (Exception e) {
            status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Exception during resolution.", e));
            result = new ResolutionResult(Outcome.Error, null, null, null, status, logger.getLog());
            if (coordination != null)
                coordination.fail(e);
        } finally {
            if (coordinator != null)
                bc.ungetService(coordSvcRef);
        }
    }
}
Also used : ReporterLogService(aQute.bnd.deployer.repository.ReporterLogService) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Coordination(org.osgi.service.coordinator.Coordination) ResolverLogger(biz.aQute.resolve.ResolverLogger) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) MultiStatus(org.eclipse.core.runtime.MultiStatus) Coordinator(org.osgi.service.coordinator.Coordinator) Wire(org.osgi.resource.Wire) ResolutionException(org.osgi.service.resolver.ResolutionException) ResolutionException(org.osgi.service.resolver.ResolutionException) ResolveProcess(biz.aQute.resolve.ResolveProcess) ArrayList(java.util.ArrayList) List(java.util.List) BndResolver(biz.aQute.resolve.BndResolver) BundleContext(org.osgi.framework.BundleContext)

Example 32 with ResolutionException

use of org.osgi.service.resolver.ResolutionException in project karaf by apache.

the class VerifyMojo method doExecute.

protected void doExecute() throws MojoExecutionException {
    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");
        }
        String filePrefix = null;
        if (System.getProperty("os.name").contains("Windows")) {
            filePrefix = "file:/";
        } else {
            filePrefix = "file:";
        }
        allDescriptors.add(filePrefix + project.getBuild().getDirectory() + File.separator + "feature" + File.separator + "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);
    }
    Set<String> successes = new LinkedHashSet<>();
    Set<String> ignored = new LinkedHashSet<>();
    Set<String> skipped = new LinkedHashSet<>();
    Map<String, Exception> failures = new LinkedHashMap<>();
    for (Feature feature : featuresToTest) {
        String id = feature.getId();
        if (feature.isBlacklisted()) {
            skipped.add(id);
            getLog().info("Verification of feature " + id + " skipped");
            continue;
        }
        try {
            verifyResolution(new CustomDownloadManager(resolver, executor), repositories, Collections.singleton(id), properties);
            successes.add(id);
            getLog().info("Verification of feature " + id + " succeeded");
        } catch (Exception e) {
            if (e.getCause() instanceof ResolutionException || !getLog().isDebugEnabled()) {
                getLog().warn(e.getMessage() + ": " + id);
                getLog().warn(e.getCause().getMessage());
            } else {
                getLog().warn(e);
            }
            failures.put(id, 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());
            String cid = String.join("+", ids);
            try {
                verifyResolution(manager, repositories, ids, properties);
                successes.add(cid);
                getLog().info("Verification of feature " + cid + " 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) {
                        ignored.add(cid);
                        getLog().warn("Feature resolution failed for " + cid + "\nMessage: " + e.getCause().getMessage());
                        continue;
                    }
                }
                if (e.getCause() instanceof ResolutionException || !getLog().isDebugEnabled()) {
                    getLog().warn(e.getMessage());
                } else {
                    getLog().warn(e);
                }
                failures.put(cid, e);
                if ("first".equals(fail)) {
                    throw e;
                }
            }
        }
    }
    int nb = successes.size() + ignored.size() + failures.size();
    getLog().info("Features verified: " + nb + ", failures: " + failures.size() + ", ignored: " + ignored.size() + ", skipped: " + skipped.size());
    if (!failures.isEmpty()) {
        getLog().info("Failures: " + String.join(", ", failures.keySet()));
    }
    if ("end".equals(fail) && !failures.isEmpty()) {
        throw new MojoExecutionException("Verification failures", new MultiException("Verification failures", new ArrayList<>(failures.values())));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) WorkspaceReader(org.eclipse.aether.repository.WorkspaceReader) FileReader(java.io.FileReader) Properties(java.util.Properties) CustomDownloadManager(org.apache.karaf.profile.assembly.CustomDownloadManager) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) FileReader(java.io.FileReader) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) org.apache.karaf.features.internal.model(org.apache.karaf.features.internal.model) Pattern(java.util.regex.Pattern) LocationPattern(org.apache.karaf.features.LocationPattern) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Hashtable(java.util.Hashtable) IOException(java.io.IOException) MultiException(org.apache.karaf.features.internal.util.MultiException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResolutionException(org.osgi.service.resolver.ResolutionException) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Requirement(org.osgi.resource.Requirement) Collection(java.util.Collection) CustomDownloadManager(org.apache.karaf.profile.assembly.CustomDownloadManager) MultiException(org.apache.karaf.features.internal.util.MultiException)

Aggregations

ResolutionException (org.osgi.service.resolver.ResolutionException)32 List (java.util.List)17 ArrayList (java.util.ArrayList)16 Resource (org.osgi.resource.Resource)16 Requirement (org.osgi.resource.Requirement)14 HashMap (java.util.HashMap)8 Resolver (org.osgi.service.resolver.Resolver)8 Capability (org.osgi.resource.Capability)7 File (java.io.File)6 BundleException (org.osgi.framework.BundleException)6 IOException (java.io.IOException)5 Test (org.junit.Test)5 BundleRevision (org.osgi.framework.wiring.BundleRevision)5 Workspace (aQute.bnd.build.Workspace)4 BndEditModel (aQute.bnd.build.model.BndEditModel)4 Collection (java.util.Collection)4 LinkedHashSet (java.util.LinkedHashSet)4 Logger (org.apache.felix.resolver.Logger)4 ResolverImpl (org.apache.felix.resolver.ResolverImpl)4 BundleCapability (org.apache.felix.resolver.test.util.BundleCapability)4