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    package org.apache.activemq.transport.stomp;
018    
019    import java.io.IOException;
020    import java.net.Socket;
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    import java.net.UnknownHostException;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.net.ServerSocketFactory;
028    import javax.net.SocketFactory;
029    
030    import org.apache.activemq.broker.BrokerContext;
031    import org.apache.activemq.broker.BrokerService;
032    import org.apache.activemq.broker.BrokerServiceAware;
033    import org.apache.activemq.transport.MutexTransport;
034    import org.apache.activemq.transport.Transport;
035    import org.apache.activemq.transport.nio.NIOTransportFactory;
036    import org.apache.activemq.transport.tcp.TcpTransport;
037    import org.apache.activemq.transport.tcp.TcpTransportServer;
038    import org.apache.activemq.util.IntrospectionSupport;
039    import org.apache.activemq.wireformat.WireFormat;
040    
041    /**
042     * A <a href="http://stomp.codehaus.org/">STOMP</a> over NIO transport factory
043     *
044     *
045     */
046    public class StompNIOTransportFactory extends NIOTransportFactory implements BrokerServiceAware {
047    
048        private BrokerContext brokerContext = null;
049    
050        protected String getDefaultWireFormatType() {
051            return "stomp";
052        }
053    
054        protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
055            return new TcpTransportServer(this, location, serverSocketFactory) {
056                protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
057                    return new StompNIOTransport(format, socket);
058                }
059            };
060        }
061    
062        protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
063            return new StompNIOTransport(wf, socketFactory, location, localLocation);
064        }
065    
066        @SuppressWarnings("rawtypes")
067        @Override
068        public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception {
069            transport = super.serverConfigure(transport, format, options);
070    
071            MutexTransport mutex = transport.narrow(MutexTransport.class);
072            if (mutex != null) {
073                mutex.setSyncOnCommand(true);
074            }
075    
076            return transport;
077        }
078    
079        @SuppressWarnings("rawtypes")
080        public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
081            transport = new StompTransportFilter(transport, format, brokerContext);
082            IntrospectionSupport.setProperties(transport, options);
083            return super.compositeConfigure(transport, format, options);
084        }
085    
086        public void setBrokerService(BrokerService brokerService) {
087            this.brokerContext = brokerService.getBrokerContext();
088        }
089    
090        @Override
091        protected Transport createInactivityMonitor(Transport transport, WireFormat format) {
092            StompInactivityMonitor monitor = new StompInactivityMonitor(transport, format);
093    
094            StompTransportFilter filter = (StompTransportFilter) transport.narrow(StompTransportFilter.class);
095            filter.setInactivityMonitor(monitor);
096    
097            return monitor;
098        }
099    }
100