X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fstreams%2Fwebsockets%2FWebSocketServer.java;h=3f70c5afe7ca0a552e0c8bc12bbf98d308e85eb3;hb=9ba2b4eca79bcc0e78099b133296801c8d45a6c4;hp=20951b01e2dd52a16b80cb5794d049c858eb2d82;hpb=58593adc3def71af7d0fb82fabd3d3330dd3fb56;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java index 20951b01e2..3f70c5afe7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java @@ -1,29 +1,33 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + package org.opendaylight.controller.sal.streams.websockets; +import com.google.common.base.Preconditions; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; - import org.opendaylight.controller.sal.streams.listeners.Notificator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Preconditions; - /** * {@link WebSocketServer} is responsible to start and stop web socket server */ public class WebSocketServer implements Runnable { - private static final Logger logger = LoggerFactory - .getLogger(WebSocketServer.class); - public static final String WEBSOCKET_SERVER_CONFIG_PROPERTY = "restconf.websocket.port"; + private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class); public static final int DEFAULT_PORT = 8181; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - private static WebSocketServer singleton = null; + private static WebSocketServer instance = null; private int port = DEFAULT_PORT; private WebSocketServer(int port) { @@ -32,18 +36,17 @@ public class WebSocketServer implements Runnable { /** * Create instance of {@link WebSocketServer} - * @param port TCP port used for this server + * + * @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; + Preconditions.checkState(instance == null, "createInstance() has already been called"); + Preconditions.checkArgument(port > 1024, "Privileged port (below 1024) is not allowed"); + + instance = new WebSocketServer(port); + return instance; } /** @@ -55,23 +58,22 @@ public class WebSocketServer implements Runnable { /** * 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; + Preconditions.checkNotNull(instance, "createInstance() must be called prior to getInstance()"); + return instance; } /** * Destroy this already created instance */ public static void destroyInstance() { - if(singleton == null) { - throw new IllegalStateException( - "createInstance() must be called prior to destroyInstance()"); - } - getInstance().stop(); + Preconditions.checkState(instance != null, "createInstance() must be called prior to destroyInstance()"); + + instance.stop(); + instance = null; } @Override @@ -80,8 +82,7 @@ public class WebSocketServer implements Runnable { workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer()); Channel ch = b.bind(port).sync().channel(); @@ -102,9 +103,11 @@ public class WebSocketServer implements Runnable { Notificator.removeAllListeners(); if (bossGroup != null) { bossGroup.shutdownGracefully(); + bossGroup = null; } if (workerGroup != null) { workerGroup.shutdownGracefully(); + workerGroup = null; } }