AriesApplicationManagerImpl implements AriesApplicationManager { 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 { Manifest applicationManifest = parseApplicationManifest (ebaFile); String appName = applicationManifest.getMainAttributes().getValue(AppConstants.APPLICATION_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); 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()); 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) { } 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 (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); 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); application.setModifiedBundles (modifiedBundles); } catch (IOException iox) { _logger.error ("APPMANAGEMENT0006E", new Object []{appPath, iox}); throw new ManagementException(iox); } return application; }  void setApplicationMetadataFactory(ApplicationMetadataFactory amf); void setDeploymentMetadataFactory(DeploymentMetadataFactory dmf); void setBundleConverters(List<BundleConverter> bcs); void setDeploymentManifestManager(DeploymentManifestManager dm); void setLocalPlatform(LocalPlatform lp); void setApplicationContextManager(AriesApplicationContextManager acm); void setBundleContext(BundleContext b); AriesApplication createApplication(IDirectory ebaFile); AriesApplication createApplication(URL url); AriesApplication resolve(AriesApplication originalApp, ResolveConstraint... constraints); AriesApplicationContext install(AriesApplication app); void uninstall(AriesApplicationContext appContext); void addApplicationListener(AriesApplicationListener l); void removeApplicationListener(AriesApplicationListener l); AriesApplicationContext update(AriesApplication app, DeploymentMetadata depMf);  }
@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)); }
@Test public void testCreateAndConversion() throws Exception { AriesApplication app = createApplication (CONVERSION_EBA); ApplicationMetadata appMeta = app.getApplicationMetadata(); assertEquals (appMeta.getApplicationName(), "conversion.eba"); assertEquals (appMeta.getApplicationSymbolicName(), "conversion.eba"); assertEquals (appMeta.getApplicationVersion(), new Version("0.0")); List<Content> appContent = appMeta.getApplicationContents(); assertEquals (2, appContent.size()); Content fbw = new ContentImpl("hello.world.jar;version=\"[1.1.0, 1.1.0]\""); Content mbl = new ContentImpl("helloWorld.war;version=\"[0.0.0, 0.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 ("hello.world.jar;deployed-version=1.1.0"); DeploymentContent dc2 = new DeploymentContentImpl ("helloWorld.war;deployed-version=0.0.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)); assertEquals(2, app.getBundleInfo().size()); BundleInfo info; info = findBundleInfo(app.getBundleInfo(), "hello.world.jar"); assertNotNull(info); assertEquals("HelloWorldJar", info.getHeaders().get(Constants.BUNDLE_NAME)); info = findBundleInfo(app.getBundleInfo(), "helloWorld.war"); assertNotNull(info); assertEquals("helloWorld.war", info.getHeaders().get(Constants.BUNDLE_NAME)); assertEquals("/test", info.getHeaders().get("Bundle-ContextPath")); }
@Test public void testStoreAndReload() throws Exception { AriesApplication app = createApplication (TEST_EBA); File dest = new File ("ariesApplicationManagerImplTest/stored.eba"); app.store(dest); 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()); }