Search in sources :

Example 56 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class DisableCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    if (origin == Origin.unload && command == Command.disable) {
        // we should only validate this for the disable command
        deployment.validateSpecifiedTarget(target);
    }
    InterceptorNotifier notifier = new InterceptorNotifier(habitat, null);
    final DeployCommandSupplementalInfo suppInfo = new DeployCommandSupplementalInfo();
    suppInfo.setAccessChecks(accessChecks);
    report.setResultType(DeployCommandSupplementalInfo.class, suppInfo);
    if (env.isDas() && DeploymentUtils.isDomainTarget(target)) {
        // for each distinct enabled version in all known targets
        Iterator it = enabledVersionsInTargets.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            appName = (String) entry.getKey();
            List<String> targets = new ArrayList<String>((Set<String>) entry.getValue());
            // replicate command to all referenced targets
            try {
                ParameterMapExtractor extractor = new ParameterMapExtractor(this);
                ParameterMap paramMap = extractor.extract(Collections.EMPTY_LIST);
                paramMap.set("DEFAULT", appName);
                notifier.ensureBeforeReported(ExtendedDeploymentContext.Phase.REPLICATION);
                ClusterOperationUtil.replicateCommand("disable", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, paramMap, habitat);
            } catch (Exception e) {
                report.failure(logger, e.getMessage());
                return;
            }
        }
    } else if (isVersionExpressionWithWildcard) {
        try {
            if (matchedVersions == Collections.EMPTY_LIST) {
                // no version matched by the expression
                // nothing to do : success
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return;
            }
            String enabledVersion = versioningService.getEnabledVersion(appName, target);
            if (matchedVersions.contains(enabledVersion)) {
                // the enabled version is matched by the expression
                appName = enabledVersion;
            } else {
                // the enabled version is not matched by the expression
                // nothing to do : success
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return;
            }
        } catch (VersioningException e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    if (target == null) {
        target = deployment.getDefaultTarget(appName, origin, _classicstyle);
    }
    if (env.isDas() || !isundeploy) {
        // on instance side for partial deployment case
        if (!deployment.isRegistered(appName)) {
            if (env.isDas()) {
                // let's only do this check for DAS to be more
                // tolerable of the partial deployment case
                report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", appName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            }
            return;
        }
        if (!DeploymentUtils.isDomainTarget(target)) {
            ApplicationRef ref = domain.getApplicationRefInTarget(appName, target);
            if (ref == null) {
                if (env.isDas()) {
                    // let's only do this check for DAS to be more
                    // tolerable of the partial deployment case
                    report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", appName, target));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                }
                return;
            }
        }
    }
    /*
         * 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.
         */
    if (env.isDas()) {
        try {
            notifier.ensureBeforeReported(ExtendedDeploymentContext.Phase.REPLICATION);
            DeploymentCommandUtils.replicateEnableDisableToContainingCluster("disable", domain, target, appName, habitat, context, this);
        } catch (Exception e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    try {
        Application app = applications.getApplication(appName);
        this.name = appName;
        // SHOULD CHECK THAT WE ARE THE CORRECT TARGET BEFORE DISABLING
        String serverName = server.getName();
        if (serverName.equals(target) || (server.getCluster() != null && server.getCluster().getName().equals(target))) {
            // wait until all applications are loaded. Otherwise we get "Application not registered"
            startupProvider.get();
            ApplicationInfo appInfo = deployment.get(appName);
            final DeploymentContext basicDC = deployment.disable(this, app, appInfo, report, logger);
            suppInfo.setDeploymentContext((ExtendedDeploymentContext) basicDC);
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error during disabling: ", e);
        if (env.isDas() || !isundeploy) {
            // we should let undeployment go through
            // on instance side for partial deployment case
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(e.getMessage());
        }
    }
    if (enabledVersionsToDisable == Collections.EMPTY_SET) {
        enabledVersionsToDisable = new HashSet<String>();
        enabledVersionsToDisable.add(appName);
    }
    // iterating all the distinct enabled versions in all targets
    Iterator it = enabledVersionsToDisable.iterator();
    while (it.hasNext()) {
        appName = (String) it.next();
        if (!isundeploy && !report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
            try {
                deployment.updateAppEnabledAttributeInDomainXML(appName, target, false);
            } catch (TransactionFailure e) {
                logger.warning("failed to set enable attribute for " + appName);
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ArrayList(java.util.ArrayList) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ParameterMap(org.glassfish.api.admin.ParameterMap) VersioningException(org.glassfish.deployment.versioning.VersioningException) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) Iterator(java.util.Iterator) VersioningException(org.glassfish.deployment.versioning.VersioningException) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) HashMap(java.util.HashMap)

Example 57 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class EnableCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    deployment.validateSpecifiedTarget(target);
    InterceptorNotifier notifier = new InterceptorNotifier(habitat, null);
    DeployCommandSupplementalInfo suppInfo = new DeployCommandSupplementalInfo();
    suppInfo.setDeploymentContext(notifier.dc());
    suppInfo.setAccessChecks(accessChecks);
    report.setResultType(DeployCommandSupplementalInfo.class, suppInfo);
    if (!deployment.isRegistered(name())) {
        report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", name()));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (!DeploymentUtils.isDomainTarget(target)) {
        ApplicationRef applicationRef = domain.getApplicationRefInTarget(name(), target);
        if (applicationRef == null) {
            report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name(), target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    // return if the application is already in enabled state
    if (domain.isAppEnabledInTarget(name(), target)) {
        logger.fine("The application is already enabled");
        return;
    }
    if (env.isDas()) {
        // try to disable the enabled version, if exist
        try {
            versioningService.handleDisable(name(), target, report, context.getSubject());
        } catch (VersioningSyntaxException e) {
            report.failure(logger, e.getMessage());
            return;
        }
        if (DeploymentUtils.isDomainTarget(target)) {
            List<String> targets = domain.getAllReferencedTargetsForApplication(name());
            // replicate command to all referenced targets
            try {
                ParameterMapExtractor extractor = new ParameterMapExtractor(this);
                ParameterMap paramMap = extractor.extract(Collections.EMPTY_LIST);
                paramMap.set("DEFAULT", name());
                notifier.ensureBeforeReported(Phase.REPLICATION);
                ClusterOperationUtil.replicateCommand("enable", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, paramMap, habitat);
            } catch (Exception e) {
                report.failure(logger, e.getMessage());
                return;
            }
        }
        try {
            notifier.ensureBeforeReported(Phase.REPLICATION);
            DeploymentCommandUtils.replicateEnableDisableToContainingCluster("enable", domain, target, name(), habitat, context, this);
        } catch (Exception e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    try {
        Application app = applications.getApplication(name());
        ApplicationRef appRef = domain.getApplicationRefInServer(server.getName(), name());
        // application is enabled.
        try {
            deployment.updateAppEnabledAttributeInDomainXML(name(), target, true);
        } catch (TransactionFailure e) {
            logger.log(Level.WARNING, "failed to set enable attribute for " + name(), e);
        }
        DeploymentContext dc = deployment.enable(target, app, appRef, report, logger);
        suppInfo.setDeploymentContext((ExtendedDeploymentContext) dc);
        if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
            // update the domain.xml
            try {
                deployment.updateAppEnabledAttributeInDomainXML(name(), target, false);
            } catch (TransactionFailure e) {
                logger.log(Level.WARNING, "failed to set enable attribute for " + name(), e);
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error during enabling: ", e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(e.getMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) ParameterMap(org.glassfish.api.admin.ParameterMap) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException)

Example 58 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class ListComponentsCommand method displaySubComponents.

private void displaySubComponents(String appName, ActionReport report, ActionReport.MessagePart part, final Subject subject) {
    ActionReport subReport = report.addSubActionsReport();
    CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("list-sub-components", subReport, subject);
    final ParameterMap parameters = new ParameterMap();
    parameters.add("DEFAULT", appName);
    parameters.add("terse", "true");
    parameters.add("resources", resources.toString());
    inv.parameters(parameters).execute();
    ActionReport.MessagePart subPart = subReport.getTopMessagePart();
    for (ActionReport.MessagePart childPart : subPart.getChildren()) {
        ActionReport.MessagePart actualChildPart = part.addChild();
        actualChildPart.setMessage("  " + childPart.getMessage());
        for (ActionReport.MessagePart grandChildPart : childPart.getChildren()) {
            actualChildPart.addChild().setMessage(grandChildPart.getMessage());
        }
    }
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) CommandRunner(org.glassfish.api.admin.CommandRunner)

Example 59 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class AppServerStartup method shutdown.

// TODO(Sahoo): Revisit this method after discussing with Jerome.
private void shutdown() {
    CommandRunner runner = commandRunnerProvider.get();
    if (runner != null) {
        final ParameterMap params = new ParameterMap();
        // By default we don't want to shutdown forcefully, as that will cause the VM to exit and that's not
        // a very good behavior for a code known to be embedded in other processes.
        final boolean noForcedShutdown = Boolean.parseBoolean(context.getArguments().getProperty(com.sun.enterprise.glassfish.bootstrap.Constants.NO_FORCED_SHUTDOWN, "true"));
        if (noForcedShutdown) {
            params.set("force", "false");
        }
        final InternalSystemAdministrator kernelIdentity = locator.getService(InternalSystemAdministrator.class);
        if (env.isDas()) {
            runner.getCommandInvocation("stop-domain", new DoNothingActionReporter(), kernelIdentity.getSubject()).parameters(params).execute();
        } else {
            runner.getCommandInvocation("_stop-instance", new DoNothingActionReporter(), kernelIdentity.getSubject()).parameters(params).execute();
        }
    }
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) CommandRunner(org.glassfish.api.admin.CommandRunner) InternalSystemAdministrator(org.glassfish.internal.api.InternalSystemAdministrator) DoNothingActionReporter(com.sun.enterprise.v3.common.DoNothingActionReporter)

Example 60 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class ApplicationLifecycle method prepareInstanceDeployParamMap.

@Override
public ParameterMap prepareInstanceDeployParamMap(DeploymentContext dc) throws Exception {
    final DeployCommandParameters params = dc.getCommandParameters(DeployCommandParameters.class);
    final Collection<String> excludedParams = new ArrayList<String>();
    excludedParams.add(DeploymentProperties.PATH);
    excludedParams.add(DeploymentProperties.DEPLOYMENT_PLAN);
    excludedParams.add(DeploymentProperties.ALT_DD);
    excludedParams.add(DeploymentProperties.RUNTIME_ALT_DD);
    // We'll force it to true ourselves.
    excludedParams.add(DeploymentProperties.UPLOAD);
    final ParameterMap paramMap;
    final ParameterMapExtractor extractor = new ParameterMapExtractor(params);
    paramMap = extractor.extract(excludedParams);
    prepareGeneratedContent(dc, paramMap);
    // set the path and plan params
    // get the location properties from the application so the token
    // will be resolved
    Application application = applications.getApplication(params.name);
    Properties appProperties = application.getDeployProperties();
    String archiveLocation = appProperties.getProperty(Application.APP_LOCATION_PROP_NAME);
    final File archiveFile = new File(new URI(archiveLocation));
    paramMap.set("DEFAULT", archiveFile.getAbsolutePath());
    String planLocation = appProperties.getProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME);
    if (planLocation != null) {
        final File actualPlan = new File(new URI(planLocation));
        paramMap.set(DeployCommandParameters.ParameterNames.DEPLOYMENT_PLAN, actualPlan.getAbsolutePath());
    }
    String altDDLocation = appProperties.getProperty(Application.ALT_DD_LOCATION_PROP_NAME);
    if (altDDLocation != null) {
        final File altDD = new File(new URI(altDDLocation));
        paramMap.set(DeployCommandParameters.ParameterNames.ALT_DD, altDD.getAbsolutePath());
    }
    String runtimeAltDDLocation = appProperties.getProperty(Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME);
    if (runtimeAltDDLocation != null) {
        final File runtimeAltDD = new File(new URI(runtimeAltDDLocation));
        paramMap.set(DeployCommandParameters.ParameterNames.RUNTIME_ALT_DD, runtimeAltDD.getAbsolutePath());
    }
    // always upload the archives to the instance side
    // but not directories.  Note that we prepare a zip file containing
    // the generated directories and pass that as a single parameter so it
    // will be uploaded even though a deployment directory is not.
    paramMap.set(DeploymentProperties.UPLOAD, "true");
    // redeployment
    if (params.previousContextRoot != null) {
        paramMap.set(DeploymentProperties.PRESERVED_CONTEXT_ROOT, params.previousContextRoot);
    }
    // pass the app props so we have the information to persist in the
    // domain.xml
    Properties appProps = dc.getAppProps();
    appProps.remove(DeploymentProperties.APP_CONFIG);
    paramMap.set(DeploymentProperties.APP_PROPS, extractor.propertiesValue(appProps, ':'));
    Properties previousVirtualServers = dc.getTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS, Properties.class);
    if (previousVirtualServers != null) {
        paramMap.set(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS, extractor.propertiesValue(previousVirtualServers, ':'));
    }
    Properties previousEnabledAttributes = dc.getTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES, Properties.class);
    if (previousEnabledAttributes != null) {
        paramMap.set(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES, extractor.propertiesValue(previousEnabledAttributes, ':'));
    }
    return paramMap;
}
Also used : ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) ParameterMap(org.glassfish.api.admin.ParameterMap) File(java.io.File) URI(java.net.URI)

Aggregations

ParameterMap (org.glassfish.api.admin.ParameterMap)149 ActionReport (org.glassfish.api.ActionReport)68 CommandRunner (org.glassfish.api.admin.CommandRunner)37 Test (org.junit.Test)25 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)24 Map (java.util.Map)20 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)19 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)18 List (java.util.List)16 ArrayList (java.util.ArrayList)15 CommandRunner (org.glassfish.embeddable.CommandRunner)15 IOException (java.io.IOException)14 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)13 Before (org.junit.Before)13 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)13 CommandException (org.glassfish.api.admin.CommandException)12 File (java.io.File)11 MessagePart (org.glassfish.api.ActionReport.MessagePart)11 Resource (com.sun.enterprise.config.serverbeans.Resource)10 Logger (java.util.logging.Logger)9