PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/nl/bitbrains/nebu/common/config/ServerConfiguration.java

https://github.com/deltaforge/nebu-common-java
Java | 72 lines | 29 code | 10 blank | 33 comment | 2 complexity | 3c93c8d9a0ef36f7f93ab3f1e7229bd6 MD5 | raw file
 1package nl.bitbrains.nebu.common.config;
 2
 3import org.jdom2.Element;
 4
 5/**
 6 * Server configuration, should be initialized through {@link Configuration}.
 7 * 
 8 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 9 */
10public final class ServerConfiguration {
11
12    public static final String ROOTNAME = "server";
13    public static final String TAG_PORT = "port";
14
15    private int port;
16
17    /**
18     * Hides the constructor.
19     */
20    private ServerConfiguration() {
21    }
22
23    /**
24     * @param portString
25     *            the port to set
26     * @throws InvalidConfigurationException
27     *             if portString is not an integer or out of range.
28     */
29    private void setPort(final String portString) throws InvalidConfigurationException {
30        Configuration.throwInvalidConfigIfNonInteger(portString, ServerConfiguration.TAG_PORT);
31        this.setPort(Integer.parseInt(portString));
32    }
33
34    /**
35     * @param port
36     *            the port to set
37     * @throws InvalidConfigurationException
38     *             if port is not in port range.
39     */
40    private void setPort(final int port) throws InvalidConfigurationException {
41        if (port < 0 || port > Configuration.MAXIMUM_PORT) {
42            throw new InvalidConfigurationException("Port is out of range",
43                    ServerConfiguration.TAG_PORT);
44        }
45        this.port = port;
46    }
47
48    /**
49     * @return the serverPort
50     */
51    public int getPort() {
52        return this.port;
53    }
54
55    /**
56     * Creates a new singleton based on an XML-element file. NB: This creates a
57     * new singleton, use with caution!
58     * 
59     * @param rootElement
60     *            element that is the root of the configuration.
61     * @return the parsed configuration.
62     * @throws InvalidConfigurationException
63     *             if the configuration is improperly specified.
64     */
65    protected static ServerConfiguration parseXMLRootElement(final Element rootElement)
66            throws InvalidConfigurationException {
67
68        final ServerConfiguration result = new ServerConfiguration();
69        result.setPort(rootElement.getChildTextTrim(ServerConfiguration.TAG_PORT));
70        return result;
71    }
72}