Author: aadamchik Date: Sun Nov 24 15:13:02 2013 New Revision: 1544990 URL: http://svn.apache.org/r1544990 Log: CAY-1882 Porting to OSGi environment
fixing JDBC driver classloading Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/DriverDataSource.java cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/PoolManager.java Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java?rev=1544990&r1=1544989&r2=1544990&view=diff ============================================================================== --- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java (original) +++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java Sun Nov 24 15:13:02 2013 @@ -18,20 +18,25 @@ ****************************************************************/ package org.apache.cayenne.configuration.server; +import java.sql.Driver; + import javax.sql.DataSource; import org.apache.cayenne.ConfigurationException; import org.apache.cayenne.configuration.Constants; import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.configuration.RuntimeProperties; +import org.apache.cayenne.conn.DriverDataSource; +import org.apache.cayenne.conn.PoolDataSource; import org.apache.cayenne.conn.PoolManager; +import org.apache.cayenne.di.AdhocObjectFactory; import org.apache.cayenne.di.Inject; import org.apache.cayenne.log.JdbcEventLogger; /** - * A DataSourceFactrory that creates a DataSource based on system properties. Properties - * can be set per domain/node name or globally, applying to all nodes without explicit - * property set. The following properties are supported: + * A DataSourceFactrory that creates a DataSource based on system properties. + * Properties can be set per domain/node name or globally, applying to all nodes + * without explicit property set. The following properties are supported: * <ul> * <li>cayenne.jdbc.driver[.domain_name.node_name] * <li>cayenne.jdbc.url[.domain_name.node_name] @@ -40,8 +45,8 @@ import org.apache.cayenne.log.JdbcEventL * <li>cayenne.jdbc.min.connections[.domain_name.node_name] * <li>cayenne.jdbc.max.conections[.domain_name.node_name] * </ul> - * At least url and driver properties must be specified for this factory to return a valid - * DataSource. + * At least url and driver properties must be specified for this factory to + * return a valid DataSource. * * @since 3.1 */ @@ -53,39 +58,30 @@ public class PropertyDataSourceFactory i @Inject protected JdbcEventLogger jdbcEventLogger; + @Inject + private AdhocObjectFactory objectFactory; + @Override public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { - String suffix = "." - + nodeDescriptor.getDataChannelDescriptor().getName() - + "." - + nodeDescriptor.getName(); + String suffix = "." + nodeDescriptor.getDataChannelDescriptor().getName() + "." + nodeDescriptor.getName(); - String driver = getProperty(Constants.JDBC_DRIVER_PROPERTY, suffix); + String driverClass = getProperty(Constants.JDBC_DRIVER_PROPERTY, suffix); String url = getProperty(Constants.JDBC_URL_PROPERTY, suffix); String username = getProperty(Constants.JDBC_USERNAME_PROPERTY, suffix); String password = getProperty(Constants.JDBC_PASSWORD_PROPERTY, suffix); - int minConnections = getIntProperty( - Constants.JDBC_MIN_CONNECTIONS_PROPERTY, - suffix, - 1); - int maxConnections = getIntProperty( - Constants.JDBC_MAX_CONNECTIONS_PROPERTY, - suffix, - 1); + int minConnections = getIntProperty(Constants.JDBC_MIN_CONNECTIONS_PROPERTY, suffix, 1); + int maxConnections = getIntProperty(Constants.JDBC_MAX_CONNECTIONS_PROPERTY, suffix, 1); + + Driver driver = objectFactory.newInstance(Driver.class, driverClass); + DriverDataSource driverDS = new DriverDataSource(driver, url, username, password); + driverDS.setLogger(jdbcEventLogger); + PoolDataSource poolDS = new PoolDataSource(driverDS); try { - return new PoolManager( - driver, - url, - minConnections, - maxConnections, - username, - password, - jdbcEventLogger, - properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME, PoolManager.MAX_QUEUE_WAIT_DEFAULT)); - } - catch (Exception e) { + return new PoolManager(poolDS, minConnections, maxConnections, username, password, properties.getLong( + Constants.SERVER_MAX_QUEUE_WAIT_TIME, PoolManager.MAX_QUEUE_WAIT_DEFAULT)); + } catch (Exception e) { jdbcEventLogger.logConnectFailure(e); throw e; } @@ -100,12 +96,8 @@ public class PropertyDataSourceFactory i try { return Integer.parseInt(string); - } - catch (NumberFormatException e) { - throw new ConfigurationException( - "Invalid int property '%s': '%s'", - propertyName, - string); + } catch (NumberFormatException e) { + throw new ConfigurationException("Invalid int property '%s': '%s'", propertyName, string); } } Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java?rev=1544990&r1=1544989&r2=1544990&view=diff ============================================================================== --- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java (original) +++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java Sun Nov 24 15:13:02 2013 @@ -18,6 +18,8 @@ ****************************************************************/ package org.apache.cayenne.configuration.server; +import java.sql.Driver; + import javax.sql.DataSource; import org.apache.cayenne.ConfigurationException; @@ -25,7 +27,10 @@ import org.apache.cayenne.configuration. import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.configuration.RuntimeProperties; import org.apache.cayenne.conn.DataSourceInfo; +import org.apache.cayenne.conn.DriverDataSource; +import org.apache.cayenne.conn.PoolDataSource; import org.apache.cayenne.conn.PoolManager; +import org.apache.cayenne.di.AdhocObjectFactory; import org.apache.cayenne.di.Inject; import org.apache.cayenne.log.JdbcEventLogger; import org.apache.commons.logging.Log; @@ -47,9 +52,12 @@ public class XMLPoolingDataSourceFactory @Inject protected JdbcEventLogger jdbcEventLogger; - @Inject + @Inject private RuntimeProperties properties; - + + @Inject + private AdhocObjectFactory objectFactory; + @Override public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { @@ -61,14 +69,21 @@ public class XMLPoolingDataSourceFactory throw new ConfigurationException(message); } + Driver driver = objectFactory.newInstance(Driver.class, dataSourceDescriptor.getJdbcDriver()); + DriverDataSource driverDS = new DriverDataSource(driver, dataSourceDescriptor.getDataSourceUrl(), + dataSourceDescriptor.getUserName(), dataSourceDescriptor.getPassword()); + driverDS.setLogger(jdbcEventLogger); + PoolDataSource poolDS = new PoolDataSource(driverDS); + try { - return new PoolManager(dataSourceDescriptor.getJdbcDriver(), dataSourceDescriptor.getDataSourceUrl(), - dataSourceDescriptor.getMinConnections(), dataSourceDescriptor.getMaxConnections(), - dataSourceDescriptor.getUserName(), dataSourceDescriptor.getPassword(), jdbcEventLogger, - properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME, PoolManager.MAX_QUEUE_WAIT_DEFAULT)); + return new PoolManager(poolDS, dataSourceDescriptor.getMinConnections(), + dataSourceDescriptor.getMaxConnections(), dataSourceDescriptor.getUserName(), + dataSourceDescriptor.getPassword(), properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME, + PoolManager.MAX_QUEUE_WAIT_DEFAULT)); } catch (Exception e) { jdbcEventLogger.logConnectFailure(e); throw e; } } + } Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/DriverDataSource.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/DriverDataSource.java?rev=1544990&r1=1544989&r2=1544990&view=diff ============================================================================== --- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/DriverDataSource.java (original) +++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/DriverDataSource.java Sun Nov 24 15:13:02 2013 @@ -52,7 +52,9 @@ public class DriverDataSource implements * Loads JDBC driver using current thread class loader. * * @since 3.0 + * @deprecated since 3.2 as class loading should not happen here. */ + @Deprecated private static Driver loadDriver(String driverClassName) throws SQLException { Class<?> driverClass; Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/PoolManager.java URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/PoolManager.java?rev=1544990&r1=1544989&r2=1544990&view=diff ============================================================================== --- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/PoolManager.java (original) +++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/conn/PoolManager.java Sun Nov 24 15:13:02 2013 @@ -56,7 +56,9 @@ public class PoolManager implements Scop * timed out and was unable to obtain a connection. */ public static class ConnectionUnavailableException extends SQLException { - public ConnectionUnavailableException(String message) { + private static final long serialVersionUID = 1063973806941023165L; + + public ConnectionUnavailableException(String message) { super(message); } } @@ -80,13 +82,20 @@ public class PoolManager implements Scop /** * Creates new PoolManager using org.apache.cayenne.conn.PoolDataSource for an * underlying ConnectionPoolDataSource. + * + * @deprecated since 3.2 This constructor causes implicit class loading that should avoided. */ + @Deprecated public PoolManager(String jdbcDriver, String dataSourceUrl, int minCons, int maxCons, String userName, String password) throws SQLException { this(jdbcDriver, dataSourceUrl, minCons, maxCons, userName, password, null, MAX_QUEUE_WAIT_DEFAULT); } + /** + * @deprecated since 3.2 This constructor causes implicit class loading that should avoided. + */ + @Deprecated public PoolManager(String jdbcDriver, String dataSourceUrl, int minCons, int maxCons, String userName, String password, JdbcEventLogger logger, long maxQueueWaitTime) throws SQLException { @@ -108,7 +117,7 @@ public class PoolManager implements Scop PoolDataSource poolDS = new PoolDataSource(driverDS); init(poolDS, minCons, maxCons, userName, password, maxQueueWaitTime); } - + /** * Creates new PoolManager with the specified policy for connection pooling and a * ConnectionPoolDataSource object. @@ -119,10 +128,28 @@ public class PoolManager implements Scop * @param maxCons Non-negative integer that specifies maximum number of simultaneuosly * open connections * @throws SQLException if pool manager can not be created. + * @deprecated since 3.2 use {@link #PoolManager(ConnectionPoolDataSource, int, int, String, String, long)} */ public PoolManager(ConnectionPoolDataSource poolDataSource, int minCons, int maxCons, String userName, String password) throws SQLException { - init(poolDataSource, minCons, maxCons, userName, password, MAX_QUEUE_WAIT_DEFAULT); + this(poolDataSource, minCons, maxCons, userName, password, PoolManager.MAX_QUEUE_WAIT_DEFAULT); + } + + /** + * Creates new PoolManager with the specified policy for connection pooling and a + * ConnectionPoolDataSource object. + * + * @param poolDataSource data source for pooled connections + * @param minCons Non-negative integer that specifies a minimum number of open + * connections to keep in the pool at all times + * @param maxCons Non-negative integer that specifies maximum number of simultaneuosly + * open connections + * @throws SQLException if pool manager can not be created. + * @since 3.2 + */ + public PoolManager(ConnectionPoolDataSource poolDataSource, int minCons, int maxCons, + String userName, String password, long maxQueueWaitTime) throws SQLException { + init(poolDataSource, minCons, maxCons, userName, password, maxQueueWaitTime); } /** Initializes pool. Normally called from constructor. */