001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.activemq.transport.ws;
019    
020    import java.net.InetSocketAddress;
021    import java.net.URI;
022    import java.util.Map;
023    
024    import org.apache.activemq.command.BrokerInfo;
025    import org.apache.activemq.transport.SocketConnectorFactory;
026    import org.apache.activemq.transport.WebTransportServerSupport;
027    import org.apache.activemq.util.IntrospectionSupport;
028    import org.apache.activemq.util.ServiceStopper;
029    import org.eclipse.jetty.server.Connector;
030    import org.eclipse.jetty.server.Server;
031    import org.eclipse.jetty.servlet.ServletContextHandler;
032    import org.eclipse.jetty.servlet.ServletHolder;
033    
034    /**
035     * Creates a web server and registers web socket server
036     *
037     */
038    public class WSTransportServer extends WebTransportServerSupport {
039    
040        public WSTransportServer(URI location) {
041            super(location);
042            this.bindAddress = location;
043            socketConnectorFactory = new SocketConnectorFactory();
044        }
045    
046        protected void doStart() throws Exception {
047            server = new Server();
048    
049            if (connector == null) {
050                connector = socketConnectorFactory.createConnector();
051            }
052    
053            URI bind = getBindLocation();
054    
055            bind();
056    
057            ServletContextHandler contextHandler =
058                    new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY);
059    
060            ServletHolder holder = new ServletHolder();
061            Map<String, Object> webSocketOptions = IntrospectionSupport.extractProperties(transportOptions, "websocket.");
062            for(Map.Entry<String,Object> webSocketEntry : webSocketOptions.entrySet()) {
063                Object value = webSocketEntry.getValue();
064                if (value != null) {
065                    holder.setInitParameter(webSocketEntry.getKey(), value.toString());
066                }
067            }
068    
069            holder.setServlet(new StompServlet());
070            contextHandler.addServlet(holder, "/");
071    
072            contextHandler.setAttribute("acceptListener", getAcceptListener());
073    
074            server.start();
075        }
076    
077        protected void doStop(ServiceStopper stopper) throws Exception {
078            Server temp = server;
079            server = null;
080            if (temp != null) {
081                temp.stop();
082            }
083        }
084    
085        public InetSocketAddress getSocketAddress() {
086            return null;
087        }
088    
089        public void setBrokerInfo(BrokerInfo brokerInfo) {
090        }
091    
092        protected void setConnector(Connector connector) {
093            this.connector = connector;
094        }
095    
096        @Override
097        public void setTransportOption(Map<String, Object> transportOptions) {
098            Map<String, Object> socketOptions = IntrospectionSupport.extractProperties(transportOptions, "transport.");
099            socketConnectorFactory.setTransportOptions(socketOptions);
100            super.setTransportOption(transportOptions);
101        }
102    
103        @Override
104        public boolean isSslServer() {
105            return false;
106        }
107    
108    }