Search in sources :

Example 6 with ParameterMapExtractor

use of org.glassfish.common.util.admin.ParameterMapExtractor in project Payara by payara.

the class DeleteLifecycleModuleCommand method execute.

public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    if (!deployment.isRegistered(name)) {
        report.setMessage(localStrings.getLocalString("lifecycle.notreg", "Lifecycle module {0} not registered", name));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (!DeploymentUtils.isDomainTarget(target)) {
        ApplicationRef ref = domain.getApplicationRefInTarget(name, target);
        if (ref == null) {
            report.setMessage(localStrings.getLocalString("lifecycle.not.referenced.target", "Lifecycle module {0} is not referenced by target {1}", name, target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    deployment.validateUndeploymentTarget(target, name);
    if (env.isDas() && DeploymentUtils.isDomainTarget(target)) {
        // replicate command to all referenced targets
        try {
            ParameterMapExtractor extractor = new ParameterMapExtractor(this);
            ParameterMap paramMap = extractor.extract(Collections.EMPTY_LIST);
            paramMap.set("DEFAULT", name);
            ClusterOperationUtil.replicateCommand("delete-lifecycle-module", FailurePolicy.Error, FailurePolicy.Ignore, FailurePolicy.Warn, targets, context, paramMap, habitat);
        } catch (Exception e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    try {
        deployment.unregisterAppFromDomainXML(name, target);
    } catch (Exception e) {
        report.setMessage("Failed to delete lifecycle module: " + e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef)

Example 7 with ParameterMapExtractor

use of org.glassfish.common.util.admin.ParameterMapExtractor in project Payara by payara.

the class DeploymentCommandUtils method replicateEnableDisableToContainingCluster.

/**
 * Replicates an enable or disable command to all instances in the cluster
 * of which the target is a member.  If the target is not cluster member
 * this method is a no-op.
 * @param commandName name of the command to replicate to cluster instances
 * @param domain domain containing the relevant configuration
 * @param target name of the target being enabled or disabled
 * @param appName name of the application being enabled or disabled
 * @param habitat hk2 habitat
 * @param context command context passed to the running enable or disable command
 * @param command command object
 * @return
 */
public static ActionReport.ExitCode replicateEnableDisableToContainingCluster(final String commandName, final Domain domain, final String target, final String appName, final ServiceLocator habitat, final AdminCommandContext context, final AdminCommand command) throws IllegalArgumentException, IllegalAccessException {
    /*
         * If the target is a cluster instance, the DAS will broadcast the command
         * to all instances in the cluster so they can all update their configs.
         */
    final Cluster containingCluster = domain.getClusterForInstance(target);
    if (containingCluster != null) {
        final ParameterMapExtractor extractor = new ParameterMapExtractor(command);
        final ParameterMap pMap = extractor.extract(Collections.EMPTY_LIST);
        pMap.set("DEFAULT", appName);
        return ClusterOperationUtil.replicateCommand(commandName, FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, containingCluster.getInstances(), context, pMap, habitat);
    }
    return ActionReport.ExitCode.SUCCESS;
}
Also used : ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ParameterMap(org.glassfish.api.admin.ParameterMap)

Example 8 with ParameterMapExtractor

use of org.glassfish.common.util.admin.ParameterMapExtractor in project Payara by payara.

the class ReDeployCommand method execute.

/**
 * Executes the command.
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (!validateParameters(name, report)) {
        return;
    }
    final ParameterMap paramMap;
    final ParameterMapExtractor extractor = new ParameterMapExtractor(this);
    try {
        paramMap = extractor.extract(excludedDeployCommandParamNames);
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
    paramMap.set("force", String.valueOf(true));
    CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("deploy", report, context.getSubject());
    inv.parameters(paramMap).inbound(context.getInboundPayload()).outbound(context.getOutboundPayload()).execute();
}
Also used : ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) CommandRunner(org.glassfish.api.admin.CommandRunner)

Example 9 with ParameterMapExtractor

use of org.glassfish.common.util.admin.ParameterMapExtractor 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 10 with ParameterMapExtractor

use of org.glassfish.common.util.admin.ParameterMapExtractor in project Payara by payara.

the class PostRegisterInstanceCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    final InstanceRegisterInstanceCommandParameters suppInfo = context.getActionReport().getResultType(InstanceRegisterInstanceCommandParameters.class);
    if (suppInfo != null && clusterName != null) {
        try {
            ParameterMapExtractor pme = new ParameterMapExtractor(suppInfo, this);
            final ParameterMap paramMap = pme.extract();
            List<String> targets = new ArrayList<String>();
            List<Server> instances = target.getInstances(this.clusterName);
            for (Server s : instances) {
                targets.add(s.getName());
            }
            ClusterOperationUtil.replicateCommand("_register-instance-at-instance", FailurePolicy.Warn, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, paramMap, habitat);
        } catch (Exception e) {
            report.failure(logger, e.getMessage());
        }
    }
}
Also used : InstanceRegisterInstanceCommandParameters(com.sun.enterprise.config.util.InstanceRegisterInstanceCommandParameters) ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) Server(com.sun.enterprise.config.serverbeans.Server) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger)

Aggregations

ParameterMapExtractor (org.glassfish.common.util.admin.ParameterMapExtractor)10 ParameterMap (org.glassfish.api.admin.ParameterMap)8 ActionReport (org.glassfish.api.ActionReport)6 ArrayList (java.util.ArrayList)3 Logger (java.util.logging.Logger)3 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)3 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)3 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)3 Server (com.sun.enterprise.config.serverbeans.Server)2 File (java.io.File)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 CommandRunner (org.glassfish.api.admin.CommandRunner)2 VersioningException (org.glassfish.deployment.versioning.VersioningException)2 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)2 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Cluster (com.sun.enterprise.config.serverbeans.Cluster)1 InstanceRegisterInstanceCommandParameters (com.sun.enterprise.config.util.InstanceRegisterInstanceCommandParameters)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1