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.tcp;
018
019import java.io.IOException;
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.net.UnknownHostException;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.net.ServerSocketFactory;
027import javax.net.SocketFactory;
028
029import org.apache.activemq.openwire.OpenWireFormat;
030import org.apache.activemq.transport.InactivityMonitor;
031import org.apache.activemq.transport.Transport;
032import org.apache.activemq.transport.TransportFactory;
033import org.apache.activemq.transport.TransportLoggerFactory;
034import org.apache.activemq.transport.TransportServer;
035import org.apache.activemq.transport.WireFormatNegotiator;
036import org.apache.activemq.util.IOExceptionSupport;
037import org.apache.activemq.util.IntrospectionSupport;
038import org.apache.activemq.util.URISupport;
039import org.apache.activemq.wireformat.WireFormat;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043/**
044 * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com (logging improvement modifications)
045 *
046 */
047public class TcpTransportFactory extends TransportFactory {
048    private static final Logger LOG = LoggerFactory.getLogger(TcpTransportFactory.class);
049
050    public TransportServer doBind(final URI location) throws IOException {
051        try {
052            Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location));
053
054            ServerSocketFactory serverSocketFactory = createServerSocketFactory();
055            TcpTransportServer server = createTcpTransportServer(location, serverSocketFactory);
056            server.setWireFormatFactory(createWireFormatFactory(options));
057            IntrospectionSupport.setProperties(server, options);
058            Map<String, Object> transportOptions = IntrospectionSupport.extractProperties(options, "transport.");
059            server.setTransportOption(transportOptions);
060            server.bind();
061
062            return server;
063        } catch (URISyntaxException e) {
064            throw IOExceptionSupport.create(e);
065        }
066    }
067
068    /**
069     * Allows subclasses of TcpTransportFactory to create custom instances of
070     * TcpTransportServer.
071     *
072     * @param location
073     * @param serverSocketFactory
074     * @return
075     * @throws IOException
076     * @throws URISyntaxException
077     */
078    protected TcpTransportServer createTcpTransportServer(final URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
079        return new TcpTransportServer(this, location, serverSocketFactory);
080    }
081
082    public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
083
084        TcpTransport tcpTransport = (TcpTransport)transport.narrow(TcpTransport.class);
085        IntrospectionSupport.setProperties(tcpTransport, options);
086
087        Map<String, Object> socketOptions = IntrospectionSupport.extractProperties(options, "socket.");
088        tcpTransport.setSocketOptions(socketOptions);
089
090        if (tcpTransport.isTrace()) {
091            try {
092                transport = TransportLoggerFactory.getInstance().createTransportLogger(transport, tcpTransport.getLogWriterName(),
093                        tcpTransport.isDynamicManagement(), tcpTransport.isStartLogging(), tcpTransport.getJmxPort());
094            } catch (Throwable e) {
095                LOG.error("Could not create TransportLogger object for: " + tcpTransport.getLogWriterName() + ", reason: " + e, e);
096            }
097        }
098
099        boolean useInactivityMonitor = "true".equals(getOption(options, "useInactivityMonitor", "true"));
100        if (useInactivityMonitor && isUseInactivityMonitor(transport)) {
101            transport = new InactivityMonitor(transport, format);
102            IntrospectionSupport.setProperties(transport, options);
103        }
104
105
106        // Only need the WireFormatNegotiator if using openwire
107        if (format instanceof OpenWireFormat) {
108            transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, tcpTransport.getMinmumWireFormatVersion());
109        }
110
111        return super.compositeConfigure(transport, format, options);
112    }
113
114    /**
115     * Returns true if the inactivity monitor should be used on the transport
116     */
117    protected boolean isUseInactivityMonitor(Transport transport) {
118        return true;
119    }
120
121    protected Transport createTransport(URI location, WireFormat wf) throws UnknownHostException, IOException {
122        URI localLocation = null;
123        String path = location.getPath();
124        // see if the path is a local URI location
125        if (path != null && path.length() > 0) {
126            int localPortIndex = path.indexOf(':');
127            try {
128                Integer.parseInt(path.substring(localPortIndex + 1, path.length()));
129                String localString = location.getScheme() + ":/" + path;
130                localLocation = new URI(localString);
131            } catch (Exception e) {
132                LOG.warn("path isn't a valid local location for TcpTransport to use", e.getMessage());
133                if(LOG.isDebugEnabled()) {
134                    LOG.debug("Failure detail", e);
135                }
136            }
137        }
138        SocketFactory socketFactory = createSocketFactory();
139        return createTcpTransport(wf, socketFactory, location, localLocation);
140    }
141
142    /**
143     * Allows subclasses of TcpTransportFactory to provide a create custom
144     * TcpTransport intances.
145     *
146     * @param location
147     * @param wf
148     * @param socketFactory
149     * @param localLocation
150     * @return
151     * @throws UnknownHostException
152     * @throws IOException
153     */
154    protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
155        return new TcpTransport(wf, socketFactory, location, localLocation);
156    }
157
158    protected ServerSocketFactory createServerSocketFactory() throws IOException {
159        return ServerSocketFactory.getDefault();
160    }
161
162    protected SocketFactory createSocketFactory() throws IOException {
163        return SocketFactory.getDefault();
164    }
165}