org.hackystat.sensorshell
Class SingleSensorShell

java.lang.Object
  extended by org.hackystat.sensorshell.SingleSensorShell
All Implemented Interfaces:
Shell

public class SingleSensorShell
extends java.lang.Object
implements Shell

Provides the implementation of a single SensorShell instance.

Author:
Philip M. Johnson

Constructor Summary
SingleSensorShell(SensorShellProperties properties, boolean isInteractive)
          Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host.
SingleSensorShell(SensorShellProperties properties, boolean isInteractive, java.lang.String tool)
          Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host.
SingleSensorShell(SensorShellProperties properties, boolean isInteractive, java.lang.String toolName, java.io.File commandFile)
          Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host.
 
Method Summary
 void add(java.util.Map<java.lang.String,java.lang.String> keyValMap)
          Converts the values in the KeyValMap to a SensorData instance and adds it to the Shell.
 void add(org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData sensorData)
          Adds the passed SensorData instance to the Shell.
 java.util.logging.Logger getLogger()
          Returns the Logger associated with this sensorshell.
 OfflineManager getOfflineManager()
          Returns the offline manager associated with this instance.
 SensorShellProperties getProperties()
          Returns the SensorShell properties instance used to create this SensorShell.
 java.util.Date getStartTime()
          Returns a Date instance indicating when this SensorShell was started.
 long getTotalSent()
          Returns the total number of instances sent by this shell's SensorDataCommand.
 boolean hasOfflineData()
          Returns true if this shell has stored any data offline.
 boolean isInteractive()
          Returns true if this sensorshell is being run interactively from the command line.
 boolean ping()
          Returns true if the host can be pinged and the email/password combination is valid.
 void print(java.lang.String line)
          Prints out the line without newline if in interactive mode.
 void println(java.lang.String line)
          Prints out the line plus newline if in interactive mode, and always logs the line.
(package private)  void printPrompt()
          Print out a prompt if in interactive mode.
(package private)  void processInputString(java.lang.String inputString)
          Process a single input string representing a command.
 void quit()
          Invokes quit() on this Shell, which invokes a final send() and closes any logging files.
(package private)  java.lang.String readLine()
          Returns a string with the next line of input from the user.
 int send()
          Immediately invokes send() on this Shell.
 void statechange(long resourceCheckSum, java.util.Map<java.lang.String,java.lang.String> keyValMap)
          Implements the "StateChange" algorithm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingleSensorShell

public SingleSensorShell(SensorShellProperties properties,
                         boolean isInteractive)
Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host. The toolName field in the log file name is set to "interactive" if the tool is invoked interactively and "tool" if it is invoked programmatically.

Parameters:
properties - The sensor properties instance for this run.
isInteractive - Whether this SensorShell is being interactively invoked or not.

SingleSensorShell

public SingleSensorShell(SensorShellProperties properties,
                         boolean isInteractive,
                         java.lang.String tool)
Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host.

Parameters:
properties - The sensor properties instance for this run.
isInteractive - Whether this SensorShell is being interactively invoked or not.
tool - Indicates the invoking tool that is added to the log file name.

SingleSensorShell

public SingleSensorShell(SensorShellProperties properties,
                         boolean isInteractive,
                         java.lang.String toolName,
                         java.io.File commandFile)
Constructs a new SensorShell instance that can be provided with notification data to be sent eventually to a specific user key and host. (For testing purposes only, you may want to disable offline data recovery.)

Parameters:
properties - The sensor properties instance for this run.
isInteractive - Whether this SensorShell is being interactively invoked or not.
toolName - The invoking tool that is added to the log file name.
commandFile - A file containing shell commands, or null if none provided.
Method Detail

getOfflineManager

public OfflineManager getOfflineManager()
Returns the offline manager associated with this instance.

Returns:
The offline manager.

processInputString

void processInputString(java.lang.String inputString)
                  throws SensorShellException
Process a single input string representing a command.

Parameters:
inputString - A command as a String.
Throws:
SensorShellException - If problems occur sending the data.

printPrompt

void printPrompt()
Print out a prompt if in interactive mode.


readLine

java.lang.String readLine()
Returns a string with the next line of input from the user. If input errors, returns the string "quit".

Returns:
A string with user input.

println

public final void println(java.lang.String line)
Prints out the line plus newline if in interactive mode, and always logs the line. Provided to clients to support logging of error messages.

Parameters:
line - The line to be printed.

print

public final void print(java.lang.String line)
Prints out the line without newline if in interactive mode.

Parameters:
line - The line to be printed.

getStartTime

public java.util.Date getStartTime()
Returns a Date instance indicating when this SensorShell was started.

Returns:
The Date when this instance started up.

hasOfflineData

public boolean hasOfflineData()
Returns true if this shell has stored any data offline.

Specified by:
hasOfflineData in interface Shell
Returns:
True if any data has been stored offline.

getTotalSent

public long getTotalSent()
Returns the total number of instances sent by this shell's SensorDataCommand.

Returns:
The total number of instances sent.

isInteractive

public boolean isInteractive()
Returns true if this sensorshell is being run interactively from the command line.

Returns:
True if sensorshell is interactive.

getLogger

public java.util.logging.Logger getLogger()
Returns the Logger associated with this sensorshell.

Returns:
The Logger.

add

public void add(java.util.Map<java.lang.String,java.lang.String> keyValMap)
         throws SensorShellException
Converts the values in the KeyValMap to a SensorData instance and adds it to the Shell. Owner will default to the hackystat user in the sensor.properties file. Timestamp and Runtime will default to the current time.

Specified by:
add in interface Shell
Parameters:
keyValMap - The map of key-value pairs.
Throws:
SensorShellException - If the Map cannot be translated into SensorData, typically because a value was passed for Timestamp or Runtime that could not be parsed into XMLGregorianCalendar. Or if problems occur sending the data.

add

public void add(org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData sensorData)
         throws SensorShellException
Adds the passed SensorData instance to the Shell.

Specified by:
add in interface Shell
Parameters:
sensorData - The SensorData instance to be queued for transmission.
Throws:
SensorShellException - If problems occur sending the data.

send

public int send()
         throws SensorShellException
Immediately invokes send() on this Shell. Note that you will rarely want to invoke this method. Instead, during normal operation you will rely on the autoSendTimeInterval to invoke send() in a separate thread, and then invoke quit() to invoke send() at the conclusion of the run.

Specified by:
send in interface Shell
Returns:
The total number of instances sent by Shell.
Throws:
SensorShellException - If problems occur sending the data.

quit

public void quit()
          throws SensorShellException
Invokes quit() on this Shell, which invokes a final send() and closes any logging files.

Specified by:
quit in interface Shell
Throws:
SensorShellException - If an exception occurred during any autosend.

ping

public boolean ping()
Returns true if the host can be pinged and the email/password combination is valid.

Specified by:
ping in interface Shell
Returns:
True if the host can be pinged and the user credentials are valid.

statechange

public void statechange(long resourceCheckSum,
                        java.util.Map<java.lang.String,java.lang.String> keyValMap)
                 throws java.lang.Exception
Implements the "StateChange" algorithm. The goal of StateChange is to add a new SensorData instance for sending to the SensorBase only when "something has changed" in the tool (which is typically an interactive editor or IDE). This method is designed to be used in conjunction with a timer-based process in the sensor client. The timer-based process should wake up at regular intervals, determine the currently "active" resource (typically a file), and compute a "checksum" for that resource representing its state. (This checksum is typically the size of the resource in bytes or characters.) Having obtained those two values, the sensor client can then create a keyValMap as if it were going to do a regular Add command, and invoke this method with it as well as with the resourceCheckSum. This method keeps track of the last Resource and last ResourceCheckSum provided to it, and if either has changed, it will automatically invoke the Add command, passing the keyValMap to it.

Thus, if an editor is running but the user is out at lunch, repeated invocations of the StateChange method will not result in any new data being sent to the server.

Specified by:
statechange in interface Shell
Parameters:
resourceCheckSum - An integer representing the state of the Resource.
keyValMap - A map of key-value pairs representing sensor data fields and properties.
Throws:
java.lang.Exception - If problems occur during the Add (if the Add actually occurs.)

getProperties

public SensorShellProperties getProperties()
Returns the SensorShell properties instance used to create this SensorShell.

Specified by:
getProperties in interface Shell
Returns:
The SensorShellProperties instance.