001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.gravity.jetty8;
023    
024    import javax.servlet.ServletContextEvent;
025    import javax.servlet.ServletContextListener;
026    
027    public class EmbeddedJettyWebSocketServerListener implements ServletContextListener {
028            
029            private EmbeddedJettyWebSocketServer server = null;
030    
031            public void contextInitialized(ServletContextEvent sce) {
032                    server = new EmbeddedJettyWebSocketServer(sce.getServletContext());
033                    
034                    String serverPort = sce.getServletContext().getInitParameter("embedded-jetty-websocket-server-port");
035                    if (serverPort != null) {
036                            try {
037                                    server.setServerPort(Integer.parseInt(serverPort));
038                            }
039                            catch (NumberFormatException e) {
040                                    throw new RuntimeException("Incorrect port " + serverPort + " defined for embedded-jetty-websocket-server-port", e);
041                            }
042                    }
043                    
044                    try {
045                            server.start();
046                    }
047                    catch (Exception e) {
048                            throw new RuntimeException("Could not start embedded Jetty websocket server", e);
049                    }
050            }
051    
052            public void contextDestroyed(ServletContextEvent sce) {
053                    try {
054                            if (server != null)
055                                    server.stop();
056                    }
057                    catch (Exception e) {
058                            throw new RuntimeException("Could not stop embedded Jetty websocket server", e);
059                    }
060            }
061    
062    }