?? nonregisteringdriver.java
字號:
/* Copyright (C) 2002-2007 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.sql.Connection;import java.sql.DriverPropertyInfo;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Properties;import java.util.StringTokenizer;/** * The Java SQL framework allows for multiple database drivers. Each driver * should supply a class that implements the Driver interface * * <p> * The DriverManager will try to load as many drivers as it can find and then * for any given connection request, it will ask each driver in turn to try to * connect to the target URL. * </p> * * <p> * It is strongly recommended that each Driver class should be small and * standalone so that the Driver class can be loaded and queried without * bringing in vast quantities of supporting code. * </p> * * <p> * When a Driver class is loaded, it should create an instance of itself and * register it with the DriverManager. This means that a user can load and * register a driver by doing Class.forName("foo.bah.Driver") * </p> * * @author Mark Matthews * @version $Id: NonRegisteringDriver.java,v 1.1.2.1 2005/05/13 18:58:38 * mmatthews Exp $ * * @see org.gjt.mm.mysql.Connection * @see java.sql.Driver */public class NonRegisteringDriver implements java.sql.Driver { private static final String REPLICATION_URL_PREFIX = "jdbc:mysql:replication://"; private static final String URL_PREFIX = "jdbc:mysql://"; private static final String MXJ_URL_PREFIX = "jdbc:mysql:mxj://"; private static final String LOADBALANCE_URL_PREFIX = "jdbc:mysql:loadbalance://"; /** * Key used to retreive the database value from the properties instance * passed to the driver. */ public static final String DBNAME_PROPERTY_KEY = "DBNAME"; /** Should the driver generate debugging output? */ public static final boolean DEBUG = false; /** Index for hostname coming out of parseHostPortPair(). */ public final static int HOST_NAME_INDEX = 0; /** * Key used to retreive the hostname value from the properties instance * passed to the driver. */ public static final String HOST_PROPERTY_KEY = "HOST"; /** * Key used to retreive the password value from the properties instance * passed to the driver. */ public static final String PASSWORD_PROPERTY_KEY = "password"; /** Index for port # coming out of parseHostPortPair(). */ public final static int PORT_NUMBER_INDEX = 1; /** * Key used to retreive the port number value from the properties instance * passed to the driver. */ public static final String PORT_PROPERTY_KEY = "PORT"; public static final String PROPERTIES_TRANSFORM_KEY = "propertiesTransform"; /** Should the driver generate method-call traces? */ public static final boolean TRACE = false; public static final String USE_CONFIG_PROPERTY_KEY = "useConfigs"; /** * Key used to retreive the username value from the properties instance * passed to the driver. */ public static final String USER_PROPERTY_KEY = "user"; /** * Gets the drivers major version number * * @return the drivers major version number */ static int getMajorVersionInternal() { return safeIntParse("@MYSQL_CJ_MAJOR_VERSION@"); //$NON-NLS-1$ } /** * Get the drivers minor version number * * @return the drivers minor version number */ static int getMinorVersionInternal() { return safeIntParse("@MYSQL_CJ_MINOR_VERSION@"); //$NON-NLS-1$ } /** * Parses hostPortPair in the form of [host][:port] into an array, with the * element of index HOST_NAME_INDEX being the host (or null if not * specified), and the element of index PORT_NUMBER_INDEX being the port (or * null if not specified). * * @param hostPortPair * host and port in form of of [host][:port] * * @return array containing host and port as Strings * * @throws SQLException * if a parse error occurs */ protected static String[] parseHostPortPair(String hostPortPair) throws SQLException { int portIndex = hostPortPair.indexOf(":"); //$NON-NLS-1$ String[] splitValues = new String[2]; String hostname = null; if (portIndex != -1) { if ((portIndex + 1) < hostPortPair.length()) { String portAsString = hostPortPair.substring(portIndex + 1); hostname = hostPortPair.substring(0, portIndex); splitValues[HOST_NAME_INDEX] = hostname; splitValues[PORT_NUMBER_INDEX] = portAsString; } else { throw SQLError.createSQLException(Messages .getString("NonRegisteringDriver.37"), //$NON-NLS-1$ SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); } } else { splitValues[HOST_NAME_INDEX] = hostPortPair; splitValues[PORT_NUMBER_INDEX] = null; } return splitValues; } private static int safeIntParse(String intAsString) { try { return Integer.parseInt(intAsString); } catch (NumberFormatException nfe) { return 0; } } /** * Construct a new driver and register it with DriverManager * * @throws SQLException * if a database error occurs. */ public NonRegisteringDriver() throws SQLException { // Required for Class.forName().newInstance() } /** * Typically, drivers will return true if they understand the subprotocol * specified in the URL and false if they don't. This driver's protocols * start with jdbc:mysql: * * @param url * the URL of the driver * * @return true if this driver accepts the given URL * * @exception SQLException * if a database-access error occurs * * @see java.sql.Driver#acceptsURL */ public boolean acceptsURL(String url) throws SQLException { return (parseURL(url, null) != null); } // // return the database name property // /** * Try to make a database connection to the given URL. The driver should * return "null" if it realizes it is the wrong kind of driver to connect to * the given URL. This will be common, as when the JDBC driverManager is * asked to connect to a given URL, it passes the URL to each loaded driver * in turn. * * <p> * The driver should raise an SQLException if it is the right driver to * connect to the given URL, but has trouble connecting to the database. * </p> * * <p> * The java.util.Properties argument can be used to pass arbitrary string * tag/value pairs as connection arguments. * </p> * * <p> * My protocol takes the form: * * <PRE> * * jdbc:mysql://host:port/database * * </PRE> * * </p> * * @param url * the URL of the database to connect to * @param info * a list of arbitrary tag/value pairs as connection arguments * * @return a connection to the URL or null if it isnt us * * @exception SQLException * if a database access error occurs * * @see java.sql.Driver#connect */ public java.sql.Connection connect(String url, Properties info) throws SQLException { if (url != null) { if (StringUtils.startsWithIgnoreCase(url, LOADBALANCE_URL_PREFIX)) { return connectLoadBalanced(url, info); } else if (StringUtils.startsWithIgnoreCase(url, REPLICATION_URL_PREFIX)) { return connectReplicationConnection(url, info); } } Properties props = null; if ((props = parseURL(url, info)) == null) { return null; } try { Connection newConn = new com.mysql.jdbc.Connection(host(props), port(props), props, database(props), url); return newConn; } catch (SQLException sqlEx) { // Don't wrap SQLExceptions, throw // them un-changed. throw sqlEx; } catch (Exception ex) { throw SQLError.createSQLException(Messages .getString("NonRegisteringDriver.17") //$NON-NLS-1$ + ex.toString() + Messages.getString("NonRegisteringDriver.18"), //$NON-NLS-1$ SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE); } } private java.sql.Connection connectLoadBalanced(String url, Properties info) throws SQLException { Properties parsedProps = parseURL(url, info); if (parsedProps == null) { return null; } String hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY); List hostList = null; if (hostValues != null) { hostList = StringUtils.split(hostValues, ",", true); } if (hostList == null) { hostList = new ArrayList(); hostList.add("localhost:3306"); } LoadBalancingConnectionProxy proxyBal = new LoadBalancingConnectionProxy( hostList, parsedProps); return (java.sql.Connection) java.lang.reflect.Proxy.newProxyInstance(this .getClass().getClassLoader(), new Class[] { java.sql.Connection.class }, proxyBal); } private java.sql.Connection connectReplicationConnection(String url, Properties info) throws SQLException { Properties parsedProps = parseURL(url, info); if (parsedProps == null) { return null; } Properties masterProps = (Properties) parsedProps.clone(); Properties slavesProps = (Properties) parsedProps.clone(); // Marker used for further testing later on, also when // debugging slavesProps.setProperty("com.mysql.jdbc.ReplicationConnection.isSlave", "true"); String hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY); if (hostValues != null) { StringTokenizer st = new StringTokenizer(hostValues, ","); StringBuffer masterHost = new StringBuffer(); StringBuffer slaveHosts = new StringBuffer(); if (st.hasMoreTokens()) { String[] hostPortPair = parseHostPortPair(st.nextToken()); if (hostPortPair[HOST_NAME_INDEX] != null) { masterHost.append(hostPortPair[HOST_NAME_INDEX]); } if (hostPortPair[PORT_NUMBER_INDEX] != null) { masterHost.append(":"); masterHost.append(hostPortPair[PORT_NUMBER_INDEX]); } } boolean firstSlaveHost = true; while (st.hasMoreTokens()) { String[] hostPortPair = parseHostPortPair(st.nextToken()); if (!firstSlaveHost) { slaveHosts.append(","); } else { firstSlaveHost = false; } if (hostPortPair[HOST_NAME_INDEX] != null) { slaveHosts.append(hostPortPair[HOST_NAME_INDEX]); } if (hostPortPair[PORT_NUMBER_INDEX] != null) { slaveHosts.append(":"); slaveHosts.append(hostPortPair[PORT_NUMBER_INDEX]); } } if (slaveHosts.length() == 0) { throw SQLError .createSQLException(
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -