Class WindowsSession
- java.lang.Object
-
- com.oracle.bedrock.runtime.remote.windows.winrm.WindowsSession
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class WindowsSession extends Object implements Closeable
This class represents a reference to a WinRS remote shell.Copyright (c) 2015. All Rights Reserved. Oracle Corporation.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.- Author:
- Jonathan Knight
-
-
Field Summary
Fields Modifier and Type Field Description static StringACTION_COMMANDThe action name for the WinRM Command actionstatic StringACTION_CREATEThe action name for the WS-Man Create actionstatic StringACTION_DELETEThe action name for the WinRM Delete actionstatic StringACTION_RECEIVEThe action name for the WinRM Receive actionstatic StringACTION_SENDThe action name for the WinRM Send actionstatic StringACTION_SIGNALThe action name for the WinRM Signal actionstatic intDEFAULT_WINRM_PORTThe default port that WinRM listens onstatic StringSIGNAL_TERMThe action name for the WinRM terminate signalstatic StringURI_REPLY_TOThe reply-to URI used in the SOAP envelopestatic StringURI_WINRM_RESOURCEThe WinRM resource URI used in the SOAP envelopestatic StringWSMAN_PATHThe default WinRM url suffix
-
Constructor Summary
Constructors Constructor Description WindowsSession(SoapConnection connection, com.oracle.bedrock.Option... options)Create a newWindowsSessionthat connects to the WinRM service on the specified host.WindowsSession(String hostName, int port, String userName, Authentication authentication, com.oracle.bedrock.Option... options)Create a newWindowsSessionthat connects to the WinRM service on the specified host.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close thisWindowsSession.protected voidconnect()Connect thisWindowsSessionto the remote Windows platform.protected voidconnect(String workingDirectory)Connect thisWindowsSessionto the remote Windows platform.protected voidconnect(String workingDirectory, Properties environment)Connect thisWindowsSessionto the remote Windows platform.protected org.w3c.soap.envelope.EnvelopecreateEnvelope(String action)protected InputStreamConnectorcreateInputStreamConnector(InputStream stdIn)protected OutputStreamConnectorcreateOutputStreamConnector(OutputStream stdOut, OutputStream stdErr)voidexecute(String command, List<String> args, InputStream stdIn, OutputStream stdOut, OutputStream stdErr)Execute the specified command in the current Shell.intexitValue()Obtain the exit code from the last executed command.protected <T> TfindFirst(Collection<?> collection, Class<T> type)StringgetCommandId()Obtain the ID of the currently executing command.protected InputStreamConnectorgetInputStreamConnector()Obtain theInputStreamConnectorthat connects the current command stdin stream.protected OutputStreamConnectorgetOutputStreamConnector()StringgetShellReferenceId()Obtain the ID of the WinRM shell being used by this session.SoapConnectiongetSoapConnection()Obtain theSoapConnectionbeing used by thisWindowsSessionto send SOAP messages.protected com.microsoft.wsman.shell.ReceiveResponsereadOutputStreams()Read the output from the current commands stdout and stderr streams.protected voidsetCommandId(String commandId)Set the ID of the currently executing command.protected voidsetInputStreamConnector(InputStreamConnector inputStreamConnector)Set theInputStreamConnectorthat connects the current command stdin stream.protected voidsetOutputStreamConnector(OutputStreamConnector outputStreamConnector)protected voidsetShellReferenceId(String id)Set the reference ID of the currently connected WinRM Shell.protected voidterminateCommand()intwaitFor(com.oracle.bedrock.Option... options)Wait for the currently executing command to terminate and return its exit value.protected voidwriteToInputStream(String message)
-
-
-
Field Detail
-
ACTION_CREATE
public static final String ACTION_CREATE
The action name for the WS-Man Create action- See Also:
- Constant Field Values
-
ACTION_COMMAND
public static final String ACTION_COMMAND
The action name for the WinRM Command action- See Also:
- Constant Field Values
-
ACTION_RECEIVE
public static final String ACTION_RECEIVE
The action name for the WinRM Receive action- See Also:
- Constant Field Values
-
ACTION_SIGNAL
public static final String ACTION_SIGNAL
The action name for the WinRM Signal action- See Also:
- Constant Field Values
-
ACTION_DELETE
public static final String ACTION_DELETE
The action name for the WinRM Delete action- See Also:
- Constant Field Values
-
ACTION_SEND
public static final String ACTION_SEND
The action name for the WinRM Send action- See Also:
- Constant Field Values
-
SIGNAL_TERM
public static final String SIGNAL_TERM
The action name for the WinRM terminate signal- See Also:
- Constant Field Values
-
URI_WINRM_RESOURCE
public static final String URI_WINRM_RESOURCE
The WinRM resource URI used in the SOAP envelope- See Also:
- Constant Field Values
-
URI_REPLY_TO
public static final String URI_REPLY_TO
The reply-to URI used in the SOAP envelope- See Also:
- Constant Field Values
-
DEFAULT_WINRM_PORT
public static final int DEFAULT_WINRM_PORT
The default port that WinRM listens on- See Also:
- Constant Field Values
-
WSMAN_PATH
public static final String WSMAN_PATH
The default WinRM url suffix- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WindowsSession
public WindowsSession(SoapConnection connection, com.oracle.bedrock.Option... options)
Create a newWindowsSessionthat connects to the WinRM service on the specified host.- Parameters:
connection- theSoapConnectionto use to send SOAP messagesoptions- theOptions controlling the session
-
WindowsSession
public WindowsSession(String hostName, int port, String userName, Authentication authentication, com.oracle.bedrock.Option... options)
Create a newWindowsSessionthat connects to the WinRM service on the specified host.- Parameters:
hostName- the host name of the host running the WinRM serviceport- the port that the WinRM service is listening onuserName- the name of the user to use to connect to the WinRM serviceauthentication- the authentication to use to connect to the WinRMoptions- theOptions controlling the session
-
-
Method Detail
-
getSoapConnection
public SoapConnection getSoapConnection()
Obtain theSoapConnectionbeing used by thisWindowsSessionto send SOAP messages.- Returns:
- the
SoapConnectionbeing used by thisWindowsSessionto send SOAP messages
-
getShellReferenceId
public String getShellReferenceId()
Obtain the ID of the WinRM shell being used by this session.- Returns:
- the ID of the WinRM shell being used by this session
-
setShellReferenceId
protected void setShellReferenceId(String id)
Set the reference ID of the currently connected WinRM Shell.- Parameters:
id- the reference ID of the currently connected WinRM Shell
-
getCommandId
public String getCommandId()
Obtain the ID of the currently executing command.- Returns:
- the ID of the currently executing command
-
setCommandId
protected void setCommandId(String commandId)
Set the ID of the currently executing command.- Parameters:
commandId- the ID of the currently executing command
-
getInputStreamConnector
protected InputStreamConnector getInputStreamConnector()
Obtain theInputStreamConnectorthat connects the current command stdin stream.- Returns:
- the
InputStreamConnectorthat connects the current command stdin stream
-
setInputStreamConnector
protected void setInputStreamConnector(InputStreamConnector inputStreamConnector)
Set theInputStreamConnectorthat connects the current command stdin stream.- Parameters:
inputStreamConnector- theInputStreamConnectorthat connects the current command stdin stream
-
getOutputStreamConnector
protected OutputStreamConnector getOutputStreamConnector()
-
setOutputStreamConnector
protected void setOutputStreamConnector(OutputStreamConnector outputStreamConnector)
-
close
public void close()
Close thisWindowsSession.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
connect
protected void connect() throws ExceptionConnect thisWindowsSessionto the remote Windows platform. The working directory will be the users home directory.- Throws:
Exception- if the connection fails
-
connect
protected void connect(String workingDirectory) throws Exception
Connect thisWindowsSessionto the remote Windows platform. The working directory will be the specified directory.- Parameters:
workingDirectory- the working directory for this session- Throws:
Exception- if the connection fails
-
connect
protected void connect(String workingDirectory, Properties environment) throws IOException
Connect thisWindowsSessionto the remote Windows platform. The working directory will be the specified directory.- Parameters:
workingDirectory- the working directory for this sessionenvironment- the environment variables to set for the session- Throws:
IOException- if the connection failsIllegalStateException- if this session is already connected
-
execute
public void execute(String command, List<String> args, InputStream stdIn, OutputStream stdOut, OutputStream stdErr) throws IOException
Execute the specified command in the current Shell.- Parameters:
command- the command to executeargs- the command line arguments for the commandstdIn- theInputStreamto use as the commands stdinstdOut- theOutputStreamto use as the commands stdoutstdErr- theOutputStreamto use as the commands stderr- Throws:
IOException- if an error occurs executing the command
-
createOutputStreamConnector
protected OutputStreamConnector createOutputStreamConnector(OutputStream stdOut, OutputStream stdErr)
-
createInputStreamConnector
protected InputStreamConnector createInputStreamConnector(InputStream stdIn)
-
terminateCommand
protected void terminateCommand() throws IOException- Throws:
IOException
-
readOutputStreams
protected com.microsoft.wsman.shell.ReceiveResponse readOutputStreams() throws IOExceptionRead the output from the current commands stdout and stderr streams.- Returns:
- the
ReceiveResponsecontaining the stdout and stderr streams - Throws:
IOException- if an error occurs
-
writeToInputStream
protected void writeToInputStream(String message) throws IOException
- Throws:
IOException
-
createEnvelope
protected org.w3c.soap.envelope.Envelope createEnvelope(String action) throws IOException
- Throws:
IOException
-
findFirst
protected <T> T findFirst(Collection<?> collection, Class<T> type)
-
exitValue
public int exitValue()
Obtain the exit code from the last executed command.- Returns:
- the exit code from the last executed command or -1 if no command has been executed
-
waitFor
public int waitFor(com.oracle.bedrock.Option... options)
Wait for the currently executing command to terminate and return its exit value.- Parameters:
options- theOptions for waiting, includingTimeout- Returns:
- the exit value from the currently executing command
-
-