Search in sources :

Example 41 with Feature

use of org.apache.karaf.features.Feature in project karaf by apache.

the class FeatureDeploymentListener method bundleChanged.

public synchronized void bundleChanged(BundleEvent bundleEvent) {
    // Only handle resolved and uninstalled events
    if (bundleEvent.getType() != BundleEvent.RESOLVED && bundleEvent.getType() != BundleEvent.UNINSTALLED) {
        return;
    }
    Bundle bundle = bundleEvent.getBundle();
    try {
        // Remove previous informations
        List<URI> repsToRemove = new ArrayList<>();
        List<String> reqsToRemove = new ArrayList<>();
        // Remove old properties
        String prefix = "bundle." + bundle.getBundleId();
        String countStr = (String) properties.remove(prefix + ".reps.count");
        if (countStr != null) {
            int count = Integer.parseInt(countStr);
            for (int i = 0; i < count; i++) {
                String rep = (String) properties.remove(prefix + ".reps.item" + i);
                repsToRemove.add(URI.create(rep));
            }
        }
        countStr = (String) properties.remove(prefix + ".reqs.count");
        if (countStr != null) {
            int count = Integer.parseInt(countStr);
            for (int i = 0; i < count; i++) {
                String req = (String) properties.remove(prefix + ".reqs.item" + i);
                reqsToRemove.add(req);
            }
        }
        saveProperties();
        // Compute new information
        List<URI> repsToAdd = new ArrayList<>();
        List<String> reqsToAdd = new ArrayList<>();
        if (bundleEvent.getType() == BundleEvent.RESOLVED) {
            Enumeration featuresUrlEnumeration = bundle.findEntries("/META-INF/" + FEATURE_PATH + "/", "*.xml", false);
            while (featuresUrlEnumeration != null && featuresUrlEnumeration.hasMoreElements()) {
                URL url = (URL) featuresUrlEnumeration.nextElement();
                URI uri = url.toURI();
                repsToAdd.add(uri);
                Repository rep = featuresService.createRepository(uri);
                Stream.of(rep.getFeatures()).filter(f -> f.getInstall() == null || Feature.DEFAULT_INSTALL_MODE.equals(f.getInstall())).map(f -> "feature:" + f.getName() + "/" + new VersionRange(f.getVersion(), true)).forEach(reqsToAdd::add);
            }
            if (!repsToAdd.isEmpty()) {
                properties.put(prefix + ".reps.count", Integer.toString(repsToAdd.size()));
                for (int i = 0; i < repsToAdd.size(); i++) {
                    properties.put(prefix + ".reps.item" + i, repsToAdd.get(i).toASCIIString());
                }
                properties.put(prefix + ".reqs.count", Integer.toString(reqsToAdd.size()));
                for (int i = 0; i < reqsToAdd.size(); i++) {
                    properties.put(prefix + ".reqs.item" + i, reqsToAdd.get(i));
                }
            }
        }
        saveProperties();
        // Call features service
        List<Repository> requiredRepos = Arrays.asList(featuresService.listRequiredRepositories());
        Set<URI> requiredReposUris = requiredRepos.stream().map(Repository::getURI).collect(Collectors.toSet());
        requiredReposUris.removeAll(repsToRemove);
        requiredReposUris.addAll(repsToAdd);
        Map<String, Set<String>> requirements = featuresService.listRequirements();
        requirements.get(ROOT_REGION).removeAll(reqsToRemove);
        requirements.get(ROOT_REGION).addAll(reqsToAdd);
        if (!reqsToRemove.isEmpty() || !reqsToAdd.isEmpty()) {
            featuresService.updateReposAndRequirements(requiredReposUris, requirements, EnumSet.noneOf(FeaturesService.Option.class));
        }
    } catch (Exception e) {
        logger.error("Unable to update deployed features for bundle: " + bundle.getSymbolicName() + " - " + bundle.getVersion(), e);
    }
}
Also used : Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) URL(java.net.URL) XMLInputFactory(javax.xml.stream.XMLInputFactory) LoggerFactory(org.slf4j.LoggerFactory) FeaturesService(org.apache.karaf.features.FeaturesService) ArrayList(java.util.ArrayList) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) XMLStreamReader(javax.xml.stream.XMLStreamReader) Map(java.util.Map) URI(java.net.URI) Bundle(org.osgi.framework.Bundle) EnumSet(java.util.EnumSet) FeaturesNamespaces(org.apache.karaf.features.FeaturesNamespaces) Properties(java.util.Properties) Logger(org.slf4j.Logger) Feature(org.apache.karaf.features.Feature) Set(java.util.Set) Repository(org.apache.karaf.features.Repository) Collectors(java.util.stream.Collectors) ArtifactUrlTransformer(org.apache.felix.fileinstall.ArtifactUrlTransformer) VersionRange(org.apache.felix.utils.version.VersionRange) BundleContext(org.osgi.framework.BundleContext) List(java.util.List) ROOT_REGION(org.apache.karaf.features.FeaturesService.ROOT_REGION) Stream(java.util.stream.Stream) java.io(java.io) QName(javax.xml.namespace.QName) Enumeration(java.util.Enumeration) EnumSet(java.util.EnumSet) Set(java.util.Set) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) URI(java.net.URI) URL(java.net.URL) Repository(org.apache.karaf.features.Repository)

Example 42 with Feature

use of org.apache.karaf.features.Feature in project karaf by apache.

the class KarafTestSupport method assertFeatureInstalled.

public void assertFeatureInstalled(String featureName, String featureVersion) throws Exception {
    Feature featureToAssert = featureService.getFeatures(featureName, featureVersion)[0];
    Feature[] features = featureService.listInstalledFeatures();
    for (Feature feature : features) {
        if (featureToAssert.equals(feature)) {
            return;
        }
    }
    Assert.fail("Feature " + featureName + (featureVersion != null ? "/" + featureVersion : "") + " should be installed but is not");
}
Also used : Feature(org.apache.karaf.features.Feature)

Example 43 with Feature

use of org.apache.karaf.features.Feature in project karaf by apache.

the class KarafTestSupport method uninstallNewFeatures.

/**
 * The feature service does not uninstall feature dependencies when uninstalling a single feature.
 * So we need to make sure we uninstall all features that were newly installed.
 *
 * @param featuresBefore
 * @throws Exception
 */
public void uninstallNewFeatures(Set<Feature> featuresBefore) throws Exception {
    Feature[] features = featureService.listInstalledFeatures();
    Set<String> uninstall = new HashSet<>();
    for (Feature curFeature : features) {
        if (!featuresBefore.contains(curFeature)) {
            uninstall.add(curFeature.getId());
        }
    }
    try {
        System.out.println("Uninstalling " + uninstall);
        featureService.uninstallFeatures(uninstall, NO_AUTO_REFRESH);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : Feature(org.apache.karaf.features.Feature) TimeoutException(java.util.concurrent.TimeoutException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) RerunTestException(org.ops4j.pax.exam.RerunTestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 44 with Feature

use of org.apache.karaf.features.Feature in project karaf by apache.

the class FeaturesServiceImplTest method testListFeatureWithoutVersion.

@Test
public void testListFeatureWithoutVersion() throws Exception {
    Feature transactionFeature = feature("transaction", "1.0.0");
    FeaturesServiceImpl impl = featuresServiceWithFeatures(transactionFeature);
    assertNotNull(impl.getFeatures("transaction", null));
    assertSame(transactionFeature, impl.getFeatures("transaction", org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION)[0]);
}
Also used : Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 45 with Feature

use of org.apache.karaf.features.Feature in project karaf by apache.

the class FeaturesServiceImplTest method testGetFeatureStripVersion.

@Test
public void testGetFeatureStripVersion() throws Exception {
    Feature transactionFeature = feature("transaction", "1.0.0");
    FeaturesServiceImpl impl = featuresServiceWithFeatures(transactionFeature);
    Feature[] features = impl.getFeatures("transaction", "  1.0.0  ");
    assertEquals(1, features.length);
    Feature feature = features[0];
    assertNotNull(feature);
    assertSame("transaction", feature.getName());
}
Also used : Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Aggregations

Feature (org.apache.karaf.features.Feature)130 Test (org.junit.Test)57 FeaturesService (org.apache.karaf.features.FeaturesService)44 HashSet (java.util.HashSet)41 Repository (org.apache.karaf.features.Repository)39 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)18 Bundle (org.osgi.framework.Bundle)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 Dependency (org.apache.karaf.features.Dependency)15 BundleInfo (org.apache.karaf.features.BundleInfo)14 Application (org.codice.ddf.admin.application.service.Application)14 URI (java.net.URI)13 VersionRange (org.apache.felix.utils.version.VersionRange)12 IOException (java.io.IOException)11 LinkedHashSet (java.util.LinkedHashSet)11 Version (org.osgi.framework.Version)11 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 EnumSet (java.util.EnumSet)9