Search in sources :

Example 26 with Repository

use of org.apache.karaf.features.Repository 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 informations
        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) OutputStream(java.io.OutputStream) Properties(java.util.Properties) Logger(org.slf4j.Logger) Feature(org.apache.karaf.features.Feature) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Repository(org.apache.karaf.features.Repository) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) 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) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) 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) IOException(java.io.IOException) Repository(org.apache.karaf.features.Repository)

Example 27 with Repository

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

the class ListFeaturesCommand method doExecute.

protected void doExecute(FeaturesService featuresService) throws Exception {
    boolean needsLegend = false;
    ShellTable table = new ShellTable();
    table.column("Name");
    table.column("Version");
    table.column("Required");
    table.column("State");
    table.column("Repository");
    table.column("Description").maxSize(50);
    if (showBlacklisted) {
        table.column("Blacklisted");
    }
    table.emptyTableText(onlyInstalled ? "No features installed" : "No features available");
    List<Repository> repos = Arrays.asList(featuresService.listRepositories());
    for (Repository r : repos) {
        List<Feature> features = Arrays.asList(r.getFeatures());
        if (ordered) {
            Collections.sort(features, new FeatureComparator());
        }
        for (Feature f : features) {
            if (onlyInstalled && !featuresService.isInstalled(f)) {
                // Filter out not installed features if we only want to see the installed ones
                continue;
            }
            if (onlyRequired && !featuresService.isRequired(f)) {
                // Filter out not installed features if we only want to see the installed ones
                continue;
            }
            if (!showBlacklisted && f.isBlacklisted()) {
                // Filter out blacklisted
                continue;
            }
            if (!showHidden && f.isHidden()) {
                // Filter out hidden feature if not asked to display those
                continue;
            }
            Row row = table.addRow();
            row.addContent(f.getName(), f.getVersion(), featuresService.isRequired(f) ? "x" : "", featuresService.getState(f.getId()), r.getName(), f.getDescription());
            if (showBlacklisted) {
                row.addContent(f.isBlacklisted() ? "yes" : "no");
            }
            if (isInstalledViaDeployDir(r.getName())) {
                needsLegend = true;
            }
        }
    }
    table.print(System.out, !noFormat);
    if (needsLegend) {
        System.out.println("* Installed via deploy directory");
    }
}
Also used : Repository(org.apache.karaf.features.Repository) ShellTable(org.apache.karaf.shell.support.table.ShellTable) Row(org.apache.karaf.shell.support.table.Row) Feature(org.apache.karaf.features.Feature)

Example 28 with Repository

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

the class ListFeaturesCommandTest method testShowHiddenFeatures.

@Test
public void testShowHiddenFeatures() throws Exception {
    FeaturesService service = EasyMock.createMock(FeaturesService.class);
    Repository repo = EasyMock.createMock(Repository.class);
    Feature feature = EasyMock.createMock(Feature.class);
    EasyMock.expect(service.listRepositories()).andReturn(new Repository[] { repo });
    EasyMock.expect(repo.getFeatures()).andReturn(new Feature[] { feature });
    EasyMock.expect(feature.isHidden()).andReturn(true).anyTimes();
    EasyMock.expect(feature.isBlacklisted()).andReturn(false).anyTimes();
    EasyMock.expect(feature.getName()).andReturn("feature");
    EasyMock.expect(feature.getId()).andReturn("feature/1.0.0");
    EasyMock.expect(service.getState(EasyMock.eq("feature/1.0.0"))).andReturn(FeatureState.Started);
    EasyMock.expect(feature.getDescription()).andReturn("description");
    EasyMock.expect(feature.getVersion()).andReturn("1.0.0");
    EasyMock.expect(service.isRequired(feature)).andReturn(true);
    EasyMock.expect(repo.getName()).andReturn("repository").anyTimes();
    EasyMock.replay(service, repo, feature);
    ListFeaturesCommand command = new ListFeaturesCommand();
    command.setFeaturesService(service);
    command.noFormat = true;
    command.showHidden = true;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baos);
    System.setOut(out);
    command.execute();
    out.flush();
    assertTrue(baos.toString().contains("feature"));
    EasyMock.verify(service, repo, feature);
}
Also used : PrintStream(java.io.PrintStream) Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 29 with Repository

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

the class ListFeaturesCommandTest method testHiddenFeatures.

@Test
public void testHiddenFeatures() throws Exception {
    FeaturesService service = EasyMock.createMock(FeaturesService.class);
    Repository repo = EasyMock.createMock(Repository.class);
    Feature feature = EasyMock.createMock(Feature.class);
    EasyMock.expect(service.listRepositories()).andReturn(new Repository[] { repo });
    EasyMock.expect(repo.getFeatures()).andReturn(new Feature[] { feature });
    EasyMock.expect(feature.isHidden()).andReturn(true);
    EasyMock.expect(feature.isBlacklisted()).andReturn(false);
    EasyMock.replay(service, repo, feature);
    ListFeaturesCommand command = new ListFeaturesCommand();
    command.setFeaturesService(service);
    command.noFormat = true;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baos);
    System.setOut(out);
    command.execute();
    out.flush();
    assertFalse(baos.toString().contains("feature"));
    EasyMock.verify(service, repo, feature);
}
Also used : PrintStream(java.io.PrintStream) Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 30 with Repository

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

the class FeaturesValidationTest method testNs10.

@Test
public void testNs10() throws Exception {
    Repository features = unmarshalAndValidate("f02.xml");
    assertNotNull(features);
}
Also used : Repository(org.apache.karaf.features.Repository) Test(org.junit.Test)

Aggregations

Repository (org.apache.karaf.features.Repository)84 Test (org.junit.Test)52 HashSet (java.util.HashSet)51 FeaturesService (org.apache.karaf.features.FeaturesService)44 Feature (org.apache.karaf.features.Feature)38 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)37 URI (java.net.URI)20 Application (org.codice.ddf.admin.application.service.Application)20 LinkedHashSet (java.util.LinkedHashSet)9 ArrayList (java.util.ArrayList)8 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 Logger (org.slf4j.Logger)7 Appender (ch.qos.logback.core.Appender)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Mockito.anyString (org.mockito.Mockito.anyString)6 EnumSet (java.util.EnumSet)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)4