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

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.TransportChannelFactorySupport;
import org.codehaus.activemq.util.JMSExceptionHelper;
import org.jgroups.Channel;
import org.jgroups.ChannelException;
import org.jgroups.ChannelFactory;
import org.jgroups.JChannelFactory;

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

/**
 * A JGroups implementation of a TransportChannelFactory
 *
 * @version $Revision: 1.2 $
 */
public class JGroupsTransportChannelFactory extends TransportChannelFactorySupport {
    private static final Log log = LogFactory.getLog(JGroupsTransportChannelFactory.class);

    private ChannelFactory channelFactory = new JChannelFactory();
    private Object channelConfiguration;
    private String channelName = "ActiveMQ";

    public JGroupsTransportChannelFactory() {
    }

    public JGroupsTransportChannelFactory(ChannelFactory channelFactory, Object channelConfiguration, String channelName) {
        this.channelFactory = channelFactory;
        this.channelConfiguration = channelConfiguration;
        this.channelName = channelName;
    }

    public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
        try {
            Channel channel = createChannel(remoteLocation);
            channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
            channel.connect(channelName);
            return populateProperties(new JGroupsTransportChannel(wireFormat, channel, null), remoteLocation);
        }
        catch (ChannelException e) {
            throw JMSExceptionHelper.newJMSException("Failed to construct JGroups Channel: " + e, e);
        }
    }

    public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
        return create(wireFormat, remoteLocation);
    }

    public boolean requiresEmbeddedBroker() {
        return true;
    }

    // Properties
    //-------------------------------------------------------------------------


    public ChannelFactory getChannelFactory() {
        return channelFactory;
    }

    public void setChannelFactory(ChannelFactory channelFactory) {
        this.channelFactory = channelFactory;
    }

    public Object getChannelConfiguration() {
        return channelConfiguration;
    }

    public void setChannelConfiguration(Object channelConfiguration) {
        this.channelConfiguration = channelConfiguration;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    protected Channel createChannel(URI remoteLocation) throws ChannelException {
        Object config = channelConfiguration;
        if (config == null) {
            // lets use the URI
            String text = remoteLocation.getSchemeSpecificPart();
            if (!text.equalsIgnoreCase("default")) {
                config = text;
            }
        }
        log.info("Configuring JGroups with: " + config);
        return channelFactory.createChannel(config);
    }
}