001    package org.hackystat.sensorshell.command;
002    
003    import java.util.logging.Handler;
004    
005    import org.hackystat.sensorshell.SensorShellException;
006    import org.hackystat.sensorshell.SensorShellProperties;
007    import org.hackystat.sensorshell.SingleSensorShell;
008    
009    /**
010     * Implements the Quit command, which sends any buffered data and closes the loggers. 
011     * @author Philip Johnson
012     */
013    public class QuitCommand extends Command {
014      
015      /** Holds the sensor data command for sending data. */
016      private SensorDataCommand sensorDataCommand;
017      
018      /** Holds the autosend instance. */
019      private AutoSendCommand autoSendCommand;
020      
021      /**
022       * Creates the QuitCommand. 
023       * @param shell The sensorshell. 
024       * @param properties The sensorproperties.
025       * @param sensorDataCommand The SensorDataCommand. 
026       * @param autoSendCommand The AutoSendCommand.
027       */
028      public QuitCommand(SingleSensorShell shell, SensorShellProperties properties, 
029          SensorDataCommand sensorDataCommand, AutoSendCommand autoSendCommand) {
030        super(shell, properties);
031        this.sensorDataCommand = sensorDataCommand;
032        this.autoSendCommand = autoSendCommand;
033      }
034      
035      /** 
036       * Quits the shell. Sends all data and closes the loggers.
037       * @throws SensorShellException if an exception occurred during an autosend event.  
038       */
039      public void quit() throws SensorShellException {
040        SensorShellException exception = null;
041        
042        // Log this command if not running interactively.
043        if (!this.shell.isInteractive()) {
044          this.shell.getLogger().info("#> quit" + cr);
045        }
046        // Try to send any remaining data. 
047        try {
048          this.sensorDataCommand.send();
049        }
050        catch (SensorShellException e) {
051          // Note that we had an exception thrown, now proceed to finally block.
052          exception = e; 
053        }
054        finally {
055          // Shut down all timers. 
056          this.autoSendCommand.quit();
057          // Log quit() information.
058          if (exception != null) {
059            this.shell.println("Error sending data during final quit: " + exception);
060          }
061          this.shell.println("Quitting SensorShell started at: " + this.shell.getStartTime());
062          this.shell.println("Total sensor data instances sent: " + this.shell.getTotalSent());
063          // Close all loggers. 
064          if (this.shell.getLogger() != null) {
065            // Close all the open handler.
066            Handler[] handlers = this.shell.getLogger().getHandlers();
067            for (int i = 0; i < handlers.length; i++) {
068              handlers[i].close();
069            }
070          }
071          // If we had an autosend exception, and there's data left, we throw it again. 
072          if ((this.autoSendCommand.getException() != null) && 
073              (this.sensorDataCommand.hasUnsentData())) {
074            throw this.autoSendCommand.getException();
075          }
076          // If we had a quit() exception, and there's data left, we throw it again. 
077          if ((exception != null) && (this.sensorDataCommand.hasUnsentData())) {
078            throw exception;
079          }
080          
081        }
082      }
083    }