Search in sources :

Example 1 with ServiceUnavailableException

use of org.osgi.service.blueprint.container.ServiceUnavailableException in project aries by apache.

the class DeploymentManifestManagerImpl method generateDeployedBundles.

/**
   * Perform provisioning to work out the 'freeze dried list' of the eba
   * @param appContent - the application content in the application.mf
   * @param useBundleContent - use bundle entry in the application.mf
   * @param providedByValueBundles - bundles contained in the eba  
   * @return
   * @throws ResolverException
   */
@Override
public DeployedBundles generateDeployedBundles(ApplicationMetadata appMetadata, Collection<ModelledResource> provideByValueBundles, Collection<Content> otherBundles) throws ResolverException {
    Collection<Content> useBundleSet = appMetadata.getUseBundles();
    Collection<Content> appContent = appMetadata.getApplicationContents();
    Collection<Content> bundlesToResolve = new ArrayList<Content>();
    Set<ImportedBundle> appContentIB = toImportedBundle(appContent);
    Set<ImportedBundle> useBundleIB = toImportedBundle(useBundleSet);
    bundlesToResolve.addAll(useBundleSet);
    bundlesToResolve.addAll(appContent);
    bundlesToResolve.addAll(otherBundles);
    Collection<ModelledResource> byValueBundles = new ArrayList<ModelledResource>(provideByValueBundles);
    ModelledResource fakeBundleResource;
    try {
        fakeBundleResource = createFakeBundle(appMetadata.getApplicationImportServices());
    } catch (InvalidAttributeException iax) {
        ResolverException rx = new ResolverException(iax);
        _logger.debug(LOG_EXIT, "generateDeploymentManifest", new Object[] { rx });
        throw rx;
    }
    byValueBundles.add(fakeBundleResource);
    Collection<ModelledResource> fakeResources = new ArrayList<ModelledResource>();
    for (PreResolveHook hook : preResolveHooks) {
        hook.collectFakeResources(fakeResources);
    }
    byValueBundles.addAll(fakeResources);
    String appSymbolicName = appMetadata.getApplicationSymbolicName();
    String appVersion = appMetadata.getApplicationVersion().toString();
    String uniqueName = appSymbolicName + "_" + appVersion;
    DeployedBundles deployedBundles = modellingHelper.createDeployedBundles(appSymbolicName, appContentIB, useBundleIB, Arrays.asList(fakeBundleResource));
    Collection<ModelledResource> bundlesToBeProvisioned = resolver.resolve(appSymbolicName, appVersion, byValueBundles, bundlesToResolve);
    pruneFakeBundlesFromResults(bundlesToBeProvisioned, fakeResources);
    if (bundlesToBeProvisioned.isEmpty()) {
        throw new ResolverException(MessageUtil.getMessage("EMPTY_DEPLOYMENT_CONTENT", uniqueName));
    }
    for (ModelledResource rbm : bundlesToBeProvisioned) {
        deployedBundles.addBundle(rbm);
    }
    Collection<ModelledResource> requiredUseBundle = deployedBundles.getRequiredUseBundle();
    if (requiredUseBundle.size() < useBundleSet.size()) {
        // Some of the use-bundle entries were redundant so resolve again with just the good ones.
        deployedBundles = modellingHelper.createDeployedBundles(appSymbolicName, appContentIB, useBundleIB, Arrays.asList(fakeBundleResource));
        bundlesToResolve.clear();
        bundlesToResolve.addAll(appContent);
        Collection<ImportedBundle> slimmedDownUseBundle = narrowUseBundles(useBundleIB, requiredUseBundle);
        bundlesToResolve.addAll(toContent(slimmedDownUseBundle));
        bundlesToBeProvisioned = resolver.resolve(appSymbolicName, appVersion, byValueBundles, bundlesToResolve);
        pruneFakeBundlesFromResults(bundlesToBeProvisioned, fakeResources);
        for (ModelledResource rbm : bundlesToBeProvisioned) {
            deployedBundles.addBundle(rbm);
        }
        requiredUseBundle = deployedBundles.getRequiredUseBundle();
    }
    // Check for circular dependencies. No shared bundle can depend on any 
    // isolated bundle. 
    Collection<ModelledResource> sharedBundles = new HashSet<ModelledResource>();
    sharedBundles.addAll(deployedBundles.getDeployedProvisionBundle());
    sharedBundles.addAll(requiredUseBundle);
    Collection<ModelledResource> appContentBundles = deployedBundles.getDeployedContent();
    Collection<Content> requiredSharedBundles = new ArrayList<Content>();
    for (ModelledResource mr : sharedBundles) {
        String version = mr.getExportedBundle().getVersion();
        String exactVersion = "[" + version + "," + version + "]";
        Content ib = ContentFactory.parseContent(mr.getExportedBundle().getSymbolicName(), exactVersion);
        requiredSharedBundles.add(ib);
    }
    // This will throw a ResolverException if the shared content does not resolve
    Collection<ModelledResource> resolvedSharedBundles = resolver.resolve(appSymbolicName, appVersion, byValueBundles, requiredSharedBundles);
    // we need to find out whether any shared bundles depend on the isolated bundles
    List<String> suspects = findSuspects(resolvedSharedBundles, sharedBundles, appContentBundles);
    // from isolated bundles. We need to build up the error message and throw a ResolverException
    if (!suspects.isEmpty()) {
        StringBuilder msgs = new StringBuilder();
        List<String> unsatisfiedRequirements = new ArrayList<String>();
        Map<String, List<String>> isolatedBundles = new HashMap<String, List<String>>();
        // Find the isolated bundles and store all the packages that they export in a map.
        for (ModelledResource mr : resolvedSharedBundles) {
            String mrName = mr.getSymbolicName() + "_" + mr.getExportedBundle().getVersion();
            if (suspects.contains(mrName)) {
                List<String> exportedPackages = new ArrayList<String>();
                isolatedBundles.put(mrName, exportedPackages);
                for (ExportedPackage ep : mr.getExportedPackages()) {
                    exportedPackages.add(ep.getPackageName());
                }
            }
        }
        // are exported from the isolated bundles.
        for (ModelledResource mr : resolvedSharedBundles) {
            String mrName = mr.getSymbolicName() + "_" + mr.getExportedBundle().getVersion();
            // if current resource isn't an isolated bundle check it's requirements
            if (!!!suspects.contains(mrName)) {
                // Iterate through the imported packages of the current shared bundle.
                for (ImportedPackage ip : mr.getImportedPackages()) {
                    String packageName = ip.getPackageName();
                    List<String> bundlesExportingPackage = new ArrayList<String>();
                    // get a match store the info away.
                    for (Map.Entry<String, List<String>> currBundle : isolatedBundles.entrySet()) {
                        List<String> exportedPackages = currBundle.getValue();
                        if (exportedPackages != null && exportedPackages.contains(packageName)) {
                            bundlesExportingPackage.add(currBundle.getKey());
                        }
                    }
                    // exception.
                    if (!!!bundlesExportingPackage.isEmpty()) {
                        String newMsg;
                        if (bundlesExportingPackage.size() > 1) {
                            newMsg = MessageUtil.getMessage("SHARED_BUNDLE_IMPORTING_FROM_ISOLATED_BUNDLES", new Object[] { mrName, packageName, bundlesExportingPackage });
                        } else {
                            newMsg = MessageUtil.getMessage("SHARED_BUNDLE_IMPORTING_FROM_ISOLATED_BUNDLE", new Object[] { mrName, packageName, bundlesExportingPackage });
                        }
                        msgs.append("\n");
                        msgs.append(newMsg);
                        unsatisfiedRequirements.add(newMsg);
                    }
                }
            }
        }
        // Well! if the msgs is empty, no need to throw an exception
        if (msgs.length() != 0) {
            String message = MessageUtil.getMessage("SUSPECTED_CIRCULAR_DEPENDENCIES", new Object[] { appSymbolicName, msgs });
            ResolverException rx = new ResolverException(message);
            rx.setUnsatisfiedRequirements(unsatisfiedRequirements);
            _logger.debug(LOG_EXIT, "generateDeploymentManifest", new Object[] { rx });
            throw (rx);
        }
    }
    checkForIsolatedContentInProvisionBundle(appSymbolicName, deployedBundles);
    if (postResolveTransformer != null)
        try {
            deployedBundles = postResolveTransformer.postResolveProcess(appMetadata, deployedBundles);
        } catch (ServiceUnavailableException e) {
            _logger.debug(MessageUtil.getMessage("POST_RESOLVE_TRANSFORMER_UNAVAILABLE", e));
        }
    return deployedBundles;
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) PreResolveHook(org.apache.aries.application.management.spi.resolve.PreResolveHook) ImportedBundle(org.apache.aries.application.modelling.ImportedBundle) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Content(org.apache.aries.application.Content) ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ServiceUnavailableException

use of org.osgi.service.blueprint.container.ServiceUnavailableException in project ddf by codice.

the class SourceOperations method isSourceAvailable.

//
// Helper methods - check visibility
//
/**
     * Checks that the specified source is valid and available.
     *
     * @param source the {@link Source} to check availability of
     * @return true if the {@link Source} is available, false otherwise
     */
boolean isSourceAvailable(Source source) {
    if (source == null) {
        LOGGER.debug("source is null, therefore not available");
        return false;
    }
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Checking if source \"{}\" is available...", source.getId());
        }
        // source is considered available unless we have checked and seen otherwise
        boolean available = true;
        Source cachedSource = frameworkProperties.getSourcePoller().getCachedSource(source);
        if (cachedSource != null) {
            available = cachedSource.isAvailable();
        }
        if (!available) {
            LOGGER.info("source \"{}\" is not available", source.getId());
        }
        return available;
    } catch (ServiceUnavailableException e) {
        LOGGER.info("Caught ServiceUnavaiableException", e);
        return false;
    } catch (Exception e) {
        LOGGER.info("Caught Exception", e);
        return false;
    }
}
Also used : ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException)

Example 3 with ServiceUnavailableException

use of org.osgi.service.blueprint.container.ServiceUnavailableException in project aries by apache.

the class TestReferences method testUnaryReference.

@SuppressWarnings("rawtypes")
@Test
public void testUnaryReference() throws Exception {
    BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
    assertNotNull(blueprintContainer);
    BindingListener listener = (BindingListener) blueprintContainer.getComponentInstance("bindingListener");
    assertNull(listener.getA());
    assertNull(listener.getReference());
    InterfaceA a = (InterfaceA) blueprintContainer.getComponentInstance("ref2");
    try {
        a.hello("world");
        fail("A ServiceUnavailableException should have been thrown");
    } catch (ServiceUnavailableException e) {
    // Ignore, expected
    }
    ServiceRegistration reg1 = bundleContext.registerService(InterfaceA.class.getName(), new InterfaceA() {

        public String hello(String msg) {
            return "Hello " + msg + "!";
        }
    }, null);
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Hello world!", a.hello("world"));
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, Integer.valueOf(1));
    ServiceRegistration reg2 = bundleContext.registerService(InterfaceA.class.getName(), new InterfaceA() {

        public String hello(String msg) {
            return "Good morning " + msg + "!";
        }
    }, props);
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Hello world!", a.hello("world"));
    reg1.unregister();
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Good morning world!", a.hello("world"));
    reg2.unregister();
    waitForAsynchronousHandling();
    assertNull(listener.getA());
    assertNull(listener.getReference());
    try {
        a.hello("world");
        fail("A ServiceUnavailableException should have been thrown");
    } catch (ServiceUnavailableException e) {
    // Ignore, expected
    }
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) InterfaceA(org.apache.aries.blueprint.sample.InterfaceA) Hashtable(java.util.Hashtable) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) BindingListener(org.apache.aries.blueprint.sample.BindingListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) DestroyTest(org.apache.aries.blueprint.sample.DestroyTest) Test(org.junit.Test)

Aggregations

ServiceUnavailableException (org.osgi.service.blueprint.container.ServiceUnavailableException)3 FederatedSource (ddf.catalog.source.FederatedSource)1 Source (ddf.catalog.source.Source)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Map (java.util.Map)1 Content (org.apache.aries.application.Content)1 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)1 ResolverException (org.apache.aries.application.management.ResolverException)1 PreResolveHook (org.apache.aries.application.management.spi.resolve.PreResolveHook)1 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)1 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)1 ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)1 ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)1 ModelledResource (org.apache.aries.application.modelling.ModelledResource)1 BindingListener (org.apache.aries.blueprint.sample.BindingListener)1