Search in sources :

Example 6 with Content

use of org.apache.aries.application.Content in project aries by apache.

the class RepositoryGenerator method find.

/**
     * the format of resource is like bundlesymbolicname;version=1.0.0, for example com.ibm.ws.eba.example.blog.api;version=1.0.0,
     */
@SuppressWarnings({ "rawtypes", "unused" })
public Resource find(String resource) throws SubsystemException {
    generateOBR();
    Content content = new ContentImpl(resource);
    String symbolicName = content.getContentName();
    // this version could possibly be a range
    String version = content.getVersion().toString();
    StringBuilder filterString = new StringBuilder();
    filterString.append("(&(name" + "=" + symbolicName + "))");
    filterString.append("(version" + "=" + version + "))");
    //org.apache.felix.bundlerepository.Resource[] res = this.repositoryAdmin.discoverResources(filterString.toString());
    Repository[] repos = this.repositoryAdmin.listRepositories();
    org.apache.felix.bundlerepository.Resource res = null;
    for (Repository repo : repos) {
        org.apache.felix.bundlerepository.Resource[] resources = repo.getResources();
        for (int i = 0; i < resources.length; i++) {
            if (resources[i].getSymbolicName().equals(symbolicName)) {
                if (resources[i].getVersion().compareTo(new Version(version)) == 0) {
                    res = resources[i];
                    break;
                }
            }
        }
    }
    if (res == null) {
        //            throw new SubsystemException("unable to find the resource " + resource);
        return null;
    }
    Map props = res.getProperties();
    Object type = props.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE);
    return new FelixResourceAdapter(res);
}
Also used : Resource(org.osgi.resource.Resource) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Repository(org.apache.felix.bundlerepository.Repository) Version(org.osgi.framework.Version) Content(org.apache.aries.application.Content) Map(java.util.Map)

Example 7 with Content

use of org.apache.aries.application.Content in project aries by apache.

the class OBRAriesResolver method resolve.

@Deprecated
@Override
public Set<BundleInfo> resolve(AriesApplication app, ResolveConstraint... constraints) throws ResolverException {
    log.trace("resolving {}", app);
    ApplicationMetadata appMeta = app.getApplicationMetadata();
    String appName = appMeta.getApplicationSymbolicName();
    Version appVersion = appMeta.getApplicationVersion();
    List<Content> appContent = appMeta.getApplicationContents();
    Collection<Content> useBundleContent = appMeta.getUseBundles();
    List<Content> contents = new ArrayList<Content>();
    contents.addAll(appContent);
    contents.addAll(useBundleContent);
    if ((constraints != null) && (constraints.length > 0)) {
        for (ResolveConstraint con : constraints) {
            contents.add(ContentFactory.parseContent(con.getBundleName(), con.getVersionRange().toString()));
        }
    }
    Resolver obrResolver = getConfiguredObrResolver(appName, appVersion.toString(), toModelledResource(app.getBundleInfo()), false);
    // add a resource describing the requirements of the application metadata.
    obrResolver.add(createApplicationResource(appName, appVersion, contents));
    if (obrResolver.resolve()) {
        Set<BundleInfo> result = new HashSet<BundleInfo>();
        List<Resource> requiredResources = retrieveRequiredResources(obrResolver);
        for (Resource resource : requiredResources) {
            BundleInfo bundleInfo = toBundleInfo(resource, false);
            result.add(bundleInfo);
        }
        if (returnOptionalResources) {
            for (Resource resource : obrResolver.getOptionalResources()) {
                BundleInfo bundleInfo = toBundleInfo(resource, true);
                result.add(bundleInfo);
            }
        }
        return result;
    } else {
        Reason[] reasons = obrResolver.getUnsatisfiedRequirements();
        //refine the list by removing the indirect unsatisfied bundles that are caused by unsatisfied packages or other bundles
        Map<String, Set<String>> refinedReqs = refineUnsatisfiedRequirements(obrResolver, reasons);
        StringBuffer reqList = new StringBuffer();
        Map<String, String> unsatisfiedRequirements = extractConsumableMessageInfo(refinedReqs);
        for (String reason : unsatisfiedRequirements.keySet()) {
            reqList.append('\n');
            reqList.append(reason);
        }
        ResolverException re = new ResolverException(MessageUtil.getMessage("RESOLVER_UNABLE_TO_RESOLVE", new Object[] { app.getApplicationMetadata().getApplicationName(), reqList }));
        re.setUnsatisfiedRequirementsAndReasons(unsatisfiedRequirements);
        log.debug(LOG_EXIT, "resolve", re);
        throw re;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ResolverException(org.apache.aries.application.management.ResolverException) ResolveConstraint(org.apache.aries.application.management.ResolveConstraint) Resolver(org.apache.felix.bundlerepository.Resolver) AriesApplicationResolver(org.apache.aries.application.management.spi.resolve.AriesApplicationResolver) ArrayList(java.util.ArrayList) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) Reason(org.apache.felix.bundlerepository.Reason) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) BundleInfo(org.apache.aries.application.management.BundleInfo) OBRBundleInfo(org.apache.aries.application.resolver.obr.impl.OBRBundleInfo) Version(org.osgi.framework.Version) Content(org.apache.aries.application.Content) HashSet(java.util.HashSet)

Example 8 with Content

use of org.apache.aries.application.Content in project aries by apache.

the class EquinoxFrameworkUtils method getSystemExtraPkgs.

public static Collection<Content> getSystemExtraPkgs(BundleContext context) {
    Set<Content> extraPkgs = new HashSet<Content>();
    String exportString = context.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
    if (exportString != null) {
        for (NameValuePair nvp : ManifestHeaderProcessor.parseExportString(exportString)) extraPkgs.add(ContentFactory.parseContent(nvp.getName(), nvp.getAttributes()));
    }
    return Collections.unmodifiableSet(extraPkgs);
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) Content(org.apache.aries.application.Content) HashSet(java.util.HashSet)

Example 9 with Content

use of org.apache.aries.application.Content in project aries by apache.

the class EquinoxFrameworkUtils method existInExports.

/**
   * check if the value in nvm already exist in the exports
   * @param key
   * @param nvm
   * @param exports
   * @return boolean whether the value in nvm already exist in the exports
   */
private static boolean existInExports(String key, Map<String, String> nvm, final Collection<Content> exports) {
    boolean value = false;
    for (Content nvp : exports) {
        if (nvp.getContentName().trim().equals(key.trim())) {
            // ok key equal.  let's check the version
            // if version is higher, we still want to import, for example javax.transaction;version=1.1
            String vi = nvm.get(Constants.VERSION_ATTRIBUTE);
            String ve = nvp.getNameValueMap().get(Constants.VERSION_ATTRIBUTE);
            if (vi == null || vi.length() == 0) {
                vi = "0.0.0";
            }
            if (ve == null || ve.length() == 0) {
                ve = "0.0.0";
            }
            if (vi.indexOf(",") == -1) {
                if (new Version(vi).compareTo(new Version(ve)) <= 0) {
                    // we got it covered in our exports
                    value = true;
                }
            } else {
                // parse vi into version range.
                VersionRange vri = ManifestHeaderProcessor.parseVersionRange(vi);
                Version minV = vri.getMinimumVersion();
                Version maxV = vri.getMaximumVersion();
                if (minV.compareTo(new Version(ve)) < 0 && maxV.compareTo(new Version(ve)) > 0) {
                    value = true;
                } else if (minV.compareTo(new Version(ve)) == 0 && !!!vri.isMinimumExclusive()) {
                    value = true;
                } else if (maxV.compareTo(new Version(ve)) == 0 && !!!vri.isMaximumExclusive()) {
                    value = true;
                }
            }
        }
    }
    return value;
}
Also used : Version(org.osgi.framework.Version) Content(org.apache.aries.application.Content) VersionRange(org.apache.aries.util.VersionRange)

Example 10 with Content

use of org.apache.aries.application.Content 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)

Aggregations

Content (org.apache.aries.application.Content)27 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 ModelledResource (org.apache.aries.application.modelling.ModelledResource)9 AriesApplication (org.apache.aries.application.management.AriesApplication)8 ResolverException (org.apache.aries.application.management.ResolverException)8 Version (org.osgi.framework.Version)8 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)7 HashSet (java.util.HashSet)6 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 DeploymentContent (org.apache.aries.application.DeploymentContent)5 HashMap (java.util.HashMap)4 DeploymentMetadata (org.apache.aries.application.DeploymentMetadata)4 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)4 VersionRange (org.apache.aries.util.VersionRange)4 List (java.util.List)3 Map (java.util.Map)3 Manifest (java.util.jar.Manifest)3 ApplicationMetadataFactoryImpl (org.apache.aries.application.impl.ApplicationMetadataFactoryImpl)3