changeset 223:d69e2b144cf0

history mode part 1 reviewed-by: vanaltj review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000756.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 12 Apr 2012 19:10:56 +0200
parents 5985416cf4b8
children 56aadbbb9156
files agent/src/main/java/com/redhat/thermostat/agent/Agent.java agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java agent/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java agent/src/main/java/com/redhat/thermostat/agent/config/AgentOptionParser.java agent/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java agent/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java agent/src/test/java/com/redhat/thermostat/TestUtils.java agent/src/test/java/com/redhat/thermostat/agent/config/AgentOptionParserTest.java common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/src/main/java/com/redhat/thermostat/common/storage/Storage.java distribution/config/agent.properties
diffstat 11 files changed, 47 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Apr 12 19:10:56 2012 +0200
@@ -135,10 +135,11 @@
             configWatcherThread = null;
             storage.removeAgentInformation();
             stopBackends();
-            // TODO
-//            if (config.getLocalMode()) {
-//                storage.purge();
-//            }
+            if (config.purge()) {
+                System.out.println("purging database");
+                logger.info("purging database");
+                storage.purge();
+            }
         } else {
             logger.warning("Attempt to stop agent which is not active");
         }
--- a/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Thu Apr 12 19:10:56 2012 +0200
@@ -67,6 +67,7 @@
 
 public final class AgentApplication extends BasicCommand {
 
+    private CommandContext contex;
     private static final String NAME = "agent";
 
     // TODO: Use LocaleResources for i18n-ized strings.
@@ -155,6 +156,9 @@
         }
         logger.fine("Agent started.");
 
+        contex.getConsole().getOutput().println("Agent id: " + agent.getId());
+        logger.fine("Agent id: " + agent.getId());
+        
         try {
             System.in.read();
         } catch (IOException e) {
@@ -168,6 +172,7 @@
     @Override
     public void run(CommandContext ctx) throws CommandException {
         try {
+            contex = ctx;
             parseArguments(Arrays.asList(ctx.getArguments()));
             if (!parser.isHelp()) {
                 runAgent();
--- a/agent/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Apr 12 19:10:56 2012 +0200
@@ -89,6 +89,12 @@
             String db = properties.getProperty(AgentProperties.DB_URL.name());
             configuration.setDatabaseURL(db);
         }
+        
+        configuration.setPurge(true);
+        if (properties.containsKey(AgentProperties.SAVE_ON_EXIT.name())) {
+            String purge = (String) properties.get(AgentProperties.SAVE_ON_EXIT.name());
+            configuration.setPurge(!Boolean.parseBoolean(purge));
+        }
     }
     
     public static Level getLogLevel(String logLevel) {
--- a/agent/src/main/java/com/redhat/thermostat/agent/config/AgentOptionParser.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/config/AgentOptionParser.java	Thu Apr 12 19:10:56 2012 +0200
@@ -67,6 +67,7 @@
 
         parser.accepts(Args.DEBUG.option, Args.DEBUG.description);
         parser.accepts(Args.HELP.option, Args.HELP.description);
+        parser.accepts(Args.SAVE_ON_EXIT.option, Args.SAVE_ON_EXIT.description);
         
         OptionSpec<String> logLevel =
                 parser.accepts(Args.LEVEL.option, Args.LEVEL.description).
@@ -82,6 +83,10 @@
             return;
         }
         
+        if (options.has(Args.SAVE_ON_EXIT.option)) {
+            configuration.setPurge(false);
+        }
+        
         if (options.has(Args.LEVEL.option)) {
             String levelString = logLevel.value(options);
             Level level = AgentConfigsUtils.getLogLevel(levelString);
@@ -119,6 +124,7 @@
         
         // TODO: localize
         LEVEL("logLevel", "log level"),
+        SAVE_ON_EXIT("saveOnExit", "save the data on exit"),
         DB("dbUrl", "connect to the given url"),
         DEBUG("debug", "launch with debug console enabled"),
         HELP("help", "print this help and exit");
--- a/agent/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java	Thu Apr 12 19:10:56 2012 +0200
@@ -42,5 +42,6 @@
     BACKENDS,
     LOG_LEVEL,
     DEBUG_CONSOLE,
-    DB_URL
+    DB_URL,
+    SAVE_ON_EXIT
 }
--- a/agent/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java	Thu Apr 12 19:10:56 2012 +0200
@@ -58,6 +58,7 @@
     
     private Level logLevel;
     private boolean debugConsole;
+    private boolean purge;
     
     private String url;
     
@@ -135,4 +136,12 @@
     public long getStartTime() {
         return startTime;
     }
+
+    void setPurge(boolean purge) {
+        this.purge = purge;
+    }
+    
+    public boolean purge() {
+        return purge;
+    }
 }
--- a/agent/src/test/java/com/redhat/thermostat/TestUtils.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/test/java/com/redhat/thermostat/TestUtils.java	Thu Apr 12 19:10:56 2012 +0200
@@ -85,6 +85,7 @@
         Properties props = new Properties();            
 
         props.setProperty(AgentProperties.BACKENDS.name(), "system");
+        props.setProperty(AgentProperties.SAVE_ON_EXIT.name(), "false");
         props.setProperty(AgentProperties.LOG_LEVEL.name(), "WARNING");
 
         props.store(new FileOutputStream(tmpConfigs), "thermostat agent test properties");
--- a/agent/src/test/java/com/redhat/thermostat/agent/config/AgentOptionParserTest.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/agent/src/test/java/com/redhat/thermostat/agent/config/AgentOptionParserTest.java	Thu Apr 12 19:10:56 2012 +0200
@@ -37,11 +37,9 @@
 package com.redhat.thermostat.agent.config;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Properties;
 import java.util.logging.Level;
 
 import junit.framework.Assert;
@@ -77,13 +75,14 @@
         args.add("testURL");
         args.add("--debug");
         
-        AgentStartupConfiguration configs = new AgentStartupConfiguration();
+        AgentStartupConfiguration configs = AgentConfigsUtils.createAgentConfigs();
         AgentOptionParser parser = new AgentOptionParser(configs, args);
         parser.parse();
         
         Assert.assertEquals("testURL", configs.getDBConnectionString());
         Assert.assertEquals(Level.ALL, configs.getLogLevel());
         Assert.assertTrue(configs.isDebugConsole());
+        Assert.assertTrue(configs.purge());
     }
     
     @Test
@@ -94,6 +93,7 @@
         args.add("FINE");
         args.add("--dbUrl");
         args.add("testURL2");
+        args.add("--saveOnExit");
         
         AgentStartupConfiguration configs = new AgentStartupConfiguration();
         AgentOptionParser parser = new AgentOptionParser(configs, args);
@@ -102,5 +102,6 @@
         Assert.assertEquals("testURL2", configs.getDBConnectionString());
         Assert.assertEquals(Level.FINE, configs.getLogLevel());
         Assert.assertFalse(configs.isDebugConsole());
+        Assert.assertFalse(configs.purge());
     }
 }
--- a/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Apr 12 19:10:56 2012 +0200
@@ -262,13 +262,14 @@
         return result;
     }
 
+    @Override
     public void purge() {
         BasicDBObject deleteKey = getAgentDBObject();
         for (DBCollection coll : collectionCache.values()) {
             coll.remove(deleteKey);
         }
     }
-
+    
     @Override
     public ConnectionKey createConnectionKey(Category category) {
         // TODO: There is probably some better place to do this, perhaps related to the inner class
--- a/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Apr 12 12:03:50 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Apr 12 19:10:56 2012 +0200
@@ -59,10 +59,11 @@
 
     public abstract void updateChunk(Chunk chunk);
 
-    /* Drop all data related to the currently running agent.
+    /**
+     * Drop all data related to the currently running agent.
      */
     public abstract void purge();
-
+    
     public abstract Cursor findAll(Chunk query);
 
     public abstract Chunk find(Chunk query);
--- a/distribution/config/agent.properties	Thu Apr 12 12:03:50 2012 -0400
+++ b/distribution/config/agent.properties	Thu Apr 12 19:10:56 2012 +0200
@@ -3,3 +3,7 @@
 # specific backend configurations will be searched under the
 # $THERMOSTAT_HOME/backends/[backend_name.properties]
 BACKENDS=system
+
+# indicates if this agent will save its data to the database on exit
+# or rather will purge the db
+SAVE_ON_EXIT=false