Search in sources :

Example 1 with DownloadManager

use of org.apache.karaf.features.internal.download.DownloadManager 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));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) FileReader(java.io.FileReader) Conditional(org.apache.karaf.features.internal.model.Conditional) Properties(java.util.Properties) CustomDownloadManager(org.apache.karaf.profile.assembly.CustomDownloadManager) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) Feature(org.apache.karaf.features.internal.model.Feature) Field(java.lang.reflect.Field) FileReader(java.io.FileReader) Features(org.apache.karaf.features.internal.model.Features) List(java.util.List) ArrayList(java.util.ArrayList) Pattern(java.util.regex.Pattern) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) 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) MojoFailureException(org.apache.maven.plugin.MojoFailureException) 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)

Example 2 with DownloadManager

use of org.apache.karaf.features.internal.download.DownloadManager in project karaf by apache.

the class FeaturesServiceImpl method doProvision.

private // all requirements
void doProvision(// all requirements
Map<String, Set<String>> requirements, // features state changes
Map<String, Map<String, FeatureState>> stateChanges, // current state
State state, // installation options
EnumSet<Option> options, // file to store the resolution or null
String outputFile) throws Exception {
    Dictionary<String, String> props = getMavenConfig();
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, "org.ops4j.pax.url.mvn");
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(cfg.downloadThreads);
    executor.setMaximumPoolSize(cfg.downloadThreads);
    DownloadManager manager = DownloadManagers.createDownloadManager(resolver, executor, cfg.scheduleDelay, cfg.scheduleMaxRun);
    try {
        Set<String> prereqs = new HashSet<>();
        while (true) {
            try {
                Deployer.DeploymentState dstate = getDeploymentState(state);
                Deployer.DeploymentRequest request = getDeploymentRequest(requirements, stateChanges, options, outputFile);
                new Deployer(manager, this.resolver, this.installSupport, this).deploy(dstate, request);
                break;
            } catch (Deployer.PartialDeploymentException e) {
                if (!prereqs.containsAll(e.getMissing())) {
                    prereqs.addAll(e.getMissing());
                    state = copyState();
                } else {
                    throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
                }
            }
        }
    } finally {
        executor.shutdown();
    }
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 DownloadManager (org.apache.karaf.features.internal.download.DownloadManager)2 FileReader (java.io.FileReader)1 Reader (java.io.Reader)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 Pattern (java.util.regex.Pattern)1 Conditional (org.apache.karaf.features.internal.model.Conditional)1 ConfigFile (org.apache.karaf.features.internal.model.ConfigFile)1 Feature (org.apache.karaf.features.internal.model.Feature)1