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; |
28 | import java.sql.SQLException; |
29 | import java.util.Properties; |
30 | import java.util.StringTokenizer; |
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 { |
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 url, Properties info) throws SQLException { |
51 | Properties parsedProps = parseURL(url, info); |
52 | |
53 | if (parsedProps == null) { |
54 | return null; |
55 | } |
56 | |
57 | Properties masterProps = (Properties)parsedProps.clone(); |
58 | Properties slavesProps = (Properties)parsedProps.clone(); |
59 | |
60 | // Marker used for further testing later on, also when |
61 | // debugging |
62 | slavesProps.setProperty("com.mysql.jdbc.ReplicationConnection.isSlave", "true"); |
63 | |
64 | String hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY); |
65 | |
66 | if (hostValues != null) { |
67 | StringTokenizer st = new StringTokenizer(hostValues, ","); |
68 | |
69 | StringBuffer masterHost = new StringBuffer(); |
70 | StringBuffer slaveHosts = new StringBuffer(); |
71 | |
72 | if (st.hasMoreTokens()) { |
73 | String[] hostPortPair = parseHostPortPair(st.nextToken()); |
74 | |
75 | if (hostPortPair[HOST_NAME_INDEX] != null) { |
76 | masterHost.append(hostPortPair[HOST_NAME_INDEX]); |
77 | } |
78 | |
79 | if (hostPortPair[PORT_NUMBER_INDEX] != null) { |
80 | masterHost.append(":"); |
81 | masterHost.append(hostPortPair[PORT_NUMBER_INDEX]); |
82 | } |
83 | } |
84 | |
85 | boolean firstSlaveHost = true; |
86 | |
87 | while (st.hasMoreTokens()) { |
88 | String[] hostPortPair = parseHostPortPair(st.nextToken()); |
89 | |
90 | if (!firstSlaveHost) { |
91 | slaveHosts.append(","); |
92 | } else { |
93 | firstSlaveHost = false; |
94 | } |
95 | |
96 | if (hostPortPair[HOST_NAME_INDEX] != null) { |
97 | slaveHosts.append(hostPortPair[HOST_NAME_INDEX]); |
98 | } |
99 | |
100 | if (hostPortPair[PORT_NUMBER_INDEX] != null) { |
101 | slaveHosts.append(":"); |
102 | slaveHosts.append(hostPortPair[PORT_NUMBER_INDEX]); |
103 | } |
104 | } |
105 | |
106 | if (slaveHosts.length() == 0) { |
107 | throw new SQLException( |
108 | "Must specify at least one slave host to connect to for master/slave replication load-balancing functionality", |
109 | SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); |
110 | } |
111 | |
112 | masterProps.setProperty(HOST_PROPERTY_KEY, masterHost.toString()); |
113 | slavesProps.setProperty(HOST_PROPERTY_KEY, slaveHosts.toString()); |
114 | } |
115 | |
116 | return new ReplicationConnection(masterProps, slavesProps); |
117 | } |
118 | } |