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