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.glassfish;
023    
024    import javax.servlet.ServletConfig;
025    import javax.servlet.ServletException;
026    import javax.servlet.ServletRegistration;
027    import javax.servlet.http.HttpServlet;
028    
029    import org.granite.gravity.Gravity;
030    import org.granite.gravity.GravityManager;
031    import org.granite.gravity.GravityServletUtil;
032    import org.granite.logging.Logger;
033    
034    import com.sun.grizzly.websockets.WebSocketApplication;
035    import com.sun.grizzly.websockets.WebSocketEngine;
036    
037    
038    public class GlassFishWebSocketServlet extends HttpServlet {
039            
040            @SuppressWarnings("unused")
041            private static final Logger log = Logger.getLogger(GlassFishWebSocketServlet.class);
042    
043            private static final long serialVersionUID = 1L;
044            
045            private WebSocketApplication app;
046            
047            @SuppressWarnings("deprecation")
048            @Override
049            public void init(ServletConfig config) throws ServletException {
050                    super.init(config);
051                    
052                    GravityServletUtil.init(config);
053                    Gravity gravity = GravityManager.getGravity(getServletContext());
054                    String mapping = null;
055                    for (ServletRegistration sr : getServletContext().getServletRegistrations().values()) {
056                            if (!sr.getClassName().equals(getClass().getName()))
057                                    continue;
058                            mapping = sr.getMappings().iterator().next(); 
059                    }
060                    app = new GlassFishWebSocketApplication(getServletContext(), gravity, mapping);
061                    try {
062                            WebSocketEngine.getEngine().register(getServletContext().getContextPath(), mapping, app);
063                    }
064                    catch (NoSuchMethodError e) {
065                            // Deprecated since Grizzly 1.5.?
066                            WebSocketEngine.getEngine().register(app);
067                    }
068            }
069    
070        @Override
071        public void destroy() {
072            WebSocketEngine.getEngine().unregister(app);
073            app = null;
074        }
075    
076    }