Search in sources :

Example 1 with ShellService

use of org.apache.felix.shell.ShellService in project Payara by payara.

the class OSGiShellCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    if (instance != null) {
        Server svr = domain.getServerNamed(instance);
        if (svr == null) {
            report.setMessage("No server target found for " + instance);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        String host = svr.getAdminHost();
        int port = svr.getAdminPort();
        try {
            ServerRemoteAdminCommand remote = new ServerRemoteAdminCommand(locator, "osgi", host, port, false, "admin", "", log);
            ParameterMap params = new ParameterMap();
            if (commandLine == null) {
                params.set("DEFAULT".toLowerCase(Locale.US), "asadmin-osgi-shell");
            } else if (commandLine instanceof String) {
                params.set("DEFAULT".toLowerCase(Locale.US), (String) commandLine);
            } else if (commandLine instanceof List) {
                params.set("DEFAULT".toLowerCase(Locale.US), (List<String>) commandLine);
            }
            if (sessionOp != null) {
                params.set("session", sessionOp);
            }
            if (sessionId != null) {
                params.set("session-id", sessionId);
            }
            report.setMessage(remote.executeCommand(params));
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
            return;
        } catch (CommandException x) {
            report.setMessage("Remote execution failed: " + x.getMessage());
            report.setFailureCause(x);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    String cmdName = "";
    String cmd = "";
    if (commandLine == null) {
        cmd = "asadmin-osgi-shell";
        cmdName = cmd;
    } else if (commandLine instanceof String) {
        cmd = (String) commandLine;
        cmdName = cmd;
    } else if (commandLine instanceof List) {
        for (Object arg : (List) commandLine) {
            if (cmd.length() == 0) {
                // first arg
                cmd = (String) arg;
                cmdName = cmd;
            } else {
                cmd += " " + (String) arg;
            }
        }
    } else if (commandLine instanceof String[]) {
        for (Object arg : (String[]) commandLine) {
            if (cmd.length() == 0) {
                // first arg
                cmd = (String) arg;
                cmdName = cmd;
            } else {
                cmd += " " + (String) arg;
            }
        }
    } else {
        // shouldn't happen...
        report.setMessage("Unable to deal with argument list of type " + commandLine.getClass().getName());
        report.setActionExitCode(ActionReport.ExitCode.WARNING);
        return;
    }
    // standard output...
    ByteArrayOutputStream bOut = new ByteArrayOutputStream(512);
    PrintStream out = new PrintStream(bOut);
    // error output...
    ByteArrayOutputStream bErr = new ByteArrayOutputStream(512);
    PrintStream err = new PrintStream(bErr);
    try {
        Object shell = null;
        ServiceReference sref = ctx.getServiceReference("org.apache.felix.service.command.CommandProcessor");
        if (sref != null) {
            shell = ctx.getService(sref);
        }
        if (shell == null) {
            // try with felix...
            sref = ctx.getServiceReference("org.apache.felix.shell.ShellService");
            if (sref != null) {
                shell = ctx.getService(sref);
            }
            if (shell == null) {
                report.setMessage("No Shell Service available");
                report.setActionExitCode(ActionReport.ExitCode.WARNING);
                return;
            } else if ("asadmin-osgi-shell".equals(cmdName)) {
                out.println("felix");
            } else {
                ShellService s = (ShellService) shell;
                s.executeCommand(cmd, out, err);
            }
        } else {
            // try with gogo...
            // GLASSFISH-19126 - prepare fake input stream...
            InputStream in = new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }

                @Override
                public int available() throws IOException {
                    return 0;
                }

                @Override
                public int read(byte[] b) throws IOException {
                    return -1;
                }

                @Override
                public int read(byte[] b, int off, int len) throws IOException {
                    return -1;
                }
            };
            CommandProcessor cp = (CommandProcessor) shell;
            if (sessionOp == null) {
                if ("asadmin-osgi-shell".equals(cmdName)) {
                    out.println("gogo");
                } else {
                    CommandSession session = cp.createSession(in, out, err);
                    session.execute(cmd);
                    session.close();
                }
            } else if ("new".equals(sessionOp)) {
                CommandSession session = cp.createSession(null, null, null);
                RemoteCommandSession remote = new RemoteCommandSession(session);
                log.log(Level.FINE, "Remote session established: {0}", remote.getId());
                sessions.put(remote.getId(), remote);
                out.println(remote.getId());
            } else if ("list".equals(sessionOp)) {
                for (String id : sessions.keySet()) {
                    out.println(id);
                }
            } else if ("execute".equals(sessionOp)) {
                RemoteCommandSession remote = sessions.get(sessionId);
                CommandSession session = remote.attach(in, out, err);
                session.execute(cmd);
                remote.detach();
            } else if ("stop".equals(sessionOp)) {
                RemoteCommandSession remote = sessions.remove(sessionId);
                CommandSession session = remote.attach(in, out, err);
                session.close();
                log.log(Level.FINE, "Remote session closed: {0}", remote.getId());
            }
        }
        out.flush();
        err.flush();
        String output = bOut.toString("UTF-8");
        String errors = bErr.toString("UTF-8");
        report.setMessage(output);
        if (errors.length() > 0) {
            report.setMessage(errors);
            report.setActionExitCode(ActionReport.ExitCode.WARNING);
        } else {
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        }
    } catch (Exception ex) {
        report.setMessage(ex.getMessage());
        report.setActionExitCode(ActionReport.ExitCode.WARNING);
    }
}
Also used : PrintStream(java.io.PrintStream) Server(com.sun.enterprise.config.serverbeans.Server) InputStream(java.io.InputStream) ServerRemoteAdminCommand(com.sun.enterprise.admin.remote.ServerRemoteAdminCommand) ParameterMap(org.glassfish.api.admin.ParameterMap) CommandException(org.glassfish.api.admin.CommandException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ActionReport(org.glassfish.api.ActionReport) RestEndpoint(org.glassfish.api.admin.RestEndpoint) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) ShellService(org.apache.felix.shell.ShellService) CommandSession(org.apache.felix.service.command.CommandSession) CommandProcessor(org.apache.felix.service.command.CommandProcessor) List(java.util.List)

Aggregations

ServerRemoteAdminCommand (com.sun.enterprise.admin.remote.ServerRemoteAdminCommand)1 Server (com.sun.enterprise.config.serverbeans.Server)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 List (java.util.List)1 CommandProcessor (org.apache.felix.service.command.CommandProcessor)1 CommandSession (org.apache.felix.service.command.CommandSession)1 ShellService (org.apache.felix.shell.ShellService)1 ActionReport (org.glassfish.api.ActionReport)1 CommandException (org.glassfish.api.admin.CommandException)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1 RestEndpoint (org.glassfish.api.admin.RestEndpoint)1 ServiceReference (org.osgi.framework.ServiceReference)1