摘自c3p0.jar,方便查看
//c3p0.properties
#
# This file is detritus from various testing attempts
# the values below may change , and often do not represent
# reasonable values for the parameters.
#
#c3p0.testConnectionOnCheckout = true
#c3p0.testConnectionOnCheckin = true
#c3p0.minPoolSize = 3
#c3p0.maxPoolSize = 20
#c3p0.checkoutTimeout = 2000
#c3p0.idleConnectionTestPeriod = 5
#c3p0.maxConnectionAge = 10
#c3p0.maxIdleTime = 2
#c3p0.maxIdleTimeExcessConnections = 1
#c3p0.propertyCycle = 1
#c3p0.numHelperThreads = 10
#c3p0.unreturnedConnectionTimeout = 15
#c3p0.debugUnreturnedConnectionStackTraces = true
#c3p0.maxStatements = 30
#c3p0.maxStatementsPerConnection = 5
#c3p0.maxAdministrativeTaskTime = 3
#c3p0.preferredTestQuery = SELECT 1
#c3p0.preferredTestQuery = SELECT a FROM emptyyukyuk WHERE a = 5
#c3p0.preferredTestQuery = SELECT a FROM testpbds WHERE a = 5
#c3p0.usesTraditionalReflectiveProxies = true
#c3p0.automaticTestTable = PoopyTestTable
#c3p0.acquireIncrement = 4
#c3p0.acquireRetryDelay = 1000
#c3p0.acquireRetryAttempts = 60
#c3p0.connectionTesterClassName = com.mchange.v2.c3p0.test.AlwaysFailConnectionTester
#c3p0.initialPoolSize = 10
c3p0.jdbcUrl = jdbc:postgresql://localhost/c3p0-test
c3p0.driverClass = org.postgresql.Driver
c3p0.user = swaldman
c3p0.password = test
#c3p0.user = poop
#c3p0.password = scoop
#com.mchange.v2.log.MLog = com.mchange.v2.log.log4j.Log4jMLog
#com.mchange.v2.log.MLog = com.mchange.v2.log.jdk14logging.Jdk14MLog
#com.mchange.v2.log.MLog = com.mchange.v2.log.FallbackMLog
#com.mchange.v2.log.NameTransformer = com.mchange.v2.log.PackageNames
#com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL = ALL
#com.mchange.v2.c3p0.VMID = poop
# This file is detritus from various testing attempts
# the values below may change , and often do not represent
# reasonable values for the parameters.
#
#c3p0.testConnectionOnCheckout = true
#c3p0.testConnectionOnCheckin = true
#c3p0.minPoolSize = 3
#c3p0.maxPoolSize = 20
#c3p0.checkoutTimeout = 2000
#c3p0.idleConnectionTestPeriod = 5
#c3p0.maxConnectionAge = 10
#c3p0.maxIdleTime = 2
#c3p0.maxIdleTimeExcessConnections = 1
#c3p0.propertyCycle = 1
#c3p0.numHelperThreads = 10
#c3p0.unreturnedConnectionTimeout = 15
#c3p0.debugUnreturnedConnectionStackTraces = true
#c3p0.maxStatements = 30
#c3p0.maxStatementsPerConnection = 5
#c3p0.maxAdministrativeTaskTime = 3
#c3p0.preferredTestQuery = SELECT 1
#c3p0.preferredTestQuery = SELECT a FROM emptyyukyuk WHERE a = 5
#c3p0.preferredTestQuery = SELECT a FROM testpbds WHERE a = 5
#c3p0.usesTraditionalReflectiveProxies = true
#c3p0.automaticTestTable = PoopyTestTable
#c3p0.acquireIncrement = 4
#c3p0.acquireRetryDelay = 1000
#c3p0.acquireRetryAttempts = 60
#c3p0.connectionTesterClassName = com.mchange.v2.c3p0.test.AlwaysFailConnectionTester
#c3p0.initialPoolSize = 10
c3p0.jdbcUrl = jdbc:postgresql://localhost/c3p0-test
c3p0.driverClass = org.postgresql.Driver
c3p0.user = swaldman
c3p0.password = test
#c3p0.user = poop
#c3p0.password = scoop
#com.mchange.v2.log.MLog = com.mchange.v2.log.log4j.Log4jMLog
#com.mchange.v2.log.MLog = com.mchange.v2.log.jdk14logging.Jdk14MLog
#com.mchange.v2.log.MLog = com.mchange.v2.log.FallbackMLog
#com.mchange.v2.log.NameTransformer = com.mchange.v2.log.PackageNames
#com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL = ALL
#com.mchange.v2.c3p0.VMID = poop
//c3p0-config.xml
<
c3p0-config
>
< default-config >
<!-- <property name="automaticTestTable">con_test</property> -->
<!-- <property name="checkoutTimeout">30000</property> -->
<!-- <property name="idleConnectionTestPeriod">30</property> -->
<!-- <property name="initialPoolSize">10</property> -->
<!-- <property name="maxIdleTime">30</property> -->
<!-- <property name="maxIdleTimeExcessConnections">10</property> -->
<!-- <property name="maxConnectionAge">60</property> -->
<!-- <property name="propertyCycle">1</property> -->
<!-- <property name="maxPoolSize">25</property> -->
<!-- <property name="minPoolSize">5</property> -->
<!-- <property name="maxStatements">0</property> -->
<!-- <property name="maxStatementsPerConnection">5</property> -->
<!-- <property name="maxAdministrativeTaskTime">4</property> -->
<!-- <property name="connectionCustomizerClassName">com.mchange.v2.c3p0.test.TestConnectionCustomizer</property> -->
<!-- <property name="unreturnedConnectionTimeout">15</property> -->
<!-- <property name="debugUnreturnedConnectionStackTraces">true</property> -->
<!--
<user-overrides user="swaldman">
<property name="debugUnreturnedConnectionStackTraces">true</property>
</user-overrides>
-->
</ default-config >
<!--
<named-config name="dumbTestConfig">
<property name="maxStatements">200</property>
<property name="jdbcUrl">jdbc:test</property>
<user-overrides user="poop">
<property name="maxStatements">300</property>
</user-overrides>
</named-config>
-->
</ c3p0-config >
< default-config >
<!-- <property name="automaticTestTable">con_test</property> -->
<!-- <property name="checkoutTimeout">30000</property> -->
<!-- <property name="idleConnectionTestPeriod">30</property> -->
<!-- <property name="initialPoolSize">10</property> -->
<!-- <property name="maxIdleTime">30</property> -->
<!-- <property name="maxIdleTimeExcessConnections">10</property> -->
<!-- <property name="maxConnectionAge">60</property> -->
<!-- <property name="propertyCycle">1</property> -->
<!-- <property name="maxPoolSize">25</property> -->
<!-- <property name="minPoolSize">5</property> -->
<!-- <property name="maxStatements">0</property> -->
<!-- <property name="maxStatementsPerConnection">5</property> -->
<!-- <property name="maxAdministrativeTaskTime">4</property> -->
<!-- <property name="connectionCustomizerClassName">com.mchange.v2.c3p0.test.TestConnectionCustomizer</property> -->
<!-- <property name="unreturnedConnectionTimeout">15</property> -->
<!-- <property name="debugUnreturnedConnectionStackTraces">true</property> -->
<!--
<user-overrides user="swaldman">
<property name="debugUnreturnedConnectionStackTraces">true</property>
</user-overrides>
-->
</ default-config >
<!--
<named-config name="dumbTestConfig">
<property name="maxStatements">200</property>
<property name="jdbcUrl">jdbc:test</property>
<user-overrides user="poop">
<property name="maxStatements">300</property>
</user-overrides>
</named-config>
-->
</ c3p0-config >
//解析properties
/**/
/*
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v2.c3p0.cfg;
import java.io. * ;
import java.lang.reflect. * ;
import java.util. * ;
import com.mchange.v2.cfg. * ;
import com.mchange.v2.log. * ;
import com.mchange.v2.c3p0.impl. * ;
public final class C3P0ConfigUtils
... {
public final static String PROPS_FILE_RSRC_PATH = "/c3p0.properties";
public final static String PROPS_FILE_PROP_PFX = "c3p0.";
public final static int PROPS_FILE_PROP_PFX_LEN = 5;
private final static String[] MISSPELL_PFXS = ...{"/c3pO", "/c3po", "/C3P0", "/C3PO"};
final static MLogger logger = MLog.getLogger( C3P0ConfigUtils.class );
static
...{
if ( logger.isLoggable(MLevel.WARNING) && C3P0ConfigUtils.class.getResource( PROPS_FILE_RSRC_PATH ) == null )
...{
// warn on a misspelling... its an ugly way to do this, but since resources are not listable...
for (int i = 0; i < MISSPELL_PFXS.length; ++i)
...{
String test = MISSPELL_PFXS[i] + ".properties";
if (C3P0ConfigUtils.class.getResource( MISSPELL_PFXS[i] + ".properties" ) != null)
...{
logger.warning("POSSIBLY MISSPELLED c3p0.properties CONFIG RESOURCE FOUND. " +
"Please ensure the file name is c3p0.properties, all lower case, " +
"with the digit 0 (NOT the letter O) in c3p0. It should be placed " +
" in the top level of c3p0's effective classpath.");
break;
}
}
}
}
public static HashMap extractHardcodedC3P0Defaults(boolean stringify)
...{
HashMap out = new HashMap();
try
...{
Method[] methods = C3P0Defaults.class.getMethods();
for (int i = 0, len = methods.length; i < len; ++i)
...{
Method m = methods[i];
int mods = m.getModifiers();
if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) != 0 && m.getParameterTypes().length == 0)
...{
if (stringify)
...{
Object val = m.invoke( null, null );
if ( val != null )
out.put( m.getName(), String.valueOf( val ) );
}
else
out.put( m.getName(), m.invoke( null, null ) );
}
}
}
catch (Exception e)
...{
logger.log( MLevel.WARNING, "Failed to extract hardcoded default config!?", e );
}
return out;
}
public static HashMap extractHardcodedC3P0Defaults()
...{ return extractHardcodedC3P0Defaults( true ); }
public static HashMap extractC3P0PropertiesResources()
...{
HashMap out = new HashMap();
// Properties props = findResourceProperties();
// props.putAll( findAllC3P0Properties() );
Properties props = findAllC3P0Properties();
for (Iterator ii = props.keySet().iterator(); ii.hasNext(); )
...{
String key = (String) ii.next();
String val = (String) props.get(key);
if ( key.startsWith(PROPS_FILE_PROP_PFX) )
out.put( key.substring(PROPS_FILE_PROP_PFX_LEN).trim(), val.trim() );
}
return out;
}
public static C3P0Config configFromFlatDefaults(HashMap flatDefaults)
...{
NamedScope defaults = new NamedScope();
defaults.props.putAll( flatDefaults );
HashMap configNamesToNamedScopes = new HashMap();
return new C3P0Config( defaults, configNamesToNamedScopes );
}
public static String getPropFileConfigProperty( String prop )
...{ return MultiPropertiesConfig.readVmConfig().getProperty( prop ); }
private static Properties findResourceProperties()
...{ return MultiPropertiesConfig.readVmConfig().getPropertiesByResourcePath(PROPS_FILE_RSRC_PATH); }
private static Properties findAllC3P0Properties()
...{ return MultiPropertiesConfig.readVmConfig().getPropertiesByPrefix("c3p0"); }
static Properties findAllC3P0SystemProperties()
...{
Properties out = new Properties();
SecurityException sampleExc = null;
try
...{
for (Iterator ii = C3P0Defaults.getKnownProperties().iterator(); ii.hasNext(); )
...{
String key = (String) ii.next();
String prefixedKey = "c3p0." + key;
String value = System.getProperty( prefixedKey );
if (value != null && value.trim().length() > 0)
out.put( key, value );
}
}
catch (SecurityException e)
...{ sampleExc = e; }
return out;
}
private C3P0ConfigUtils()
...{}
}
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v2.c3p0.cfg;
import java.io. * ;
import java.lang.reflect. * ;
import java.util. * ;
import com.mchange.v2.cfg. * ;
import com.mchange.v2.log. * ;
import com.mchange.v2.c3p0.impl. * ;
public final class C3P0ConfigUtils
... {
public final static String PROPS_FILE_RSRC_PATH = "/c3p0.properties";
public final static String PROPS_FILE_PROP_PFX = "c3p0.";
public final static int PROPS_FILE_PROP_PFX_LEN = 5;
private final static String[] MISSPELL_PFXS = ...{"/c3pO", "/c3po", "/C3P0", "/C3PO"};
final static MLogger logger = MLog.getLogger( C3P0ConfigUtils.class );
static
...{
if ( logger.isLoggable(MLevel.WARNING) && C3P0ConfigUtils.class.getResource( PROPS_FILE_RSRC_PATH ) == null )
...{
// warn on a misspelling... its an ugly way to do this, but since resources are not listable...
for (int i = 0; i < MISSPELL_PFXS.length; ++i)
...{
String test = MISSPELL_PFXS[i] + ".properties";
if (C3P0ConfigUtils.class.getResource( MISSPELL_PFXS[i] + ".properties" ) != null)
...{
logger.warning("POSSIBLY MISSPELLED c3p0.properties CONFIG RESOURCE FOUND. " +
"Please ensure the file name is c3p0.properties, all lower case, " +
"with the digit 0 (NOT the letter O) in c3p0. It should be placed " +
" in the top level of c3p0's effective classpath.");
break;
}
}
}
}
public static HashMap extractHardcodedC3P0Defaults(boolean stringify)
...{
HashMap out = new HashMap();
try
...{
Method[] methods = C3P0Defaults.class.getMethods();
for (int i = 0, len = methods.length; i < len; ++i)
...{
Method m = methods[i];
int mods = m.getModifiers();
if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) != 0 && m.getParameterTypes().length == 0)
...{
if (stringify)
...{
Object val = m.invoke( null, null );
if ( val != null )
out.put( m.getName(), String.valueOf( val ) );
}
else
out.put( m.getName(), m.invoke( null, null ) );
}
}
}
catch (Exception e)
...{
logger.log( MLevel.WARNING, "Failed to extract hardcoded default config!?", e );
}
return out;
}
public static HashMap extractHardcodedC3P0Defaults()
...{ return extractHardcodedC3P0Defaults( true ); }
public static HashMap extractC3P0PropertiesResources()
...{
HashMap out = new HashMap();
// Properties props = findResourceProperties();
// props.putAll( findAllC3P0Properties() );
Properties props = findAllC3P0Properties();
for (Iterator ii = props.keySet().iterator(); ii.hasNext(); )
...{
String key = (String) ii.next();
String val = (String) props.get(key);
if ( key.startsWith(PROPS_FILE_PROP_PFX) )
out.put( key.substring(PROPS_FILE_PROP_PFX_LEN).trim(), val.trim() );
}
return out;
}
public static C3P0Config configFromFlatDefaults(HashMap flatDefaults)
...{
NamedScope defaults = new NamedScope();
defaults.props.putAll( flatDefaults );
HashMap configNamesToNamedScopes = new HashMap();
return new C3P0Config( defaults, configNamesToNamedScopes );
}
public static String getPropFileConfigProperty( String prop )
...{ return MultiPropertiesConfig.readVmConfig().getProperty( prop ); }
private static Properties findResourceProperties()
...{ return MultiPropertiesConfig.readVmConfig().getPropertiesByResourcePath(PROPS_FILE_RSRC_PATH); }
private static Properties findAllC3P0Properties()
...{ return MultiPropertiesConfig.readVmConfig().getPropertiesByPrefix("c3p0"); }
static Properties findAllC3P0SystemProperties()
...{
Properties out = new Properties();
SecurityException sampleExc = null;
try
...{
for (Iterator ii = C3P0Defaults.getKnownProperties().iterator(); ii.hasNext(); )
...{
String key = (String) ii.next();
String prefixedKey = "c3p0." + key;
String value = System.getProperty( prefixedKey );
if (value != null && value.trim().length() > 0)
out.put( key, value );
}
}
catch (SecurityException e)
...{ sampleExc = e; }
return out;
}
private C3P0ConfigUtils()
...{}
}
//解析xml
/**/
/*
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v2.c3p0.cfg;
import java.io. * ;
import java.util. * ;
import javax.xml.parsers. * ;
import org.w3c.dom. * ;
import com.mchange.v2.log. * ;
import com.mchange.v1.xml.DomParseUtils;
public final class C3P0ConfigXmlUtils
... {
public final static String XML_CONFIG_RSRC_PATH = "/c3p0-config.xml";
final static MLogger logger = MLog.getLogger( C3P0ConfigXmlUtils.class );
public final static String LINESEP;
private final static String[] MISSPELL_PFXS = ...{"/c3p0", "/c3pO", "/c3po", "/C3P0", "/C3PO"};
private final static char[] MISSPELL_LINES = ...{'-', '_'};
private final static String[] MISSPELL_CONFIG = ...{"config", "CONFIG"};
private final static String[] MISSPELL_XML = ...{"xml", "XML"};
// its an ugly way to do this, but since resources are not listable...
//
// this is only executed once, and does about 40 tests (for now)
// should I care about the cost in initialization time?
//
// should only be run if we've checked for the correct file, but
// not found it
private final static void warnCommonXmlConfigResourceMisspellings()
...{
if (logger.isLoggable( MLevel.WARNING) )
...{
for (int a = 0, lena = MISSPELL_PFXS.length; a < lena; ++a)
...{
StringBuffer sb = new StringBuffer(16);
sb.append( MISSPELL_PFXS[a] );
for (int b = 0, lenb = MISSPELL_LINES.length; b < lenb; ++b)
...{
sb.append(MISSPELL_LINES[b]);
for (int c = 0, lenc = MISSPELL_CONFIG.length; c < lenc; ++c)
...{
sb.append(MISSPELL_CONFIG[c]);
sb.append('.');
for (int d = 0, lend = MISSPELL_XML.length; d < lend; ++d)
...{
sb.append(MISSPELL_XML[d]);
String test = sb.toString();
if (!test.equals(XML_CONFIG_RSRC_PATH))
...{
Object hopefullyNull = C3P0ConfigXmlUtils.class.getResource( test );
if (hopefullyNull != null)
...{
logger.warning("POSSIBLY MISSPELLED c3p0-conf.xml RESOURCE FOUND. " +
"Please ensure the file name is c3p0-config.xml, all lower case, " +
"with the digit 0 (NOT the letter O) in c3p0. It should be placed " +
" in the top level of c3p0's effective classpath.");
return;
}
}
}
}
}
}
}
}
static
...{
String ls;
try
...{ ls = System.getProperty("line.separator", " "); }
catch (Exception e)
...{ ls = " "; }
LINESEP = ls;
}
public static C3P0Config extractXmlConfigFromDefaultResource() throws Exception
...{
InputStream is = null;
try
...{
is = C3P0ConfigUtils.class.getResourceAsStream(XML_CONFIG_RSRC_PATH);
if ( is == null )
...{
warnCommonXmlConfigResourceMisspellings();
return null;
}
else
return extractXmlConfigFromInputStream( is );
}
finally
...{
try ...{ if (is != null) is.close(); }
catch (Exception e)
...{
if ( logger.isLoggable( MLevel.FINE ) )
logger.log(MLevel.FINE,"Exception on resource InputStream close.", e);
}
}
}
public static C3P0Config extractXmlConfigFromInputStream(InputStream is) throws Exception
...{
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse( is );
return extractConfigFromXmlDoc(doc);
}
public static C3P0Config extractConfigFromXmlDoc(Document doc) throws Exception
...{
Element docElem = doc.getDocumentElement();
if (docElem.getTagName().equals("c3p0-config"))
...{
NamedScope defaults;
HashMap configNamesToNamedScopes = new HashMap();
Element defaultConfigElem = DomParseUtils.uniqueChild( docElem, "default-config" );
if (defaultConfigElem != null)
defaults = extractNamedScopeFromLevel( defaultConfigElem );
else
defaults = new NamedScope();
NodeList nl = DomParseUtils.immediateChildElementsByTagName(docElem, "named-config");
for (int i = 0, len = nl.getLength(); i < len; ++i)
...{
Element namedConfigElem = (Element) nl.item(i);
String configName = namedConfigElem.getAttribute("name");
if (configName != null && configName.length() > 0)
...{
NamedScope namedConfig = extractNamedScopeFromLevel( namedConfigElem );
configNamesToNamedScopes.put( configName, namedConfig);
}
else
logger.warning("Configuration XML contained named-config element without name attribute: " + namedConfigElem);
}
return new C3P0Config( defaults, configNamesToNamedScopes );
}
else
throw new Exception("Root element of c3p0 config xml should be 'c3p0-config', not '" + docElem.getTagName() + "'.");
}
private static NamedScope extractNamedScopeFromLevel(Element elem)
...{
HashMap props = extractPropertiesFromLevel( elem );
HashMap userNamesToOverrides = new HashMap();
NodeList nl = DomParseUtils.immediateChildElementsByTagName(elem, "user-overrides");
for (int i = 0, len = nl.getLength(); i < len; ++i)
...{
Element perUserConfigElem = (Element) nl.item(i);
String userName = perUserConfigElem.getAttribute("user");
if (userName != null && userName.length() > 0)
...{
HashMap userProps = extractPropertiesFromLevel( perUserConfigElem );
userNamesToOverrides.put( userName, userProps );
}
else
logger.warning("Configuration XML contained user-overrides element without user attribute: " + LINESEP + perUserConfigElem);
}
return new NamedScope(props, userNamesToOverrides);
}
private static HashMap extractPropertiesFromLevel(Element elem)
...{
// System.err.println( "extractPropertiesFromLevel()" );
HashMap out = new HashMap();
try
...{
NodeList nl = DomParseUtils.immediateChildElementsByTagName(elem, "property");
int len = nl.getLength();
for (int i = 0; i < len; ++i)
...{
Element propertyElem = (Element) nl.item(i);
String propName = propertyElem.getAttribute("name");
if (propName != null && propName.length() > 0)
...{
String propVal = DomParseUtils.allTextFromElement(propertyElem, true);
out.put( propName, propVal );
//System.err.println( propName + " -> " + propVal );
}
else
logger.warning("Configuration XML contained property element without name attribute: " + LINESEP + propertyElem);
}
}
catch (Exception e)
...{
logger.log( MLevel.WARNING,
"An exception occurred while reading config XML. " +
"Some configuration information has probably been ignored.",
e );
}
return out;
}
private C3P0ConfigXmlUtils()
...{}
}
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v2.c3p0.cfg;
import java.io. * ;
import java.util. * ;
import javax.xml.parsers. * ;
import org.w3c.dom. * ;
import com.mchange.v2.log. * ;
import com.mchange.v1.xml.DomParseUtils;
public final class C3P0ConfigXmlUtils
... {
public final static String XML_CONFIG_RSRC_PATH = "/c3p0-config.xml";
final static MLogger logger = MLog.getLogger( C3P0ConfigXmlUtils.class );
public final static String LINESEP;
private final static String[] MISSPELL_PFXS = ...{"/c3p0", "/c3pO", "/c3po", "/C3P0", "/C3PO"};
private final static char[] MISSPELL_LINES = ...{'-', '_'};
private final static String[] MISSPELL_CONFIG = ...{"config", "CONFIG"};
private final static String[] MISSPELL_XML = ...{"xml", "XML"};
// its an ugly way to do this, but since resources are not listable...
//
// this is only executed once, and does about 40 tests (for now)
// should I care about the cost in initialization time?
//
// should only be run if we've checked for the correct file, but
// not found it
private final static void warnCommonXmlConfigResourceMisspellings()
...{
if (logger.isLoggable( MLevel.WARNING) )
...{
for (int a = 0, lena = MISSPELL_PFXS.length; a < lena; ++a)
...{
StringBuffer sb = new StringBuffer(16);
sb.append( MISSPELL_PFXS[a] );
for (int b = 0, lenb = MISSPELL_LINES.length; b < lenb; ++b)
...{
sb.append(MISSPELL_LINES[b]);
for (int c = 0, lenc = MISSPELL_CONFIG.length; c < lenc; ++c)
...{
sb.append(MISSPELL_CONFIG[c]);
sb.append('.');
for (int d = 0, lend = MISSPELL_XML.length; d < lend; ++d)
...{
sb.append(MISSPELL_XML[d]);
String test = sb.toString();
if (!test.equals(XML_CONFIG_RSRC_PATH))
...{
Object hopefullyNull = C3P0ConfigXmlUtils.class.getResource( test );
if (hopefullyNull != null)
...{
logger.warning("POSSIBLY MISSPELLED c3p0-conf.xml RESOURCE FOUND. " +
"Please ensure the file name is c3p0-config.xml, all lower case, " +
"with the digit 0 (NOT the letter O) in c3p0. It should be placed " +
" in the top level of c3p0's effective classpath.");
return;
}
}
}
}
}
}
}
}
static
...{
String ls;
try
...{ ls = System.getProperty("line.separator", " "); }
catch (Exception e)
...{ ls = " "; }
LINESEP = ls;
}
public static C3P0Config extractXmlConfigFromDefaultResource() throws Exception
...{
InputStream is = null;
try
...{
is = C3P0ConfigUtils.class.getResourceAsStream(XML_CONFIG_RSRC_PATH);
if ( is == null )
...{
warnCommonXmlConfigResourceMisspellings();
return null;
}
else
return extractXmlConfigFromInputStream( is );
}
finally
...{
try ...{ if (is != null) is.close(); }
catch (Exception e)
...{
if ( logger.isLoggable( MLevel.FINE ) )
logger.log(MLevel.FINE,"Exception on resource InputStream close.", e);
}
}
}
public static C3P0Config extractXmlConfigFromInputStream(InputStream is) throws Exception
...{
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse( is );
return extractConfigFromXmlDoc(doc);
}
public static C3P0Config extractConfigFromXmlDoc(Document doc) throws Exception
...{
Element docElem = doc.getDocumentElement();
if (docElem.getTagName().equals("c3p0-config"))
...{
NamedScope defaults;
HashMap configNamesToNamedScopes = new HashMap();
Element defaultConfigElem = DomParseUtils.uniqueChild( docElem, "default-config" );
if (defaultConfigElem != null)
defaults = extractNamedScopeFromLevel( defaultConfigElem );
else
defaults = new NamedScope();
NodeList nl = DomParseUtils.immediateChildElementsByTagName(docElem, "named-config");
for (int i = 0, len = nl.getLength(); i < len; ++i)
...{
Element namedConfigElem = (Element) nl.item(i);
String configName = namedConfigElem.getAttribute("name");
if (configName != null && configName.length() > 0)
...{
NamedScope namedConfig = extractNamedScopeFromLevel( namedConfigElem );
configNamesToNamedScopes.put( configName, namedConfig);
}
else
logger.warning("Configuration XML contained named-config element without name attribute: " + namedConfigElem);
}
return new C3P0Config( defaults, configNamesToNamedScopes );
}
else
throw new Exception("Root element of c3p0 config xml should be 'c3p0-config', not '" + docElem.getTagName() + "'.");
}
private static NamedScope extractNamedScopeFromLevel(Element elem)
...{
HashMap props = extractPropertiesFromLevel( elem );
HashMap userNamesToOverrides = new HashMap();
NodeList nl = DomParseUtils.immediateChildElementsByTagName(elem, "user-overrides");
for (int i = 0, len = nl.getLength(); i < len; ++i)
...{
Element perUserConfigElem = (Element) nl.item(i);
String userName = perUserConfigElem.getAttribute("user");
if (userName != null && userName.length() > 0)
...{
HashMap userProps = extractPropertiesFromLevel( perUserConfigElem );
userNamesToOverrides.put( userName, userProps );
}
else
logger.warning("Configuration XML contained user-overrides element without user attribute: " + LINESEP + perUserConfigElem);
}
return new NamedScope(props, userNamesToOverrides);
}
private static HashMap extractPropertiesFromLevel(Element elem)
...{
// System.err.println( "extractPropertiesFromLevel()" );
HashMap out = new HashMap();
try
...{
NodeList nl = DomParseUtils.immediateChildElementsByTagName(elem, "property");
int len = nl.getLength();
for (int i = 0; i < len; ++i)
...{
Element propertyElem = (Element) nl.item(i);
String propName = propertyElem.getAttribute("name");
if (propName != null && propName.length() > 0)
...{
String propVal = DomParseUtils.allTextFromElement(propertyElem, true);
out.put( propName, propVal );
//System.err.println( propName + " -> " + propVal );
}
else
logger.warning("Configuration XML contained property element without name attribute: " + LINESEP + propertyElem);
}
}
catch (Exception e)
...{
logger.log( MLevel.WARNING,
"An exception occurred while reading config XML. " +
"Some configuration information has probably been ignored.",
e );
}
return out;
}
private C3P0ConfigXmlUtils()
...{}
}
/**/
/*
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v1.xml;
import java.util. * ;
import org.xml.sax. * ;
import org.w3c.dom. * ;
import com.mchange.v1.util.DebugUtils;
public final class DomParseUtils
... {
final static boolean DEBUG = true;
/** *//**
* @return null if child doesn't exist.
*/
public static String allTextFromUniqueChild(Element elem, String childTagName)
throws DOMException
...{ return allTextFromUniqueChild( elem, childTagName, false ); }
/** *//**
* @return null if child doesn't exist.
*/
public static String allTextFromUniqueChild(Element elem, String childTagName, boolean trim)
throws DOMException
...{
Element uniqueChild = uniqueChildByTagName( elem, childTagName );
if (uniqueChild == null)
return null;
else
return DomParseUtils.allTextFromElement( uniqueChild, trim );
}
public static Element uniqueChild(Element elem, String childTagName) throws DOMException
...{ return uniqueChildByTagName( elem, childTagName); }
/** *//**
* @deprecated use uniqueChild(Element elem, String childTagName)
*/
public static Element uniqueChildByTagName(Element elem, String childTagName) throws DOMException
...{
NodeList nl = elem.getElementsByTagName(childTagName);
int len = nl.getLength();
if (DEBUG)
DebugUtils.myAssert( len <= 1 ,
"There is more than one (" + len + ") child with tag name: " +
childTagName + "!!!" );
return (len == 1 ? (Element) nl.item( 0 ) : null);
}
public static String allText(Element elem) throws DOMException
...{ return allTextFromElement( elem ); }
public static String allText(Element elem, boolean trim) throws DOMException
...{ return allTextFromElement( elem, trim ); }
/** *//** @deprecated use allText(Element elem) */
public static String allTextFromElement(Element elem) throws DOMException
...{ return allTextFromElement( elem, false); }
/** *//** @deprecated use allText(Element elem, boolean trim) */
public static String allTextFromElement(Element elem, boolean trim) throws DOMException
...{
StringBuffer textBuf = new StringBuffer();
NodeList nl = elem.getChildNodes();
for (int j = 0, len = nl.getLength(); j < len; ++j)
...{
Node node = nl.item(j);
if (node instanceof Text) //includes Text and CDATA!
textBuf.append(node.getNodeValue());
}
String out = textBuf.toString();
return ( trim ? out.trim() : out );
}
public static String[] allTextFromImmediateChildElements( Element parent, String tagName )
throws DOMException
...{ return allTextFromImmediateChildElements( parent, tagName, false ); }
public static String[] allTextFromImmediateChildElements( Element parent, String tagName, boolean trim )
throws DOMException
...{
NodeList nl = immediateChildElementsByTagName( parent, tagName );
int len = nl.getLength();
String[] out = new String[ len ];
for (int i = 0; i < len; ++i)
out[i] = allText( (Element) nl.item(i), trim );
return out;
}
public static NodeList immediateChildElementsByTagName( Element parent, String tagName )
throws DOMException
...{ return getImmediateChildElementsByTagName( parent, tagName ); }
/** *//**
* @deprecated use immediateChildrenByTagName( Element parent, String tagName )
*/
public static NodeList getImmediateChildElementsByTagName( Element parent, String tagName )
throws DOMException
...{
final List nodes = new ArrayList();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
if (child instanceof Element && ((Element) child).getTagName().equals(tagName))
nodes.add(child);
return new NodeList()
...{
public int getLength()
...{ return nodes.size(); }
public Node item( int i )
...{ return (Node) nodes.get( i ); }
};
}
public static String allTextFromUniqueImmediateChild(Element elem, String childTagName)
throws DOMException
...{
Element uniqueChild = uniqueImmediateChildByTagName( elem, childTagName );
if (uniqueChild == null)
return null;
return DomParseUtils.allTextFromElement( uniqueChild );
}
public static Element uniqueImmediateChild(Element elem, String childTagName)
throws DOMException
...{ return uniqueImmediateChildByTagName( elem, childTagName); }
/** *//**
* @deprecated use uniqueImmediateChild(Element elem, String childTagName)
*/
public static Element uniqueImmediateChildByTagName(Element elem, String childTagName)
throws DOMException
...{
NodeList nl = getImmediateChildElementsByTagName(elem, childTagName);
int len = nl.getLength();
if (DEBUG)
DebugUtils.myAssert( len <= 1 ,
"There is more than one (" + len + ") child with tag name: " +
childTagName + "!!!" );
return (len == 1 ? (Element) nl.item( 0 ) : null);
}
/** *//**
* @deprecated use Element.getAttribute(String val)
*/
public static String attrValFromElement(Element element, String attrName)
throws DOMException
...{
Attr attr = element.getAttributeNode( attrName );
return (attr == null ? null : attr.getValue());
}
private DomParseUtils()
...{}
}
* Distributed as part of c3p0 v.0.9.1.2
*
* Copyright (C) 2005 Machinery For Change, Inc.
*
* Author: Steve Waldman <swaldman@mchange.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1, as
* published by the Free Software Foundation.
*
* This software 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 software; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.mchange.v1.xml;
import java.util. * ;
import org.xml.sax. * ;
import org.w3c.dom. * ;
import com.mchange.v1.util.DebugUtils;
public final class DomParseUtils
... {
final static boolean DEBUG = true;
/** *//**
* @return null if child doesn't exist.
*/
public static String allTextFromUniqueChild(Element elem, String childTagName)
throws DOMException
...{ return allTextFromUniqueChild( elem, childTagName, false ); }
/** *//**
* @return null if child doesn't exist.
*/
public static String allTextFromUniqueChild(Element elem, String childTagName, boolean trim)
throws DOMException
...{
Element uniqueChild = uniqueChildByTagName( elem, childTagName );
if (uniqueChild == null)
return null;
else
return DomParseUtils.allTextFromElement( uniqueChild, trim );
}
public static Element uniqueChild(Element elem, String childTagName) throws DOMException
...{ return uniqueChildByTagName( elem, childTagName); }
/** *//**
* @deprecated use uniqueChild(Element elem, String childTagName)
*/
public static Element uniqueChildByTagName(Element elem, String childTagName) throws DOMException
...{
NodeList nl = elem.getElementsByTagName(childTagName);
int len = nl.getLength();
if (DEBUG)
DebugUtils.myAssert( len <= 1 ,
"There is more than one (" + len + ") child with tag name: " +
childTagName + "!!!" );
return (len == 1 ? (Element) nl.item( 0 ) : null);
}
public static String allText(Element elem) throws DOMException
...{ return allTextFromElement( elem ); }
public static String allText(Element elem, boolean trim) throws DOMException
...{ return allTextFromElement( elem, trim ); }
/** *//** @deprecated use allText(Element elem) */
public static String allTextFromElement(Element elem) throws DOMException
...{ return allTextFromElement( elem, false); }
/** *//** @deprecated use allText(Element elem, boolean trim) */
public static String allTextFromElement(Element elem, boolean trim) throws DOMException
...{
StringBuffer textBuf = new StringBuffer();
NodeList nl = elem.getChildNodes();
for (int j = 0, len = nl.getLength(); j < len; ++j)
...{
Node node = nl.item(j);
if (node instanceof Text) //includes Text and CDATA!
textBuf.append(node.getNodeValue());
}
String out = textBuf.toString();
return ( trim ? out.trim() : out );
}
public static String[] allTextFromImmediateChildElements( Element parent, String tagName )
throws DOMException
...{ return allTextFromImmediateChildElements( parent, tagName, false ); }
public static String[] allTextFromImmediateChildElements( Element parent, String tagName, boolean trim )
throws DOMException
...{
NodeList nl = immediateChildElementsByTagName( parent, tagName );
int len = nl.getLength();
String[] out = new String[ len ];
for (int i = 0; i < len; ++i)
out[i] = allText( (Element) nl.item(i), trim );
return out;
}
public static NodeList immediateChildElementsByTagName( Element parent, String tagName )
throws DOMException
...{ return getImmediateChildElementsByTagName( parent, tagName ); }
/** *//**
* @deprecated use immediateChildrenByTagName( Element parent, String tagName )
*/
public static NodeList getImmediateChildElementsByTagName( Element parent, String tagName )
throws DOMException
...{
final List nodes = new ArrayList();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
if (child instanceof Element && ((Element) child).getTagName().equals(tagName))
nodes.add(child);
return new NodeList()
...{
public int getLength()
...{ return nodes.size(); }
public Node item( int i )
...{ return (Node) nodes.get( i ); }
};
}
public static String allTextFromUniqueImmediateChild(Element elem, String childTagName)
throws DOMException
...{
Element uniqueChild = uniqueImmediateChildByTagName( elem, childTagName );
if (uniqueChild == null)
return null;
return DomParseUtils.allTextFromElement( uniqueChild );
}
public static Element uniqueImmediateChild(Element elem, String childTagName)
throws DOMException
...{ return uniqueImmediateChildByTagName( elem, childTagName); }
/** *//**
* @deprecated use uniqueImmediateChild(Element elem, String childTagName)
*/
public static Element uniqueImmediateChildByTagName(Element elem, String childTagName)
throws DOMException
...{
NodeList nl = getImmediateChildElementsByTagName(elem, childTagName);
int len = nl.getLength();
if (DEBUG)
DebugUtils.myAssert( len <= 1 ,
"There is more than one (" + len + ") child with tag name: " +
childTagName + "!!!" );
return (len == 1 ? (Element) nl.item( 0 ) : null);
}
/** *//**
* @deprecated use Element.getAttribute(String val)
*/
public static String attrValFromElement(Element element, String attrName)
throws DOMException
...{
Attr attr = element.getAttributeNode( attrName );
return (attr == null ? null : attr.getValue());
}
private DomParseUtils()
...{}
}