GUI -- Fixed a shut-down exception by explicitly closing all web-sockets on shutdown.

Change-Id: I3264a9029f36fdba150a3d729a2dd8c9024805d3
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
index 0eeda52..5706734 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
@@ -23,7 +23,10 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.mastership.MastershipService;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
@@ -66,6 +69,8 @@
     private final AltTopoViewMessageHandler topoHandler =
             new AltTopoViewMessageHandler();
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MastershipService mastershipService;
 
     // Creates core UI extension
     private UiExtension createCoreExtension() {
@@ -111,6 +116,7 @@
 
     @Deactivate
     public void deactivate() {
+        UiWebSocketServlet.closeAll();
         unregister(core);
         log.info("Stopped");
     }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
index f262202..4ccf9ea 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
@@ -35,15 +35,28 @@
 
     private static final long PING_DELAY_MS = 5000;
 
+    private static UiWebSocketServlet instance;
+
     private ServiceDirectory directory = new DefaultServiceDirectory();
 
     private final Set<UiWebSocket> sockets = new HashSet<>();
     private final Timer timer = new Timer();
     private final TimerTask pruner = new Pruner();
 
+    /**
+     * Closes all currently open UI web-sockets.
+     */
+    public static void closeAll() {
+        if (instance != null) {
+            instance.sockets.forEach(UiWebSocket::close);
+            instance.sockets.clear();
+        }
+    }
+
     @Override
     public void init() throws ServletException {
         super.init();
+        instance = this;
         timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
     }