001package co.codewizards.cloudstore.ls.core; 002 003import static co.codewizards.cloudstore.core.util.StringUtil.*; 004 005import java.util.ArrayList; 006import java.util.List; 007import java.util.Map; 008import java.util.SortedSet; 009import java.util.TreeSet; 010import java.util.regex.Pattern; 011 012import co.codewizards.cloudstore.core.Uid; 013import co.codewizards.cloudstore.core.config.Config; 014import co.codewizards.cloudstore.core.config.ConfigImpl; 015 016public class LsConfig { 017 018 private static final Uid PROCESS_ID = new Uid(); 019 020 /** 021 * {@link Config}-key controlling whether the local-server enabled. 022 * @see #DEFAULT_LOCAL_SERVER_ENABLED 023 * @see #isLocalServerEnabled() 024 */ 025 public static final String CONFIG_KEY_LOCAL_SERVER_ENABLED = "localServer.enabled"; 026 /** 027 * Default value for {@link #CONFIG_KEY_LOCAL_SERVER_ENABLED}. 028 */ 029 public static final boolean DEFAULT_LOCAL_SERVER_ENABLED = true; 030 031 /** 032 * {@link Config}-key controlling whether the separate local-server-<b>process</b> is launched. 033 * @see #DEFAULT_LOCAL_SERVER_PROCESS_ENABLED 034 * @see #isLocalServerProcessEnabled() 035 */ 036 public static final String CONFIG_KEY_LOCAL_SERVER_PROCESS_ENABLED = "localServerProcess.enabled"; 037 /** 038 * Default value for {@link #CONFIG_KEY_LOCAL_SERVER_PROCESS_ENABLED} 039 */ 040 public static final boolean DEFAULT_LOCAL_SERVER_PROCESS_ENABLED = true; 041 042 /** 043 * {@link Config}-key controlling the timeout in milliseconds the primary (first launched) process waits for 044 * the separate local-server-process to become available. 045 * @see #DEFAULT_LOCAL_SERVER_PROCESS_START_TIMEOUT 046 * @see #getLocalServerProcessStartTimeout() 047 */ 048 public static final String CONFIG_KEY_LOCAL_SERVER_PROCESS_START_TIMEOUT = "localServerProcess.startTimeout"; 049 /** 050 * Default value for {@link #CONFIG_KEY_LOCAL_SERVER_PROCESS_START_TIMEOUT} 051 */ 052 public static final long DEFAULT_LOCAL_SERVER_PROCESS_START_TIMEOUT = 120000L; 053 054 /** 055 * Controls the value passed as 056 * <a href="http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html">{@code -Xmx}</a> 057 * to the child-process, thus specifying the maximum heap size of the local-server's JVM. 058 * <p> 059 * Possible values are everything understood by the JVM after the "-Xmx", for example: 060 * <ul> 061 * <li>"1G" for 1 <a href="https://en.wikipedia.org/wiki/Gibibyte">Gibibyte</a> 062 * <li>"2g" for 2 <a href="https://en.wikipedia.org/wiki/Gibibyte">Gibibyte</a> 063 * <li>"512M" for 512 <a href="https://en.wikipedia.org/wiki/Mebibyte">Mebibyte</a> 064 * <li>"256M" for 256 <a href="https://en.wikipedia.org/wiki/Mebibyte">Mebibyte</a> 065 * </ul> 066 * <p> 067 * This only has an effect, if {@link #CONFIG_KEY_LOCAL_SERVER_PROCESS_ENABLED} is <code>true</code>. 068 * @see #DEFAULT_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE 069 * @see #getLocalServerProcessMaxHeapSize() 070 */ 071 public static final String CONFIG_KEY_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE = "localServerProcess.maxHeapSize"; 072 073 /** 074 * Default value for {@link #CONFIG_KEY_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE} 075 */ 076 public static final String DEFAULT_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE = ""; 077 078 public static final Pattern CONFIG_KEY_PATTERN_LOCAL_SERVER_PROCESS_VM_ARGS = Pattern.compile("localServerProcess\\.vmArgs\\[([^]]+)\\]"); 079 080 private LsConfig() { 081 } 082 083 /** 084 * Is the local-server enabled? 085 * <p> 086 * Controls, whether a TCP (HTTP+REST) server is started on localhost. 087 * <p> 088 * If <code>false</code>, it also prevents the local-server-<b>process</b> from being launched. 089 * Thus in order to launch the local-server-process, both {@code isLocalServerEnabled()} 090 * and {@link #isLocalServerProcessEnabled()} must be <code>true</code>. 091 * @return <code>true</code>, if the local-server should be listening; <code>false</code> otherwise. 092 * @see #CONFIG_KEY_LOCAL_SERVER_ENABLED 093 */ 094 public static boolean isLocalServerEnabled() { 095 return ConfigImpl.getInstance().getPropertyAsBoolean( 096 CONFIG_KEY_LOCAL_SERVER_ENABLED, 097 DEFAULT_LOCAL_SERVER_ENABLED); 098 } 099 100 /** 101 * Should the separate local-server-<b>process</b> be launched? 102 * <p> 103 * Controls, whether a TCP (HTTP+REST) server is started in a separate process, 104 * i.e. whether the current process should launch a separate process. 105 * <p> 106 * If <code>false</code>, the local-server (if {@linkplain #isLocalServerEnabled() enabled}) 107 * runs inside the primary (first-launched) VM process. 108 * <p> 109 * Note: In order to launch the local-server-process, both {@link #isLocalServerEnabled()} 110 * and {@code isLocalServerProcessEnabled()} must be <code>true</code>. 111 * @return <code>true</code>, if the local-server should be listening; <code>false</code> otherwise. 112 * @see #CONFIG_KEY_LOCAL_SERVER_PROCESS_ENABLED 113 */ 114 public static boolean isLocalServerProcessEnabled() { 115 return ConfigImpl.getInstance().getPropertyAsBoolean( 116 CONFIG_KEY_LOCAL_SERVER_PROCESS_ENABLED, 117 DEFAULT_LOCAL_SERVER_PROCESS_ENABLED); 118 } 119 120 /** 121 * Gets the timeout in milliseconds the primary (first launched) process waits for 122 * the separate local-server-process to become available. 123 * <p> 124 * If the local-server does not get ready within this timeout, an exception is thrown. 125 * @return the timeout in milliseconds within which the local-server-process must be 126 * launched completely (i.e. the TCP server become available). 127 * @see #CONFIG_KEY_LOCAL_SERVER_PROCESS_START_TIMEOUT 128 */ 129 public static long getLocalServerProcessStartTimeout() { 130 final long timeoutMs = ConfigImpl.getInstance().getPropertyAsPositiveOrZeroLong( 131 CONFIG_KEY_LOCAL_SERVER_PROCESS_START_TIMEOUT, 132 DEFAULT_LOCAL_SERVER_PROCESS_START_TIMEOUT); 133 return timeoutMs; 134 } 135 136 public static String getLocalServerProcessMaxHeapSize() { 137 final String maxHeapSize = ConfigImpl.getInstance().getPropertyAsNonEmptyTrimmedString( 138 CONFIG_KEY_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE, 139 DEFAULT_LOCAL_SERVER_PROCESS_MAX_HEAP_SIZE); 140 return emptyToNull(maxHeapSize); 141 } 142 143 public static List<String> getLocalServerProcessVmArgs() { 144 List<String> result = new ArrayList<String>(); 145 Config config = ConfigImpl.getInstance(); 146 Map<String, List<String>> key2GroupsMatching = config.getKey2GroupsMatching(CONFIG_KEY_PATTERN_LOCAL_SERVER_PROCESS_VM_ARGS); 147 SortedSet<String> keys = new TreeSet<>(key2GroupsMatching.keySet()); 148 for (String key : keys) { 149 String value = config.getPropertyAsNonEmptyTrimmedString(key, null); 150 if (value != null && ! value.isEmpty()) 151 result.add(value); 152 } 153 return result; 154 } 155 156 public static Uid getProcessId() { 157 return PROCESS_ID; 158 } 159}