Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / Batch.java
index ff60520..36a88b6 100644 (file)
@@ -73,7 +73,7 @@ public abstract class Batch {
 
     protected static final String STARS = "*****";
 
-    protected final Cluster cluster; 
+    protected static Cluster cluster; 
     protected static AuthzEnv env;
     protected static Session session;
     protected static Set<String> specialNames;
@@ -81,6 +81,8 @@ public abstract class Batch {
     protected static boolean dryRun; 
     protected static String batchEnv;
 
+       private static File logdir;
+
     public static final String CASS_ENV = "CASS_ENV";
     public static final String LOG_DIR = "LOG_DIR";
     protected static final String MAX_EMAILS="MAX_EMAILS";
@@ -88,7 +90,10 @@ public abstract class Batch {
     public static final String GUI_URL="GUI_URL";
     
     protected final Organization org;
-    
+       protected String version;
+       protected static final Date now = new Date();
+       protected static final Date never = new Date(0);
+       
     protected Batch(AuthzEnv env) throws APIException, IOException, OrganizationException {
         if (batchEnv != null) {
             env.info().log("Redirecting to ",batchEnv,"environment");
@@ -100,7 +105,8 @@ public abstract class Batch {
                     CassAccess.CASSANDRA_CLUSTERS_PASSWORD,
                     VERSION,GUI_URL,MAX_EMAILS,
                     LOG_DIR,
-                    "SPECIAL_NAMES"
+                    "SPECIAL_NAMES",
+                    "MAIL_TEST_TO"
                     }) {
                 if ((str = env.getProperty(batchEnv+'.'+key))!=null) {
                     env.setProperty(key, str);
@@ -109,7 +115,9 @@ public abstract class Batch {
         }
 
         // Setup for Dry Run
-        cluster = CassAccess.cluster(env,batchEnv);
+        if(cluster==null) {
+               cluster = CassAccess.cluster(env,batchEnv);
+        }
         env.info().log("cluster name - ",cluster.getClusterName());
         String dryRunStr = env.getProperty( "DRY_RUN" );
         if ( dryRunStr == null || "false".equals(dryRunStr.trim()) ) {
@@ -120,6 +128,9 @@ public abstract class Batch {
         }
 
         org = OrganizationFactory.init(env);
+        if(org==null) {
+               throw new OrganizationException("Organization MUST be defined for Batch");
+        }
         org.setTestMode(dryRun);
 
         // Special names to allow behaviors beyond normal rules
@@ -138,10 +149,12 @@ public abstract class Batch {
                 }
             }
         }
+        
+        version = env.getProperty(VERSION,Config.AAF_DEFAULT_API_VERSION);
     }
 
     protected abstract void run(AuthzTrans trans);
-    protected abstract void _close(AuthzTrans trans);
+    protected void _close(AuthzTrans trans) {}
     
     public String[] args() {
         return env.get(ssargs);
@@ -324,16 +337,22 @@ public abstract class Batch {
         }
     }
     
-    protected static String logDir() {
-        String ld = env.getProperty(LOG_DIR);
-        if (ld==null) {
-            if (batchEnv==null) { // Deployed Batch doesn't use different ENVs, and a common logdir
-                ld = "logs/";
-            } else {
-                ld = "logs/"+batchEnv;
-            }
-        }
-        return ld;
+    protected static File logDir() {
+        if(logdir == null) {
+               String ld = env.getProperty(LOG_DIR);
+               if (ld==null) {
+                   if (batchEnv==null) { // Deployed Batch doesn't use different ENVs, and a common logdir
+                       ld = "logs/";
+                   } else {
+                       ld = "logs/"+batchEnv;
+                   }
+               }
+               logdir = new File(ld);
+               if(!logdir.exists()) {
+                       logdir.mkdirs();
+               }
+        } 
+        return logdir;
     }
     protected int count(String str, char c) {
         if (str==null || str.isEmpty()) {
@@ -349,7 +368,13 @@ public abstract class Batch {
 
     public final void close(AuthzTrans trans) {
         _close(trans);
-        cluster.close();
+        if(session!=null) {
+               session.close();
+               session = null;
+        }
+        if(cluster!=null && !cluster.isClosed()) {
+            cluster.close();
+        }
     }
 
     public static void main(String[] args) {
@@ -494,7 +519,14 @@ public abstract class Batch {
        
                        }
                        if (batch != null) {
-                           batch.run(trans);
+                               try {
+                                       batch.run(trans);
+                           } catch (Exception e) {
+                               if(cluster!=null && !cluster.isClosed()) {
+                                       cluster.close();
+                               }
+                               trans.error().log(e);
+                           }
                        }
                    } finally {
                        tt.done();
@@ -510,9 +542,10 @@ public abstract class Batch {
             }
 
         } catch (Exception e) {
+               if(cluster!=null && !cluster.isClosed()) {
+                       cluster.close();
+               }
             e.printStackTrace(System.err);
-            // Exceptions thrown by DB aren't stopping the whole process.
-            System.exit(1);
         }
     }