Class AbstractConsoleSession<O,R extends ConsoleRequest<R>>

java.lang.Object
org.dellroad.jct.core.AbstractConsoleSession<O,R>
Type Parameters:
O - associated owner type
R - associated request type
All Implemented Interfaces:
ConsoleSession<O,R>
Direct Known Subclasses:
AbstractExecSession, AbstractShellSession

public abstract class AbstractConsoleSession<O,R extends ConsoleRequest<R>> extends Object implements ConsoleSession<O,R>
Support superclass for ConsoleSession implementations.
  • Field Details

    • owner

      protected final O owner
    • request

      protected final R extends ConsoleRequest<R> request
  • Constructor Details

    • AbstractConsoleSession

      protected AbstractConsoleSession(O owner, R request)
      Constructor.
      Parameters:
      owner - session owner
      request - associated request
      Throws:
      IllegalArgumentException - if either parameter is null
  • Method Details

    • getOwner

      public O getOwner()
      Description copied from interface: ConsoleSession
      Get the owner, i.e., the console component that created this session.
      Specified by:
      getOwner in interface ConsoleSession<O,R extends ConsoleRequest<R>>
      Returns:
      owning component
    • getRequest

      public R getRequest()
      Description copied from interface: ConsoleSession
      Get the request from which this session was created.
      Specified by:
      getRequest in interface ConsoleSession<O,R extends ConsoleRequest<R>>
      Returns:
      original request
    • execute

      public int execute() throws InterruptedException
      Description copied from interface: ConsoleSession
      Execute this session synchronously in the current thread.

      Instances should ensure any associated resources are cleaned up when this method returns, whether normally or via thrown exception.

      Specified by:
      execute in interface ConsoleSession<O,R extends ConsoleRequest<R>>
      Returns:
      zero if successful, non-zero error code if an error occurred
      Throws:
      InterruptedException - if execution is interrupted via ConsoleSession.interrupt()
    • interrupt

      public boolean interrupt()
      Description copied from interface: ConsoleSession
      Interrupt the execution of this session.

      This method no effect unless ConsoleSession.execute() has been invoked and not yet returned.

      How this method is handled is up to the session: in general, it should cause the execution of ConsoleSession.execute() to be interrupted, such that ConsoleSession.execute() then either throws an InterruptedException or at least returns immediately. In general, it should have roughly the same effect as one would expect when pressing Control-C on a controlling terminal.

      Specified by:
      interrupt in interface ConsoleSession<O,R extends ConsoleRequest<R>>
      Returns:
      true if execution was interrupted, false if it was not possible to interrupt execution
    • doExecute

      protected abstract int doExecute() throws InterruptedException
      Execute this session.

      This method is invoked by execute() to do the actual work.

      Returns:
      zero if successful, non-zero error code if an error occurred
      Throws:
      InterruptedException - if the current thread is interrupted
    • doInterrupt

      protected boolean doInterrupt(Thread thread)
      Interrupt execution.

      This method is invoked by interrupt() to actually do whatever is needed to interrupt execution, but only if execute() is still executing.

      The implementation in AbstractConsoleSession just invokes Thread.interrupt() on the given thread.

      NOTE: When this method is invoked, the current instance will be locked.

      Parameters:
      thread - the thread currently executing execute()
      Returns:
      true if execution was interrupted, false if it was not possible to interrupt execution
      Throws:
      IllegalArgumentException - if thread is null