Search in sources :

Example 6 with ImportedPackage

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

the class ImportedPackageImpl method equals.

@Override
public boolean equals(Object thing) {
    if (thing == this) {
        return true;
    } else {
        if (thing instanceof ImportedPackage) {
            ImportedPackage otherPackage = (ImportedPackage) thing;
            if (!this.getPackageName().equals(otherPackage.getPackageName())) {
                return false;
            }
            if (!this.getVersionRange().equals(otherPackage.getVersionRange())) {
                return false;
            }
            if (!this.getPackageName().equals(otherPackage.getPackageName())) {
                return false;
            }
            Map<String, String> otherAttributes = otherPackage.getAttributes();
            if (!attributesAreEqual(otherAttributes)) {
                return false;
            }
        }
        return true;
    }
}
Also used : ImportedPackage(org.apache.aries.application.modelling.ImportedPackage)

Example 7 with ImportedPackage

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

the class ImportedPackageTest method testEqualsForTrivialPackagesWithDifferentVersion.

@Test
public void testEqualsForTrivialPackagesWithDifferentVersion() throws InvalidAttributeException {
    String packageName = "package.name";
    ImportedPackage package1 = instantiatePackage(packageName, "1.0.0");
    ImportedPackage package2 = instantiatePackage(packageName, "1.0.1");
    assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
}
Also used : ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) Test(org.junit.Test)

Example 8 with ImportedPackage

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

the class PackageRequirementMergerTest method testMergeValid.

@Test
public void testMergeValid() throws Exception {
    Collection<ImportedPackage> reqs = new ArrayList<ImportedPackage>();
    reqs.add(newImportedPackage("a", "1.0.0"));
    reqs.add(newImportedPackage("a", "2.0.0"));
    reqs.add(newImportedPackage("a", "3.0.0"));
    reqs.add(newImportedPackage("b", "1.0.0"));
    reqs.add(newImportedPackage("b", "2.0.0"));
    reqs.add(newImportedPackage("c", "1.0.0"));
    PackageRequirementMerger merger = new PackageRequirementMerger(reqs);
    Assert.assertTrue(merger.isMergeSuccessful());
    Assert.assertTrue(merger.getInvalidRequirements().isEmpty());
    Collection<ImportedPackage> result = merger.getMergedRequirements();
    Collection<ImportedPackage> expected = new ArrayList<ImportedPackage>();
    expected.add(newImportedPackage("a", "3.0.0"));
    expected.add(newImportedPackage("b", "2.0.0"));
    expected.add(newImportedPackage("c", "1.0.0"));
    Assert.assertTrue(result.toString(), isEqual(result, expected));
}
Also used : ArrayList(java.util.ArrayList) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) PackageRequirementMerger(org.apache.aries.application.modelling.internal.PackageRequirementMerger) Test(org.junit.Test)

Example 9 with ImportedPackage

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

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

ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)23 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)9 PackageRequirementMerger (org.apache.aries.application.modelling.internal.PackageRequirementMerger)7 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 ResolverException (org.apache.aries.application.management.ResolverException)3 ModelledResource (org.apache.aries.application.modelling.ModelledResource)3 Map (java.util.Map)2 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)2 ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)2 ImportedService (org.apache.aries.application.modelling.ImportedService)2 VersionRange (org.apache.aries.util.VersionRange)2 List (java.util.List)1 Content (org.apache.aries.application.Content)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 ImportedPackageImpl (org.apache.aries.application.modelling.impl.ImportedPackageImpl)1