001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.distributed.jgroups;
019
020import net.sf.hajdbc.distributed.CommandDispatcher;
021import net.sf.hajdbc.distributed.CommandDispatcherFactory;
022import net.sf.hajdbc.distributed.MembershipListener;
023import net.sf.hajdbc.distributed.Stateful;
024
025import org.jgroups.Channel;
026import org.jgroups.JChannel;
027
028/**
029 * Factory for creating a JGroups instrumented command dispatcher.
030
031 * @author Paul Ferraro
032 */
033public class JGroupsCommandDispatcherFactory implements CommandDispatcherFactory
034{
035        private static final long serialVersionUID = 5135621114239237376L;
036        
037        public static final long DEFAULT_TIMEOUT = 60000;
038        public static final String DEFAULT_STACK = "udp.xml";
039        
040        private String stack = DEFAULT_STACK;
041        private long timeout = DEFAULT_TIMEOUT;
042        private String name;
043
044        @Override
045        public String getId()
046        {
047                return "jgroups";
048        }
049
050        @Override
051        public <C> CommandDispatcher<C> createCommandDispatcher(String id, C context, Stateful stateful, MembershipListener membershipListener) throws Exception
052        {
053                Channel channel = new JChannel(this.stack);
054                if (this.name != null)
055                {
056                        channel.setName(this.name);
057                }
058                return new JGroupsCommandDispatcher<C>(id, channel, this.timeout, context, stateful, membershipListener);
059        }
060
061        public String getName()
062        {
063                return this.name;
064        }
065
066        public void setName(String name)
067        {
068                this.name = name;
069        }
070
071        public String getStack()
072        {
073                return this.stack;
074        }
075
076        public void setStack(String stack)
077        {
078                this.stack = stack;
079        }
080
081        public long getTimeout()
082        {
083                return this.timeout;
084        }
085
086        public void setTimeout(long timeout)
087        {
088                this.timeout = timeout;
089        }
090}