Search in sources :

Example 6 with ImportedBundle

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

the class DeployedBundlesTest method validDeployedBundles.

private DeployedBundles validDeployedBundles() throws Exception {
    Collection<ImportedBundle> content = new ArrayList<ImportedBundle>();
    Collection<ImportedBundle> uses = new ArrayList<ImportedBundle>();
    content.add(new ImportedBundleImpl("bundle.a", "1.0.0"));
    content.add(new ImportedBundleImpl("bundle.b", "1.0.0"));
    uses.add(new ImportedBundleImpl("bundle.c", "1.0.0"));
    uses.add(new ImportedBundleImpl("bundle.d", "1.0.0"));
    return new ModellingHelperImpl().createDeployedBundles("test", content, uses, null);
}
Also used : ImportedBundleImpl(org.apache.aries.application.modelling.impl.ImportedBundleImpl) ArrayList(java.util.ArrayList) ImportedBundle(org.apache.aries.application.modelling.ImportedBundle) ModellingHelperImpl(org.apache.aries.application.modelling.utils.impl.ModellingHelperImpl)

Example 7 with ImportedBundle

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

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

the class DeploymentManifestManagerImpl method toImportedBundle.

/**
   * Covert a collection of contents to a collection of ImportedBundle objects
   * @param content a collection of content
   * @return a collection of ImportedBundle objects
   * @throws ResolverException
   */
private Set<ImportedBundle> toImportedBundle(Collection<Content> content) throws ResolverException {
    _logger.debug(LOG_ENTRY, "toImportedBundle", new Object[] { content });
    Set<ImportedBundle> result = new HashSet<ImportedBundle>();
    for (Content c : content) {
        try {
            result.add(modellingManager.getImportedBundle(c.getContentName(), c.getVersion().toString()));
        } catch (InvalidAttributeException iax) {
            ResolverException rx = new ResolverException(iax);
            _logger.debug(LOG_EXIT, "toImportedBundle", new Object[] { rx });
            throw rx;
        }
    }
    _logger.debug(LOG_EXIT, "toImportedBundle", new Object[] { result });
    return result;
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) Content(org.apache.aries.application.Content) ImportedBundle(org.apache.aries.application.modelling.ImportedBundle) HashSet(java.util.HashSet)

Example 9 with ImportedBundle

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

the class AbstractBundleResourceTest method testBundleResource.

@Test
public void testBundleResource() throws Exception {
    assertEquals("The bundle symbolic name is wrong.", "test.bundle1", bundleResource.getSymbolicName());
    assertEquals("The bundle version is wrong.", "2.0.0.build-121", bundleResource.getVersion().toString());
    assertEquals("The bundle presentation name is wrong.", "Test Bundle", bundleResource.getExportedBundle().getAttributes().get(ModellingConstants.OBR_PRESENTATION_NAME));
    int count = 0;
    for (ImportedPackage ip : bundleResource.getImportedPackages()) {
        String filter = ip.getAttributeFilter();
        Map<String, String> parsedFilterElements = ManifestHeaderProcessor.parseFilter(filter);
        if (ip.getPackageName().equals("org.osgi.framework")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "org.osgi.framework");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "1.3.0");
        } else if (ip.getPackageName().equals("aries.ws.kernel.file")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.kernel.file");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "0.0.0");
        } else if (ip.getPackageName().equals("aries.wsspi.application.aries")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.wsspi.application.aries");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "0.0.0");
            assertEquals("Company wrong", parsedFilterElements.get("company"), "yang");
            assertTrue("mandatory filter missing", filter.contains("(mandatory:<*company)"));
        } else if (ip.getPackageName().equals("aries.ws.ffdc")) {
            count++;
            assertEquals("The filter is wrong.", "(&(package=aries.ws.ffdc)(version>=0.0.0))", ip.getAttributeFilter());
            assertTrue("Optional import not correctly represented", ip.isOptional());
        } else if (ip.getPackageName().equals("aries.ws.app.framework.plugin")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.app.framework.plugin");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "[1.0.0,2.0.0)");
        } else if (ip.getPackageName().equals("aries.ejs.ras")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ejs.ras");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "0.0.0");
        } else if (ip.getPackageName().equals("aries.ws.event")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.event");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "1.0.0");
        } else if (ip.getPackageName().equals("aries.wsspi.app.container.aries")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.wsspi.app.container.aries");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "0.0.0");
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("bundle-symbolic-name"), "B");
            assertEquals("Wrong bundle version", parsedFilterElements.get("bundle-version"), "[1.2.0,2.2.0)");
        } else if (ip.getPackageName().equals("aries.ws.eba.bla")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.eba.bla");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "0.0.0");
        } else if (ip.getPackageName().equals("aries.ws.eba.launcher")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.eba.launcher");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "[1.0.0,2.0.0]");
            assertTrue("Dynamic-ImportPackage should be optional", ip.isOptional());
        } else if (ip.getPackageName().equals("aries.ws.eba.bundle4")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.eba.bundle4");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "3.0.0");
        } else if (ip.getPackageName().equals("aries.ws.eba.bundle5")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.eba.bundle5");
            assertEquals("Wrong package version", parsedFilterElements.get("version"), "3.0.0");
        } else if (ip.getPackageName().equals("aries.ws.eba.bundle6")) {
            count++;
            assertEquals("Wrong package", parsedFilterElements.get("package"), "aries.ws.eba.bundle6");
            assertEquals("The filter is wrong.", "(&(package=aries.ws.eba.bundle6)(version>=0.0.0))", ip.getAttributeFilter());
        } else if (ip.getPackageName().equals("aries.ws.eba.bundle7")) {
            count++;
            assertEquals("The filter is wrong.", "(&(package=aries.ws.eba.bundle7)(version>=0.0.0))", ip.getAttributeFilter());
        }
    }
    for (ImportedBundle ib : bundleResource.getRequiredBundles()) {
        String filter = ib.getAttributeFilter();
        Map<String, String> parsedFilterElements = ManifestHeaderProcessor.parseFilter(filter);
        if (ib.getSymbolicName().equals("com.acme.facade")) {
            count++;
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("symbolicname"), "com.acme.facade");
            assertEquals("Wrong bundle version", parsedFilterElements.get("version"), "3.0.0");
        } else if (ib.getSymbolicName().equals("com.acme.bar")) {
            count++;
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("symbolicname"), "com.acme.bar");
        } else if (ib.getSymbolicName().equals("aries.ws.eba.framework")) {
            count++;
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("symbolicname"), "aries.ws.eba.framework");
            assertEquals("Wrong bundle version", parsedFilterElements.get("version"), "(3.0.0,4.0.0)");
        } else if (ib.getSymbolicName().equals("com.de.ba")) {
            count++;
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("symbolicname"), "com.de.ba");
        } else if (ib.getSymbolicName().equals("com.ab.de")) {
            count++;
            assertEquals("Wrong bundle symbolic name", parsedFilterElements.get("symbolicname"), "com.ab.de");
        }
    }
    for (ImportedService svc : bundleResource.getImportedServices()) {
        if (svc.getInterface().equals("aries.ws.eba.import")) {
            count++;
            String filter = svc.getAttributeFilter();
            Map<String, String> parsedFilterElements = ManifestHeaderProcessor.parseFilter(filter);
            assertEquals("Wrong object class", parsedFilterElements.get("objectClass"), "aries.ws.eba.import");
            assertTrue("(service=service) should be present", svc.getAttributeFilter().contains("(service=service)"));
            assertTrue("(mandatory:<*service) should be present", svc.getAttributeFilter().contains("(mandatory:<*service)"));
        }
    }
    assertEquals("Not all requirements are listed.", bundleResource.getImportedPackages().size() + bundleResource.getImportedServices().size() + bundleResource.getRequiredBundles().size(), count);
    //verify the capability
    int verifiedExport = 0;
    for (ExportedPackage cap : bundleResource.getExportedPackages()) {
        if (cap.getPackageName().equals("aries.ws.eba.bundle1")) {
            verifiedExport++;
            assertEquals("The export package is not expected.", "2.2.0", cap.getVersion());
            assertEquals("The export package is not expected.", "test.bundle1", cap.getAttributes().get("bundle-symbolic-name"));
            assertEquals("The export package is not expected.", "2.0.0.build-121", cap.getAttributes().get("bundle-version").toString());
        } else if (cap.getPackageName().equals("aries.ws.eba.bundle2")) {
            verifiedExport++;
            assertEquals("The export package is not expected.", "3", cap.getVersion());
        } else if (cap.getPackageName().equals("aries.ws.eba.bundle3")) {
            verifiedExport++;
            assertEquals("The export package is not expected.", "3", cap.getVersion());
        }
    }
    assertEquals("The number of exports are not expected.", bundleResource.getExportedPackages().size(), verifiedExport);
    // bundle resource
    assertEquals("The bundle resource is wrong.", "Test Bundle", bundleResource.getExportedBundle().getAttributes().get(ModellingConstants.OBR_PRESENTATION_NAME));
    assertEquals("The bundle resource is wrong.", "2.0.0.build-121", bundleResource.getExportedBundle().getVersion());
    assertEquals("The bundle resource is wrong.", "test.bundle1", bundleResource.getExportedBundle().getSymbolicName());
    for (ExportedService svc : bundleResource.getExportedServices()) {
        assertEquals("The export service is wrong", "aries.ws.eba.export", svc.getInterfaces().iterator().next());
    }
}
Also used : ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) ExportedService(org.apache.aries.application.modelling.ExportedService) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) ImportedService(org.apache.aries.application.modelling.ImportedService) ImportedBundle(org.apache.aries.application.modelling.ImportedBundle) Test(org.junit.Test)

Example 10 with ImportedBundle

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

the class ModellingHelperImpl method buildFragmentHost_.

public static ImportedBundle buildFragmentHost_(String fragmentHostHeader) throws InvalidAttributeException {
    logger.debug(LOG_ENTRY, "buildFragmentHost_", new Object[] { fragmentHostHeader });
    if (fragmentHostHeader == null) {
        return null;
    }
    Map<String, Map<String, String>> parsedFragHost = ManifestHeaderProcessor.parseImportString(fragmentHostHeader);
    if (parsedFragHost.size() != 1)
        throw new InvalidAttributeException(MessageUtil.getMessage("MORE_THAN_ONE_FRAG_HOST", new Object[] { fragmentHostHeader }, "An internal error occurred. A bundle fragment manifest must define exactly one Fragment-Host entry. The following entry was found" + fragmentHostHeader + "."));
    String hostName = parsedFragHost.keySet().iterator().next();
    Map<String, String> attribs = parsedFragHost.get(hostName);
    String bundleVersion = attribs.remove(BUNDLE_VERSION_ATTRIBUTE);
    if (bundleVersion != null && attribs.get(VERSION_ATTRIBUTE) == null) {
        attribs.put(VERSION_ATTRIBUTE, bundleVersion);
    }
    attribs.put(ModellingConstants.OBR_SYMBOLIC_NAME, hostName);
    String filter = ManifestHeaderProcessor.generateFilter(attribs);
    ImportedBundle result = new ImportedBundleImpl(filter, attribs);
    logger.debug(LOG_EXIT, "buildFragmentHost_", result);
    return result;
}
Also used : InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) ImportedBundleImpl(org.apache.aries.application.modelling.impl.ImportedBundleImpl) ImportedBundle(org.apache.aries.application.modelling.ImportedBundle) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)11 ArrayList (java.util.ArrayList)4 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)4 Content (org.apache.aries.application.Content)3 ResolverException (org.apache.aries.application.management.ResolverException)3 ImportedBundleImpl (org.apache.aries.application.modelling.impl.ImportedBundleImpl)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 AriesApplicationResolver (org.apache.aries.application.management.spi.resolve.AriesApplicationResolver)2 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)2 ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)2 ModellingHelperImpl (org.apache.aries.application.modelling.utils.impl.ModellingHelperImpl)2 Resolver (org.apache.felix.bundlerepository.Resolver)2 List (java.util.List)1 PreResolveHook (org.apache.aries.application.management.spi.resolve.PreResolveHook)1 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)1 ExportedService (org.apache.aries.application.modelling.ExportedService)1 ImportedService (org.apache.aries.application.modelling.ImportedService)1 ModelledResource (org.apache.aries.application.modelling.ModelledResource)1