Search in sources :

Example 6 with ExportedPackage

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

the class ExportedPackageTest method testEqualsForEqualTrivialPackages.

@Test
public void testEqualsForEqualTrivialPackages() {
    String packageName = "package.name";
    String version = "1.0.0";
    ExportedPackage package1 = instantiatePackage(packageName, version);
    ExportedPackage package2 = instantiatePackage(packageName, version);
    assertEquals(package1, package2);
}
Also used : ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) Test(org.junit.Test)

Example 7 with ExportedPackage

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

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

the class DeploymentGeneratorTest method checkBasicCircularDependenciesDetected.

@Test
public void checkBasicCircularDependenciesDetected() throws Exception {
    // Override Resolver behaviour. 
    //ImportedBundle isolated = new ImportedBundleImpl ("test.isolated" , "1.0.0"); 
    // When we resolve isolated, we're going to get another bundle which has a dependency on isolated. 
    Collection<ModelledResource> cmr = new ArrayList<ModelledResource>();
    ExportedPackage testIsolatedPkg = createExportedPackage("test.isolated", "1.0.0", new String[] { "test.shared" }, new String[] { "test.isolated.pkg" });
    cmr.add(testIsolatedPkg.getBundle());
    ExportedPackage testSharedPkg = createExportedPackage("test.shared", "1.0.0", new String[] { "test.isolated.pkg" }, new String[] { "test.shared" });
    cmr.add(testSharedPkg.getBundle());
    _resolver.addResult(cmr);
    // The second time DeploymentGenerator calls the Resolver, it will provide just 
    // test.shared. The resolver will return test.shared _plus_ test.isolated. 
    _resolver.addResult(cmr);
    Skeleton.getSkeleton(appMetadata).setReturnValue(new MethodCall(ApplicationMetadata.class, "getApplicationContents"), Arrays.asList(mockContent("test.isolated", "1.0.0")));
    try {
        DeployedBundles deployedBundles = deplMFMgr.generateDeployedBundles(appMetadata, new ArrayList<ModelledResource>(), new ArrayList<Content>());
        deplMFMgr.generateDeploymentManifest(appMetadata.getApplicationSymbolicName(), appMetadata.getApplicationVersion().toString(), deployedBundles);
    } catch (ResolverException rx) {
        List<String> usr = rx.getUnsatisfiedRequirements();
        assertEquals("One unsatisfied requirement expected, not " + usr.size(), usr.size(), 1);
        String chkMsg = "Shared bundle test.shared_1.0.0 has a dependency for package " + "test.shared which is exported from application bundle [test.isolated_1.0.0]";
        assertTrue(chkMsg + " expected, not " + usr, usr.contains(chkMsg));
        return;
    }
    fail("ResolverException expected");
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ArrayList(java.util.ArrayList) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) MethodCall(org.apache.aries.unittest.mocks.MethodCall) ModelledResource(org.apache.aries.application.modelling.ModelledResource) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) Content(org.apache.aries.application.Content) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 9 with ExportedPackage

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

the class DeploymentGeneratorTest method checkMultipleCircularDependenciesDetected.

/**
   * This method checks that the a more complicated circular dependency issues the correct error message
   * and checks that the details listed in the exception are correct. 
   * @throws Exception
   */
@Test
public void checkMultipleCircularDependenciesDetected() throws Exception {
    Collection<ModelledResource> cmr = new ArrayList<ModelledResource>();
    ExportedPackage testIsolated1 = createExportedPackage("test.isolated1", "1.0.0", new String[] { "test.isolated1", "test.isolated2" }, new String[] { "test.shared1", "test.shared2" });
    cmr.add(testIsolated1.getBundle());
    ExportedPackage testIsolated2 = createExportedPackage("test.isolated2", "1.0.0", new String[] { "test.isolated1", "test.isolated2" }, new String[] { "test.shared1", "test.shared2" });
    cmr.add(testIsolated2.getBundle());
    ExportedPackage testShared1 = createExportedPackage("test.shared1", "1.0.0", new String[] { "test.shared1", "test.shared2" }, new String[] { "test.isolated1", "test.isolated2" });
    cmr.add(testShared1.getBundle());
    ExportedPackage testShared2 = createExportedPackage("test.shared2", "1.0.0", new String[] { "test.shared1", "test.shared2" }, new String[] { "test.isolated1", "test.isolated2" });
    cmr.add(testShared2.getBundle());
    _resolver.addResult(cmr);
    // The second time DeploymentGenerator calls the Resolver, it will provide just 
    // test.shared. The resolver will return test.shared _plus_ test.isolated. 
    _resolver.addResult(cmr);
    Skeleton.getSkeleton(appMetadata).setReturnValue(new MethodCall(ApplicationMetadata.class, "getApplicationContents"), Arrays.asList(mockContent("test.isolated1", "1.0.0"), mockContent("test.isolated2", "1.0.0")));
    app = Skeleton.newMock(AriesApplication.class);
    Skeleton.getSkeleton(app).setReturnValue(new MethodCall(AriesApplication.class, "getApplicationMetadata"), appMetadata);
    try {
        DeployedBundles deployedBundles = deplMFMgr.generateDeployedBundles(appMetadata, Arrays.asList(new ModelledResource[] { testIsolated1.getBundle(), testIsolated2.getBundle() }), new ArrayList<Content>());
        deplMFMgr.generateDeploymentManifest(appMetadata.getApplicationSymbolicName(), appMetadata.getApplicationVersion().toString(), deployedBundles);
    } catch (ResolverException rx) {
        // Get the unsatisfied Requirements
        List<String> unsatisfiedReqs = rx.getUnsatisfiedRequirements();
        // Ensure we've got 4 unsatisfied Requirements
        assertEquals("4 unsatisfied requirements expected, but got " + Arrays.toString(unsatisfiedReqs.toArray()), 4, unsatisfiedReqs.size());
        List<String> checkMessages = new ArrayList<String>();
        // Now load an array with the expected messages.
        checkMessages.add("Shared bundle test.shared1_1.0.0 has a dependency for package test.isolated1 which " + "is exported from application bundles [test.isolated1_1.0.0, test.isolated2_1.0.0]");
        checkMessages.add("Shared bundle test.shared1_1.0.0 has a dependency for package test.isolated2 which " + "is exported from application bundles [test.isolated1_1.0.0, test.isolated2_1.0.0]");
        checkMessages.add("Shared bundle test.shared2_1.0.0 has a dependency for package test.isolated1 which " + "is exported from application bundles [test.isolated1_1.0.0, test.isolated2_1.0.0]");
        checkMessages.add("Shared bundle test.shared2_1.0.0 has a dependency for package test.isolated2 which " + "is exported from application bundles [test.isolated1_1.0.0, test.isolated2_1.0.0]");
        // because some unsatisfied reqs have spaces at the end of the string.
        for (String unsatisfiedReq : unsatisfiedReqs) {
            assertTrue(unsatisfiedReq + " is not an expected msg", checkMessages.contains(unsatisfiedReq.trim()));
        }
    }
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ArrayList(java.util.ArrayList) AriesApplication(org.apache.aries.application.management.AriesApplication) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) MethodCall(org.apache.aries.unittest.mocks.MethodCall) ModelledResource(org.apache.aries.application.modelling.ModelledResource) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) Content(org.apache.aries.application.Content) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 10 with ExportedPackage

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

the class ModellerTest method checkTestBundleResource.

private void checkTestBundleResource(ModelledResource resource) {
    assertNotNull(resource);
    assertEquals("file:///test.bundle.uri", resource.getLocation());
    // sanity check that we have parsed the manifest and package imports / exports
    assertEquals("test.bundle", resource.getSymbolicName());
    assertEquals("1.0.0", resource.getVersion());
    assertEquals(1, resource.getExportedPackages().size());
    assertEquals(3, resource.getImportedPackages().size());
    boolean foundFirstPackage = false;
    for (ImportedPackage pack : resource.getImportedPackages()) {
        if ("javax.jms".equals(pack.getPackageName()) && "1.1.0".equals(pack.getVersionRange()))
            foundFirstPackage = true;
    }
    assertTrue(foundFirstPackage);
    ExportedPackage epack = resource.getExportedPackages().iterator().next();
    assertEquals("wibble", epack.getPackageName());
    assertEquals("1.0.0", epack.getVersion());
    assertEquals("true", epack.getAttributes().get("directive:"));
    // sanity check that we have parsed the services
    assertEquals(4, resource.getExportedServices().size());
    assertEquals(4, resource.getImportedServices().size());
    boolean foundFirst = false;
    for (ImportedService service : resource.getImportedServices()) {
        if ("foo.bar.MyInjectedService".equals(service.getInterface())) {
            foundFirst = true;
            assertTrue(service.isOptional());
            assertFalse(service.isList());
            assertEquals("anOptionalReference", service.getId());
        }
    }
    assertTrue(foundFirst);
}
Also used : ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) ImportedService(org.apache.aries.application.modelling.ImportedService)

Aggregations

ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)15 Test (org.junit.Test)8 ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)6 ModelledResource (org.apache.aries.application.modelling.ModelledResource)6 ArrayList (java.util.ArrayList)5 ResolverException (org.apache.aries.application.management.ResolverException)4 List (java.util.List)3 Content (org.apache.aries.application.Content)3 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)2 ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)2 ImportedService (org.apache.aries.application.modelling.ImportedService)2 MethodCall (org.apache.aries.unittest.mocks.MethodCall)2 HashSet (java.util.HashSet)1 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)1 AriesApplication (org.apache.aries.application.management.AriesApplication)1 PreResolveHook (org.apache.aries.application.management.spi.resolve.PreResolveHook)1 ExportedService (org.apache.aries.application.modelling.ExportedService)1