+ private static WebSocketServer singleton = null;
+ private int port = DEFAULT_PORT;
+
+ private WebSocketServer(int port) {
+ this.port = port;
+ }
+
+ /**
+ * Create instance of {@link WebSocketServer}
+ *
+ * @param port
+ * TCP port used for this server
+ * @return instance of {@link WebSocketServer}
+ */
+ public static WebSocketServer createInstance(int port) {
+ if (singleton != null) {
+ throw new IllegalStateException("createInstance() has already been called");
+ }
+ if (port < 1024) {
+ throw new IllegalArgumentException("Privileged port (below 1024) is not allowed");
+ }
+ singleton = new WebSocketServer(port);
+ return singleton;
+ }
+
+ /**
+ * Return websocket TCP port
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /**
+ * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}
+ *
+ * @return instance of {@link WebSocketServer}
+ */
+ public static WebSocketServer getInstance() {
+ Preconditions.checkNotNull(singleton, "createInstance() must be called prior to getInstance()");
+ return singleton;
+ }
+
+ /**
+ * Destroy this already created instance
+ */
+ public static void destroyInstance() {
+ if (singleton == null) {
+ throw new IllegalStateException("createInstance() must be called prior to destroyInstance()");
+ }
+ getInstance().stop();
+ }