Search in sources :

Example 1 with BundleInfo

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

the class RepositoryDescriptorGenerator method generateRepositoryDescriptor.

public static Document generateRepositoryDescriptor(String name, Set<BundleInfo> bundles) throws ParserConfigurationException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = doc.createElement("repository");
    root.setAttribute("name", name);
    doc.appendChild(root);
    for (BundleInfo info : bundles) {
        Element resource = doc.createElement("resource");
        resource.setAttribute(Resource.VERSION, info.getVersion().toString());
        resource.setAttribute("uri", info.getLocation());
        resource.setAttribute(Resource.SYMBOLIC_NAME, info.getSymbolicName());
        resource.setAttribute(Resource.PRESENTATION_NAME, info.getHeaders().get(Constants.BUNDLE_NAME));
        resource.setAttribute(Resource.ID, info.getSymbolicName() + "/" + info.getVersion());
        root.appendChild(resource);
        addBundleCapability(doc, resource, info);
        for (Content p : info.getExportPackage()) {
            addPackageCapability(doc, resource, info, p);
        }
        for (Content p : info.getImportPackage()) {
            addPackageRequirement(doc, resource, info, p);
        }
        for (Content p : info.getRequireBundle()) {
            addBundleRequirement(doc, resource, info, p);
        }
    }
    return doc;
}
Also used : BundleInfo(org.apache.aries.application.management.BundleInfo) Content(org.apache.aries.application.Content) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 2 with BundleInfo

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

the class ApplicationRepositoryTest method testBundleNotInApp.

@Test
public void testBundleNotInApp() {
    AriesApplication app = Skeleton.newMock(AriesApplication.class);
    BundleInfo bi = Skeleton.newMock(BundleInfo.class);
    Skeleton.getSkeleton(bi).setReturnValue(new MethodCall(BundleInfo.class, "getSymbolicName"), "test.bundle");
    Skeleton.getSkeleton(bi).setReturnValue(new MethodCall(BundleInfo.class, "getVersion"), new Version("1.0.0"));
    Skeleton.getSkeleton(app).setReturnValue(new MethodCall(AriesApplication.class, "getBundleInfo"), new HashSet<BundleInfo>());
    ApplicationRepository rep = new ApplicationRepository(app);
    BundleSuggestion sug = rep.suggestBundleToUse(new DeploymentContentImpl("test.bundle", new Version("2.0.0")));
    assertNull("We have apparently found a bundle that is not in the application in the ApplicationRepository", sug);
}
Also used : DeploymentContentImpl(org.apache.aries.application.impl.DeploymentContentImpl) BundleInfo(org.apache.aries.application.management.BundleInfo) Version(org.osgi.framework.Version) AriesApplication(org.apache.aries.application.management.AriesApplication) BundleSuggestion(org.apache.aries.application.management.spi.repository.BundleRepository.BundleSuggestion) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Test(org.junit.Test)

Example 3 with BundleInfo

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

the class AriesApplicationManagerImplTest method createApplication.

private AriesApplication createApplication(String fileName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ManagementException, ResolverException {
    // This next block is a very long winded way of constructing a BundleInfoImpl
    // against the existing (BundleManifest bm, String location) constructor. If we
    // find we need a String-based BundleInfoImpl constructor for other reasons,
    // we could change to using it here.
    Set<BundleInfo> nextResolverResult = new HashSet<BundleInfo>();
    String persistenceLibraryLocation = "../src/test/resources/bundles/repository/a.handy.persistence.library.jar";
    File persistenceLibrary = new File(persistenceLibraryLocation);
    BundleManifest mf = BundleManifest.fromBundle(persistenceLibrary);
    BundleInfo resolvedPersistenceLibrary = new SimpleBundleInfo(mf, persistenceLibraryLocation);
    Field v = SimpleBundleInfo.class.getDeclaredField("_version");
    v.setAccessible(true);
    v.set(resolvedPersistenceLibrary, new Version("1.1.0"));
    nextResolverResult.add(resolvedPersistenceLibrary);
    _resolver.setNextResult(nextResolverResult);
    IDirectory testEba = FileSystem.getFSRoot(new File(fileName));
    AriesApplication app = _appMgr.createApplication(testEba);
    app = _appMgr.resolve(app);
    return app;
}
Also used : Field(java.lang.reflect.Field) BundleInfo(org.apache.aries.application.management.BundleInfo) SimpleBundleInfo(org.apache.aries.application.utils.management.SimpleBundleInfo) Version(org.osgi.framework.Version) IDirectory(org.apache.aries.util.filesystem.IDirectory) BundleManifest(org.apache.aries.util.manifest.BundleManifest) AriesApplication(org.apache.aries.application.management.AriesApplication) SimpleBundleInfo(org.apache.aries.application.utils.management.SimpleBundleInfo) IFile(org.apache.aries.util.filesystem.IFile) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with BundleInfo

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

the class AriesApplicationManagerImpl method createApplication.

/**
 * Create an AriesApplication from a .eba file: a zip file with a '.eba' extension
 */
public AriesApplication createApplication(IDirectory ebaFile) throws ManagementException {
    ApplicationMetadata applicationMetadata = null;
    DeploymentMetadata deploymentMetadata = null;
    Map<String, BundleConversion> modifiedBundles = new HashMap<String, BundleConversion>();
    AriesApplicationImpl application = null;
    String appPath = ebaFile.toString();
    try {
        // try to read the app name out of the application.mf
        Manifest applicationManifest = parseApplicationManifest(ebaFile);
        String appName = applicationManifest.getMainAttributes().getValue(AppConstants.APPLICATION_NAME);
        // If the application name is null, we will try to get the file name.
        if (appName == null || appName.isEmpty()) {
            String fullPath = appPath;
            if (fullPath.endsWith("/")) {
                fullPath = fullPath.substring(0, fullPath.length() - 1);
            }
            int last_slash = fullPath.lastIndexOf("/");
            appName = fullPath.substring(last_slash + 1, fullPath.length());
        }
        IFile deploymentManifest = ebaFile.getFile(AppConstants.DEPLOYMENT_MF);
        /* We require that all other .jar and .war files included by-value be valid bundles
       * because a DEPLOYMENT.MF has been provided. If no DEPLOYMENT.MF, migrate 
       * wars to wabs, plain jars to bundles
       */
        Set<BundleInfo> extraBundlesInfo = new HashSet<BundleInfo>();
        for (IFile f : ebaFile) {
            if (f.isDirectory()) {
                continue;
            }
            BundleManifest bm = getBundleManifest(f);
            if (bm != null) {
                if (bm.isValid()) {
                    _logger.debug("File {} is a valid bundle. Adding it to bundle list.", f.getName());
                    extraBundlesInfo.add(new SimpleBundleInfo(bm, f.toURL().toExternalForm()));
                } else if (deploymentManifest == null) {
                    _logger.debug("File {} is not a valid bundle. Attempting to convert it.", f.getName());
                    // We have a jar that needs converting to a bundle, or a war to migrate to a WAB
                    // We only do this if a DEPLOYMENT.MF does not exist.
                    BundleConversion convertedBinary = null;
                    Iterator<BundleConverter> converters = _bundleConverters.iterator();
                    List<ConversionException> conversionExceptions = Collections.emptyList();
                    while (converters.hasNext() && convertedBinary == null) {
                        try {
                            BundleConverter converter = converters.next();
                            _logger.debug("Converting file using {} converter", converter);
                            convertedBinary = converter.convert(ebaFile, f);
                        } catch (ServiceException sx) {
                        // We'll get this if our optional BundleConverter has not been injected.
                        } catch (ConversionException cx) {
                            conversionExceptions.add(cx);
                        }
                    }
                    if (conversionExceptions.size() > 0) {
                        for (ConversionException cx : conversionExceptions) {
                            _logger.error("APPMANAGEMENT0004E", new Object[] { f.getName(), appName, cx });
                        }
                        throw new ManagementException(MessageUtil.getMessage("APPMANAGEMENT0005E", appName));
                    }
                    if (convertedBinary != null) {
                        _logger.debug("File {} was successfully converted. Adding it to bundle list.", f.getName());
                        modifiedBundles.put(f.getName(), convertedBinary);
                        extraBundlesInfo.add(convertedBinary.getBundleInfo());
                    } else {
                        _logger.debug("File {} was not converted.", f.getName());
                    }
                } else {
                    _logger.debug("File {} was ignored. It is not a valid bundle and DEPLOYMENT.MF is present", f.getName());
                }
            } else {
                _logger.debug("File {} was ignored. It has no manifest file.", f.getName());
            }
        }
        // if Application-Content header was not specified build it based on the bundles included by value
        if (applicationManifest.getMainAttributes().getValue(AppConstants.APPLICATION_CONTENT) == null) {
            String appContent = buildAppContent(extraBundlesInfo);
            applicationManifest.getMainAttributes().putValue(AppConstants.APPLICATION_CONTENT, appContent);
        }
        ManifestDefaultsInjector.updateManifest(applicationManifest, appName, ebaFile);
        applicationMetadata = _applicationMetadataFactory.createApplicationMetadata(applicationManifest);
        if (deploymentManifest != null) {
            deploymentMetadata = _deploymentMetadataFactory.parseDeploymentMetadata(deploymentManifest);
            // Validate: symbolic names must match
            String appSymbolicName = applicationMetadata.getApplicationSymbolicName();
            String depSymbolicName = deploymentMetadata.getApplicationSymbolicName();
            if (!appSymbolicName.equals(depSymbolicName)) {
                throw new ManagementException(MessageUtil.getMessage("APPMANAGEMENT0002E", appName, appSymbolicName, depSymbolicName));
            }
        }
        application = new AriesApplicationImpl(applicationMetadata, extraBundlesInfo, _localPlatform);
        application.setDeploymentMetadata(deploymentMetadata);
        // Store a reference to any modified bundles
        application.setModifiedBundles(modifiedBundles);
    } catch (IOException iox) {
        _logger.error("APPMANAGEMENT0006E", new Object[] { appPath, iox });
        throw new ManagementException(iox);
    }
    return application;
}
Also used : ConversionException(org.apache.aries.application.management.spi.convert.ConversionException) DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) IFile(org.apache.aries.util.filesystem.IFile) HashMap(java.util.HashMap) BundleManifest(org.apache.aries.util.manifest.BundleManifest) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) BundleManifest(org.apache.aries.util.manifest.BundleManifest) ResolveConstraint(org.apache.aries.application.management.ResolveConstraint) BundleConverter(org.apache.aries.application.management.spi.convert.BundleConverter) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) ManagementException(org.apache.aries.application.management.ManagementException) BundleInfo(org.apache.aries.application.management.BundleInfo) SimpleBundleInfo(org.apache.aries.application.utils.management.SimpleBundleInfo) ServiceException(org.osgi.framework.ServiceException) Iterator(java.util.Iterator) SimpleBundleInfo(org.apache.aries.application.utils.management.SimpleBundleInfo) List(java.util.List) BundleConversion(org.apache.aries.application.management.spi.convert.BundleConversion) HashSet(java.util.HashSet)

Example 5 with BundleInfo

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

Aggregations

BundleInfo (org.apache.aries.application.management.BundleInfo)13 SimpleBundleInfo (org.apache.aries.application.utils.management.SimpleBundleInfo)5 HashSet (java.util.HashSet)4 Version (org.osgi.framework.Version)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)3 Content (org.apache.aries.application.Content)3 AriesApplication (org.apache.aries.application.management.AriesApplication)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DeploymentMetadata (org.apache.aries.application.DeploymentMetadata)2 DeploymentContentImpl (org.apache.aries.application.impl.DeploymentContentImpl)2 ResolveConstraint (org.apache.aries.application.management.ResolveConstraint)2 BundleConversion (org.apache.aries.application.management.spi.convert.BundleConversion)2