Search in sources :

Example 11 with BundleInfo

use of org.apache.karaf.features.BundleInfo in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServices.

/**
     * Tests the {@link ApplicationServiceBean#getServices(String)} method
     *
     * @throws Exception
     */
@Test
public void testGetServices() throws Exception {
    ApplicationServiceBean serviceBean = new ApplicationServiceBean(testAppService, testConfigAdminExt, mBeanServer) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Map<String, Object>> services = new ArrayList<>();
    Map<String, Object> testService1 = new HashMap<>();
    List<Map<String, Object>> testService1Configs = new ArrayList<>();
    Map<String, Object> testConfig1 = new HashMap<>();
    testConfig1.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig1);
    services.add(testService1);
    testService1.put("configurations", testService1Configs);
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleInfo(org.apache.karaf.features.BundleInfo) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with BundleInfo

use of org.apache.karaf.features.BundleInfo in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServicesMetatypeInfo.

/**
     * Tests the {@link ApplicationServiceBean#getServices(String)} method
     * for the case where the services do not have the "configurations" key
     * and there is MetatypeInformation present for each service.
     * <p>
     * This test mostly just checks that
     *
     * @throws Exception
     */
@Test
public void testGetServicesMetatypeInfo() throws Exception {
    ApplicationServiceBean serviceBean = new ApplicationServiceBean(testAppService, testConfigAdminExt, mBeanServer) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    ServiceTracker testServiceTracker = mock(ServiceTracker.class);
    serviceBean.setServiceTracker(testServiceTracker);
    MetaTypeService testMTS = mock(MetaTypeService.class);
    MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
    when(testServiceTracker.getService()).thenReturn(testMTS);
    when(testMTS.getMetaTypeInformation(any(Bundle.class))).thenReturn(testMTI);
    when(testMTI.getPids()).thenReturn(new String[] { "001", "002" });
    when(testMTI.getFactoryPids()).thenReturn(new String[] { "001", "002" });
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
    List<Map<String, Object>> services = new ArrayList<>();
    Map<String, Object> testService2 = mock(HashMap.class);
    Map<String, Object> testService1 = mock(HashMap.class);
    services.add(testService1);
    services.add(testService2);
    List<Map<String, Object>> testService1Configs = new ArrayList<>();
    Map<String, Object> testConfig1 = new HashMap<>();
    testConfig1.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig1);
    List<Map<String, Object>> testService2Configs = new ArrayList<>();
    Map<String, Object> testConfig2 = new HashMap<>();
    testConfig2.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig2);
    when(testService1.get("factory")).thenReturn(true);
    when(testService2.get("factory")).thenReturn(false);
    when(testService1.get("configurations")).thenReturn(testService1Configs);
    when(testService2.get("configurations")).thenReturn(testService2Configs);
    when(testService1.get("id")).thenReturn("001");
    when(testService2.get("id")).thenReturn("002");
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) ServiceTracker(org.osgi.util.tracker.ServiceTracker) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) BundleInfo(org.apache.karaf.features.BundleInfo) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with BundleInfo

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

the class AddToRepositoryMojo method copyBundlesConditionalToDestRepository.

private void copyBundlesConditionalToDestRepository(List<? extends BundleInfo> artifactRefsConditional) throws MojoExecutionException {
    for (BundleInfo artifactRef : artifactRefsConditional) {
        if (ignoreDependencyFlag || (!ignoreDependencyFlag && !artifactRef.isDependency())) {
            Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
            // Avoid getting NPE on artifact.getFile in some cases 
            resolveArtifact(artifact, remoteRepos);
            if (artifact != null) {
                copy(artifact, repository);
            }
        }
    }
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) Artifact(org.apache.maven.artifact.Artifact)

Example 14 with BundleInfo

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

the class FeaturesDumpProvider method writeDump.

/**
     * {@inheritDoc}
     */
protected void writeDump(OutputStreamWriter outputStreamWriter) throws Exception {
    // creates header
    outputStreamWriter.write("Repositories:\n");
    // list repositories
    for (Repository repo : features.listRepositories()) {
        outputStreamWriter.write(repo.getURI() + " (" + repo.getName() + ")\n");
    }
    // list features
    outputStreamWriter.write("\nfeatures:\n");
    for (Feature feature : features.listFeatures()) {
        outputStreamWriter.write(feature.getName() + " " + feature.getVersion());
        outputStreamWriter.write(" installed: " + features.isInstalled(feature));
        outputStreamWriter.write("\nBundles:\n");
        for (BundleInfo bundle : feature.getBundles()) {
            outputStreamWriter.write("\t" + bundle.getLocation());
            if (bundle.getStartLevel() != 0) {
                outputStreamWriter.write(" start level " + bundle.getStartLevel());
            }
            outputStreamWriter.write("\n\n");
        }
    }
    // flush & close stream
    outputStreamWriter.close();
}
Also used : Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) Feature(org.apache.karaf.features.Feature)

Example 15 with BundleInfo

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

the class Subsystem method downloadBundles.

@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, Set<String> overrides, String featureResolutionRange, final String serviceRequirements, RepositoryManager repos) throws Exception {
    for (Subsystem child : children) {
        child.downloadBundles(manager, overrides, featureResolutionRange, serviceRequirements, repos);
    }
    final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
    final Downloader downloader = manager.createDownloader();
    final Map<BundleInfo, Conditional> infos = new HashMap<>();
    if (feature != null) {
        for (Conditional cond : feature.getConditional()) {
            for (final BundleInfo bi : cond.getBundles()) {
                infos.put(bi, cond);
            }
        }
        for (BundleInfo bi : feature.getBundles()) {
            infos.put(bi, null);
        }
    }
    boolean removeServiceRequirements;
    if (FeaturesService.SERVICE_REQUIREMENTS_DISABLE.equals(serviceRequirements)) {
        removeServiceRequirements = true;
    } else if (feature != null && FeaturesService.SERVICE_REQUIREMENTS_DEFAULT.equals(serviceRequirements)) {
        removeServiceRequirements = FeaturesNamespaces.URI_1_0_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_1_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_1.equals(feature.getNamespace());
    } else {
        removeServiceRequirements = false;
    }
    for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
        final BundleInfo bi = entry.getKey();
        final String loc = bi.getLocation();
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    for (String override : overrides) {
        final String loc = Overrides.extractUrl(override);
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    if (feature != null) {
        for (Library library : feature.getLibraries()) {
            if (library.isExport()) {
                final String loc = library.getLocation();
                downloader.download(loc, provider -> {
                    bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
                });
            }
        }
    }
    downloader.await();
    Overrides.override(bundles, overrides);
    if (feature != null) {
        // Add conditionals
        Map<Conditional, Resource> resConds = new HashMap<>();
        for (Conditional cond : feature.getConditional()) {
            FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
            addIdentityRequirement(this, resCond, false);
            addIdentityRequirement(resCond, this, true);
            installable.add(resCond);
            resConds.put(cond, resCond);
        }
        // Add features
        FeatureResource resFeature = FeatureResource.build(feature, featureResolutionRange, bundles);
        addIdentityRequirement(resFeature, this);
        installable.add(resFeature);
        // Add dependencies
        for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
            final BundleInfo bi = entry.getKey();
            final String loc = bi.getLocation();
            final Conditional cond = entry.getValue();
            ResourceImpl res = bundles.get(loc);
            int sl = bi.getStartLevel() <= 0 ? feature.getStartLevel() : bi.getStartLevel();
            if (bi.isDependency()) {
                addDependency(res, false, bi.isStart(), sl);
            } else {
                doAddDependency(res, cond == null, bi.isStart(), sl);
            }
            if (cond != null) {
                addIdentityRequirement(res, resConds.get(cond), true);
            }
        }
        for (Library library : feature.getLibraries()) {
            if (library.isExport()) {
                final String loc = library.getLocation();
                ResourceImpl res = bundles.get(loc);
                addDependency(res, false, false, 0);
            }
        }
        for (String uri : feature.getResourceRepositories()) {
            BaseRepository repo = repos.getRepository(feature.getRepositoryUrl(), uri);
            for (Resource resource : repo.getResources()) {
                ResourceImpl res = cloneResource(resource);
                addDependency(res, false, true, 0);
            }
        }
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        boolean dependency = Boolean.parseBoolean(bundle.getAttribute("dependency"));
        boolean start = bundle.getAttribute("start") == null || Boolean.parseBoolean(bundle.getAttribute("start"));
        int startLevel = 0;
        try {
            startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
        } catch (NumberFormatException e) {
        // Ignore
        }
        if (dependency) {
            addDependency(bundles.get(loc), false, start, startLevel);
        } else {
            doAddDependency(bundles.get(loc), true, start, startLevel);
            addIdentityRequirement(this, bundles.get(loc));
        }
    }
    // Compute dependencies
    for (DependencyInfo info : dependencies.values()) {
        installable.add(info.resource);
        addIdentityRequirement(info.resource, this, info.mandatory);
    }
}
Also used : FeatureResource(org.apache.karaf.features.internal.resolver.FeatureResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FeatureResource(org.apache.karaf.features.internal.resolver.FeatureResource) Resource(org.osgi.resource.Resource) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Downloader(org.apache.karaf.features.internal.download.Downloader) Conditional(org.apache.karaf.features.Conditional) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) BundleInfo(org.apache.karaf.features.BundleInfo) Clause(org.apache.felix.utils.manifest.Clause) Library(org.apache.karaf.features.Library) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

BundleInfo (org.apache.karaf.features.BundleInfo)19 HashSet (java.util.HashSet)11 Feature (org.apache.karaf.features.Feature)9 Bundle (org.osgi.framework.Bundle)8 HashMap (java.util.HashMap)7 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Conditional (org.apache.karaf.features.Conditional)4 FeaturesService (org.apache.karaf.features.FeaturesService)4 Repository (org.apache.karaf.features.Repository)4 Dependency (org.apache.karaf.features.Dependency)3 Resource (org.osgi.resource.Resource)3 List (java.util.List)2 Mockito.anyString (org.mockito.Mockito.anyString)2 BundleRevision (org.osgi.framework.wiring.BundleRevision)2 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)2 MetaTypeService (org.osgi.service.metatype.MetaTypeService)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1