com.sptci.system
Class CommandExecutor

java.lang.Object
  extended by com.sptci.system.CommandExecutor

public class CommandExecutor
extends Object

The class that is used to interact with the system. Provides methods to log user to the system and execute the password change command.

Modified version of class documented in informit.com.

Version:
$Id: CommandExecutor.java 3252 2007-05-12 19:12:31Z rakesh $
Author:
Rakesh Vidyadharan 2007-04-21

Field Summary
private  TelnetClient client
          The telnet client instance to use to communicate with the server.
private  InputStream in
          The InputStream for the telnet session.
private static Logger logger
          The logger to use to log errors/messages to.
static String LOGIN_PROMPT
          The name of the property that contains the login prompt sent back by telnet server when a connection attempt is made.
private  PrintStream out
          The OutputStream for the telnet session.
static String PASSWORD_PROMPT
          The name of the property that contains the password prompt sent back by telnet server after a response is sent to the LOGIN_PROMPT "password"
(package private)  int port
          The port on which to connect to the server.
static String PORT
          The name of the property that contains the port on which a telnet session will be established.
static String PROMPT
          The name of the property that contains the shell prompt character used by the system.
(package private)  Properties properties
          The properties object that contains the configured properties for this instance.
static String PROPERTY_FILE
          The name of the system property that points to the properties file used to configure this instance.
(package private)  String server
          The server to log on to affect the change in password.
static String SERVER
          The name of the property that contains the hostname of the server to which a telnet session will be initiated.
(package private)  String user
          The user who is trying to change his password.
 
Constructor Summary
CommandExecutor()
          Create a new instance of the class;
 
Method Summary
 void connect(String password)
          Connect to the server via telnet and log the user to the system.
 void connect(String user, String password)
          Connect to the server via telnet and log the user to the system.
 void disconnect()
          Disconnect from the telnet session.
private  String execute(String command)
          Send a command for execution to the telnet session.
 String getUser()
          Returns user.
 void password(String password, String newpassword)
          Change the user's password.
private  String readLoginResponse()
          Read the response from the server after a login attempt.
private  String readPasswordResponse(String success, String error, String message)
          Read the response from the server after the current password has been entered during a password change request.
private  String readUntil(String pattern)
          Read the response from the server until the specified pattern is found.
 void setUser(String user)
          Set user.
private  void write(String response)
          Write a response to the telnet OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final Logger logger
The logger to use to log errors/messages to.


PROPERTY_FILE

public static final String PROPERTY_FILE
The name of the system property that points to the properties file used to configure this instance. "password.properties.file"

See Also:
Constant Field Values

SERVER

public static final String SERVER
The name of the property that contains the hostname of the server to which a telnet session will be initiated. "server"

See Also:
Constant Field Values

PORT

public static final String PORT
The name of the property that contains the port on which a telnet session will be established. This propery is optional. By default a connection to the standard telnet port 23 will be attempted. "port"

See Also:
Constant Field Values

LOGIN_PROMPT

public static final String LOGIN_PROMPT
The name of the property that contains the login prompt sent back by telnet server when a connection attempt is made. "login"

See Also:
Constant Field Values

PASSWORD_PROMPT

public static final String PASSWORD_PROMPT
The name of the property that contains the password prompt sent back by telnet server after a response is sent to the LOGIN_PROMPT "password"

See Also:
Constant Field Values

PROMPT

public static final String PROMPT
The name of the property that contains the shell prompt character used by the system.

Note: This of course depends upon the shell used by the user and the user's settings.

"prompt"

See Also:
Constant Field Values

server

final String server
The server to log on to affect the change in password.


port

final int port
The port on which to connect to the server.


user

String user
The user who is trying to change his password.


client

private TelnetClient client
The telnet client instance to use to communicate with the server.


properties

final Properties properties
The properties object that contains the configured properties for this instance.


in

private InputStream in
The InputStream for the telnet session.


out

private PrintStream out
The OutputStream for the telnet session.

Constructor Detail

CommandExecutor

public CommandExecutor()
                throws RuntimeException
Create a new instance of the class;

Throws:
RuntimeException - If errors are encountered while initialising the instance from the PROPERTY_FILE file.
Method Detail

connect

public void connect(String password)
             throws SocketException,
                    IOException,
                    AuthenticationFailedException
Connect to the server via telnet and log the user to the system. This method assumes that caller has either invoked setUser(java.lang.String) or connect( String, String ) prior to calling this method.

Throws:
SocketException - If the socket timeout could not be set.
IOException - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
AuthenticationFailedException - If the login attempt failed due to username or password error.
See Also:
connect( String, String )

connect

public void connect(String user,
                    String password)
             throws SocketException,
                    IOException,
                    AuthenticationFailedException
Connect to the server via telnet and log the user to the system.

Throws:
SocketException - If the socket timeout could not be set.
IOException - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
AuthenticationFailedException - If the login attempt failed due to username or password error.
See Also:
readUntil(java.lang.String), write(java.lang.String), readLoginResponse()

password

public void password(String password,
                     String newpassword)
              throws IOException,
                     PasswordException
Change the user's password.

Parameters:
password - The current password of the user.
newpassword - The new password to set.
Throws:
IOException - If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
PasswordException - If the current or new password are incorrect or insufficient

disconnect

public void disconnect()
Disconnect from the telnet session.


readUntil

private String readUntil(String pattern)
                  throws IOException
Read the response from the server until the specified pattern is found.

Parameters:
pattern - The pattern until which the response from the server is to be read
Throws:
IOException - If errors are encountered while reading/writing to the socket streams.

readLoginResponse

private String readLoginResponse()
                          throws IOException,
                                 AuthenticationFailedException
Read the response from the server after a login attempt. Handle either a successful login or a failed login. Failed logins are indicated when the system responds with a fresh login prompt along with an error message.

Throws:
IOException - If errors are encountered while reading/writing to the socket streams.
AuthenticationFailedException - If the login attempt failed due to username or password error.

readPasswordResponse

private String readPasswordResponse(String success,
                                    String error,
                                    String message)
                             throws IOException,
                                    PasswordException
Read the response from the server after the current password has been entered during a password change request.

Parameters:
success - The pattern that indicates a successful entry
error - The pattern that indicates an error
message - The message to use in PasswordException.
Throws:
IOException - If errors are encountered while reading/writing to the socket streams.
PasswordException - If the current password specified is incorrect.

write

private void write(String response)
            throws IOException
Write a response to the telnet OutputStream.

Parameters:
response - The response that is to be written to the stream.
Throws:
IOException - If errors are encountered while reading/writing to the socket streams.

execute

private String execute(String command)
                throws IOException
Send a command for execution to the telnet session.

Parameters:
command - The command that is to be executed.
Throws:
IOException - If errors are encountered while sending the command for execution.
See Also:
write(java.lang.String), readUntil(java.lang.String)

getUser

public String getUser()
Returns user.

Returns:
The value/reference of/to user.

setUser

public void setUser(String user)
Set user.

Parameters:
user - The value to set.