/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

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