Merge branch 'task/JAL-3247_JAL-3246_JAL-3254_JAL-3236_merge' into task/JAL-3141_JAL...
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / Application.java
index 17e98f8..0de5c8a 100644 (file)
@@ -26,6 +26,7 @@ import java.util.zip.GZIPInputStream;
 import com.sun.management.OperatingSystemMXBean;
 import java.lang.management.ManagementFactory;
 
+import jalview.bin.MemorySetting;
 
 import com.threerings.getdown.util.*;
 // avoid ambiguity with java.util.Base64 which we can't use as it's 1.8+
@@ -597,6 +598,11 @@ public class Application
         // process, our caller can use the appbase to download a new configuration file
         _appbase = config.getString("appbase");
         
+        // see if locatorConfig override
+        if (locatorConfig != null && !StringUtil.isBlank(locatorConfig.getString("appbase"))) {
+          _appbase = locatorConfig.getString("appbase");
+        }
+        
         if (_appbase == null) {
             throw new RuntimeException("m.missing_appbase");
         }
@@ -748,28 +754,34 @@ public class Application
             jvmmempc = config.getInt(appPrefix + "jvmmempc", jvmmempc);
         }
         if (0 <= jvmmempc && jvmmempc <= 100) {
-            final Object o = ManagementFactory.getOperatingSystemMXBean();
-
-            try {
-                if (o instanceof OperatingSystemMXBean) {
-                    final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
-                    long physicalMem = osb.getTotalPhysicalMemorySize();
-                    long requestedMem = physicalMem*jvmmempc/100;
-                    String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(requestedMem)};
-                    // remove other max heap size arg
-                    ARG: for (int i = 0; i < _jvmargs.size(); i++) {
-                           if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
-                                _jvmargs.remove(i);
-                           }
-                    }
-                    addAll(maxMemHeapArg, _jvmargs);
+          
+          long maxMemLong = -1;
+
+          try
+          {
+            maxMemLong = MemorySetting.memPercent(jvmmempc);
+          } catch (Exception e)
+          {
+            e.printStackTrace();
+          } catch (Throwable t)
+          {
+            t.printStackTrace();
+          }
 
-                }
-            }
-            catch (NoClassDefFoundError e) {
-                // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-                System.out.println("No com.sun.management.OperatingSystemMXBean. Cannot use 'jvmmempc'.");
+          if (maxMemLong > 0)
+          {
+            
+            String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(maxMemLong)};
+            // remove other max heap size arg
+            ARG: for (int i = 0; i < _jvmargs.size(); i++) {
+              if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
+                _jvmargs.remove(i);
+              }
             }
+            addAll(maxMemHeapArg, _jvmargs);
+            
+          }
+
         } else if (jvmmempc != -1) {
           System.out.println("'jvmmempc' value must be in range 0 to 100 (read as '"+Integer.toString(jvmmempc)+"')");
         }
@@ -1844,7 +1856,6 @@ public class Application
     public static void addStartupFile(String filename) {
       _startupFiles.add(new File(filename));
     }
-
     
     private Config createLocatorConfig(Config.ParseOpts opts) {
       if (_locatorFile == null) {
@@ -1861,19 +1872,24 @@ public class Application
           log.warning("Given locator file does not exist", "file", _locatorFile);
         }
         
-        // appbase is sanitised in HostWhitelist and here!
-        Map<String, Object> tmpdata = new HashMap<>();
-        tmpdata.put("appbase", tmpConfig.getString("appbase"));
-        tmpdata.put("appargs", tmpConfig.getString("appargs"));
-        tmpdata.put("jvmargs", tmpConfig.getString("jvmargs"));
-        tmpdata.put(LOCATOR_FILE_EXTENSION+"replace", tmpConfig.getString(LOCATOR_FILE_EXTENSION+"replace"));
-        tmpdata.put(LOCATOR_FILE_EXTENSION+"merge", tmpConfig.getString(LOCATOR_FILE_EXTENSION+"merge"));
-        locatorConfig = new Config(tmpdata);
+        // appbase is sanitised in HostWhitelist
+        Map<String, Object> tmpData = new HashMap<>();
+        for (Map.Entry<String, Object> entry : tmpConfig.getData().entrySet()) {
+          String key = entry.getKey();
+          Object value = entry.getValue();
+          String mkey = key.indexOf('.') > -1 ? key.substring(key.indexOf('.') + 1) : key;
+          if (Config.allowedReplaceKeys.contains(mkey) || Config.allowedMergeKeys.contains(mkey)) {
+            tmpData.put(key, value);
+          }
+        }
+        locatorConfig = new Config(tmpData);
         
       } catch (Exception e) {
         log.warning("Failure reading locator file",  "file", _locatorFile, e);
       }
       
+      log.info("Returning locatorConfig", locatorConfig);
+      
       return locatorConfig;
     }