/**
 *
 * Copyright 2005-2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.transport.tcp;

import org.activeio.command.WireFormat;
import org.apache.activemq.openwire.OpenWireFormat;
import org.apache.activemq.transport.MutexTransport;
import org.apache.activemq.transport.ResponseCorrelator;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFactory;
import org.apache.activemq.transport.TransportLogger;
import org.apache.activemq.transport.TransportServer;
import org.apache.activemq.transport.WireFormatNegotiator;
import org.apache.activemq.util.IOExceptionSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.URISupport;

import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class TcpTransportFactory extends TransportFactory {

    public TransportServer doBind(String brokerId, final URI location) throws IOException {
        try {
            Map options = new HashMap(URISupport.parseParamters(location));

            TcpTransportServer server = new TcpTransportServer(location);
            server.setWireFormatFactory(createWireFormatFactory(options));
            IntrospectionSupport.setProperties(server, options);

            return server;
        }
        catch (URISyntaxException e) {
            throw IOExceptionSupport.create(e);
        }
    }

    public Transport configure(Transport transport, WireFormat format, Map options) {
        IntrospectionSupport.setProperties(transport, options);
        TcpTransport tcpTransport = (TcpTransport) transport;
        if (tcpTransport.isTrace()) {
            transport = new TransportLogger(transport);
        }

        // TODO: missing inactivity monitor
        // transport = new InactivityMonitor(transport,
        // temp.getMaxInactivityDuration(), activityMonitor.getReadCounter(),
        // activityMonitor.getWriteCounter());
        if( format instanceof OpenWireFormat )
            transport = new WireFormatNegotiator(transport, format, tcpTransport.getMinmumWireFormatVersion());
        
        transport = new MutexTransport(transport);
        transport = new ResponseCorrelator(transport);
        return transport;
    }

    public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
        IntrospectionSupport.setProperties(transport, options);
        TcpTransport tcpTransport = (TcpTransport) transport;
        if (tcpTransport.isTrace()) {
            transport = new TransportLogger(transport);
        }

        // TODO: missing inactivity monitor
        // transport = new InactivityMonitor(transport,
        // temp.getMaxInactivityDuration(), activityMonitor.getReadCounter(),
        // activityMonitor.getWriteCounter());
        transport = new WireFormatNegotiator(transport, format, tcpTransport.getMinmumWireFormatVersion());
        return transport;
    }

    protected Transport createTransport(URI location, WireFormat wf) throws UnknownHostException, IOException {
        return new TcpTransport(wf, location);
    }

    protected ServerSocketFactory createServerSocketFactory() {
        return ServerSocketFactory.getDefault();
    }

    protected SocketFactory createSocketFactory() {
        return SocketFactory.getDefault();
    }
}

@@ -656,16 +656,257 @@
 t.tcp;%0A%0A
+import java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.UnknownHostException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport javax.net.ServerSocketFactory;%0Aimport javax.net.SocketFactory;%0A
 import o
@@ -1523,312 +1523,234 @@
 rt;%0A
-%0A
 import 
-javax.net.ServerSocketFactory;%0Aimport javax.net.SocketFactory;%0A%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.UnknownHostException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Apublic class TcpTransportFactory extends TransportFactory %7B%0A
+org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Apublic class TcpTransportFactory extends TransportFactory %7B%0A    private static final Log log = LogFactory.getLog(TcpTransportFactory.class);
 %0A   
@@ -3879,17 +3879,16 @@
 ocation,
- 
 WireForm
@@ -3926,59 +3926,746 @@
 ion,
- IOException %7B%0A        return new TcpTransport(wf, 
+IOException%7B%0A        URI localLocation=null;%0A        String path=location.getPath();%0A        // see if the path is a local URI location%0A        if(path!=null&&path.length()%3E0)%7B%0A            int localPortIndex=path.indexOf(':');%0A            try%7B%0A                Integer.parseInt(path.substring((localPortIndex+1),path.length()));%0A                String localString=location.getScheme()+ %22:/%22 + path;%0A                localLocation=new URI(localString);%0A            %7Dcatch(Exception e)%7B%0A                log.warn(%22path isn't a valid local location for TcpTransport to use%22,e);%0A            %7D%0A        %7D%0A        if(localLocation!=null)%7B%0A            return new TcpTransport(wf,location,localLocation);%0A        %7D%0A        return new TcpTransport(wf,
 loca

support defining local address and port for a socket - e.g: ssl://localhost:5666/localhost:60606 where the path (localhost:60606) - defines the local address and local port