/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.codehaus.activemq.transport.ember;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.transport.TransportChannel;
import org.codehaus.activemq.transport.TransportChannelFactory;
import org.codehaus.activemq.util.IdGenerator;
import pyrasun.eio.EIOGlobalContext;
import pyrasun.eio.EIOPoolingStrategy;
import pyrasun.eio.services.EmberServiceController;
import pyrasun.eio.services.EmberServiceException;
import pyrasun.eio.services.bytearray.ByteArrayClientService;
import pyrasun.eio.services.bytearray.ByteArrayServerClient;

import javax.jms.JMSException;
import java.io.IOException;
import java.net.URI;

/**
 * An EmberIO (using NIO) implementation of a TransportChannelFactory
 *
 * @version $Revision: 1.2 $
 */
public class EmberTransportChannelFactory extends EmberSupport implements TransportChannelFactory {

    protected static final Log log = LogFactory.getLog(EmberTransportChannelFactory.class);

    private IdGenerator idGenerator = new IdGenerator();

    public EmberTransportChannelFactory() {
    }

    public EmberTransportChannelFactory(EIOGlobalContext context, EIOPoolingStrategy ioPoolingStrategy) {
        super(context, ioPoolingStrategy);
    }

    /**
     * Create a Channel to a remote Node - e.g. a Broker
     *
     * @param wireFormat
     * @param remoteLocation
     * @return the TransportChannel bound to the remote node
     * @throws JMSException
     */
    public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
        try {
            String id = idGenerator.generateId();
            EmberServiceController controller = getController();

            ByteArrayServerClient client = createClient(controller, remoteLocation, id);
            return new EmberTransportChannel(wireFormat, getContext(), controller, client);
        }
        catch (IOException ioe) {
            JMSException jmsEx = new JMSException("Initialization of TransportChannel failed: " + ioe);
            jmsEx.setLinkedException(ioe);
            throw jmsEx;
        }
        catch (EmberServiceException e) {
            JMSException jmsEx = new JMSException("Initialization of TransportChannel failed: " + e);
            jmsEx.setLinkedException(e);
            throw jmsEx;
        }
    }

    /**
     * Create a Channel to a remote Node - e.g. a Broker
     *
     * @param wireFormat
     * @param remoteLocation
     * @param localLocation  -
     *                       e.g. local InetAddress and local port
     * @return the TransportChannel bound to the remote node
     * @throws JMSException
     */
    public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
        return create(wireFormat, remoteLocation);
    }

    public boolean requiresEmbeddedBroker() {
        return false;
    }

    /**
     * @param remoteLocation
     * @param id
     * @return @throws
     *         JMSException
     * @throws EmberServiceException
     * @throws IOException
     */
    protected ByteArrayServerClient createClient(EmberServiceController controller, URI remoteLocation, String id) throws JMSException,
            EmberServiceException, IOException {
        ByteArrayClientService service = createNioService(controller);
        ByteArrayServerClient client = service.createClient(remoteLocation.getHost(), remoteLocation.getPort(), id, null);
        return client;
    }

    /**
     * Factory method to create a new ObjectClientService
     *
     * @throws JMSException if it could not be created
     */
    protected ByteArrayClientService createNioService(EmberServiceController controller) throws JMSException {
        ByteArrayClientService service;
        try {
            service = new ByteArrayClientService(getContext(), getIoPoolingStrategy());
            controller.addService(service);
        }
        catch (IOException e) {
            throw createJMSException("Creation of NIO service failed: ", e);
        }
        return service;
    }

}