Search in sources :

Example 1 with DeploymentException

use of org.osgi.service.deploymentadmin.DeploymentException in project sling by apache.

the class DeployPckTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    // get and check symbolic name
    final String symbolicName = (String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_SYMBOLICMAME);
    if (symbolicName == null) {
        logger.error("Resource {} has no symbolic name - ignoring.", tr);
        this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        return;
    }
    // get package if available
    final DeploymentPackage dp = this.deploymentAdmin.getDeploymentPackage(symbolicName);
    if (tr.getState() == ResourceState.INSTALL) {
        InputStream is = null;
        try {
            is = tr.getInputStream();
            if (is == null) {
                // something went wrong
                logger.error("Resource {} does not provide an input stream!", tr);
                this.getResourceGroup().setFinishState(ResourceState.IGNORED);
            } else {
                final Version newVersion = new Version((String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_VERSION));
                // check version
                if (dp != null) {
                    final int compare = dp.getVersion().compareTo(newVersion);
                    if (compare < 0) {
                        // installed version is lower -> update
                        this.deploymentAdmin.installDeploymentPackage(is);
                        ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                        this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                    } else if (compare >= 0) {
                        logger.debug("Deployment package " + symbolicName + " " + newVersion + " is not installed, package with higher or same version is already installed.");
                    }
                } else {
                    this.deploymentAdmin.installDeploymentPackage(is);
                    ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                }
            }
        } catch (final DeploymentException e) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } catch (final IOException ioe) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
        }
    } else {
        // uninstall
        if (dp != null) {
            try {
                dp.uninstall();
            } catch (final DeploymentException e) {
                logger.error("Unable to uninstall deployment package {} from resource {}", symbolicName, tr);
            }
        } else {
            logger.info("Unable to find deployment package with symbolic name {} for uninstalling.", symbolicName);
        }
        this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Version(org.osgi.framework.Version) InputStream(java.io.InputStream) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)1 Version (org.osgi.framework.Version)1 DeploymentException (org.osgi.service.deploymentadmin.DeploymentException)1 DeploymentPackage (org.osgi.service.deploymentadmin.DeploymentPackage)1