/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

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

import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportServer;
import org.apache.activemq.util.IOExceptionSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.URISupport;
import org.apache.activemq.wireformat.WireFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Automatically generated socket.close() calls to simulate network faults
 */
public class TcpFaultyTransportFactory extends TcpTransportFactory {

   private static final Logger LOG = LoggerFactory.getLogger(TcpFaultyTransportFactory.class);

   protected TcpFaultyTransport createTcpFaultyTransport(WireFormat wf,
                                                         SocketFactory socketFactory,
                                                         URI location,
                                                         URI localLocation) throws UnknownHostException, IOException {
      return new TcpFaultyTransport(wf, socketFactory, location, localLocation);
   }

   protected Transport createTransport(URI location, WireFormat wf) throws UnknownHostException, IOException {
      URI localLocation = null;
      String path = location.getPath();
      // see if the path is a local URI location
      if (path != null && path.length() > 0) {
         int localPortIndex = path.indexOf(':');
         try {
            Integer.parseInt(path.substring(localPortIndex + 1, path.length()));
            String localString = location.getScheme() + ":/" + path;
            localLocation = new URI(localString);
         }
         catch (Exception e) {
            LOG.warn("path isn't a valid local location for TcpTransport to use", e);
         }
      }
      SocketFactory socketFactory = createSocketFactory();
      return createTcpFaultyTransport(wf, socketFactory, location, localLocation);
   }

   protected TcpFaultyTransportServer createTcpFaultyTransportServer(final URI location,
                                                                     ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
      return new TcpFaultyTransportServer(this, location, serverSocketFactory);
   }

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

         ServerSocketFactory serverSocketFactory = createServerSocketFactory();
         TcpFaultyTransportServer server = createTcpFaultyTransportServer(location, serverSocketFactory);
         server.setWireFormatFactory(createWireFormatFactory(options));
         IntrospectionSupport.setProperties(server, options);
         Map<String, Object> transportOptions = IntrospectionSupport.extractProperties(options, "transport.");
         server.setTransportOption(transportOptions);
         server.bind();

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

   protected SocketFactory createSocketFactory() throws IOException {
      return SocketTstFactory.getDefault();
   }

   protected ServerSocketFactory createServerSocketFactory() throws IOException {
      return ServerSocketTstFactory.getDefault();
   }
}
