KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > NonRegisteringReplicationDriver


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7  
8
9  There are special exceptions to the terms and conditions of the GPL
10  as it is applied to this software. View the full text of the
11  exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
12  software distribution.
13
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23  */

24
25 package com.mysql.jdbc;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 /**
33  * Driver that opens two connections, one two a replication master, and another
34  * to one or more slaves, and decides to use master when the connection is not
35  * read-only, and use slave(s) when the connection is read-only.
36  *
37  * @version $Id: NonRegisteringReplicationDriver.java,v 1.1.2.1 2005/05/13
38  * 18:58:37 mmatthews Exp $
39  */

40 public class NonRegisteringReplicationDriver extends NonRegisteringDriver {
41     public NonRegisteringReplicationDriver() throws SQLException JavaDoc {
42         super();
43     }
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
49      */

50     public Connection connect(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
51         Properties JavaDoc parsedProps = parseURL(url, info);
52
53         Properties JavaDoc masterProps = parsedProps;
54         Properties JavaDoc slavesProps = parsedProps;
55
56         String JavaDoc hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY);
57
58         if (hostValues != null) {
59             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(hostValues, ",");
60
61             StringBuffer JavaDoc masterHost = new StringBuffer JavaDoc();
62             StringBuffer JavaDoc slaveHosts = new StringBuffer JavaDoc();
63
64             if (st.hasMoreTokens()) {
65                 String JavaDoc[] hostPortPair = parseHostPortPair(st.nextToken());
66
67                 if (hostPortPair[HOST_NAME_INDEX] != null) {
68                     masterHost.append(hostPortPair[HOST_NAME_INDEX]);
69                 }
70
71                 if (hostPortPair[PORT_NUMBER_INDEX] != null) {
72                     masterHost.append(":");
73                     masterHost.append(hostPortPair[PORT_NUMBER_INDEX]);
74                 }
75             }
76
77             boolean firstSlaveHost = true;
78
79             while (st.hasMoreTokens()) {
80                 String JavaDoc[] hostPortPair = parseHostPortPair(st.nextToken());
81
82                 if (!firstSlaveHost) {
83                     slaveHosts.append(",");
84                 } else {
85                     firstSlaveHost = false;
86                 }
87
88                 if (hostPortPair[HOST_NAME_INDEX] != null) {
89                     slaveHosts.append(hostPortPair[HOST_NAME_INDEX]);
90                 }
91
92                 if (hostPortPair[PORT_NUMBER_INDEX] != null) {
93                     slaveHosts.append(":");
94                     slaveHosts.append(hostPortPair[PORT_NUMBER_INDEX]);
95                 }
96             }
97
98             if (slaveHosts.length() == 0) {
99                 throw new SQLException JavaDoc(
100                         "Must specify at least one slave host to connect to for master/slave replication load-balancing functionality",
101                         SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE);
102             }
103
104             masterProps.setProperty(HOST_PROPERTY_KEY, masterHost.toString());
105             slavesProps.setProperty(HOST_PROPERTY_KEY, slaveHosts.toString());
106         }
107
108         return new ReplicationConnection(masterProps, slavesProps);
109     }
110 }
111
Popular Tags