Search in sources :

Example 1 with CommandInterceptor

use of com.oracle.bedrock.runtime.options.CommandInterceptor in project oracle-bedrock by coherence-community.

the class JSchRemoteTerminal method launch.

@Override
public RemoteApplicationProcess launch(Launchable launchable, Class<? extends Application> applicationClass, OptionsByType optionsByType) {
    // acquire the remote platform on which to launch the application
    RemotePlatform platform = getRemotePlatform();
    // establish a specialized SocketFactory for JSch
    JSchSocketFactory socketFactory = new JSchSocketFactory();
    // initially there's no session
    Session session = null;
    try {
        // create the remote session
        session = sessionFactory.createSession(platform.getAddress().getHostName(), platform.getPort(), platform.getUserName(), platform.getAuthentication(), socketFactory, optionsByType);
        ChannelExec execChannel = (ChannelExec) session.openChannel("exec");
        // (re)define the "local.address" variable so that we can use for resolving the platform
        optionsByType.add(Variable.with("local.address", socketFactory.getLastLocalAddress().getHostAddress()));
        // ----- establish the remote environment variables -----
        String environmentVariables = "";
        // get the remote environment variables for the remote application
        Properties variables = launchable.getEnvironmentVariables(platform, optionsByType);
        // determine the format to use for setting variables
        String format;
        Shell shell = optionsByType.getOrSetDefault(Shell.class, Shell.isUnknown());
        switch(shell.getType()) {
            case SH:
            case BASH:
                format = "export %s=%s ; ";
                break;
            case CSH:
            case TSCH:
                format = "setenv %s %s ; ";
                break;
            default:
                // when we don't know, assume something bash-like
                format = "export %s=%s ; ";
                break;
        }
        List<String> arguments = launchable.getCommandLineArguments(platform, optionsByType);
        CommandInterceptor interceptor = optionsByType.get(CommandInterceptor.class);
        String executableName = launchable.getCommandToExecute(platform, optionsByType);
        File workingDirectory = optionsByType.get(WorkingDirectory.class).resolve(platform, optionsByType);
        String remoteCommand;
        if (interceptor == null) {
            for (String variableName : variables.stringPropertyNames()) {
                environmentVariables += String.format(format, variableName, StringHelper.doubleQuoteIfNecessary(variables.getProperty(variableName)));
            }
            // ----- establish the application command line to execute -----
            // determine the command to execute remotely
            StringBuilder command = new StringBuilder(executableName);
            // add the arguments
            for (String arg : arguments) {
                command.append(" ").append(arg);
            }
            // the actual remote command must include changing to the remote directory
            remoteCommand = environmentVariables + String.format("cd %s ; %s", workingDirectory, command);
        } else {
            remoteCommand = interceptor.onExecute(executableName, arguments, variables, workingDirectory);
        }
        execChannel.setCommand(remoteCommand);
        // ----- establish the remote application process to represent the remote application -----
        // establish a RemoteApplicationProcess representing the remote application
        RemoteApplicationProcess process = new JschRemoteApplicationProcess(session, execChannel);
        if (optionsByType.get(LaunchLogging.class).isEnabled()) {
            Table diagnosticsTable = optionsByType.get(Table.class);
            if (diagnosticsTable != null && LOGGER.isLoggable(Level.INFO)) {
                diagnosticsTable.addRow("Application Executable ", executableName);
                LOGGER.log(Level.INFO, "Oracle Bedrock " + Bedrock.getVersion() + ": Starting Application...\n" + "------------------------------------------------------------------------\n" + diagnosticsTable.toString() + "\n" + "------------------------------------------------------------------------\n");
            }
        }
        // connect the channel
        execChannel.connect(session.getTimeout());
        return process;
    } catch (JSchException e) {
        if (session != null) {
            session.disconnect();
        }
        throw new RuntimeException("Failed to create remote application", e);
    }
}
Also used : RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) JSchException(com.jcraft.jsch.JSchException) WorkingDirectory(com.oracle.bedrock.runtime.options.WorkingDirectory) Table(com.oracle.bedrock.table.Table) CommandInterceptor(com.oracle.bedrock.runtime.options.CommandInterceptor) Properties(java.util.Properties) ChannelExec(com.jcraft.jsch.ChannelExec) Shell(com.oracle.bedrock.runtime.options.Shell) LaunchLogging(com.oracle.bedrock.options.LaunchLogging) File(java.io.File) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) Session(com.jcraft.jsch.Session)

Aggregations

ChannelExec (com.jcraft.jsch.ChannelExec)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 LaunchLogging (com.oracle.bedrock.options.LaunchLogging)1 CommandInterceptor (com.oracle.bedrock.runtime.options.CommandInterceptor)1 Shell (com.oracle.bedrock.runtime.options.Shell)1 WorkingDirectory (com.oracle.bedrock.runtime.options.WorkingDirectory)1 RemoteApplicationProcess (com.oracle.bedrock.runtime.remote.RemoteApplicationProcess)1 RemotePlatform (com.oracle.bedrock.runtime.remote.RemotePlatform)1 Table (com.oracle.bedrock.table.Table)1 File (java.io.File)1 Properties (java.util.Properties)1