Search in sources :

Example 1 with ManagementException

use of org.apache.aries.application.management.ManagementException 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 2 with ManagementException

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

the class AriesApplicationManagerImpl method createApplication.

/**
   * Create an application from a URL. 
   * The first version of this method isn't smart enough to check whether
   * the input URL is file://
   */
public AriesApplication createApplication(URL url) throws ManagementException {
    OutputStream os = null;
    AriesApplication app = null;
    try {
        File tempFile = _localPlatform.getTemporaryFile();
        InputStream is = url.openStream();
        os = new FileOutputStream(tempFile);
        IOUtils.copy(is, os);
        IDirectory downloadedSource = FileSystem.getFSRoot(tempFile);
        app = createApplication(downloadedSource);
    } catch (IOException iox) {
        throw new ManagementException(iox);
    } finally {
        IOUtils.close(os);
    }
    return app;
}
Also used : ManagementException(org.apache.aries.application.management.ManagementException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IDirectory(org.apache.aries.util.filesystem.IDirectory) AriesApplication(org.apache.aries.application.management.AriesApplication) IOException(java.io.IOException) IFile(org.apache.aries.util.filesystem.IFile) File(java.io.File)

Example 3 with ManagementException

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

the class ApplicationContextManagerImpl method update.

public AriesApplicationContext update(AriesApplication app, DeploymentMetadata oldMetadata) throws UpdateException {
    ApplicationContextImpl oldCtx = _appToContextMap.get(app);
    if (oldCtx == null) {
        throw new IllegalArgumentException("AriesApplication " + app.getApplicationMetadata().getApplicationSymbolicName() + "/" + app.getApplicationMetadata().getApplicationVersion() + " cannot be updated because it is not installed");
    }
    uninstall(oldCtx);
    try {
        AriesApplicationContext newCtx = getApplicationContext(app);
        if (oldCtx.getApplicationState() == ApplicationState.ACTIVE) {
            newCtx.start();
        }
        return newCtx;
    } catch (BundleException e) {
        throw new UpdateException("Update failed: " + e.getMessage(), e, false, null);
    } catch (ManagementException e) {
        throw new UpdateException("Update failed: " + e.getMessage(), e, false, null);
    }
}
Also used : ManagementException(org.apache.aries.application.management.ManagementException) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) BundleException(org.osgi.framework.BundleException) UpdateException(org.apache.aries.application.management.UpdateException)

Aggregations

ManagementException (org.apache.aries.application.management.ManagementException)3 IOException (java.io.IOException)2 IFile (org.apache.aries.util.filesystem.IFile)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Manifest (java.util.jar.Manifest)1 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)1 DeploymentMetadata (org.apache.aries.application.DeploymentMetadata)1 AriesApplication (org.apache.aries.application.management.AriesApplication)1 AriesApplicationContext (org.apache.aries.application.management.AriesApplicationContext)1 BundleInfo (org.apache.aries.application.management.BundleInfo)1 ResolveConstraint (org.apache.aries.application.management.ResolveConstraint)1 UpdateException (org.apache.aries.application.management.UpdateException)1 BundleConversion (org.apache.aries.application.management.spi.convert.BundleConversion)1