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