Batch, Remove unneeded Classes, refine, etc
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / bodies / NotifyBody.java
index b36cf64..ae7daa0 100644 (file)
@@ -29,13 +29,17 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
+import org.onap.aaf.auth.batch.helpers.LastNotified;
 import org.onap.aaf.auth.batch.reports.Notify;
 import org.onap.aaf.auth.env.AuthzTrans;
 import org.onap.aaf.cadi.Access;
@@ -46,19 +50,24 @@ public abstract class NotifyBody {
        private static final Map<String,NotifyBody> bodyMap = new HashMap<>();
 
        protected Map<String,List<List<String>>> rows;
+       protected final String env;
+       protected final String gui_url;
+       
        private final String name;
        private final String type;
        private String date;
        private int escalation;
        private int count;
        
-       public NotifyBody(final String type, final String name) {
+       public NotifyBody(Access access, final String type, final String name) {
                rows = new TreeMap<>();
                this.name = name;
                this.type = type;
                date="";
                escalation = 1;
                count = 0;
+               env = access.getProperty("CASS_ENV","DEVL");
+               gui_url = access.getProperty("GUI_URL", "");
        }
        
        public void store(List<String> row) {
@@ -122,6 +131,22 @@ public abstract class NotifyBody {
         */
        protected abstract String user(List<String> row);
        
+       /**
+        * Provide a context-sensitive Subject, which includes ENV as well as details
+        * 
+        * @return
+        */
+       public abstract String subject();
+
+       /**
+        * Record the fact that a particular Notification was marked as "sent" by Emailer.
+        * 
+        * @param trans
+        * @param approver
+        * @param ln
+        */
+       public abstract void record(AuthzTrans trans, StringBuilder query, String id, List<String> notified, LastNotified ln);
+       
        /**
         * Get Notify Body based on key of
         * type|name
@@ -153,53 +178,67 @@ public abstract class NotifyBody {
                Package pkg = NotifyBody.class.getPackage();
                String path = pkg.getName().replace('.', '/');
                URL url = cl.getResource(path);
-               if(url == null) {
-                       throw new APIException("Cannot load resources from " + path);
-               }
-               File dir;
-               try {
-                       dir = new File(url.toURI());
-               } catch (URISyntaxException e) {
-                       throw new APIException(e);
+               List<String> classNames = new ArrayList<>();
+               String urlString = url.toString();
+               if(urlString.startsWith("jar:file:")) {
+                       int exclam = urlString.lastIndexOf('!');
+                       JarFile jf = new JarFile(urlString.substring(9,exclam));
+                       try {
+                               Enumeration<JarEntry> jfe = jf.entries();
+                               while(jfe.hasMoreElements()) {
+                                       String name = jfe.nextElement().getName();
+                                       if(name.startsWith(path) && name.endsWith(".class")) {
+                                               classNames.add(name.substring(0,name.length()-6).replace('/', '.'));
+                                       }
+                               }
+                       } finally {
+                               jf.close();
+                       }
+               } else {
+                       File dir = new File(url.getFile());
+                       for( String f : dir.list()) {
+                               if(f.endsWith(".class")) {
+                                       classNames.add(pkg.getName()+'.'+f.substring(0,f.length()-6));
+                               }
+                       }
                }
-               if(dir.exists()) {
-                       String[] files = dir.list();
-                       if(files!=null) {
-                               for(String sf : files) {
-                                       int dot = sf.indexOf('.');
-                                       if(dot>=0) {
-                                               String cls = pkg.getName()+'.'+sf.substring(0,dot);
-                                               try {
-                                                       Class<?> c = cl.loadClass(cls);
-                                                       if(c!=null) {
-                                                               if(!Modifier.isAbstract(c.getModifiers())) {
-                                                                       Constructor<?> cst = c.getConstructor(Access.class);
-                                                                       NotifyBody nb = (NotifyBody)cst.newInstance(access);
-                                                                       if(nb!=null) {
-                                                                               bodyMap.put("info|"+nb.name, nb);
-                                                                               bodyMap.put(nb.type+'|'+nb.name, nb);
-                                                                       }
-                                                               }
-                                                       }
-                                               } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
-                                                       e.printStackTrace();
+               for(String cls : classNames) {
+                       try {
+                               Class<?> c = cl.loadClass(cls);
+                               if(c!=null) {
+                                       if(!Modifier.isAbstract(c.getModifiers())) {
+                                               Constructor<?> cst = c.getConstructor(Access.class);
+                                               NotifyBody nb = (NotifyBody)cst.newInstance(access);
+                                               if(nb!=null) {
+                                                       bodyMap.put("info|"+nb.name, nb);
+                                                       bodyMap.put(nb.type+'|'+nb.name, nb);
                                                }
                                        }
                                }
+                       } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+                               e.printStackTrace();
                        }
                }
        }
 
-       protected void println(StringBuilder sb, int indent, Object ... objs) {
+       protected void print(StringBuilder sb, int indent, Object ... objs) {
                for(int i=0;i<indent;++i) {
                        sb.append(' ');
                }
                for(Object o : objs) {
                        sb.append(o.toString());
                }
+       }
+                       
+       protected void println(StringBuilder sb, int indent, Object ... objs) {
+               print(sb,indent,objs);
                sb.append('\n');
        }
-       
+
+       protected void printf(StringBuilder sb, int indent, String fmt, Object ... objs) {
+               print(sb,indent,String.format(fmt, objs));
+       }
+
        protected String printCell(StringBuilder sb, int indent, String current, String prev) {
                if(current.equals(prev)) {
                        println(sb,indent,DUPL);