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.websocket;
023    
024    import javax.servlet.ServletContextEvent;
025    import javax.servlet.ServletContextListener;
026    
027    public class PolicyFileServerListener implements ServletContextListener {
028            
029            private PolicyFileServer server = null;
030    
031            public void contextInitialized(ServletContextEvent sce) {
032                    server = new PolicyFileServer();
033                    
034                    String serverPort = sce.getServletContext().getInitParameter("flashPolicyFileServer-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 flash-policy-file-server-port", e);
041                            }
042                    }
043                    
044                    String allow = sce.getServletContext().getInitParameter("flashPolicyFileServer-allowDomains");
045                    if (allow != null) {
046                            String[] a = allow.split(",");
047                            String[] domains = new String[a.length];
048                            String[] ports = new String[a.length];
049                            for (int i = 0; i < a.length; i++) {
050                                    int idx = a[i].indexOf(":");
051                                    if (idx < 0) {
052                                            domains[i] = a[i];
053                                            ports[i] = "80,443";
054                                    }
055                                    else {
056                                            domains[i] = a[i].substring(0, idx);
057                                            ports[i] = a[i].substring(idx+1);
058                                    }
059                            }
060                            server.setAllowDomains(domains);
061                            server.setAllowPorts(ports);
062                    }
063                    
064                    server.start();
065            }
066    
067            public void contextDestroyed(ServletContextEvent sce) {
068                    server.stop();
069            }
070    
071    }