Search in sources :

Example 1 with CommandRunner

use of org.glassfish.api.admin.CommandRunner 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 2 with CommandRunner

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

the class InserverCommandRunnerHelper method runCommand.

public ActionReport runCommand(final String command, final ParameterMap parameters, final ActionReport report, final Subject subject) {
    try {
        final AdminCommand adminCommand = commandRunner.getCommand(command, report, logger);
        if (adminCommand == null) {
            // maybe commandRunner already reported the failure?
            if (report.getActionExitCode() == ActionReport.ExitCode.FAILURE)
                return report;
            String message = adminStrings.getLocalString("adapter.command.notfound", "Command {0} not found", command);
            // cound't find command, not a big deal
            logger.log(Level.FINE, message);
            report.setMessage(message);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return report;
        }
        CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation(command, report, subject);
        inv.parameters(parameters).execute();
    } catch (Throwable t) {
        /*
             * Must put the error information into the report
             * for the client to see it.
             */
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(t);
        report.setMessage(t.getLocalizedMessage());
        report.setActionDescription("Last-chance exception handler");
    }
    return report;
}
Also used : AdminCommand(org.glassfish.api.admin.AdminCommand) CommandRunner(org.glassfish.api.admin.CommandRunner)

Example 3 with CommandRunner

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

the class DeployRemoteArchiveCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    CommandRunner commandRunner = serviceLocator.getService(CommandRunner.class);
    ActionReport actionReport = context.getActionReport();
    // Initialise to null so we can do a null check later
    File fileToDeploy = null;
    // Should treat uppercase letters as equivalent to lowercase
    // in schema names (section 3.1 of the RFC 2396)
    String lowerPath = path.toLowerCase();
    // Assume only Http or Https connections are direct URIs
    if (lowerPath.startsWith("http://") || lowerPath.startsWith("https://")) {
        try {
            URI pathURI = new URI(path);
            // Download the file to temp, and return a File object to pass to the deploy command
            fileToDeploy = URIUtils.convertToFile(pathURI);
            // Get file name from URI
            String fileName = new File(pathURI.getPath()).getName();
            // If a name hasn't been provided, get it from the file name
            if (name == null) {
                name = JavaArchiveUtils.removeJavaArchiveExtension(fileName, true);
            }
            // If a context root hasn't been provided, get it from the file name
            if (contextroot == null) {
                contextroot = "/" + JavaArchiveUtils.removeJavaArchiveExtension(fileName, true);
            }
        } catch (IOException | URISyntaxException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
            actionReport.setMessage("Exception converting URI to File: " + path);
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    } else {
        try {
            // If the path String doesn't start with Http or Https, then assume it's a GAV coordinate
            logger.log(Level.FINE, "Path does not appear to be a URI, will attempt to read as GAV coordinate");
            // Get the URI for the given GAV coordinate
            Entry<String, URI> artefactEntry = GAVConvertor.getArtefactMapEntry(path, additionalRepositories);
            // Download the file to temp, and return a File object to pass to the deploy command
            fileToDeploy = URIUtils.convertToFile(artefactEntry.getValue());
            // If a name hasn't been provided, get it from the artefact URI
            if (name == null) {
                String fileName = new File(artefactEntry.getValue().getPath()).getName();
                name = JavaArchiveUtils.removeJavaArchiveExtension(fileName, true);
            }
            // If a context root hasn't been provided, get it from the artefact name
            if (contextroot == null) {
                contextroot = "/" + artefactEntry.getKey();
            }
        } catch (IOException | URISyntaxException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
            actionReport.setMessage("Exception converting GAV to File: " + path);
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
    // Only continue if we actually have a file to deploy
    if (fileToDeploy != null) {
        ActionReport subReport = actionReport.addSubActionsReport();
        CommandRunner.CommandInvocation commandInvocation = commandRunner.getCommandInvocation("deploy", subReport, context.getSubject());
        ParameterMap parameters = createAndPopulateParameterMap(fileToDeploy);
        commandInvocation.parameters(parameters);
        commandInvocation.execute();
    } else {
        actionReport.setMessage("Provided path does not appear to be a valid URI or GAV coordinate: " + path + "\nSee the server log for more details");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ActionReport(org.glassfish.api.ActionReport) CommandRunner(org.glassfish.api.admin.CommandRunner) File(java.io.File) URI(java.net.URI)

Example 4 with CommandRunner

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

the class ResourceUtil method runCommand.

/**
 * Executes the specified __asadmin command.
 * @param commandName
 * @param parameters
 * @param subject
 * @param managedJob
 * @return
 */
public static RestActionReporter runCommand(String commandName, ParameterMap parameters, Subject subject, boolean managedJob) {
    AsadminRecorderService asadminRecorderService = Globals.get(AsadminRecorderService.class);
    if (asadminRecorderService != null && asadminRecorderService.isEnabled()) {
        asadminRecorderService.recordAsadminCommand(commandName, parameters);
    }
    AdminAuditService auditService = Globals.getDefaultHabitat().getService(AdminAuditService.class);
    if (auditService != null && auditService.isEnabled()) {
        auditService.recordAsadminCommand(commandName, parameters, subject);
    }
    CommandRunner cr = Globals.getDefaultHabitat().getService(CommandRunner.class);
    RestActionReporter ar = new RestActionReporter();
    final CommandInvocation commandInvocation = cr.getCommandInvocation(commandName, ar, subject);
    if (managedJob) {
        commandInvocation.managedJob();
    }
    commandInvocation.parameters(parameters).execute();
    addCommandLog(ar, commandName, parameters);
    return ar;
}
Also used : RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) AdminAuditService(fish.payara.audit.AdminAuditService) AsadminRecorderService(fish.payara.asadmin.recorder.AsadminRecorderService) CommandRunner(org.glassfish.api.admin.CommandRunner) CommandInvocation(org.glassfish.api.admin.CommandRunner.CommandInvocation)

Example 5 with CommandRunner

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

the class PayaraServerNameGenerator method getNodeNames.

private static List<String> getNodeNames(ActionReport report, AdminCommandContext context, CommandRunner commandRunner) {
    List<String> nodeNames = new ArrayList<>();
    CommandRunner.CommandInvocation listNodesCommand = commandRunner.getCommandInvocation("list-nodes", report, context.getSubject());
    listNodesCommand.execute();
    Properties extraProperties = listNodesCommand.report().getExtraProperties();
    if (extraProperties != null) {
        nodeNames.addAll((List<String>) extraProperties.get("nodeNames"));
    }
    return nodeNames;
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) CommandRunner(org.glassfish.api.admin.CommandRunner)

Aggregations

CommandRunner (org.glassfish.api.admin.CommandRunner)26 ParameterMap (org.glassfish.api.admin.ParameterMap)14 ActionReport (org.glassfish.api.ActionReport)12 ArrayList (java.util.ArrayList)8 Properties (java.util.Properties)5 RestActionReporter (org.glassfish.admin.rest.utils.xml.RestActionReporter)4 IOException (java.io.IOException)3 RemoteRestAdminCommand (com.sun.enterprise.admin.remote.RemoteRestAdminCommand)2 Server (com.sun.enterprise.config.serverbeans.Server)2 File (java.io.File)2 AdminCommand (org.glassfish.api.admin.AdminCommand)2 CommandException (org.glassfish.api.admin.CommandException)2 CommandModel (org.glassfish.api.admin.CommandModel)2 CommandInvocation (org.glassfish.api.admin.CommandRunner.CommandInvocation)2 InternalSystemAdministrator (org.glassfish.internal.api.InternalSystemAdministrator)2 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)1 ServerRemoteRestAdminCommand (com.sun.enterprise.admin.remote.ServerRemoteRestAdminCommand)1 DoNothingActionReporter (com.sun.enterprise.admin.report.DoNothingActionReporter)1 PlainTextActionReporter (com.sun.enterprise.admin.report.PlainTextActionReporter)1 Application (com.sun.enterprise.config.serverbeans.Application)1