Search in sources :

Example 11 with DeployedBundles

use of org.apache.aries.application.modelling.DeployedBundles in project aries by apache.

the class DeployedBundlesTest method testGetImportPackage_InvalidDuplicates.

@Test
public void testGetImportPackage_InvalidDuplicates() throws Exception {
    DeployedBundles deployedBundles = getSimpleDeployedBundles(ternary.CONTENT, ternary.CONTENT, ternary.NONE);
    deployedBundles.addBundle(createModelledResource("bundle.a", "1.0.0", Arrays.asList("package.c;version=\"[1.0.0,2.0.0)\""), new ArrayList<String>()));
    deployedBundles.addBundle(createModelledResource("bundle.b", "1.0.0", Arrays.asList("package.c;version=2.0.0"), new ArrayList<String>()));
    deployedBundles.addBundle(createModelledResource("bundle.c", "1.0.0", new ArrayList<String>(), Arrays.asList("package.c;version=2.0.0;was_internal=true")));
    // Check that the incompatible version requirements cannot be resolved.
    String importPackageEntry = null;
    try {
        importPackageEntry = deployedBundles.getImportPackage();
        Assert.fail("Expected exception. ImportPackage=" + importPackageEntry);
    } catch (ResolverException e) {
    // We expect to reach this point if the test passes.
    }
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ArrayList(java.util.ArrayList) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) Test(org.junit.Test)

Example 12 with DeployedBundles

use of org.apache.aries.application.modelling.DeployedBundles in project aries by apache.

the class DeployedBundlesTest method testGetImportPackage_ValidResolutionAttribute.

@Test
public void testGetImportPackage_ValidResolutionAttribute() throws Exception {
    DeployedBundles deployedBundles = getSimpleDeployedBundles(ternary.CONTENT, ternary.CONTENT, ternary.NONE);
    deployedBundles.addBundle(createModelledResource("bundle.a", "1.0.0", Arrays.asList("package.c;version=1.0.0;resolution:=optional"), new ArrayList<String>()));
    deployedBundles.addBundle(createModelledResource("bundle.b", "1.0.0", Arrays.asList("package.c;version=1.0.0"), new ArrayList<String>()));
    deployedBundles.addBundle(createModelledResource("bundle.c", "1.0.0", new ArrayList<String>(), Arrays.asList("package.c;version=1.0.0")));
    // Check that the resulting resolution directive is not optional.
    String importPackageEntry = null;
    try {
        importPackageEntry = deployedBundles.getImportPackage();
    } catch (ResolverException e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
    String expectedResult = "package.c;version=1.0.0";
    Assert.assertTrue("ImportPackage=" + importPackageEntry, isEqual(importPackageEntry, expectedResult));
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ArrayList(java.util.ArrayList) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) Test(org.junit.Test)

Example 13 with DeployedBundles

use of org.apache.aries.application.modelling.DeployedBundles 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 14 with DeployedBundles

use of org.apache.aries.application.modelling.DeployedBundles in project aries by apache.

the class ConfigurationPostResolverTest method validatePostResolveTransform.

/**
 * This test validates that the transformer is correctly detecting the config admin package. Checks
 * are performed to validate that an existing import package is still honored etc.
 *
 * @throws Exception
 */
@Test
public void validatePostResolveTransform() throws Exception {
    RichBundleContext ctx = new RichBundleContext(bundleContext);
    PostResolveTransformer transformer = ctx.getService(PostResolveTransformer.class);
    Assert.assertNotNull("Unable to locate transformer", transformer);
    /**
     * Try processing deployed content that doesn't have any import for the
     * org.osgi.service.cm package, the resultant imports should be unaffected.
     */
    ApplicationMetadata mockApplicationMetadata = Skeleton.newMock(ApplicationMetadata.class);
    MockDeployedBundles originalDeployedBundles = new MockDeployedBundles();
    originalDeployedBundles.setDeployedContent(getNonConfigModelledResources());
    DeployedBundles transformedDeployedBundles = transformer.postResolveProcess(mockApplicationMetadata, originalDeployedBundles);
    Assert.assertNotNull("An instance should have been returned", transformedDeployedBundles);
    Assert.assertEquals(originalDeployedBundles.getImportPackage(), transformedDeployedBundles.getImportPackage());
    /**
     * Now try processing a deployed bundles instances that has an import for the org.osgi.service.cm package in multiple
     * modelled resources with an empty import package set in the mock deployed bundles instance.
     */
    originalDeployedBundles = new MockDeployedBundles();
    originalDeployedBundles.setDeployedContent(getConfigModelledResources());
    transformedDeployedBundles = transformer.postResolveProcess(mockApplicationMetadata, originalDeployedBundles);
    Assert.assertNotNull("An instance should have been returned", transformedDeployedBundles);
    Assert.assertNotSame("Missing config package", originalDeployedBundles.getImportPackage(), transformedDeployedBundles.getImportPackage());
    Assert.assertEquals("Missing config package", "org.osgi.service.cm;version=\"1.2.0\"", transformedDeployedBundles.getImportPackage());
    /**
     * Now try processing a deployed bundles instances that has an import for the org.osgi.service.cm package in multiple
     * modelled resources with a populated import package set in the mock deployed bundles instance.
     */
    originalDeployedBundles = new MockDeployedBundles();
    originalDeployedBundles.setDeployedContent(getConfigModelledResources());
    originalDeployedBundles.setImportPackage("org.foo.bar;version=\1.0.0\",org.bar.foo;version=\"1.0.0\"");
    transformedDeployedBundles = transformer.postResolveProcess(mockApplicationMetadata, originalDeployedBundles);
    Assert.assertNotNull("An instance should have been returned", transformedDeployedBundles);
    Assert.assertNotSame("Missing config package", originalDeployedBundles.getImportPackage(), transformedDeployedBundles.getImportPackage());
    Assert.assertEquals("Missing config package", "org.foo.bar;version=\1.0.0\",org.bar.foo;version=\"1.0.0\",org.osgi.service.cm;version=\"1.2.0\"", transformedDeployedBundles.getImportPackage());
}
Also used : RichBundleContext(org.apache.aries.itest.RichBundleContext) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) PostResolveTransformer(org.apache.aries.application.management.spi.resolve.PostResolveTransformer) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.itest.AbstractIntegrationTest)

Example 15 with DeployedBundles

use of org.apache.aries.application.modelling.DeployedBundles in project aries by apache.

the class ModellingHelperImpl method createDeployedBundles.

public DeployedBundles createDeployedBundles(String assetName, Collection<ImportedBundle> appContentNames, Collection<ImportedBundle> appUseBundleNames, Collection<ModelledResource> fakeServiceProvidingBundles) {
    logger.debug(LOG_ENTRY, "createDeployedBundles", new Object[] { assetName, appContentNames, appUseBundleNames, fakeServiceProvidingBundles });
    DeployedBundles result = new DeployedBundlesImpl(assetName, appContentNames, appUseBundleNames, fakeServiceProvidingBundles);
    logger.debug(LOG_EXIT, "createDeployedBundles", result);
    return result;
}
Also used : DeployedBundlesImpl(org.apache.aries.application.modelling.impl.DeployedBundlesImpl) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles)

Aggregations

DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)26 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)17 ResolverException (org.apache.aries.application.management.ResolverException)17 ModelledResource (org.apache.aries.application.modelling.ModelledResource)10 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)8 Content (org.apache.aries.application.Content)7 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 Manifest (java.util.jar.Manifest)4 AriesApplication (org.apache.aries.application.management.AriesApplication)4 List (java.util.List)3 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)3 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)2 ServiceUnavailableException (org.osgi.service.blueprint.container.ServiceUnavailableException)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Attributes (java.util.jar.Attributes)1 ResolveConstraint (org.apache.aries.application.management.ResolveConstraint)1