use of com.oracle.bedrock.runtime.options.Shell 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);
}
}
Aggregations