JAL-3348 jvmmempc multiple values shouldn't be merged
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / util / Config.java
index 4fc5e16..8767ae0 100644 (file)
@@ -14,6 +14,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -254,6 +255,9 @@ public class Config
     public String[] getMultiValue (String name)
     {
         Object value = _data.get(name);
+        if (value == null) {
+          return new String[] {};
+        }
         if (value instanceof String) {
             return new String[] { (String)value };
         } else {
@@ -373,6 +377,101 @@ public class Config
         String os = bits[0], arch = (bits.length > 1) ? bits[1] : "";
         return (osname.indexOf(os) != -1) && (osarch.indexOf(arch) != -1);
     }
+    
+    public void mergeConfig(Config newValues, boolean merge) {
+      
+      for (Map.Entry<String, Object> entry : newValues.getData().entrySet()) {
+        
+        String key = entry.getKey();
+        Object nvalue = entry.getValue();
+
+        String mkey = key.indexOf('.') > -1 ? key.substring(key.indexOf('.') + 1) : key;
+        if (merge && allowedMergeKeys.contains(mkey)) {
+          
+          // merge multi values
+          
+          Object value = _data.get(key);
+          
+          if (value == null) {
+            _data.put(key, nvalue);
+          } else if (value instanceof String) {
+            if (nvalue instanceof String) {
+              
+              // value is String, nvalue is String
+              _data.put(key, new String[] { (String)value, (String)nvalue });
+              
+            } else if (nvalue instanceof String[]) {
+              
+              // value is String, nvalue is String[]
+              String[] nvalues = (String[])nvalue;
+              String[] newvalues = new String[nvalues.length+1];
+              newvalues[0] = (String)value;
+              System.arraycopy(nvalues, 0, newvalues, 1, nvalues.length);
+              _data.put(key, newvalues);
+              
+            }
+          } else if (value instanceof String[]) {
+            if (nvalue instanceof String) {
+              
+              // value is String[], nvalue is String
+              String[] values = (String[])value;
+              String[] newvalues = new String[values.length+1];
+              System.arraycopy(values, 0, newvalues, 0, values.length);
+              newvalues[values.length] = (String)nvalue;
+              _data.put(key, newvalues);
+              
+            } else if (nvalue instanceof String[]) {
+              
+              // value is String[], nvalue is String[]
+              String[] values = (String[])value;
+              String[] nvalues = (String[])nvalue;
+              String[] newvalues = new String[values.length + nvalues.length];
+              System.arraycopy(values, 0, newvalues, 0, values.length);
+              System.arraycopy(nvalues, 0, newvalues, values.length, newvalues.length);
+              _data.put(key, newvalues);
+              
+            }
+          }
+          
+        } else if (allowedReplaceKeys.contains(mkey)){
+          
+          // replace value
+          _data.put(key, nvalue);
+          
+        } else {
+          log.warning("Not merging key '"+key+"' into config");
+        }
+
+      }
+      
+    }
+    
+    public String toString() {
+      StringBuilder sb = new StringBuilder();
+      for (Map.Entry<String, Object> entry : getData().entrySet()) {
+        String key = entry.getKey();
+        Object val = entry.getValue();
+        sb.append(key);
+        sb.append("=");
+        if (val instanceof String) {
+          sb.append((String)val);
+        } else if (val instanceof String[]) {
+          sb.append(Arrays.toString((String[])val));
+        } else {
+          sb.append("Value not String or String[]");
+        }
+        sb.append("\n");
+      }
+      return sb.toString();
+    }
+    
+    public Map<String, Object> getData() {
+      return _data;
+    }
 
     private final Map<String, Object> _data;
+    public static final List<String> allowedReplaceKeys = Arrays.asList("appbase","apparg","jvmarg","jvmmempc"); // these are the ones we might use
+    public static final List<String> allowedMergeKeys = Arrays.asList("apparg","jvmarg"); // these are the ones we might use
+    //private final List<String> allowedMergeKeys = Arrays.asList("apparg","jvmarg","resource","code","java_location"); // (not exhaustive list here)
 }