com.foxsmart.ic.net
Class MindTermSshProtocolHandler

java.lang.Object
  extended by com.foxsmart.ic.net.MindTermSshProtocolHandler
All Implemented Interfaces:
CliProtocolHandler, ProtocolHandler

public class MindTermSshProtocolHandler
extends java.lang.Object
implements CliProtocolHandler

The MindTermSshProtocolHandler class helps with the handling of sending and receiving of data to and from an SSH client using the MindTerm implementation. This class is a wrapper to the MindTermSshProtocolHandlerImpl class which contains the implementation. The implementation is separated out to ensure the getAvailable method can be implemented without the mindterm.jar implementation being on the classpath. Otherwise, a NoClassDefFoundError exception will be thrown when instantiating this handler (i.e. the exception will get thrown before the handler can determine if the implementation is available or not).


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.foxsmart.ic.net.ProtocolHandler
ProtocolHandler.Protocol
 
Field Summary
protected  org.apache.commons.logging.Log log
           
 
Fields inherited from interface com.foxsmart.ic.net.CliProtocolHandler
CR, DEFAULT_NO_DATA_READ_DELAY, PASSWORD_PROMPT, USERNAME_PROMPT
 
Fields inherited from interface com.foxsmart.ic.net.ProtocolHandler
DEFAULT_PROTOCOL_TIMEOUT_SECONDS
 
Constructor Summary
MindTermSshProtocolHandler()
          Default Constructor.
 
Method Summary
protected  void checkForValidHandler()
           
 void connect(java.lang.String hostname, java.lang.Integer port, java.lang.String username, java.lang.String password, java.lang.Integer timeout)
          Connects to the hostname at the specified port number and authenticates with the remote device.
 void disconnect()
          Disconnects the socket connection.
 java.lang.String expect(java.util.List matches)
          Reads data from the device until one of the expected strings is found.
 java.lang.String expect(java.util.List matches, boolean includeMatch)
          Reads data from the device until one of the expected strings is found.
 java.lang.String expect(java.util.List matches, boolean includeMatch, java.lang.Integer timeout)
          Reads data from the device until one of a list of expected strings is found.
 java.lang.String expect(java.lang.String match)
          Reads data from the device until the expected string is found.
 java.lang.String expect(java.lang.String match, boolean includeMatch)
          Reads data from the device until the expected string is found.
 java.lang.String expect(java.lang.String match, boolean includeMatch, java.lang.Integer timeout)
          Reads data from the device until the expected string is found.
 java.lang.String getAscii(java.lang.String string, int pos)
          Gets a formatted ASCII dump of the passed in string.
 boolean getAvailable()
          Returns whether this protocol handler is available.
 int getDefaultNoDataReadDelay()
          Gets the default no data read delay.
 java.lang.Integer getDefaultProtocolTimeout()
          Gets the default operation timeout value.
 java.io.InputStream getInputStream()
          Returns the connection input stream.
 java.io.OutputStream getOutputStream()
          Returns the connection output stream.
 boolean getPasswordUsed()
          Gets whether the password was used for authentication.
 ProtocolHandler.Protocol getProtocol()
          Returns the SSH protocol.
 int getSshProtocolVersion()
           
 boolean getUsernameUsed()
          Gets whether the username was used for authentication.
 boolean isConnected()
          Returns true if the client is currently connected to a server.
 java.lang.String read()
          Reads the next set of data from the device until enough time has passed that we assume no more data will arrive.
 java.lang.String read(java.lang.Integer timeout)
          Reads the next set of data from the device until enough time has passed that we assume no more data will arrive.
 void sendCommand(java.lang.String command)
          Sends a command to the device.
 void sendCommand(java.lang.String command, boolean containsPassword)
          Sends a command to the device.
 void setDefaultNoDataReadDelay(int delay)
          Modifies the default no data read delay in 1/10 seconds.
 void setDefaultProtocolTimeout(java.lang.Integer timeout)
          Modifies the default operation protocol timeout value.
 void setPasswordUsed(boolean passwordUsed)
          Sets whether the password was used for authentication.
 void setUsernameUsed(boolean usernameUsed)
          Sets whether the username was used for authentication.
 java.lang.String toString()
           
 void write(java.lang.String text)
          Writes the specified string to the device.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log
Constructor Detail

MindTermSshProtocolHandler

public MindTermSshProtocolHandler()
Default Constructor.

Method Detail

getProtocol

public ProtocolHandler.Protocol getProtocol()
Returns the SSH protocol.

Specified by:
getProtocol in interface ProtocolHandler
Returns:
the SSH protocol.

getAvailable

public boolean getAvailable()
Returns whether this protocol handler is available.

Specified by:
getAvailable in interface ProtocolHandler
Returns:
true when the MindTerm implementation is available in the classpath or false if not.

connect

public void connect(java.lang.String hostname,
                    java.lang.Integer port,
                    java.lang.String username,
                    java.lang.String password,
                    java.lang.Integer timeout)
             throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Connects to the hostname at the specified port number and authenticates with the remote device. A socket connection will be established when this call is complete.

Specified by:
connect in interface CliProtocolHandler
Parameters:
hostname - The name of the remote host.
port - The port to connect to on the remote host. If null is passed in, a default port will be used.
username - The username to connect with. Null should be passed in if no username is required.
password - The password to connect with. Null should be passed in if no password is required.
timeout - The timeout in seconds. If null is passed in, a default timeout will be used.
Throws:
AuthenticationFailedException - when a successful connection could not be made
java.net.SocketException - if the socket timeout could not be set.
java.io.IOException - if the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
ThreadTimeoutException - if a timeout occurred while connecting to the router.
java.net.UnknownHostException - If the hostname cannot be resolved.
java.lang.Exception - if any other problems were encountered.

isConnected

public boolean isConnected()
Description copied from interface: CliProtocolHandler
Returns true if the client is currently connected to a server.

Specified by:
isConnected in interface CliProtocolHandler
Returns:
True if the client is currently connected to a server, false otherwise.

disconnect

public void disconnect()
                throws java.io.IOException
Description copied from interface: CliProtocolHandler
Disconnects the socket connection.

Specified by:
disconnect in interface CliProtocolHandler
Throws:
java.io.IOException - If there is an error closing the socket.

getInputStream

public java.io.InputStream getInputStream()
Description copied from interface: CliProtocolHandler
Returns the connection input stream. You should not close the stream when you finish with it. Rather, you should call disconnect.

Specified by:
getInputStream in interface CliProtocolHandler
Returns:
The connection input stream.

getOutputStream

public java.io.OutputStream getOutputStream()
Description copied from interface: CliProtocolHandler
Returns the connection output stream. You should not close the stream when you finish with it. Rather, you should call disconnect.

Specified by:
getOutputStream in interface CliProtocolHandler
Returns:
the connection output stream.

write

public void write(java.lang.String text)
           throws java.io.IOException
Description copied from interface: CliProtocolHandler
Writes the specified string to the device.

Specified by:
write in interface CliProtocolHandler
Parameters:
text - The text to write to the device.
Throws:
java.io.IOException - if there was a problem writing the data.

sendCommand

public void sendCommand(java.lang.String command)
                 throws java.io.IOException
Description copied from interface: CliProtocolHandler
Sends a command to the device. This method functions the same as calling the write method with the exception that a carriage return is appended. This method is the same as sendCommand(command, false).

Specified by:
sendCommand in interface CliProtocolHandler
Parameters:
command - The command to send to the device.
Throws:
java.io.IOException - if there was a problem sending the command.

sendCommand

public void sendCommand(java.lang.String command,
                        boolean containsPassword)
                 throws java.io.IOException
Description copied from interface: CliProtocolHandler
Sends a command to the device. This method functions the same as calling the write method with the exception that a carriage return is appended.

Specified by:
sendCommand in interface CliProtocolHandler
Parameters:
command - The command to send to the device.
containsPassword - true if the command contains a password or false if not. Send true to cause the password to only be logged in "trace" mode.
Throws:
java.io.IOException - if there was a problem sending the command.

expect

public java.lang.String expect(java.lang.String match,
                               boolean includeMatch)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until the expected string is found. Once the expected string is found, the data buffer up to the point where the string was found is returned. A default timeout of 1 minute will be used.

Specified by:
expect in interface CliProtocolHandler
Parameters:
match - The string to search for.
includeMatch - If true, the match string is returned in the resulting data string. If false, the match string is not returned as part of the resultant string.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

expect

public java.lang.String expect(java.util.List matches,
                               boolean includeMatch)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until one of the expected strings is found. Once an expected string is found, the data buffer up to the point where the string was found is returned. A default timeout of 1 minute will be used.

Specified by:
expect in interface CliProtocolHandler
Parameters:
matches - The strings to search for.
includeMatch - If true, the match string is returned in the resulting data string. If false, the match string is not returned as part of the resultant string.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

expect

public java.lang.String expect(java.lang.String match)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until the expected string is found. Once the expected string is found, the data buffer up to the point where the string was found is returned. A default timeout of 1 minute will be used and the match string will not be included in the resultant string.

Specified by:
expect in interface CliProtocolHandler
Parameters:
match - The string to search for.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

expect

public java.lang.String expect(java.util.List matches)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until one of the expected strings is found. Once an expected string is found, the data buffer up to the point where the string was found is returned. A default timeout of 1 minute will be used and the match string will not be included in the resultant string.

Specified by:
expect in interface CliProtocolHandler
Parameters:
matches - The strings to search for.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

read

public java.lang.String read()
                      throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads the next set of data from the device until enough time has passed that we assume no more data will arrive. The default timeout will be used.

Specified by:
read in interface CliProtocolHandler
Returns:
the next set of read data.
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

expect

public java.lang.String expect(java.lang.String match,
                               boolean includeMatch,
                               java.lang.Integer timeout)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until the expected string is found. Once the expected string is found, the data buffer up to the point where the string was found is returned.

Specified by:
expect in interface CliProtocolHandler
Parameters:
match - The string to search for.
includeMatch - If true, the match string is returned in the resulting data string. If false, the match string is not returned as part of the resultant string.
timeout - The timeout value in seconds to wait for the data to be returned. If null is passed, no timeout will be used. Beware that if the expected match string is not found or if the device doesn't respond, this method will not return.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

expect

public java.lang.String expect(java.util.List matches,
                               boolean includeMatch,
                               java.lang.Integer timeout)
                        throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads data from the device until one of a list of expected strings is found. Once the expected string is found, the data buffer up to the point where the string was found is returned.

Specified by:
expect in interface CliProtocolHandler
Parameters:
matches - The strings to search for.
includeMatch - If true, the match string is returned in the resulting data string. If false, the match string is not returned as part of the resultant string.
timeout - The timeout value in seconds to wait for the data to be returned. If null is passed, no timeout will be used. Beware that if the expected match string is not found or if the device doesn't respond, this method will not return.
Returns:
the expected data buffer
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

read

public java.lang.String read(java.lang.Integer timeout)
                      throws java.lang.Exception
Description copied from interface: CliProtocolHandler
Reads the next set of data from the device until enough time has passed that we assume no more data will arrive.

Specified by:
read in interface CliProtocolHandler
Parameters:
timeout - The timeout value in seconds to wait for the data to be returned. If null is passed, no timeout will be used. Beware that if the expected match string is not found or if the device doesn't respond, this method will not return.
Returns:
the next set of read data.
Throws:
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.

getAscii

public java.lang.String getAscii(java.lang.String string,
                                 int pos)
Description copied from interface: CliProtocolHandler
Gets a formatted ASCII dump of the passed in string.

Specified by:
getAscii in interface CliProtocolHandler
Parameters:
string - the string to log
pos - the byte position of the read string
Returns:
the formatted ASCII dump.

setDefaultProtocolTimeout

public void setDefaultProtocolTimeout(java.lang.Integer timeout)
Description copied from interface: ProtocolHandler
Modifies the default operation protocol timeout value.

Specified by:
setDefaultProtocolTimeout in interface ProtocolHandler
Parameters:
timeout - The default timeout value in seconds to wait for the data to be returned. If null is passed, no timeout will be used. Beware that if an expected match string is not found or if the device doesn't respond, the expect method will not return.

getDefaultProtocolTimeout

public java.lang.Integer getDefaultProtocolTimeout()
Description copied from interface: ProtocolHandler
Gets the default operation timeout value.

Specified by:
getDefaultProtocolTimeout in interface ProtocolHandler
Returns:
the timeout in seconds.

setDefaultNoDataReadDelay

public void setDefaultNoDataReadDelay(int delay)
Description copied from interface: CliProtocolHandler
Modifies the default no data read delay in 1/10 seconds. The "no data read delay" is the amount of time that the protocol waits when receiving no data that a data read is considered complete.

Specified by:
setDefaultNoDataReadDelay in interface CliProtocolHandler
Parameters:
delay - The default no data read delay.

getDefaultNoDataReadDelay

public int getDefaultNoDataReadDelay()
Description copied from interface: CliProtocolHandler
Gets the default no data read delay.

Specified by:
getDefaultNoDataReadDelay in interface CliProtocolHandler
Returns:
the delay.

setUsernameUsed

public void setUsernameUsed(boolean usernameUsed)
Description copied from interface: CliProtocolHandler
Sets whether the username was used for authentication.

Specified by:
setUsernameUsed in interface CliProtocolHandler
Parameters:
usernameUsed - True if the username was used or false if not.

getUsernameUsed

public boolean getUsernameUsed()
Description copied from interface: CliProtocolHandler
Gets whether the username was used for authentication.

Specified by:
getUsernameUsed in interface CliProtocolHandler
Returns:
True if the username was used or false if not.

setPasswordUsed

public void setPasswordUsed(boolean passwordUsed)
Description copied from interface: CliProtocolHandler
Sets whether the password was used for authentication.

Specified by:
setPasswordUsed in interface CliProtocolHandler
Parameters:
passwordUsed - True if the password was used or false if not.

getPasswordUsed

public boolean getPasswordUsed()
Description copied from interface: CliProtocolHandler
Gets whether the password was used for authentication.

Specified by:
getPasswordUsed in interface CliProtocolHandler
Returns:
True if the password was used or false if not.

getSshProtocolVersion

public int getSshProtocolVersion()

checkForValidHandler

protected void checkForValidHandler()
                             throws java.lang.RuntimeException
Throws:
java.lang.RuntimeException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.