Search in sources :

Example 36 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class UndeployCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    // for each matched version
    for (String appName : matchedVersions) {
        if (target == null) {
            target = deployment.getDefaultTarget(appName, origin, _classicstyle);
        }
        ApplicationInfo info = deployment.get(appName);
        Application application = apps.getModule(Application.class, appName);
        if (application == null) {
            report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", appName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        deployment.validateUndeploymentTarget(target, appName);
        if (!DeploymentUtils.isDomainTarget(target)) {
            ApplicationRef ref = domain.getApplicationRefInTarget(appName, target);
            if (ref == null) {
                report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", appName, target));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
        ReadableArchive source = null;
        if (info == null) {
            // disabled application or application failed to be
            // loaded for some reason
            URI uri = null;
            try {
                uri = new URI(application.getLocation());
            } catch (URISyntaxException e) {
                logger.severe("Cannot determine original location for application : " + e.getMessage());
            }
            if (uri != null) {
                File location = new File(uri);
                if (location.exists()) {
                    try {
                        source = archiveFactory.openArchive(location);
                    } catch (IOException e) {
                        logger.log(Level.INFO, e.getMessage(), e);
                    }
                } else {
                    logger.warning("Originally deployed application at " + location + " not found");
                }
            }
        } else {
            source = info.getSource();
        }
        if (source == null) {
            logger.fine("Cannot get source archive for undeployment");
            // server is in a consistent state after restart
            try {
                deployment.unregisterAppFromDomainXML(appName, target);
            } catch (TransactionFailure e) {
                logger.warning("Module " + appName + " not found in configuration");
            }
            // also remove application from runtime registry
            if (info != null) {
                appRegistry.remove(appName);
            }
            return;
        }
        File sourceFile = new File(source.getURI());
        if (!source.exists()) {
            logger.log(Level.WARNING, "Cannot find application bits at " + sourceFile.getPath() + ". Please restart server to ensure server is in a consistent state before redeploy the application.");
            // server is in a consistent state after restart
            try {
                deployment.unregisterAppFromDomainXML(appName, target);
            } catch (TransactionFailure e) {
                logger.warning("Module " + appName + " not found in configuration");
            }
            // also remove application from runtime registry
            if (info != null) {
                appRegistry.remove(appName);
            }
            return;
        }
        // now start the normal undeploying
        this.name = appName;
        this._type = application.archiveType();
        ExtendedDeploymentContext deploymentContext = null;
        try {
            deploymentContext = deployment.getBuilder(logger, this, report).source(source).build();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Cannot create context for undeployment ", e);
            report.setMessage(localStrings.getLocalString("undeploy.contextcreation.failed", "Cannot create context for undeployment : {0} ", e.getMessage()));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        final InterceptorNotifier notifier = new InterceptorNotifier(habitat, deploymentContext);
        final DeployCommandSupplementalInfo suppInfo = new DeployCommandSupplementalInfo();
        suppInfo.setDeploymentContext(deploymentContext);
        report.setResultType(DeployCommandSupplementalInfo.class, suppInfo);
        final Properties appProps = deploymentContext.getAppProps();
        appProps.putAll(application.getDeployProperties());
        if (properties != null) {
            appProps.putAll(properties);
        }
        deploymentContext.setModulePropsMap(application.getModulePropertiesMap());
        events.send(new Event<DeploymentContext>(Deployment.UNDEPLOYMENT_VALIDATION, deploymentContext), false);
        if (report.getActionExitCode() == ActionReport.ExitCode.FAILURE) {
            // status as failure, return
            return;
        }
        // disable the application first for non-DAS target
        if (env.isDas() && !DeploymentUtils.isDASTarget(target)) {
            ActionReport subReport = report.addSubActionsReport();
            CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("disable", subReport, context.getSubject());
            try {
                final ParameterMapExtractor extractor = new ParameterMapExtractor(this);
                final ParameterMap parameters = extractor.extract(Collections.EMPTY_LIST);
                parameters.set("DEFAULT", appName);
                parameters.add(DeploymentProperties.IS_UNDEPLOY, Boolean.TRUE.toString());
                inv.parameters(parameters).execute();
                if (subReport.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
                    // if disable application failed
                    // we should just return
                    report.setMessage(localStrings.getLocalString("disable.command.failed", "{0} disabled failed", appName));
                    return;
                }
                if (DeploymentUtils.isDomainTarget(target)) {
                    List<String> targets = domain.getAllReferencedTargetsForApplication(appName);
                    // replicate command to all referenced targets
                    parameters.remove("isUndeploy");
                    notifier.ensureBeforeReported(ExtendedDeploymentContext.Phase.REPLICATION);
                    ClusterOperationUtil.replicateCommand("undeploy", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, parameters, habitat);
                }
            } catch (Exception e) {
                report.failure(logger, e.getMessage());
                return;
            }
        }
        /*
             * Extract the generated artifacts from the application's properties
             * and record them in the DC.  This will be useful, for example,
             * during Deployer.clean.
             */
        final Artifacts generatedArtifacts = DeploymentUtils.generatedArtifacts(application);
        generatedArtifacts.record(deploymentContext);
        if (info != null) {
            deployment.undeploy(appName, deploymentContext);
        }
        // check if it's directory deployment
        boolean isDirectoryDeployed = Boolean.valueOf(application.getDirectoryDeployed());
        // and warning case
        if (!report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
            // so far I am doing this after the unload, maybe this should be moved before...
            try {
                // remove the "application" element
                deployment.unregisterAppFromDomainXML(appName, target);
            } catch (TransactionFailure e) {
                logger.warning("Module " + appName + " not found in configuration");
            }
            // remove context from generated
            deploymentContext.clean();
            // free file descriptors
            try {
                Class clazz = Class.forName("sun.net.www.protocol.jar.JarFileFactory", true, URL.class.getClassLoader());
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    if ("fileCache".equals(field.getName())) {
                        field.setAccessible(true);
                        HashMap<String, JarFile> files = (HashMap<String, JarFile>) field.get(null);
                        Set<JarFile> jars = new HashSet<>();
                        jars.addAll(files.values());
                        for (JarFile file : jars) {
                            file.close();
                        }
                    }
                }
            } catch (ClassNotFoundException | IllegalAccessException | SecurityException | IllegalArgumentException ex) {
                logger.log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
            // perform full GC after cleaning to ensure full clean up
            System.gc();
            // if directory deployment then do not remove the directory
            if ((!keepreposdir) && !isDirectoryDeployed && source.exists()) {
                /*
                     * Delete the repository directory as an archive so
                     * any special handling (such as stale file handling)
                     * known to the archive can run.
                     */
                source.delete();
            }
        }
    // else a message should have been provided.
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) HashMap(java.util.HashMap) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) URISyntaxException(java.net.URISyntaxException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) ActionReport(org.glassfish.api.ActionReport) URI(java.net.URI) URL(java.net.URL) Field(java.lang.reflect.Field) ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) CommandRunner(org.glassfish.api.admin.CommandRunner) HashSet(java.util.HashSet) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) URISyntaxException(java.net.URISyntaxException) VersioningException(org.glassfish.deployment.versioning.VersioningException) IOException(java.io.IOException) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Artifacts(org.glassfish.deployment.common.Artifacts) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 37 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class ApplicationLifecycle method enable.

@Override
public ExtendedDeploymentContext enable(String target, Application app, ApplicationRef appRef, ActionReport report, Logger logger) throws Exception {
    ReadableArchive archive = null;
    try {
        DeployCommandParameters commandParams = app.getDeployParameters(appRef);
        // if the application is already loaded, do not load again
        ApplicationInfo appInfo = appRegistry.get(commandParams.name);
        if (appInfo != null && appInfo.isLoaded()) {
            return null;
        }
        commandParams.origin = DeployCommandParameters.Origin.load;
        commandParams.command = DeployCommandParameters.Command.enable;
        commandParams.target = target;
        commandParams.enabled = Boolean.TRUE;
        Properties contextProps = app.getDeployProperties();
        Map<String, Properties> modulePropsMap = app.getModulePropertiesMap();
        ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(app);
        URI uri = new URI(app.getLocation());
        File file = new File(uri);
        if (!file.exists()) {
            throw new Exception(localStrings.getLocalString("fnf", "File not found {0}", file.getAbsolutePath()));
        }
        archive = archiveFactory.openArchive(file);
        final ExtendedDeploymentContext deploymentContext = getBuilder(logger, commandParams, report).source(archive).build();
        Properties appProps = deploymentContext.getAppProps();
        appProps.putAll(contextProps);
        savedAppConfig.store(appProps);
        if (modulePropsMap != null) {
            deploymentContext.setModulePropsMap(modulePropsMap);
        }
        deploy(getSniffersFromApp(app), deploymentContext);
        return deploymentContext;
    } finally {
        try {
            if (archive != null) {
                archive.close();
            }
        } catch (IOException ioe) {
        // ignore
        }
    }
}
Also used : IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) URI(java.net.URI) File(java.io.File) PropertyVetoException(java.beans.PropertyVetoException) RetryableException(org.jvnet.hk2.config.RetryableException) MultiException(org.glassfish.hk2.api.MultiException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException)

Example 38 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class EmbeddedDeployerImpl method deploy.

@Override
public String deploy(ReadableArchive archive, DeployCommandParameters params) {
    // ensure server is started. start it if not started.
    try {
        server.start();
    } catch (LifecycleException e) {
        throw new RuntimeException(e);
    }
    ActionReport report = new PlainTextActionReporter();
    if (params == null) {
        params = new DeployCommandParameters();
    }
    ExtendedDeploymentContext initialContext = new DeploymentContextImpl(report, archive, params, env);
    ArchiveHandler archiveHandler = null;
    try {
        archiveHandler = deployment.getArchiveHandler(archive);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (archiveHandler == null) {
        throw new RuntimeException("Cannot find archive handler for source archive");
    }
    if (params.name == null) {
        params.name = archiveHandler.getDefaultApplicationName(archive, initialContext);
    }
    ExtendedDeploymentContext context = null;
    try {
        context = deployment.getBuilder(logger, params, report).source(archive).archiveHandler(archiveHandler).build(initialContext);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (params.property != null) {
        context.getAppProps().putAll(params.property);
    }
    if (params.properties != null) {
        context.getAppProps().putAll(params.properties);
    }
    ApplicationInfo appInfo = null;
    try {
        appInfo = deployment.deploy(context);
    } catch (Exception e) {
        logger.log(Level.SEVERE, KernelLoggerInfo.deployException, e);
    }
    if (appInfo != null) {
        boolean isDirectory = new File(archive.getURI().getPath()).isDirectory();
        EmbeddedDeployedInfo info = new EmbeddedDeployedInfo(appInfo, context.getModulePropsMap(), context.getAppProps(), isDirectory);
        deployedApps.put(appInfo.getName(), info);
        return appInfo.getName();
    }
    return null;
}
Also used : IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) PropertyVetoException(java.beans.PropertyVetoException) IOException(java.io.IOException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter) File(java.io.File)

Example 39 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class EmbeddedDeployerImpl method undeploy.

@Override
public void undeploy(String name, UndeployCommandParameters params) {
    ActionReport report = habitat.getService(ActionReport.class, "plain");
    EmbeddedDeployedInfo info = deployedApps.get(name);
    ApplicationInfo appInfo = info != null ? info.appInfo : null;
    if (appInfo == null) {
        appInfo = deployment.get(name);
    }
    if (appInfo == null) {
        report.setMessage("Cannot find deployed application of name " + name);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    ReadableArchive source = appInfo.getSource();
    if (source == null) {
        report.setMessage("Cannot get source archive for undeployment");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (params == null) {
        params = new UndeployCommandParameters(name);
    }
    params.origin = UndeployCommandParameters.Origin.undeploy;
    ExtendedDeploymentContext deploymentContext;
    try {
        deploymentContext = deployment.getBuilder(logger, params, report).source(source).build();
        if (info != null) {
            for (ModuleInfo module : appInfo.getModuleInfos()) {
                info.map.put(module.getName(), module.getModuleProps());
                deploymentContext.getModuleProps().putAll(module.getModuleProps());
            }
            deploymentContext.setModulePropsMap(info.map);
            deploymentContext.getAppProps().putAll(info.appProps);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Cannot create context for undeployment ", e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    deployment.undeploy(name, deploymentContext);
    if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
        if (params.keepreposdir == null) {
            params.keepreposdir = false;
        }
        if (!params.keepreposdir && info != null && !info.isDirectory && source.exists()) {
            FileUtils.whack(new File(source.getURI()));
        }
        // remove context from generated
        deploymentContext.clean();
    }
}
Also used : UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) File(java.io.File)

Aggregations

ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)39 ActionReport (org.glassfish.api.ActionReport)23 IOException (java.io.IOException)17 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)17 File (java.io.File)16 Logger (java.util.logging.Logger)11 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)11 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)10 Properties (java.util.Properties)9 URI (java.net.URI)7 Deployment (org.glassfish.internal.deployment.Deployment)7 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)6 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)6 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)6 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)6 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)6 Application (com.sun.enterprise.deployment.Application)5 ArrayList (java.util.ArrayList)5 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)5 VersioningException (org.glassfish.deployment.versioning.VersioningException)5