?? dbdriver.java
字號:
/* ===========================================================
* JDBMonitor : a flexiable JDBC Monitor for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2006-2006, by yang zhongke
*
* Project Info: http://www.cownew.com
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* DBDriver.java
* ---------------
* (C) Copyright 2006-2006, by yang zhongke
*
* Original Author: yang zhongke;
*
* Changes
* -------
*
*/
package com.cownew.JDBMonitor.jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.cownew.JDBMonitor.common.CommonUtils;
import com.cownew.JDBMonitor.common.ConfigFileInfo;
import com.cownew.JDBMonitor.common.ConfigFileReader;
import com.cownew.JDBMonitor.common.DBListenerInfo;
import com.cownew.JDBMonitor.common.InstanceUtils;
/**
* the JDBC driver of the DBMonitor
* @author yang zhongke
*/
public class DBDriver implements java.sql.Driver
{
public synchronized Connection connect(String url, Properties info)
throws SQLException {
if(!acceptsURL(url))
{
throw new SQLException("Illegal URL Format:"+url);
}
Matcher match = getURLMatcher(url);
match.matches();
match.group();
String configFile = match.group(1);
String realUrl = match.group(2);
ConfigFileInfo configInfo = null;
InputStream is = null;
try
{
//get the configFileInfo from configFile
if(ConfigFileReader.isRealFilePath(configFile))
{
is = new FileInputStream(configFile);
}
else
{
is = this.getClass().getResourceAsStream(configFile);
}
configInfo = ConfigFileReader.getConfigFileInfo(is);
//register the necessary JDBCDriver
//generally, it's the real JdbcDriver of the database to be logged
registerRealJdbcDriver(configInfo);
} catch (FileNotFoundException e)
{
throw JdbcUtils.toSQLException(e);
}
finally
{
InstanceUtils.closeInStream(is);
}
//get the real database connection of the database to be logged
Connection cn = DriverManager.getConnection(realUrl, info);
//if the monitor is not active,return the real database connection
if(!configInfo.isActive())
{
return cn;
}
List lisList = configInfo.getListenerInfoList();
DBListenerInfo[] dbListenerInfos = new DBListenerInfo[lisList.size()];
for(int i=0,n=lisList.size();i<n;i++)
{
DBListenerInfo lisInfo = (DBListenerInfo) lisList.get(i);
dbListenerInfos[i] = lisInfo;
}
return new DBConnection(cn,dbListenerInfos);
}
/**
* get the regular expression matcher of the jdbcurl
* @param url
* @return
*/
private Matcher getURLMatcher(String url)
{
Pattern pattern = Pattern.compile("listenerconfig=(.+):url=(.+)");
Matcher match = pattern.matcher(url);
return match;
}
private synchronized void registerRealJdbcDriver(ConfigFileInfo configInfo)
throws SQLException
{
List list = configInfo.getRealJdbcDriverList();
for (int i = 0, n = list.size(); i < n; i++)
{
try
{
Class.forName(list.get(i).toString());
} catch (ClassNotFoundException e)
{
throw JdbcUtils.toSQLException(e);
}
}
}
public boolean acceptsURL(String url) throws SQLException {
Matcher m = getURLMatcher(url);
return m.matches();
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException
{
DriverPropertyInfo[] props = new DriverPropertyInfo[1];
props[0] = new DriverPropertyInfo("author", "Yang Zhongke");
return props;
}
public int getMajorVersion() {
return 1;
}
public int getMinorVersion() {
return 0;
}
public boolean jdbcCompliant() {
return true;
}
static
{
try
{
DriverManager.registerDriver(new DBDriver());
} catch (Exception e)
{
throw CommonUtils.toRuntimeException(e);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -