Refactor remove, init, and updateSharedConfig APIs to check if applicaiton is running (#1052)

Add logic to the start of remove, init, and updateSharedConfig methods
in FluoAdminImpl to check if applicaiton is already running.
This will eliminate the need to perform this check in clients that
call these methods.

Closes #1003
diff --git a/modules/command/src/main/java/org/apache/fluo/command/FluoInit.java b/modules/command/src/main/java/org/apache/fluo/command/FluoInit.java
index 7d83f34..c73d8bc 100644
--- a/modules/command/src/main/java/org/apache/fluo/command/FluoInit.java
+++ b/modules/command/src/main/java/org/apache/fluo/command/FluoInit.java
@@ -134,13 +134,6 @@
 
     try (FluoAdminImpl admin = new FluoAdminImpl(config)) {
 
-      if (admin.applicationRunning()) {
-        System.err.println("Error - The Fluo '" + config.getApplicationName() + "' application"
-            + " is already running and must be stopped before running 'fluo init'. "
-            + " Aborted initialization.");
-        System.exit(-1);
-      }
-
       FluoAdmin.InitializationOptions initOpts = new FluoAdmin.InitializationOptions();
 
       if (opts.getUpdate()) {
@@ -185,14 +178,16 @@
 
       System.out.println("Initializing Fluo '" + config.getApplicationName()
           + "' application using " + opts.getAppPropsPath());
-      try {
-        admin.initialize(initOpts);
-      } catch (Exception e) {
-        System.out.println("Initialization failed due to the following exception:");
-        e.printStackTrace();
-        System.exit(-1);
-      }
+
+      admin.initialize(initOpts);
       System.out.println("Initialization is complete.");
+    } catch (FluoAdmin.AlreadyInitializedException e) {
+      System.err.println(e.getMessage());
+      System.exit(-1);
+    } catch (Exception e) {
+      System.out.println("Initialization failed due to the following exception:");
+      e.printStackTrace();
+      System.exit(-1);
     }
   }
 }
diff --git a/modules/command/src/main/java/org/apache/fluo/command/FluoRemove.java b/modules/command/src/main/java/org/apache/fluo/command/FluoRemove.java
index 0c337d2..5426e41 100644
--- a/modules/command/src/main/java/org/apache/fluo/command/FluoRemove.java
+++ b/modules/command/src/main/java/org/apache/fluo/command/FluoRemove.java
@@ -15,7 +15,9 @@
 
 package org.apache.fluo.command;
 
+import org.apache.fluo.api.client.FluoAdmin;
 import org.apache.fluo.api.config.FluoConfiguration;
+import org.apache.fluo.api.exceptions.FluoException;
 import org.apache.fluo.core.client.FluoAdminImpl;
 
 public class FluoRemove {
@@ -30,22 +32,16 @@
     config = FluoAdminImpl.mergeZookeeperConfig(config);
 
     try (FluoAdminImpl admin = new FluoAdminImpl(config)) {
-      if (admin.applicationRunning()) {
-        System.err.println("Error - The Fluo '" + config.getApplicationName() + "' application"
-            + " is already running and must be stopped before running 'fluo remove'. "
-            + " Aborted remove.");
-        System.exit(-1);
-      }
       System.out.println("Removing Fluo '" + config.getApplicationName());
-      try {
-        admin.remove();
-      } catch (Exception e) {
-        System.out.println("Remove failed due to the following exception:");
-        e.printStackTrace();
-        System.exit(-1);
-      }
+      admin.remove();
       System.out.println("Remove is complete.");
-
+    } catch (FluoException e) {
+      System.err.println(e.getMessage());
+      System.exit(-1);
+    } catch (Exception e) {
+      System.out.println("Remove failed due to the following exception:");
+      e.printStackTrace();
+      System.exit(-1);
     }
   }
 }
diff --git a/modules/core/src/main/java/org/apache/fluo/core/client/FluoAdminImpl.java b/modules/core/src/main/java/org/apache/fluo/core/client/FluoAdminImpl.java
index 8bea85f..4056366 100644
--- a/modules/core/src/main/java/org/apache/fluo/core/client/FluoAdminImpl.java
+++ b/modules/core/src/main/java/org/apache/fluo/core/client/FluoAdminImpl.java
@@ -107,7 +107,11 @@
     Preconditions.checkArgument(
         config.getObserverJarsUrl().isEmpty() || config.getObserverInitDir().isEmpty(),
         "Only one of 'fluo.observer.init.dir' and 'fluo.observer.jars.url' can be set");
-
+    if (applicationRunning()) {
+      throw new AlreadyInitializedException("Error - The Fluo '" + config.getApplicationName()
+          + "' application" + " is already running and must be stopped before initializing. "
+          + " Aborted initialization.");
+    }
     if (zookeeperInitialized() && !opts.getClearZookeeper()) {
       throw new AlreadyInitializedException(
           "Fluo application already initialized at " + config.getAppZookeepers());
@@ -204,6 +208,10 @@
 
   @Override
   public void remove() {
+    if (applicationRunning()) {
+      throw new FluoException("Error - The Fluo '" + config.getApplicationName() + "' application"
+          + " is already running and must be stopped before removing. Aborted remove.");
+    }
     if (!config.hasRequiredAdminProps()) {
       throw new IllegalArgumentException("Admin configuration is missing required properties");
     }
@@ -325,6 +333,11 @@
     if (!config.hasRequiredAdminProps()) {
       throw new IllegalArgumentException("Admin configuration is missing required properties");
     }
+    if (applicationRunning()) {
+      throw new FluoException("Error - The Fluo '" + config.getApplicationName() + "' application"
+          + " is already running and must be stopped before updating shared configuration. "
+          + " Aborted update.");
+    }
     Properties sharedProps = new Properties();
     Iterator<String> iter = config.getKeys();
     while (iter.hasNext()) {
diff --git a/modules/integration-tests/src/main/java/org/apache/fluo/integration/impl/AppConfigIT.java b/modules/integration-tests/src/main/java/org/apache/fluo/integration/impl/AppConfigIT.java
index 332a3da..27d68b3 100644
--- a/modules/integration-tests/src/main/java/org/apache/fluo/integration/impl/AppConfigIT.java
+++ b/modules/integration-tests/src/main/java/org/apache/fluo/integration/impl/AppConfigIT.java
@@ -63,6 +63,7 @@
     appConfig.setProperty("myapp.sizeLimit", 40000);
     appConfig.setProperty("myapp.timeLimit", 30000);
     try (FluoAdmin admin = FluoFactory.newAdmin(config)) {
+      miniFluo.close();
       admin.updateSharedConfig();
     }