001 package org.apache.jcs.auxiliary.disk.jdbc;
002
003 import org.apache.commons.logging.Log;
004 import org.apache.commons.logging.LogFactory;
005
006 /** This is just a helper util. */
007 public class JDBCDiskCachePoolAccessFactory
008 {
009 /** The local logger. */
010 private final static Log log = LogFactory.getLog( JDBCDiskCachePoolAccessFactory.class );
011
012 /**
013 * Creates a JDBCDiskCachePoolAccess object from the JDBCDiskCachePoolAccessAttributes. This is
014 * used by the connection pool manager.
015 * <p>
016 * @param poolAttributes
017 * @return JDBCDiskCachePoolAccess
018 * @throws Exception
019 */
020 public static JDBCDiskCachePoolAccess createPoolAccess( JDBCDiskCachePoolAccessAttributes poolAttributes )
021 throws Exception
022 {
023 return createPoolAccess( poolAttributes.getDriverClassName(), poolAttributes.getPoolName(), poolAttributes
024 .getUrl()
025 + poolAttributes.getDatabase(), poolAttributes.getUserName(), poolAttributes.getPassword(), poolAttributes
026 .getMaxActive() );
027 }
028
029 /**
030 * Creates a JDBCDiskCachePoolAccess object from the JDBCDiskCacheAttributes. Use this when not
031 * using the connection pool manager.
032 * <p>
033 * @param cattr
034 * @return JDBCDiskCachePoolAccess
035 * @throws Exception
036 */
037 public static JDBCDiskCachePoolAccess createPoolAccess( JDBCDiskCacheAttributes cattr )
038 throws Exception
039 {
040 return createPoolAccess( cattr.getDriverClassName(), cattr.getName(), cattr.getUrl() + cattr.getDatabase(),
041 cattr.getUserName(), cattr.getPassword(), cattr.getMaxActive() );
042 }
043
044 /**
045 * Creates a pool access object and registers the driver.
046 * <p>
047 * @param driverClassName
048 * @param poolName
049 * @param fullURL = (url + database)
050 * @param userName
051 * @param password
052 * @param maxActive
053 * @return JDBCDiskCachePoolAccess
054 * @throws Exception
055 */
056 public static JDBCDiskCachePoolAccess createPoolAccess( String driverClassName, String poolName, String fullURL,
057 String userName, String password, int maxActive )
058 throws Exception
059 {
060 JDBCDiskCachePoolAccess poolAccess = null;
061
062 try
063 {
064 // org.gjt.mm.mysql.Driver
065 Class.forName( driverClassName );
066 }
067 catch ( ClassNotFoundException e )
068 {
069 log.error( "Couldn't find class for driver [" + driverClassName + "]", e );
070 }
071
072 poolAccess = new JDBCDiskCachePoolAccess( poolName );
073
074 poolAccess.setupDriver( fullURL, userName, password, maxActive );
075
076 poolAccess.logDriverStats();
077
078 if ( log.isInfoEnabled() )
079 {
080 log.info( "Created: " + poolAccess );
081 }
082
083 return poolAccess;
084 }
085 }