Merge "Sonar Fix: NotifyApprovals.java"
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / PropAccess.java
index a35777f..461ef43 100644 (file)
@@ -26,6 +26,8 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -89,12 +91,22 @@ public class PropAccess implements Access {
         this.logIt = logIt;
         Properties nprops=new Properties();
         int eq;
-        for(String arg : args) {
-            if((eq=arg.indexOf('='))>0) {
-                nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
+        for (String arg : args) {
+            if ((eq=arg.indexOf('='))>0) {
+               String key = arg.substring(0, eq);
+                if(Config.CADI_PROP_FILES.equals(key)) {
+                       nprops.setProperty(key,arg.substring(eq+1));
+                }
             }
         }
         init(nprops);
+        
+        // Re-overlay Args
+        for (String arg : args) {
+            if ((eq=arg.indexOf('='))>0) {
+                props.setProperty(arg.substring(0, eq),arg.substring(eq+1));
+            }
+        }
     }
     
     protected void init(Properties p) {
@@ -103,29 +115,40 @@ public class PropAccess implements Access {
         level=DEFAULT.maskOf();
         
         props = new Properties();
-        // First, load related System Properties
-        for(Entry<Object,Object> es : System.getProperties().entrySet()) {
+        
+        // Find the "cadi_prop_files"
+        //  First in VM Args
+        for (Entry<Object,Object> es : System.getProperties().entrySet()) {
             String key = es.getKey().toString();
-            for(String start : new String[] {"cadi_","aaf_","cm_"}) {
-                if(key.startsWith(start)) {
-                    props.put(key, es.getValue());
-                }
-            }            
+            if(Config.CADI_PROP_FILES.equals(key)) {
+               props.put(key,es.getValue().toString());
+            }
         }
+        
         // Second, overlay or fill in with Passed in Props
-        if(p!=null) {
+        if (p!=null) {
             props.putAll(p);
         }
         
         // Third, load any Chained Property Files
         load(props.getProperty(Config.CADI_PROP_FILES));
         
+        // Fourth, System.getProperties takes precedence over Files
+        for (Entry<Object,Object> es : System.getProperties().entrySet()) {
+            String key = es.getKey().toString();
+            for (String start : new String[] {"HOSTNAME","cadi_","aaf_","cm_"}) {
+                if (key.startsWith(start)) {
+                    props.put(key, es.getValue());
+                }
+            }            
+        }
+        
         String sLevel = props.getProperty(Config.CADI_LOGLEVEL); 
-        if(sLevel!=null) {
+        if (sLevel!=null) {
             level=Level.valueOf(sLevel).maskOf(); 
         }
         // Setup local Symmetrical key encryption
-        if(symm==null) {
+        if (symm==null) {
             try {
                 symm = Symm.obtain(this);
             } catch (CadiException e) {
@@ -142,18 +165,18 @@ public class PropAccess implements Access {
 
     private void specialConversions() {
         // Critical - if no Security Protocols set, then set it.  We'll just get messed up if not
-        if(props.get(Config.CADI_PROTOCOLS)==null) {
+        if (props.get(Config.CADI_PROTOCOLS)==null) {
             props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
         }
         
         Object temp;
         temp=props.get(Config.CADI_PROTOCOLS);
-        if(props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
+        if (props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
             props.put(Config.HTTPS_PROTOCOLS, temp);
         }
         
-        if(temp!=null) {
-            if("1.7".equals(System.getProperty("java.specification.version")) 
+        if (temp!=null) {
+            if ("1.7".equals(System.getProperty("java.specification.version")) 
                     && (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) {
                 System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
             }
@@ -161,20 +184,20 @@ public class PropAccess implements Access {
     }
 
     private void load(String cadi_prop_files) {
-        if(cadi_prop_files==null) {
+        if (cadi_prop_files==null) {
             return;
         }
         String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
         int prev = 0, end = cadi_prop_files.length();
         int idx;
         String filename;
-        while(prev<end) {
+        while (prev<end) {
             idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
-            if(idx<0) {
+            if (idx<0) {
                 idx = end;
             }
             File file = new File(filename=cadi_prop_files.substring(prev,idx));
-            if(file.exists()) {
+            if (file.exists()) {
                 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
                 try {
                     FileInputStream fis = new FileInputStream(file);
@@ -182,12 +205,12 @@ public class PropAccess implements Access {
                         props.load(fis);
                         // Recursively Load
                         String chainProp = props.getProperty(Config.CADI_PROP_FILES);
-                        if(chainProp!=null) {
-                            if(recursionProtection==null) {
+                        if (chainProp!=null) {
+                            if (recursionProtection==null) {
                                 recursionProtection = new ArrayList<>();
                                 recursionProtection.add(cadi_prop_files);
                             }
-                            if(!recursionProtection.contains(chainProp)) {
+                            if (!recursionProtection.contains(chainProp)) {
                                 recursionProtection.add(chainProp);
                                 load(chainProp); // recurse
                             }
@@ -205,23 +228,23 @@ public class PropAccess implements Access {
         }
         
         // Trim 
-        for(Entry<Object, Object> es : props.entrySet()) {
+        for (Entry<Object, Object> es : props.entrySet()) {
             Object value = es.getValue();
-            if(value instanceof String) {
+            if (value instanceof String) {
                 String trim = ((String)value).trim();
                 // Remove Beginning/End Quotes, which might be there if mixed with Bash Props
                 int s = 0, e=trim.length()-1;
-                if(s<e && trim.charAt(s)=='"' && trim.charAt(e)=='"') {
+                if (s<e && trim.charAt(s)=='"' && trim.charAt(e)=='"') {
                     trim=trim.substring(s+1,e);
                 }
-                if(trim!=value) { // Yes, I want OBJECT equals
+                if (trim!=value) { // Yes, I want OBJECT equals
                     props.setProperty((String)es.getKey(), trim);
                 }
             }
         }
         // Reset Symm if Keyfile Changes:
         String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
-        if((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
+        if ((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
             try {
                 symm = Symm.obtain(this);
             } catch (CadiException e) {
@@ -234,7 +257,7 @@ public class PropAccess implements Access {
         }
         
         String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
-        if(loglevel!=null) {
+        if (loglevel!=null) {
             try {
                 level=Level.valueOf(loglevel).maskOf();
             } catch (IllegalArgumentException e) {
@@ -253,7 +276,7 @@ public class PropAccess implements Access {
 
     @Override
     public void log(Level level, Object ... elements) {
-        if(willLog(level)) {
+        if (willLog(level)) {
             logIt.push(level,elements);
         }
     }
@@ -270,50 +293,62 @@ public class PropAccess implements Access {
         sb.append(name);
         
         int end = elements.length;
-        if(end<=0) {
+        if (end<=0) {
             sb.append("] ");
         } else {
             int idx = 0;
-            if(elements[idx] instanceof Integer) {
+            if(elements[idx]!=null  && 
+               elements[idx] instanceof Integer) {
                 sb.append('-');
                 sb.append(elements[idx]);
                 ++idx;
             }
             sb.append("] ");
-            String s;
-            boolean first = true;
-            for(Object o : elements) {
-                if(o!=null) {
-                    s=o.toString();
-                    if(first) {
-                        first = false;
-                    } else {
-                        int l = s.length();
-                        if(l>0)    {
-                            switch(s.charAt(l-1)) {
-                                case ' ':
-                                    break;
-                                default:
-                                    sb.append(' ');
-                            }
-                        }
-                    }
-                    sb.append(s);
-                }
-            }
+            write(true,sb,elements);
         }
         return sb;
     }
+    
+    private static boolean write(boolean first, StringBuilder sb, Object[] elements) {
+       String s;
+        for (Object o : elements) {
+            if (o!=null) {
+               if(o.getClass().isArray()) {
+                       first = write(first,sb,(Object[])o);
+               } else {
+                       s=o.toString();
+                       if (first) {
+                           first = false;
+                       } else {
+                           int l = s.length();
+                           if (l>0)    {
+                               switch(s.charAt(l-1)) {
+                                   case ' ':
+                                       break;
+                                   default:
+                                       sb.append(' ');
+                               }
+                           }
+                       }
+                       sb.append(s);
+               }
+            }
+        }
+        return first;
+    }
 
     @Override
     public void log(Exception e, Object... elements) {
-        log(Level.ERROR,e.getMessage(),elements);
-        e.printStackTrace(System.err);
+       StringWriter sw = new StringWriter();
+       PrintWriter pw = new PrintWriter(sw);
+       pw.println();
+       e.printStackTrace(pw);
+        log(Level.ERROR,elements,sw.toString());
     }
 
     @Override
     public void printf(Level level, String fmt, Object... elements) {
-        if(willLog(level)) {
+        if (willLog(level)) {
             log(level,String.format(fmt, elements));
         }
     }
@@ -362,9 +397,9 @@ public class PropAccess implements Access {
     }
 
     public void setProperty(String tag, String value) {
-        if(value!=null) {
+        if (value!=null) {
             props.put(tag, value);
-            if(Config.CADI_KEYFILE.equals(tag)) {
+            if (Config.CADI_KEYFILE.equals(tag)) {
                 // reset decryption too
                 try {
                     symm = Symm.obtain(this);
@@ -392,10 +427,17 @@ public class PropAccess implements Access {
             ps.println(buildMsg(level,elements));
             ps.flush();
         }
-        
     }
 
     public void set(LogIt logit) {
         logIt = logit;
     }
+
+    public void setStreamLogIt(PrintStream ps) {
+        logIt = new StreamLogIt(ps);
+    }
+
+    public String toString() {
+       return props.toString();
+    }
 }