001/*
002 * Copyright (c) 2015-2017 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2017-09-04 12:59:35 -0700 (Mon, 04 Sep 2017) $' 
007 * '$Revision: 1407 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.module.webview;
031
032import java.util.Set;
033import java.util.concurrent.CountDownLatch;
034
035import org.kepler.module.ModuleShutdownable;
036import org.kepler.webview.server.WebViewServer;
037
038import ptolemy.util.MessageHandler;
039
040/** Perform cleanup for the web-view module.
041 * 
042 * @author Daniel Crawl
043 * @version $Id: Shutdown.java 1407 2017-09-04 19:59:35Z crawl $
044 */
045public class Shutdown implements ModuleShutdownable
046{
047
048    /** Perform any module-specific cleanup. */
049    @Override
050    public void shutdownModule() {
051        
052        WebViewServer.shutdown();
053        
054        Set<String> ids = WebViewServer.vertx().deploymentIDs();
055        WebViewServer.vertx().undeploy(ids.iterator().next(),
056            result -> {
057                shutdownLatch.countDown();
058            }            
059        );        
060        waitForShutdown(null);
061        
062    }
063    
064    /** Block until the servers have shutdown. 
065     *  @param args The command line arguments (if any). These are present
066     *  since this method is called from org.kepler.Kepler via reflection
067     *  for -wvdaemon. 
068     */
069    public static void waitForShutdown(String[] args) {
070        try {
071            shutdownLatch.await();
072        } catch (InterruptedException e) {
073            MessageHandler.error("Interrupted waiting for shut down.", e);
074        }
075    }
076    
077    /** A count down latch to denote when the server(s) have shut down. */
078    public final static CountDownLatch shutdownLatch = new CountDownLatch(1);
079}