Org. logicalcobwebs. proxool. configuration. propertyconfigurator. Java

Source: Internet
Author: User

The Hibernate framework reads the connection pool configuration information and has some requirements on the connection pool configuration file.

For example, the configuration key value must be prefixed with JDBC ..........

Package org. logicalcobwebs. proxool. configuration;

Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Import org. logicalcobwebs. proxool. proxoolconstants;
Import org. logicalcobwebs. proxool. proxoolexception;
Import org. logicalcobwebs. proxool. proxoolfacade;

Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. Map;
Import java. util. properties;

/**
* Uses a standard Java properties file to configure proxool. For example:
*
* <PRE>
* Jdbc-0.proxool.alias = property-test
* Jdbc-0.proxool.driver-url = JDBC: HSQLDB :.
* Jdbc-0.proxool.driver-class = org. HSQLDB. jdbcdriver
* Jdbc-0.user = foo
* Jdbc-0.password = bar
* Jdbc-0.proxool.house-keeping-sleep-time = 40000
* Jdbc-0.proxool.house-keeping-test-sql = select current_date
* Jdbc-0.proxool.maximum-connection-count = 10
* Jdbc-0.proxool.minimum-connection-count = 3
* Jdbc-0.proxool.maximum-connection-lifetime = 18000000
* Jdbc-0.proxool.simultaneous-build-throttle = 5
* Jdbc-0.proxool.recently-started-threshold = 40000
* Jdbc-0.proxool.overload-without-refusal-lifetime = 50000
* Jdbc-0.proxool.maximum-active-time = 60000
* Jdbc-0.proxool.verbose = true
* Jdbc-0.proxool.trace = true
* Jdbc-0.proxool.fatal-sql-exception = fatal error
* Jdbc-0.proxool.prototype-count = 2
*
* Jdbc-1.proxool.alias = property-test-2
* Jdbc-1.proxool.driver-url = JDBC: HSQLDB :.
* Jdbc-1.proxool.driver-class = org. HSQLDB. jdbcdriver
* Jdbc-1.user = Scott
* Jdbc-1.password = Tiger
* Jdbc-1.proxool.house-keeping-sleep-time = 40000
* Jdbc-1.proxool.house-keeping-test-sql = select current_date
* Jdbc-1.proxool.maximum-connection-count = 10
* Jdbc-1.proxool.minimum-connection-count = 3
* Jdbc-1.proxool.maximum-connection-lifetime = 18000000
* Jdbc-1.proxool.simultaneous-build-throttle = 5
* Jdbc-1.proxool.recently-started-threshold = 40000
* Jdbc-1.proxool.overload-without-refusal-lifetime = 50000
* Jdbc-1.proxool.maximum-active-time = 60000
* Jdbc-1.proxool.verbose = true
* Jdbc-1.proxool.trace = true
* Jdbc-1.proxool.fatal-sql-exception = fatal error
* Jdbc-1.proxool.prototype-count = 2
* </PRE>
*
* <P> the first word (up to the first dot) must start with "JDBC", but it can
* Be anything you like. Use unique names to identify each pool. Any property
* Not starting with "JDBC" will be ignored. </P>
* <P>
* The properties prefixed with "proxool." will be used by proxool while
* The properties that are not prefixed will be passed on to
* Delegate JDBC driver.
* </P>
*
* @ Version $ revision: 1.11 $, $ Date: 2006/01/18 14:39:58 $
* @ Author Bill horsman (bill@logicalcobwebs.co.uk)
* @ Author $ Author: billhorsman $ (current maintainer)
* @ Since proxool 0.5
*/
Public class propertyconfigurator {
Private Static final log = logfactory. getlog (propertyconfigurator. Class );

Protected static final string prefix = "JDBC ";

Private Static final string dot = ".";

Private Static final string example_format = prefix + "*" + dot + "*";

/**
* Configure proxool with the given properties file.
* @ Param filename the filename of the properties file.
* @ Throws proxoolexception if the configuration fails.
*/
Public static void configure (string filename) throws proxoolexception {
Properties Properties = new properties ();
Try {
Properties. Load (New fileinputstream (filename ));
} Catch (ioexception e ){
Throw new proxoolexception ("couldn't load property file" + filename );
}
Configure (properties );
}

/**
* Configure proxool with the given properties.
* @ Param properties the properties instance to use.
* @ Throws proxoolexception if the configuration fails.
*/
Public static void configure (properties Properties) throws proxoolexception {
Final map propertiesmap = new hashmap ();
Final iterator allpropertykeysiterator = properties. keyset (). iterator ();
Properties proxoolproperties = NULL;

While (allpropertykeysiterator. hasnext ()){
String key = (string) allpropertykeysiterator. Next ();
String value = properties. getproperty (key );

If (key. startswith (prefix )){
Int A = key. indexof (DOT );
If (A =-1 ){
Throw new proxoolexception ("property" + key + "must be of the format" + example_format );
}
Final string tag = key. substring (0, );
Final string name = key. substring (a + 1 );
Proxoolproperties = (properties) propertiesmap. Get (TAG );
If (proxoolproperties = NULL ){
Proxoolproperties = new properties ();
Propertiesmap. Put (TAG, proxoolproperties );
}
Proxoolproperties. Put (name, value );
}
}

Final iterator tags = propertiesmap. keyset (). iterator ();
While (tags. hasnext ()){
Proxoolproperties = (properties) propertiesmap. Get (tags. Next ());
// Make sure that required propeties are defined
// And build the URL
// Check that we have defined the minimum information
Final string driverclass = proxoolproperties. getproperty (proxoolconstants. driver_class_property );
Final string driverurl = proxoolproperties. getproperty (proxoolconstants. driver_url_property );
If (driverclass = NULL | driverurl = NULL ){
Throw new proxoolexception ("You must define the" + proxoolconstants. driver_class_property + "and"
+ Proxoolconstants. driver_url_property + ".");
}
Final string alias = proxoolproperties. getproperty (proxoolconstants. alias_property );

// Build the URL; optionally defining a name
Stringbuffer url = new stringbuffer ();
URL. append ("proxool ");
If (alias! = NULL ){
URL. append (proxoolconstants. alias_delimiter );
URL. append (alias );
Proxoolproperties. Remove (proxoolconstants. alias_property );
}
URL. append (proxoolconstants. url_delimiter );
URL. append (driverclass );
Proxoolproperties. Remove (proxoolconstants. driver_class_property );
URL. append (proxoolconstants. url_delimiter );
URL. append (driverurl );
Proxoolproperties. Remove (proxoolconstants. driver_url_property );
If (log. isdebugenabled ()){
Log. debug ("created URL:" + URL );
}

Proxoolfacade. registerconnectionpool (URL. tostring (), proxoolproperties );
}
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.