/** 
 * 
 * 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.activemq.transport;

import org.activemq.io.WireFormat;
import org.activemq.util.FactoryFinder;

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

/**
 * locates a protocol specific TransportServerChannelFactory
 *
 * @version $Revision: 1.1.1.1 $
 */
public class TransportServerChannelProvider {

    private static FactoryFinder finder = new FactoryFinder("META-INF/services/org/activemq/transport/server/");

    /**
     * Create a Channel used for the Broker to listen to remove connections
     *
     * @param bindAddress
     * @return the TransportServerChannel bound to the address
     * @throws JMSException
     */
    public static TransportServerChannel create(WireFormat wireFormat, URI bindAddress) throws JMSException {
        return getFactory(bindAddress.getScheme()).create(wireFormat, bindAddress);
    }

    /**
     * Create a Channel used for the Broker to listen to remove connections
     *
     * @param bindAddress
     * @return the TransportServerChannel bound to the address
     * @throws JMSException
     */
    public static TransportServerChannel newInstance(WireFormat wireFormat, String bindAddress) throws JMSException, URISyntaxException {
        return create(wireFormat, new URI(bindAddress));
    }

    protected static TransportServerChannelFactory getFactory(String protocol) throws JMSException {
        try {
            Object value = finder.newInstance(protocol);
            if (value instanceof TransportServerChannelFactory) {
                return (TransportServerChannelFactory) value;
            }
            else {
                throw new JMSException("Factory does not implement TransportServerChannelFactory: " + value);
            }
        }
        catch (IllegalAccessException e) {
            throw createJMSexception(protocol, e);
        }
        catch (InstantiationException e) {
            throw createJMSexception(protocol, e);
        }
        catch (IOException e) {
            throw createJMSexception(protocol, e);
        }
        catch (ClassNotFoundException e) {
            throw createJMSexception(protocol, e);
        }

    }

    protected static JMSException createJMSexception(String protocol, Exception e) {
        JMSException answer = new JMSException("Could not load protocol: " + protocol + ". Reason: " + e);
        answer.setLinkedException(e);
        return answer;
    }
}