driverconfig.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 824 行 · 第 1/2 页

JAVA
824
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.sql;import com.caucho.config.program.ConfigProgram;import com.caucho.config.program.ContainerProgram;import com.caucho.config.program.PropertyStringProgram;import com.caucho.config.Config;import com.caucho.config.ConfigException;import com.caucho.config.program.PropertyValueProgram;import com.caucho.config.types.InitParam;import com.caucho.naming.Jndi;import com.caucho.tools.profiler.ConnectionPoolDataSourceWrapper;import com.caucho.tools.profiler.DriverWrapper;import com.caucho.tools.profiler.ProfilerPoint;import com.caucho.tools.profiler.ProfilerPointConfig;import com.caucho.tools.profiler.XADataSourceWrapper;import com.caucho.util.Alarm;import com.caucho.util.L10N;import com.caucho.lifecycle.Lifecycle;import javax.annotation.PostConstruct;import javax.resource.spi.ManagedConnectionFactory;import javax.sql.ConnectionPoolDataSource;import javax.sql.PooledConnection;import javax.sql.XADataSource;import java.lang.reflect.*;import java.sql.Connection;import java.sql.Driver;import java.sql.SQLException;import java.util.HashMap;import java.util.Iterator;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;/** * Configures the database driver. */public class DriverConfig{  protected static final Logger log    = Logger.getLogger(DriverConfig.class.getName());  private static final L10N L = new L10N(DriverConfig.class);  private static final int TYPE_UNKNOWN = 0;  private static final int TYPE_DRIVER = 1;  private static final int TYPE_POOL = 2;  private static final int TYPE_XA = 3;  private static final int TYPE_JCA = 4;  /**   * The beginning of the URL used to connect to a database with   * this pooled connection driver.   */  private static final String URL_PREFIX = "jdbc:caucho:" ;  /**   * The key used to look into the properties passed to the   * connect method to find the username.   */  public static final String PROPERTY_USER = "user" ;  /**   * The key used to look into the properties passed to the   * connect method to find the password.   */  public static final String PROPERTY_PASSWORD = "password" ;  private DBPoolImpl _dbPool;  private Class _driverClass;  private String _driverURL;  private String _user;  private String _password;  private Properties _info;  private ContainerProgram _init = new ContainerProgram();  private int _driverType;  private Object _driverObject;  private ManagedConnectionFactory _jcaDataSource;  private ConnectionPoolDataSource _poolDataSource;  private XADataSource _xaDataSource;  private Driver _driver;  private Lifecycle _lifecycle = new Lifecycle();  private DriverAdmin _admin = new DriverAdmin(this);  // statistics  private long _connectionCountTotal;  private long _connectionFailCountTotal;  private long _lastFailTime;  private ProfilerPoint _profilerPoint;  /**   * Null constructor for the Driver interface; called by the JNDI   * configuration.  Applications should not call this directly.   */  public DriverConfig(DBPoolImpl pool)  {    _dbPool = pool;    _info = new Properties();  }  /**   * Returns the DBPool.   */  public DBPoolImpl getDBPool()  {    return _dbPool;  }  /**   * Sets the driver as data source.   */  public void setDriverType(String type)    throws ConfigException  {    if ("ConnectionPoolDataSource".equals(type)) {      _driverType = TYPE_POOL;    }    else if ("XADataSource".equals(type)) {      _driverType = TYPE_XA;    }    else if ("ManagedConnectionFactory".equals(type)) {      _driverType = TYPE_JCA;    }    else if ("Driver".equals(type)) {      _driverType = TYPE_DRIVER;    }    else if (hasDriverTypeMethod(_driverClass)) {      _init.addProgram(new PropertyStringProgram("driverType", type));    }    else {      throw new ConfigException(L.l("'{0}' is an unknown driver type. Valid types are 'ConnectionPoolDataSource', 'XADataSource' and 'Driver'"));    }  }  private boolean hasDriverTypeMethod(Class cl)  {    if (cl == null)      return false;    for (Method method : cl.getMethods()) {      if (method.getName().equals("setDriverType")	  && method.getParameterTypes().length == 1) {	return true;      }    }    return false;  }  /**   * Sets the driver as data source.   */  public void setDataSource(Object dataSource)    throws ConfigException  {    if (dataSource instanceof String)      dataSource = Jndi.lookup((String) dataSource);    if (_driverType == TYPE_XA)      _xaDataSource = (XADataSource) dataSource;    else if (_driverType == TYPE_POOL)      _poolDataSource = (ConnectionPoolDataSource) dataSource;    else if (dataSource instanceof XADataSource)      _xaDataSource = (XADataSource) dataSource;    else if (dataSource instanceof ConnectionPoolDataSource)      _poolDataSource = (ConnectionPoolDataSource) dataSource;    else if (dataSource instanceof ManagedConnectionFactory)      _jcaDataSource = (ManagedConnectionFactory) dataSource;    else      throw new ConfigException(L.l("data-source '{0}' is of type '{1}' which does not implement XADataSource or ConnectionPoolDataSource.",                                    dataSource,                                    dataSource.getClass().getName()));  }  /**   * Returns the JDBC driver class for the pooled object.   */  public Class getDriverClass()  {    return _driverClass;  }  /**   * Sets the JDBC driver class underlying the pooled object.   */  public void setType(Class driverClass)    throws ConfigException  {    _driverClass = driverClass;    if (! Driver.class.isAssignableFrom(driverClass)	&& ! XADataSource.class.isAssignableFrom(driverClass)	&& ! ConnectionPoolDataSource.class.isAssignableFrom(driverClass)	&& ! ManagedConnectionFactory.class.isAssignableFrom(driverClass))      throw new ConfigException(L.l("'{0}' is not a valid database type.",                                    driverClass.getName()));    Config.checkCanInstantiate(driverClass);  }  public String getType()  {    return _driverClass.getName();  }  /**   * Returns the connection's JDBC url.   */  public String getURL()  {    return _driverURL;  }  /**   * Sets the connection's JDBC url.   */  public void setURL(String url)  {    _driverURL = url;    _lifecycle.setName("JdbcDriver[" + url + "]");  }  /**   * Adds to the builder program.   */  public void addBuilderProgram(ConfigProgram program)  {    _init.addProgram(program);  }  /**   * Returns the connection's user.   */  public String getUser()  {    return _user;  }  /**   * Sets the connection's user.   */  public void setUser(String user)  {    _user = user;  }  /**   * Returns the connection's password   */  public String getPassword()  {    return _password;  }  /**   * Sets the connection's password   */  public void setPassword(String password)  {    _password = password;  }  /**   * Sets a property from the underlying driver.  Used to set driver   * properties not handled by DBPool.   *   * @param name property name for the driver   * @param value the driver's value of the property name   */  public void setInitParam(InitParam initParam)  {    validateInitParam();        HashMap<String,String> paramMap = initParam.getParameters();    Iterator<String> iter = paramMap.keySet().iterator();    while (iter.hasNext()) {      String key = iter.next();      _info.setProperty(key, paramMap.get(key));    }  }  /**   * Sets a property from the underlying driver.  Used to set driver   * properties not handled by DBPool.   *   * @param name property name for the driver   * @param value the driver's value of the property name   */  public void setInitParam(String key, String value)  {    _info.setProperty(key, value);  }  /**   * Returns the properties.   */  public Properties getInfo()  {    return _info;  }  /**   * Returns the driver object.   */  public Driver getDriver()    throws SQLException  {    Object obj = getDriverObject();    if (obj instanceof Driver)      return (Driver) obj;    else      return null;  }  /**   * Sets the driver object.   */  public void setDriver(Driver driver)    throws SQLException  {    _driver = driver;    _driverObject = driver;  }  /**   * Returns the driver pool.   */  public ConnectionPoolDataSource getPoolDataSource()    throws SQLException  {    return _poolDataSource;  }  /**   * Sets the pooled data source driver.   */  public void setPoolDataSource(ConnectionPoolDataSource pDataSource)    throws SQLException  {    _poolDataSource = pDataSource;    _driverObject = _poolDataSource;  }  /**   * Returns any XADataSource.   */  public XADataSource getXADataSource()  {    return _xaDataSource;  }  /**   * Sets the xa data source driver.   */  public void setXADataSource(XADataSource xaDataSource)    throws SQLException  {    _xaDataSource = xaDataSource;    _driverObject = _xaDataSource;  }  /**   * Returns the managed connection factory.   */  public ManagedConnectionFactory getManagedConnectionFactory()  {    return _jcaDataSource;  }  /**   * Returns true if the driver is XA enabled.   */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?