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