P6Spy源码分析-属性⽂件加载
P6Spy使⽤:[url]donald-draper.iteye/blog/2319646[/url]
使⽤P6Spy的时候⽤到这⼀句我们来看这⼀句的内涵:
P6DataSource p6DSource = new P6DataSource(cpDSource)
// P6DataSource p6DSource = new P6DataSource(cpDSource)
public class P6DataSource extends P6Base
implements DataSource, Referenceable, Serializable
{
//source是通过构造传⼊的数据源c3p0或Druid
public P6DataSource(DataSource source)
{
rds = source;
}
//初始化驱动及⽇志模块
public static void initMethod()
{
P6SpyDriverCore.initMethod((ine.spy.P6SpyDriver.class).getName());
}
//获取Connection
public Connection getConnection()
throws SQLException
{
数组去重的方法filter
if(rds == null)
bindDataSource();
return P6SpyDriverCore.Connection());
}
protected DataSource rds;
protected String rdsName;
//通过static语句块调⽤初始化⽅法
static
{
initMethod();
}
}
初始化属性线路:初始化驱动及⽇志相关信息
P6DataSource,通过static语句块调⽤初始化⽅法initMethod(),initMethod()中⼀句很重要的
P6SpyDriverCore.initMethod((ine.spy.P6SpyDriver.class).getName());再来看看P6SpyDriverCore 的initMethod的⽅法都做了什么事情
public abstract class P6SpyDriverCore
implements Driver
{
public static synchronized void initMethod(String spydriver)
{
if(initialized)
return;
String path = PropertiesPath();
if(path == null)
{
foundSpyProperties = false;
return;
}
foundSpyProperties = true;
//初始化spy.properties属性⽂件
P6SpyProperties properties = new P6SpyProperties();
P6SpyOptions coreOptions = new P6SpyOptions();
OptionReloader.add(coreOptions, properties);
String className = "no class";
String classType = "driver";
try
try
{
//realdriver
ArrayList driverNames = null;
//⽇志模块
ArrayList modules = null;
//获取驱动名
driverNames = P6SpyOptions.allDriverNames();
/
/获取所有⽇志模块
modules = P6SpyOptions.allModules();
boolean hasModules = modules.size() > 0;
Iterator i = null;
classType = "driver";
Driver realDriver;
for(i = driverNames.iterator(); i.hasNext(); P6LogQuery.logDebug("Registered driver: " + className + ", realdriver: " + realDriver))
properties是什么文件{
P6SpyDriver spy = null;
if(hasModules)
{
spy = new P6SpyDriver();
}
className = (();
deregister(className);
realDriver = (Driver)P6Util.forName(className).newInstance();
DeregisterDrivers())
//注册驱动sql.jdbc.Driver
if(hasModules)
{
spy.setPassthru(realDriver);
realDrivers.add(realDriver);
}
}
if(hasModules)
{
factories = new ArrayList();
classType = "factory";
inemon.P6Options options;
for(i = modules.iterator(); i.hasNext(); P6LogQuery.logDebug("Registered factory: " + className + " with options: " + options))
{
className = (();
//module.log=ine.logging.P6LogFactory
//module.outage=ine.outage.P6OutageFactory
P6Factory factory = (P6Factory)P6Util.forName(className).newInstance();
factories.add(factory);
options = Options();
if(options != null)
OptionReloader.add(options, properties);
}
}
initialized = true;
for(Enumeration e = Drivers(); e.hasMoreElements(); P6LogQuery.logDebug("Driver manager reporting driver registered: " + e.nextEle        }
catch(Exception e)
{
String err = "Error registering " + classType + "  [" + className + "]\nCaused By: " + e.toString();
P6LogQuery.logError(err);
throw new P6DriverNotFoundError(err);
}
}
//P6DataSource的getConnection⽅法条⽤P6SpyDriverCore的wrapConnection(Connection realConnection)⽅法
public static Connection wrapConnection(Connection realConnection)
throws SQLException
throws SQLException
{
Connection con = realConnection;
if(factories != null)
{
for(Iterator it = factories.iterator(); it.hasNext();)
{
P6Factory factory = (();
//这⾥是重点,这⾥是通过P6Factory来获取连接,P6SpyDriverCore
//在初始化initMethod已经P6LogFactory,P6OutageFactory
/
/module.log=ine.logging.P6LogFactory
//module.outage=ine.outage.P6OutageFactory
con = Connection(con);
}
}
return con;
}
private String getRealUrl(String url)
{
UsePrefix())
return url.startsWith("p6spy:") ? url.substring("p6spy:".length()) : null;
else
return url;
}
public boolean acceptsURL(String p0)
throws SQLException
{
String realUrl = getRealUrl(p0);
boolean accepts = false;
if(passthru == null && initialized)
{
if(realDrivers.size() == 0)
throw new SQLException("P6 has no drivers registered");
findPassthru(realUrl);
if(passthru == null)
throw new SQLException("P6 can't find a driver to accept url (" + realUrl + ") from the " + realDrivers.size() + " drivers P6 knows about. The current drive        }
if(realUrl != null)
accepts = passthru.acceptsURL(realUrl);
return accepts;
}
protected Driver passthru;
protected static boolean initialized = false;
protected static ArrayList factories;
[color=red] protected static ArrayList realDrivers = new ArrayList();[/color]
protected static boolean foundSpyProperties;
}
这⾥我们先来看⼀下p6spy的属性配置,再看P6LogFactory获取连接及⽇志
P6SpyProperties属性⽂件操作类:
public class P6SpyProperties
{
public static void initMethod()
{
//设置属性⽂件名
Property("spy.properties", "spy.properties"));
}
public P6SpyProperties()
{
File propertiesFile = new File(propertiesPath);
ists())
{
long lastModified = propertiesFile.lastModified();
if(lastModified != propertiesLastModified)
{
wordpress建站教程网propertiesLastModified = lastModified;
//加载spy.properties⽂件
properties = P6Util.loadProperties(SPY_PROPERTIES_FILE);
} else
{
properties = null;
}
}
}
public static String getPropertiesPath()
{
return propertiesPath;
}
/
plsql哪个版本最好用
/设置属性⽂件名
public static void setSpyProperties(String _properties)
{
SPY_PROPERTIES_FILE = _properties != null ? _properties : "spy.properties";        propertiesPath = findPropertiesPath();
propertiesLastModified = -1L;
}
protected static String findPropertiesPath()
{
String propertiesPath = P6Util.classPathFile(SPY_PROPERTIES_FILE);
if(propertiesPath != null)
{
File propertiesFile = new File(propertiesPath);
ists())
return propertiesPath;
}
return null;
}
quickedit高级版破解
public Properties forceReadProperties()
{
File propertiesFile = new File(propertiesPath);
ists())
{
long lastModified = propertiesFile.lastModified();
properties = P6Util.loadProperties(SPY_PROPERTIES_FILE);
}
return properties;
}
protected static final String OPTIONS_FILE_PROPERTY = "spy.properties";
protected static final String DFLT_OPTIONS_FILE = "spy.properties";
protected static String SPY_PROPERTIES_FILE;
protected static long propertiesLastModified = -1L;
protected static String propertiesPath;
public Properties properties;
/
/通过静态语句块调⽤initMethod⽅法
static
{
initMethod();
}
}
P6SpyOptions配置选项类:set,get⽅法对应配置⽂件选项
public class P6SpyOptions extends P6Options
{
public static void setAutoflush(String _autoflush)
{
autoflush = P6Util.isTrue(_autoflush, false);
}
public static boolean getAutoflush()
{
return autoflush;
}
public static void setExclude(String _exclude)
{
exclude = _exclude;
}
public static void setExcludecategories(String _excludecategories)    {
excludecategories = _excludecategories;
}
public static void setFilter(String _filter)
{
filter = P6Util.isTrue(_filter, false);
}
public static void setIncludecategories(String _includecategories)    {
includecategories = _includecategories;
}
public static void setDeregisterDrivers(String trueOrFalse)
{
deregister = P6Util.isTrue(trueOrFalse, false);
}
public static void setLogfile(String _logfile)
{
logfile = _logfile;
if(logfile == null)
logfile = "spy.log";
}
public static void setAppender(String className)
{
appender = className;
}
//实际驱动设置
public static void setRealdriver(String _realdriver)
{
realdriver = _realdriver;
图片边框素材网
}
public static void setRealdriver2(String _realdriver2)
{
realdriver2 = _realdriver2;
}
public static void setAppend(String _append)
{
append = P6Util.isTrue(_append, true);
}
public static boolean getAppend()
{
return append;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。