Search in sources :

Example 1 with DeploymentMetadata

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

the class OBRResolverAdvancedTest method testDemoApp.

@Test
public void testDemoApp() throws Exception {
    // do not provision against the local runtime
    System.setProperty(AppConstants.PROVISON_EXCLUDE_LOCAL_REPO_SYSPROP, "true");
    generateOBRRepoXML(false, TRANSITIVE_BUNDLE_BY_REFERENCE + ".jar", CORE_BUNDLE_BY_REFERENCE + ".jar", USE_BUNDLE_BY_REFERENCE + ".jar");
    RepositoryAdmin repositoryAdmin = context().getService(RepositoryAdmin.class);
    Repository[] repos = repositoryAdmin.listRepositories();
    for (Repository repo : repos) {
        repositoryAdmin.removeRepository(repo.getURI());
    }
    repositoryAdmin.addRepository(new File("repository.xml").toURI().toURL());
    AriesApplicationManager manager = context().getService(AriesApplicationManager.class);
    AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("demo.eba")));
    // installing requires a valid url for the bundle in repository.xml.
    app = manager.resolve(app);
    DeploymentMetadata depMeta = app.getDeploymentMetadata();
    List<DeploymentContent> provision = depMeta.getApplicationProvisionBundles();
    Collection<DeploymentContent> useBundles = depMeta.getDeployedUseBundle();
    Collection<Content> importPackages = depMeta.getImportPackage();
    assertEquals(provision.toString(), 2, provision.size());
    assertEquals(useBundles.toString(), 1, useBundles.size());
    assertEquals(importPackages.toString(), 4, importPackages.size());
    List<String> bundleSymbolicNames = new ArrayList<String>();
    for (DeploymentContent dep : provision) {
        bundleSymbolicNames.add(dep.getContentName());
    }
    assertTrue("Bundle " + TRANSITIVE_BUNDLE_BY_REFERENCE + " not found.", bundleSymbolicNames.contains(TRANSITIVE_BUNDLE_BY_REFERENCE));
    assertTrue("Bundle " + TRANSITIVE_BUNDLE_BY_VALUE + " not found.", bundleSymbolicNames.contains(TRANSITIVE_BUNDLE_BY_VALUE));
    bundleSymbolicNames.clear();
    for (DeploymentContent dep : useBundles) {
        bundleSymbolicNames.add(dep.getContentName());
    }
    assertTrue("Bundle " + USE_BUNDLE_BY_REFERENCE + " not found.", bundleSymbolicNames.contains(USE_BUNDLE_BY_REFERENCE));
    Collection<String> packages = new ArrayList<String>();
    Map<String, String> maps = new HashMap<String, String>();
    maps.put("version", "0.0.0");
    maps.put("bundle-symbolic-name", "use.bundle.by.reference");
    maps.put("bundle-version", "[1.0.0,1.0.0]");
    Content useContent = ContentFactory.parseContent("a.b.c", maps);
    assertTrue("Use Bundle not found in import packags", importPackages.contains(useContent));
    for (Content c : importPackages) {
        packages.add(c.getContentName());
    }
    assertTrue("package javax.naming not found", packages.contains("javax.naming"));
    assertTrue("package p.q.r not found", packages.contains("p.q.r"));
    assertTrue("package x.y.z not found", packages.contains("x.y.z"));
    assertTrue("package a.b.c not found", packages.contains("a.b.c"));
    AriesApplicationContext ctx = manager.install(app);
    ctx.start();
    Set<Bundle> bundles = ctx.getApplicationContent();
    assertEquals("Number of bundles provisioned in the app", 5, bundles.size());
    ctx.stop();
    manager.uninstall(ctx);
}
Also used : DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) AriesApplicationManager(org.apache.aries.application.management.AriesApplicationManager) AriesApplication(org.apache.aries.application.management.AriesApplication) ArrayList(java.util.ArrayList) DeploymentContent(org.apache.aries.application.DeploymentContent) Repository(org.apache.felix.bundlerepository.Repository) Content(org.apache.aries.application.Content) DeploymentContent(org.apache.aries.application.DeploymentContent) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) File(java.io.File) AbstractIntegrationTest(org.apache.aries.itest.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with DeploymentMetadata

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

the class OBRResolverTest method testBlogApp.

@Test
public void testBlogApp() throws Exception {
    // provision against the local runtime
    System.setProperty(AppConstants.PROVISON_EXCLUDE_LOCAL_REPO_SYSPROP, "false");
    generateOBRRepoXML(TRANSITIVE_BUNDLE_BY_REFERENCE + ".jar", CORE_BUNDLE_BY_REFERENCE + ".jar");
    RepositoryAdmin repositoryAdmin = context().getService(RepositoryAdmin.class);
    Repository[] repos = repositoryAdmin.listRepositories();
    for (Repository repo : repos) {
        repositoryAdmin.removeRepository(repo.getURI());
    }
    repositoryAdmin.addRepository(new File("repository.xml").toURI().toURL());
    AriesApplicationManager manager = context().getService(AriesApplicationManager.class);
    AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("blog.eba")));
    // installing requires a valid url for the bundle in repository.xml.
    app = manager.resolve(app);
    DeploymentMetadata depMeta = app.getDeploymentMetadata();
    List<DeploymentContent> provision = depMeta.getApplicationProvisionBundles();
    assertEquals(provision.toString(), 3, provision.size());
    List<String> bundleSymbolicNames = new ArrayList<String>();
    for (DeploymentContent dep : provision) {
        bundleSymbolicNames.add(dep.getContentName());
    }
    assertTrue("Bundle " + TRANSITIVE_BUNDLE_BY_REFERENCE + " not found.", bundleSymbolicNames.contains(TRANSITIVE_BUNDLE_BY_REFERENCE));
    assertTrue("Bundle " + TRANSITIVE_BUNDLE_BY_VALUE + " not found.", bundleSymbolicNames.contains(TRANSITIVE_BUNDLE_BY_VALUE));
    assertTrue("Bundle " + BUNDLE_IN_FRAMEWORK + " not found.", bundleSymbolicNames.contains(BUNDLE_IN_FRAMEWORK));
    AriesApplicationContext ctx = manager.install(app);
    ctx.start();
    Set<Bundle> bundles = ctx.getApplicationContent();
    assertEquals("Number of bundles provisioned in the app", 4, bundles.size());
    ctx.stop();
    manager.uninstall(ctx);
}
Also used : DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Bundle(org.osgi.framework.Bundle) AriesApplicationManager(org.apache.aries.application.management.AriesApplicationManager) AriesApplication(org.apache.aries.application.management.AriesApplication) ArrayList(java.util.ArrayList) DeploymentContent(org.apache.aries.application.DeploymentContent) Repository(org.apache.felix.bundlerepository.Repository) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) File(java.io.File) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.itest.AbstractIntegrationTest)

Example 3 with DeploymentMetadata

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

the class AriesApplicationManagerImplTest method testCreate.

@Test
public void testCreate() throws Exception {
    AriesApplication app = createApplication(TEST_EBA);
    ApplicationMetadata appMeta = app.getApplicationMetadata();
    assertEquals(appMeta.getApplicationName(), "Test application");
    assertEquals(appMeta.getApplicationSymbolicName(), "org.apache.aries.application.management.test");
    assertEquals(appMeta.getApplicationVersion(), new Version("1.0"));
    List<Content> appContent = appMeta.getApplicationContents();
    assertEquals(appContent.size(), 2);
    Content fbw = new ContentImpl("foo.bar.widgets;version=1.0.0");
    Content mbl = new ContentImpl("my.business.logic;version=1.0.0");
    assertTrue(appContent.contains(fbw));
    assertTrue(appContent.contains(mbl));
    DeploymentMetadata dm = app.getDeploymentMetadata();
    List<DeploymentContent> dcList = dm.getApplicationDeploymentContents();
    assertEquals(2, dcList.size());
    DeploymentContent dc1 = new DeploymentContentImpl("foo.bar.widgets;deployed-version=1.1.0");
    DeploymentContent dc2 = new DeploymentContentImpl("my.business.logic;deployed-version=1.1.0");
    DeploymentContent dc3 = new DeploymentContentImpl("a.handy.persistence.library;deployed-version=1.1.0");
    assertTrue(dcList.contains(dc1));
    assertTrue(dcList.contains(dc2));
    dcList = dm.getApplicationProvisionBundles();
    assertEquals(1, dcList.size());
    assertTrue(dcList.contains(dc3));
}
Also used : DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) DeploymentContentImpl(org.apache.aries.application.impl.DeploymentContentImpl) ApplicationMetadata(org.apache.aries.application.ApplicationMetadata) Version(org.osgi.framework.Version) Content(org.apache.aries.application.Content) DeploymentContent(org.apache.aries.application.DeploymentContent) AriesApplication(org.apache.aries.application.management.AriesApplication) ContentImpl(org.apache.aries.application.impl.ContentImpl) DeploymentContentImpl(org.apache.aries.application.impl.DeploymentContentImpl) DeploymentContent(org.apache.aries.application.DeploymentContent) Test(org.junit.Test)

Example 4 with DeploymentMetadata

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

the class AriesApplicationManagerImplTest method testStoreAndReload.

@Test
public void testStoreAndReload() throws Exception {
    AriesApplication app = createApplication(TEST_EBA);
    File dest = new File("ariesApplicationManagerImplTest/stored.eba");
    app.store(dest);
    /* Dest should be a zip file with four entries:
     *  /foo.bar.widgets.jar
     *  /my.business.logic.jar
     *  /META-INF/APPLICATION.MF
     *  /META-INF/DEPLOYMENT.MF
     */
    IDirectory storedEba = FileSystem.getFSRoot(dest);
    assertNotNull(storedEba);
    assertEquals(storedEba.listFiles().size(), 3);
    IFile ifile = storedEba.getFile("META-INF/APPLICATION.MF");
    assertNotNull(ifile);
    ifile = storedEba.getFile("META-INF/DEPLOYMENT.MF");
    assertNotNull(ifile);
    ifile = storedEba.getFile("foo.bar.widgets.jar");
    assertNotNull(ifile);
    ifile = storedEba.getFile("my.business.logic.jar");
    assertNotNull(ifile);
    AriesApplication newApp = _appMgr.createApplication(storedEba);
    DeploymentMetadata dm = newApp.getDeploymentMetadata();
    assertEquals(2, dm.getApplicationDeploymentContents().size());
    assertEquals(1, dm.getApplicationProvisionBundles().size());
    assertEquals(dm.getApplicationSymbolicName(), app.getApplicationMetadata().getApplicationSymbolicName());
    assertEquals(dm.getApplicationVersion(), app.getApplicationMetadata().getApplicationVersion());
}
Also used : DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) IFile(org.apache.aries.util.filesystem.IFile) IDirectory(org.apache.aries.util.filesystem.IDirectory) AriesApplication(org.apache.aries.application.management.AriesApplication) IFile(org.apache.aries.util.filesystem.IFile) File(java.io.File) Test(org.junit.Test)

Example 5 with DeploymentMetadata

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

Aggregations

DeploymentMetadata (org.apache.aries.application.DeploymentMetadata)15 AriesApplication (org.apache.aries.application.management.AriesApplication)11 Test (org.junit.Test)10 DeploymentContent (org.apache.aries.application.DeploymentContent)6 AriesApplicationContext (org.apache.aries.application.management.AriesApplicationContext)6 Content (org.apache.aries.application.Content)4 AriesApplicationContextManager (org.apache.aries.application.management.spi.runtime.AriesApplicationContextManager)4 MethodCall (org.apache.aries.unittest.mocks.MethodCall)4 Version (org.osgi.framework.Version)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)3 UpdateException (org.apache.aries.application.management.UpdateException)3 AbstractIntegrationTest (org.apache.aries.itest.AbstractIntegrationTest)3 Bundle (org.osgi.framework.Bundle)3 ContentImpl (org.apache.aries.application.impl.ContentImpl)2 DeploymentContentImpl (org.apache.aries.application.impl.DeploymentContentImpl)2 AriesApplicationManager (org.apache.aries.application.management.AriesApplicationManager)2 BundleInfo (org.apache.aries.application.management.BundleInfo)2