001package co.codewizards.cloudstore.ls.core;
002
003import static co.codewizards.cloudstore.core.io.StreamUtil.*;
004import static co.codewizards.cloudstore.core.oio.OioFileFactory.*;
005import static java.util.Objects.*;
006
007import java.io.IOException;
008import java.io.InputStream;
009import java.io.OutputStream;
010import java.util.Properties;
011
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015import co.codewizards.cloudstore.core.config.ConfigDir;
016import co.codewizards.cloudstore.core.io.LockFile;
017import co.codewizards.cloudstore.core.io.LockFileFactory;
018import co.codewizards.cloudstore.core.oio.File;
019
020public class LocalServerPropertiesManager {
021
022        private static final Logger logger = LoggerFactory.getLogger(LocalServerPropertiesManager.class);
023
024        public static final String PROPERTY_KEY_PORT = "port";
025        public static final String PROPERTY_KEY_PASSWORD = "password";
026
027        private static final class Holder {
028                public static final LocalServerPropertiesManager instance = new LocalServerPropertiesManager();
029        }
030
031        private volatile Properties localServerProperties;
032        private File localServerPropertiesFile;
033
034        protected LocalServerPropertiesManager() {
035        }
036
037        public static LocalServerPropertiesManager getInstance() {
038                return Holder.instance;
039        }
040
041        protected File getLocalServerPropertiesFile() {
042                if (localServerPropertiesFile == null)
043                        localServerPropertiesFile = createFile(ConfigDir.getInstance().getFile(), "localServer.properties");
044
045                return localServerPropertiesFile;
046        }
047
048        protected Properties getLocalServerProperties() {
049                Properties properties = localServerProperties;
050                if (properties == null) {
051                        properties = new Properties();
052
053                        try (final LockFile lockFile = LockFileFactory.getInstance().acquire(getLocalServerPropertiesFile(), 30000);) {
054                                try (InputStream in = castStream(lockFile.createInputStream())) {
055                                        final Properties p = localServerProperties;
056                                        if (p != null)
057                                                return p;
058
059                                        properties.load(in);
060                                        localServerProperties = properties;
061                                }
062                        } catch (IOException x) {
063                                throw new RuntimeException(x);
064                        }
065                }
066                return properties;
067        }
068
069        public void writeLocalServerProperties() {
070                final Properties properties = getLocalServerProperties();
071
072                try (final LockFile lockFile = LockFileFactory.getInstance().acquire(getLocalServerPropertiesFile(), 30000);) {
073                        try (OutputStream out = castStream(lockFile.createOutputStream())) {
074                                properties.store(out, null);
075                        }
076                } catch (IOException x) {
077                        throw new RuntimeException(x);
078                }
079        }
080
081        public int getPort() {
082                final String s = getLocalServerProperties().getProperty(PROPERTY_KEY_PORT);
083                try {
084                        final int port = Integer.parseInt(s);
085                        if (port < 1 || port > 65535)
086                                throw new NumberFormatException("Port is out of range: " + port);
087
088                        return port;
089                } catch (NumberFormatException x) {
090                        logger.warn("getPort: " + x, x);
091                        return -1;
092                }
093        }
094
095        public void setPort(final int port) {
096                if (port < 1 || port > 65535)
097                        throw new IllegalArgumentException("Port is out of range: " + port);
098
099                getLocalServerProperties().setProperty(PROPERTY_KEY_PORT, Integer.toString(port));
100        }
101
102        public String getBaseUrl() {
103                final int port = LocalServerPropertiesManager.getInstance().getPort();
104                if (port < 0)
105                        return null;
106
107                final String baseUrl = "http://127.0.0.1:" + port + '/';
108                return baseUrl;
109        }
110
111        public String getPassword() {
112                return getLocalServerProperties().getProperty(PROPERTY_KEY_PASSWORD);
113        }
114
115        public void setPassword(final String password) {
116                requireNonNull(password, "password");
117                getLocalServerProperties().setProperty(PROPERTY_KEY_PASSWORD, password);
118        }
119
120        public void clear() {
121                localServerProperties = null;
122        }
123}