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