Search in sources :

Example 1 with AdminCommand

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

the class RuntimeRootImpl method restartDomain.

public void restartDomain() {
    final ModulesRegistry registry = InjectedValues.getInstance().getModulesRegistry();
    final AdminCommandContext ctx = new AdminCommandContextImpl(AMXLoggerInfo.getLogger(), new PlainTextActionReporter());
    final AdminCommand cmd = new RestartDomainCommand(registry);
    cmd.execute(ctx);
}
Also used : AdminCommandContextImpl(org.glassfish.api.admin.AdminCommandContextImpl) ModulesRegistry(com.sun.enterprise.module.ModulesRegistry) AdminCommand(org.glassfish.api.admin.AdminCommand) RestartDomainCommand(com.sun.enterprise.v3.admin.RestartDomainCommand) AdminCommandContext(org.glassfish.api.admin.AdminCommandContext) PlainTextActionReporter(com.sun.enterprise.admin.report.PlainTextActionReporter)

Example 2 with AdminCommand

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

the class ListCommandDescriptorsCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    setAdminCommands();
    sort();
    for (AdminCommand cmd : adminCmds) {
        cliCmds.add(reflect(cmd));
    }
    ActionReport report = context.getActionReport();
    StringBuilder sb = new StringBuilder();
    sb.append("ALL COMMANDS: ").append(EOL);
    for (CLICommand cli : cliCmds) {
        sb.append(cli.toString()).append(EOL);
    }
    report.setMessage(sb.toString());
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : AdminCommand(org.glassfish.api.admin.AdminCommand) ActionReport(org.glassfish.api.ActionReport)

Example 3 with AdminCommand

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

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

the class AdminAdapter method doCommand.

private ActionReport doCommand(String requestURI, Request req, ActionReport report, Payload.Outbound outboundPayload, Subject subject) throws ProcessHttpCommandRequestException {
    if (!requestURI.startsWith(getContextRoot())) {
        String msg = adminStrings.getLocalString("adapter.panic", "Wrong request landed in AdminAdapter {0}", requestURI);
        report.setMessage(msg);
        LogHelper.getDefaultLogger().info(msg);
        return report;
    }
    // wbn handle no command and no slash-suffix
    String command = "";
    if (requestURI.length() > getContextRoot().length() + 1) {
        command = requestURI.substring(getContextRoot().length() + 1);
    }
    String scope = getScope(command);
    command = getCommandAfterScope(command);
    String qs = req.getQueryString();
    final ParameterMap parameters = extractParameters(qs);
    String passwordOptions = req.getHeader("X-passwords");
    if (passwordOptions != null) {
        decodePasswords(parameters, passwordOptions);
    }
    try {
        // If the upload option is present and false, this needs converting to a path
        String uploadValue = parameters.getOne("upload");
        if ("false".equalsIgnoreCase(uploadValue)) {
            Buffer contentBuffer = req.getInputBuffer().getBuffer();
            int capacity = contentBuffer.capacity();
            byte[] path = new byte[capacity];
            for (int i = 0; i < capacity; i++) {
                path[i] = contentBuffer.get(i);
            }
            // path as passed in
            parameters.add("path", new String(path));
            // remove to prevent exception as this is not a param in the command class
            parameters.remove("upload");
        }
        Payload.Inbound inboundPayload = PayloadImpl.Inbound.newInstance(req.getContentType(), req.getInputStream());
        if (aalogger.isLoggable(Level.FINE)) {
            aalogger.log(Level.FINE, "***** AdminAdapter {0}  *****", req.getMethod());
        }
        // or not the upload flag is set.
        if ("true".equalsIgnoreCase(uploadValue)) {
            // This is a file that needs to be extracted
            File extractLocation = new File(domain.getApplicationRoot() + File.separator + "upload-" + inboundPayload.hashCode() + File.separator);
            if (!extractLocation.mkdirs()) {
                aalogger.log(Level.WARNING, strings.getLocalString("payload.mkdirsFailed", "Attempt to create directories for {0} failed; no further information is available. Continuing.", extractLocation.getAbsolutePath()));
            }
            PayloadFilesManager.Perm permFileManager = new PayloadFilesManager.Perm(extractLocation, null, aalogger);
            permFileManager.processParts(inboundPayload);
            parameters.add("path", extractLocation.toString());
            parameters.remove("upload");
        }
        AdminCommand adminCommand = commandRunner.getCommand(scope, command, report, aalogger);
        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
            aalogger.log(Level.FINE, message);
            report.setMessage(message);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return report;
        }
        // Validate admin command eTag
        String modelETag = req.getHeader(RemoteRestAdminCommand.COMMAND_MODEL_MATCH_HEADER);
        if (modelETag != null && !commandRunner.validateCommandModelETag(adminCommand, modelETag)) {
            String message = adminStrings.getLocalString("commandmodel.etag.invalid", "Cached command model for command {0} is invalid.", command);
            aalogger.log(Level.FINE, message);
            report.setMessage(message);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            throw new ProcessHttpCommandRequestException(report, HttpStatus.PRECONDITION_FAILED_412);
        }
        // Execute
        if (validatePrivacy(adminCommand)) {
            // if (adminCommand.getClass().getAnnotation(Visibility.class).privacy().equals(visibility.privacy())) {
            // todo : needs to be changed, we should reuse adminCommand
            CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation(scope, command, report, subject, parameters.containsKey("notify"));
            inv.parameters(parameters).inbound(inboundPayload).outbound(outboundPayload).execute();
            try {
                // note it has become extraordinarily difficult to change the reporter!
                CommandRunnerImpl.ExecutionContext inv2 = (CommandRunnerImpl.ExecutionContext) inv;
                report = inv2.report();
            } catch (Exception e) {
            }
        } else {
            report.failure(aalogger, adminStrings.getLocalString("adapter.wrongprivacy", "Command {0} does not have {1} visibility", command, privacyClass.getSimpleName().toLowerCase(Locale.ENGLISH)), null);
            return report;
        }
    } catch (ProcessHttpCommandRequestException reqEx) {
        throw reqEx;
    } 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 AdminAdapter exception handler");
    }
    return report;
}
Also used : Buffer(org.glassfish.grizzly.Buffer) ParameterMap(org.glassfish.api.admin.ParameterMap) LoginException(javax.security.auth.login.LoginException) RemoteAdminAccessException(org.glassfish.internal.api.RemoteAdminAccessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Payload(org.glassfish.api.admin.Payload) RemoteRestAdminCommand(com.sun.enterprise.admin.remote.RemoteRestAdminCommand) AdminCommand(org.glassfish.api.admin.AdminCommand) PayloadFilesManager(org.glassfish.admin.payload.PayloadFilesManager) File(java.io.File) CommandRunner(org.glassfish.api.admin.CommandRunner)

Example 5 with AdminCommand

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

the class CommandResourceMetaData method getRestRedirectPointToBean.

@SuppressWarnings("unchecked")
public static List<CommandResourceMetaData> getRestRedirectPointToBean(String beanName) {
    synchronized (restRedirects) {
        if (restRedirects.isEmpty()) {
            final ServiceLocator habitat = Globals.getDefaultHabitat();
            processConfigBeans(habitat);
            List<ActiveDescriptor<?>> iter = habitat.getDescriptors(BuilderHelper.createContractFilter(AdminCommand.class.getName()));
            for (ActiveDescriptor<?> ad : iter) {
                if (!(ad.getQualifiers().contains(RestEndpoints.class.getName()))) {
                    continue;
                }
                if (!ad.isReified()) {
                    try {
                        habitat.reifyDescriptor(ad);
                    } catch (MultiException me) {
                        // If we can't see the command, forget it
                        continue;
                    }
                }
                final Class<? extends AdminCommand> clazz = (Class<? extends AdminCommand>) ad.getImplementationClass();
                RestEndpoints endpoints = clazz.getAnnotation(RestEndpoints.class);
                if (endpoints != null) {
                    RestEndpoint[] list = endpoints.value();
                    if ((list != null) && (list.length > 0)) {
                        for (RestEndpoint endpoint : list) {
                            Service service = clazz.getAnnotation(Service.class);
                            String configBean = endpoint.configBean().getSimpleName();
                            CommandResourceMetaData metaData = new CommandResourceMetaData();
                            metaData.command = service.name();
                            metaData.httpMethod = endpoint.opType().name();
                            metaData.resourcePath = endpoint.path().isEmpty() ? service.name() : endpoint.path();
                            metaData.displayName = endpoint.description().isEmpty() ? metaData.resourcePath : endpoint.description();
                            metaData.commandParams = new ParameterMetaData[endpoint.params().length];
                            int index = 0;
                            for (RestParam param : endpoint.params()) {
                                ParameterMetaData currentParam = new ParameterMetaData();
                                metaData.commandParams[index] = currentParam;
                                currentParam.name = param.name();
                                currentParam.value = param.value();
                                index++;
                            }
                            addCommandMetaData(configBean, metaData);
                        }
                    }
                }
            }
        }
    }
    return restRedirects.get(beanName);
}
Also used : ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Service(org.jvnet.hk2.annotations.Service) RestEndpoints(org.glassfish.api.admin.RestEndpoints) RestEndpoint(org.glassfish.api.admin.RestEndpoint) RestEndpoint(org.glassfish.api.admin.RestEndpoint) RestParam(org.glassfish.api.admin.RestParam) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) AdminCommand(org.glassfish.api.admin.AdminCommand) MultiException(org.glassfish.hk2.api.MultiException)

Aggregations

AdminCommand (org.glassfish.api.admin.AdminCommand)6 CommandRunner (org.glassfish.api.admin.CommandRunner)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 RemoteRestAdminCommand (com.sun.enterprise.admin.remote.RemoteRestAdminCommand)1 PlainTextActionReporter (com.sun.enterprise.admin.report.PlainTextActionReporter)1 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)1 RestartDomainCommand (com.sun.enterprise.v3.admin.RestartDomainCommand)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LoginException (javax.security.auth.login.LoginException)1 PayloadFilesManager (org.glassfish.admin.payload.PayloadFilesManager)1 ActionReport (org.glassfish.api.ActionReport)1 AdminCommandContext (org.glassfish.api.admin.AdminCommandContext)1 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1 Payload (org.glassfish.api.admin.Payload)1 RestEndpoint (org.glassfish.api.admin.RestEndpoint)1 RestEndpoints (org.glassfish.api.admin.RestEndpoints)1 RestParam (org.glassfish.api.admin.RestParam)1