001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008:
009: There are special exceptions to the terms and conditions of the GPL
010: as it is applied to this software. View the full text of the
011: exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
012: software distribution.
013:
014: This program is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: GNU General Public License for more details.
018:
019: You should have received a copy of the GNU General Public License
020: along with this program; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023: */
024:
025: package com.mysql.jdbc;
026:
027: import java.sql.Connection;
028: import java.sql.SQLException;
029: import java.util.Properties;
030: import java.util.StringTokenizer;
031:
032: /**
033: * Driver that opens two connections, one two a replication master, and another
034: * to one or more slaves, and decides to use master when the connection is not
035: * read-only, and use slave(s) when the connection is read-only.
036: *
037: * @version $Id: NonRegisteringReplicationDriver.java,v 1.1.2.1 2005/05/13
038: * 18:58:37 mmatthews Exp $
039: */
040: public class NonRegisteringReplicationDriver extends
041: NonRegisteringDriver {
042: public NonRegisteringReplicationDriver() throws SQLException {
043: super ();
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
050: */
051: public Connection connect(String url, Properties info)
052: throws SQLException {
053: Properties parsedProps = parseURL(url, info);
054:
055: if (parsedProps == null) {
056: return null;
057: }
058:
059: Properties masterProps = (Properties) parsedProps.clone();
060: Properties slavesProps = (Properties) parsedProps.clone();
061:
062: // Marker used for further testing later on, also when
063: // debugging
064: slavesProps.setProperty(
065: "com.mysql.jdbc.ReplicationConnection.isSlave", "true");
066:
067: String hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY);
068:
069: if (hostValues != null) {
070: StringTokenizer st = new StringTokenizer(hostValues, ",");
071:
072: StringBuffer masterHost = new StringBuffer();
073: StringBuffer slaveHosts = new StringBuffer();
074:
075: if (st.hasMoreTokens()) {
076: String[] hostPortPair = parseHostPortPair(st
077: .nextToken());
078:
079: if (hostPortPair[HOST_NAME_INDEX] != null) {
080: masterHost.append(hostPortPair[HOST_NAME_INDEX]);
081: }
082:
083: if (hostPortPair[PORT_NUMBER_INDEX] != null) {
084: masterHost.append(":");
085: masterHost.append(hostPortPair[PORT_NUMBER_INDEX]);
086: }
087: }
088:
089: boolean firstSlaveHost = true;
090:
091: while (st.hasMoreTokens()) {
092: String[] hostPortPair = parseHostPortPair(st
093: .nextToken());
094:
095: if (!firstSlaveHost) {
096: slaveHosts.append(",");
097: } else {
098: firstSlaveHost = false;
099: }
100:
101: if (hostPortPair[HOST_NAME_INDEX] != null) {
102: slaveHosts.append(hostPortPair[HOST_NAME_INDEX]);
103: }
104:
105: if (hostPortPair[PORT_NUMBER_INDEX] != null) {
106: slaveHosts.append(":");
107: slaveHosts.append(hostPortPair[PORT_NUMBER_INDEX]);
108: }
109: }
110:
111: if (slaveHosts.length() == 0) {
112: throw SQLError
113: .createSQLException(
114: "Must specify at least one slave host to connect to for master/slave replication load-balancing functionality",
115: SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE);
116: }
117:
118: masterProps.setProperty(HOST_PROPERTY_KEY, masterHost
119: .toString());
120: slavesProps.setProperty(HOST_PROPERTY_KEY, slaveHosts
121: .toString());
122: }
123:
124: return new ReplicationConnection(masterProps, slavesProps);
125: }
126: }
|