01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.distributable;
22:
23: import net.sf.hajdbc.DatabaseCluster;
24: import net.sf.hajdbc.DatabaseClusterDecorator;
25:
26: import org.jgroups.Channel;
27: import org.jgroups.JChannelFactory;
28:
29: /**
30: * @author Paul Ferraro
31: *
32: */
33: public class DistributableDatabaseClusterDecorator implements
34: DatabaseClusterDecorator {
35: private JChannelFactory factory;
36: private String config = "stacks.xml"; //$NON-NLS-1$
37: private String stack = "udp-sync"; //$NON-NLS-1$
38: private int timeout = 1000;
39:
40: public Channel createChannel(String name) throws Exception {
41: return this .factory.createMultiplexerChannel(this .stack, name);
42: }
43:
44: /**
45: * Returns the number of milliseconds to allow for jgroups cluster operations
46: * @return a number of milliseconds
47: */
48: public int getTimeout() {
49: return this .timeout;
50: }
51:
52: /**
53: * @see net.sf.hajdbc.DatabaseClusterDecorator#decorate(net.sf.hajdbc.DatabaseCluster)
54: */
55: @Override
56: public <D> void decorate(DatabaseCluster<D> databaseCluster)
57: throws Exception {
58: if (this .factory == null) {
59: this .factory = new JChannelFactory();
60:
61: this .factory.setDomain(JChannelFactory.class.getPackage()
62: .getName());
63: this .factory.setMultiplexerConfig(this .config);
64: this .factory.create();
65: }
66:
67: databaseCluster.setLockManager(new DistributableLockManager(
68: databaseCluster, this ));
69: databaseCluster.setStateManager(new DistributableStateManager(
70: databaseCluster, this ));
71: }
72:
73: @Override
74: protected void finalize() {
75: this.factory.destroy();
76: }
77: }
|