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 Details

    • ACTION_CREATE

      public static final String ACTION_CREATE
      The action name for the WS-Man Create action
      See Also:
    • ACTION_COMMAND

      public static final String ACTION_COMMAND
      The action name for the WinRM Command action
      See Also:
    • ACTION_RECEIVE

      public static final String ACTION_RECEIVE
      The action name for the WinRM Receive action
      See Also:
    • ACTION_SIGNAL

      public static final String ACTION_SIGNAL
      The action name for the WinRM Signal action
      See Also:
    • ACTION_DELETE

      public static final String ACTION_DELETE
      The action name for the WinRM Delete action
      See Also:
    • ACTION_SEND

      public static final String ACTION_SEND
      The action name for the WinRM Send action
      See Also:
    • SIGNAL_TERM

      public static final String SIGNAL_TERM
      The action name for the WinRM terminate signal
      See Also:
    • URI_WINRM_RESOURCE

      public static final String URI_WINRM_RESOURCE
      The WinRM resource URI used in the SOAP envelope
      See Also:
    • URI_REPLY_TO

      public static final String URI_REPLY_TO
      The reply-to URI used in the SOAP envelope
      See Also:
    • DEFAULT_WINRM_PORT

      public static final int DEFAULT_WINRM_PORT
      The default port that WinRM listens on
      See Also:
    • WSMAN_PATH

      public static final String WSMAN_PATH
      The default WinRM url suffix
      See Also:
  • Constructor Details

    • WindowsSession

      public WindowsSession(SoapConnection connection, com.oracle.bedrock.Option... options)
      Create a new WindowsSession that connects to the WinRM service on the specified host.
      Parameters:
      connection - the SoapConnection to use to send SOAP messages
      options - the Options controlling the session
    • WindowsSession

      public WindowsSession(String hostName, int port, String userName, Authentication authentication, com.oracle.bedrock.Option... options)
      Create a new WindowsSession that connects to the WinRM service on the specified host.
      Parameters:
      hostName - the host name of the host running the WinRM service
      port - the port that the WinRM service is listening on
      userName - the name of the user to use to connect to the WinRM service
      authentication - the authentication to use to connect to the WinRM
      options - the Options controlling the session
  • Method Details

    • getSoapConnection

      public SoapConnection getSoapConnection()
      Obtain the SoapConnection being used by this WindowsSession to send SOAP messages.
      Returns:
      the SoapConnection being used by this WindowsSession to 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 the InputStreamConnector that connects the current command stdin stream.
      Returns:
      the InputStreamConnector that connects the current command stdin stream
    • setInputStreamConnector

      protected void setInputStreamConnector(InputStreamConnector inputStreamConnector)
      Set the InputStreamConnector that connects the current command stdin stream.
      Parameters:
      inputStreamConnector - the InputStreamConnector that connects the current command stdin stream
    • getOutputStreamConnector

      protected OutputStreamConnector getOutputStreamConnector()
    • setOutputStreamConnector

      protected void setOutputStreamConnector(OutputStreamConnector outputStreamConnector)
    • close

      public void close()
      Close this WindowsSession.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • connect

      protected void connect() throws Exception
      Connect this WindowsSession to 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 this WindowsSession to 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 this WindowsSession to the remote Windows platform. The working directory will be the specified directory.
      Parameters:
      workingDirectory - the working directory for this session
      environment - the environment variables to set for the session
      Throws:
      IOException - if the connection fails
      IllegalStateException - 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 execute
      args - the command line arguments for the command
      stdIn - the InputStream to use as the commands stdin
      stdOut - the OutputStream to use as the commands stdout
      stdErr - the OutputStream to 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 IOException
      Read the output from the current commands stdout and stderr streams.
      Returns:
      the ReceiveResponse containing 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 - the Options for waiting, including Timeout
      Returns:
      the exit value from the currently executing command