Search in sources :

Example 11 with ResolverException

use of org.apache.aries.application.management.ResolverException 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 12 with ResolverException

use of org.apache.aries.application.management.ResolverException 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 13 with ResolverException

use of org.apache.aries.application.management.ResolverException in project aries by apache.

the class DeploymentGeneratorTest method checkBundleInAppContentAndProvisionContent.

@Test
public void checkBundleInAppContentAndProvisionContent() throws Exception {
    List<ModelledResource> cmr = new ArrayList<ModelledResource>();
    cmr.add(createModelledResource("test.api", "1.1.0", Collections.<String>emptyList(), Arrays.asList("test.api.pack;version=1.1.0")));
    cmr.add(createModelledResource("test.api", "1.0.0", Collections.<String>emptyList(), Arrays.asList("test.api.pack;version=1.0.0")));
    cmr.add(createModelledResource("test.consumer", "1.0.0", Arrays.asList("test.api.pack;version=\"[1.0.0,2.0.0)\""), Collections.<String>emptyList()));
    cmr.add(createModelledResource("test.provider", "1.0.0", Arrays.asList("test.api.pack;version=\"[1.0.0,1.1.0)\""), Collections.<String>emptyList()));
    // 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.api", "1.1.0"), mockContent("test.consumer", "1.0.0"), mockContent("test.provider", "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[] { cmr.get(0), cmr.get(2), cmr.get(3) }), new ArrayList<Content>());
        deplMFMgr.generateDeploymentManifest(appMetadata.getApplicationSymbolicName(), appMetadata.getApplicationVersion().toString(), deployedBundles);
        fail("Expected exception because we can't provision an isolated bundle twice");
    } catch (ResolverException rx) {
    }
}
Also used : ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) ResolverException(org.apache.aries.application.management.ResolverException) Content(org.apache.aries.application.Content) 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) Test(org.junit.Test)

Example 14 with ResolverException

use of org.apache.aries.application.management.ResolverException 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 15 with ResolverException

use of org.apache.aries.application.management.ResolverException in project aries by apache.

the class DeploymentManifestManagerImpl method generateDeploymentManifest.

/**
   * Perform provisioning to work out the 'freeze dried list' of the eba
   * @param app - Aries application
   * @param ResolveConstraint - resolver constraint for limiting the resolving results
   * @return manifest the generated deployment manifest
   * @throws ResolverException
   */
@Override
public Manifest generateDeploymentManifest(AriesApplication app, ResolveConstraint... constraints) throws ResolverException {
    _logger.debug(LOG_ENTRY, "generateDeploymentManifest", new Object[] { app, constraints });
    ApplicationMetadata appMetadata = app.getApplicationMetadata();
    Collection<ModelledResource> byValueBundles = null;
    try {
        // find out blueprint information
        byValueBundles = getByValueBundles(app);
    // find out by value bundles and then by reference bundles
    } catch (Exception e) {
        throw new ResolverException(e);
    }
    Collection<Content> bundlesToResolve = new ArrayList<Content>();
    bundlesToResolve.addAll(appMetadata.getApplicationContents());
    bundlesToResolve.addAll(app.getApplicationMetadata().getUseBundles());
    //If we pass in provision bundles (e.g. import deployment manifest sanity check), we add them into our bundlesToResolve set.
    // This is because we want to make sure all bundles we passed into resolver the same as what we are going to get from resolver. 
    List<Content> restrictedReqs = new ArrayList<Content>();
    for (ResolveConstraint constraint : constraints) {
        Content content = ContentFactory.parseContent(constraint.getBundleName(), constraint.getVersionRange().toString());
        restrictedReqs.add(content);
    }
    DeployedBundles deployedBundles = generateDeployedBundles(appMetadata, byValueBundles, restrictedReqs);
    Manifest man = generateDeploymentManifest(appMetadata.getApplicationSymbolicName(), appMetadata.getApplicationVersion().toString(), deployedBundles);
    _logger.debug(LOG_EXIT, "generateDeploymentManifest", new Object[] { man });
    return man;
}
Also used : ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) ResolverException(org.apache.aries.application.management.ResolverException) ResolveConstraint(org.apache.aries.application.management.ResolveConstraint) Content(org.apache.aries.application.Content) ArrayList(java.util.ArrayList) DeployedBundles(org.apache.aries.application.modelling.DeployedBundles) Manifest(java.util.jar.Manifest) ResolverException(org.apache.aries.application.management.ResolverException) IOException(java.io.IOException) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) ModellerException(org.apache.aries.application.modelling.ModellerException) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Aggregations

ResolverException (org.apache.aries.application.management.ResolverException)26 ArrayList (java.util.ArrayList)20 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)17 Test (org.junit.Test)15 ModelledResource (org.apache.aries.application.modelling.ModelledResource)11 Content (org.apache.aries.application.Content)8 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)6 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)5 HashSet (java.util.HashSet)4 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)4 List (java.util.List)3 ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)3 ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)3 MethodCall (org.apache.aries.unittest.mocks.MethodCall)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Manifest (java.util.jar.Manifest)2 AriesApplication (org.apache.aries.application.management.AriesApplication)2 ResolveConstraint (org.apache.aries.application.management.ResolveConstraint)2