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