This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit 79e45cf2b5604d4d278e57768a6e845e855b1a5e Author: Andrus Adamchik <[email protected]> AuthorDate: Fri May 15 17:51:14 2026 -0400 DbAdapterFactory.createAdapter(..), DataSourceFactory.getDataSource(..) and other such factories should not declare a checked exception --- .../configuration/DriverDataSourceFactory.java | 35 ++++----- .../org/apache/cayenne/di/AdhocObjectFactory.java | 19 +++-- .../cayenne/di/spi/DefaultAdhocObjectFactory.java | 25 ++++--- .../configuration/DataSourceDescriptor.java | 17 +++-- .../configuration/runtime/DataSourceFactory.java | 9 +-- .../configuration/runtime/DbAdapterFactory.java | 9 +-- .../runtime/DefaultDataNodeFactory.java | 2 +- .../runtime/DefaultDbAdapterFactory.java | 17 ++--- .../runtime/DelegatingDataSourceFactory.java | 5 +- .../runtime/JNDIDataSourceFactory.java | 18 ++--- .../runtime/PropertyDataSourceFactory.java | 86 +++++++++++----------- .../runtime/XMLPoolingDataSourceFactory.java | 59 +++++++-------- .../cayenne/runtime/FixedDataSourceFactory.java | 2 +- .../configuration/mock/MockDataSourceFactory1.java | 6 +- .../runtime/JNDIDataSourceFactoryIT.java | 8 +- .../RuntimeCaseSharedDataSourceFactory.java | 2 +- 16 files changed, 166 insertions(+), 153 deletions(-) diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/configuration/DriverDataSourceFactory.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/configuration/DriverDataSourceFactory.java index 0aa59b1f6..927292ec4 100644 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/configuration/DriverDataSourceFactory.java +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/configuration/DriverDataSourceFactory.java @@ -19,10 +19,6 @@ package org.apache.cayenne.dbsync.reverse.configuration; -import java.sql.Driver; - -import javax.sql.DataSource; - import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.configuration.DataSourceDescriptor; import org.apache.cayenne.configuration.runtime.DataSourceFactory; @@ -30,25 +26,30 @@ import org.apache.cayenne.datasource.DriverDataSource; import org.apache.cayenne.di.AdhocObjectFactory; import org.apache.cayenne.di.Inject; +import javax.sql.DataSource; +import java.sql.Driver; + /** * @since 4.0 */ public class DriverDataSourceFactory implements DataSourceFactory { - private AdhocObjectFactory objectFactory; + private final AdhocObjectFactory objectFactory; - public DriverDataSourceFactory(@Inject AdhocObjectFactory objectFactory) { - this.objectFactory = objectFactory; - } + public DriverDataSourceFactory(@Inject AdhocObjectFactory objectFactory) { + this.objectFactory = objectFactory; + } - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { - DataSourceDescriptor dataSourceDescriptor = nodeDescriptor.getDataSourceDescriptor(); - if (dataSourceDescriptor == null) { - throw new IllegalArgumentException("'nodeDescriptor' contains no datasource descriptor"); - } + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { + DataSourceDescriptor dataSourceDescriptor = nodeDescriptor.getDataSourceDescriptor(); + if (dataSourceDescriptor == null) { + throw new IllegalArgumentException("'nodeDescriptor' contains no datasource descriptor"); + } - Driver driver = objectFactory.<Driver>getJavaClass(dataSourceDescriptor.getJdbcDriver()).getDeclaredConstructor().newInstance(); - return new DriverDataSource(driver, dataSourceDescriptor.getDataSourceUrl(), dataSourceDescriptor.getUserName(), - dataSourceDescriptor.getPassword()); - } + return new DriverDataSource( + objectFactory.newInstance(Driver.class, dataSourceDescriptor.getJdbcDriver(), true), + dataSourceDescriptor.getDataSourceUrl(), + dataSourceDescriptor.getUserName(), + dataSourceDescriptor.getPassword()); + } } diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java b/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java index b3f406bd6..1a151a6ed 100644 --- a/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java +++ b/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java @@ -21,21 +21,30 @@ package org.apache.cayenne.di; /** * Creates objects for user-provided String class names, injecting dependencies * into them. - * + * * @since 3.1 */ public interface AdhocObjectFactory { /** - * Returns an instance of "className" that implements "superType", injecting - * dependencies from the registry into it. + * Returns an instance of "className" that implements "superType", injecting dependencies from the registry into it. + */ + default <T> T newInstance(Class<? super T> superType, String className) { + return newInstance(superType, className, false); + } + + /** + * Returns an instance of "className" that implements "superType", injecting dependencies from the registry into it + * if requested + * + * @since 5.0 */ - <T> T newInstance(Class<? super T> superType, String className); + <T> T newInstance(Class<? super T> superType, String className, boolean skipInjection); /** * Returns a Java class loaded using ClassLoader returned from * {@link ClassLoaderManager#getClassLoader(String)} for a given class name. - * + * * @since 4.0 */ <T> Class<? extends T> getJavaClass(String className); diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java index 22452177b..4c6beb327 100644 --- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java +++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java @@ -29,7 +29,7 @@ import org.apache.cayenne.di.Provider; * A default implementation of {@link AdhocObjectFactory} that creates objects * using default no-arg constructor and injects dependencies into annotated * fields. Note that constructor injection is not supported by this factory. - * + * * @since 3.1 */ public class DefaultAdhocObjectFactory implements AdhocObjectFactory { @@ -45,9 +45,8 @@ public class DefaultAdhocObjectFactory implements AdhocObjectFactory { this.classLoaderManager = classLoaderManager; } - @SuppressWarnings("unchecked") @Override - public <T> T newInstance(Class<? super T> superType, String className) { + public <T> T newInstance(Class<? super T> superType, String className, boolean skipInjection) { if (superType == null) { throw new NullPointerException("Null superType"); @@ -63,17 +62,23 @@ public class DefaultAdhocObjectFactory implements AdhocObjectFactory { throw new DIRuntimeException("Class %s is not assignable to %s", className, superType.getName()); } - T instance; + return skipInjection + ? newInstanceNoInjection(type) + : newInstanceWithInjection(type); + } + + private <T> T newInstanceNoInjection(Class<T> type) { try { - Provider<T> provider0 = new ConstructorInjectingProvider<>(type, (DefaultInjector) injector); - Provider<T> provider1 = new FieldInjectingProvider<>(provider0, (DefaultInjector) injector); - instance = provider1.get(); + return type.getDeclaredConstructor().newInstance(); } catch (Exception e) { - throw new DIRuntimeException("Error creating instance of class %s of type %s", e, className, - superType.getName()); + throw new DIRuntimeException("Error creating instance of type %s", e, type.getName()); } + } - return instance; + private <T> T newInstanceWithInjection(Class<T> type) { + Provider<T> provider0 = new ConstructorInjectingProvider<>(type, (DefaultInjector) injector); + Provider<T> provider1 = new FieldInjectingProvider<>(provider0, (DefaultInjector) injector); + return provider1.get(); } @SuppressWarnings("unchecked") diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/DataSourceDescriptor.java b/cayenne/src/main/java/org/apache/cayenne/configuration/DataSourceDescriptor.java index d35abe95e..9d4cad8fe 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/DataSourceDescriptor.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/DataSourceDescriptor.java @@ -19,14 +19,15 @@ package org.apache.cayenne.configuration; -import java.io.Serializable; -import java.util.Objects; - import org.apache.cayenne.util.XMLEncoder; import org.apache.cayenne.util.XMLSerializable; +import java.io.Serializable; +import java.util.Objects; + /** - * Helper JavaBean class that holds DataSource information for the Cayenne-managed DataSource. + * Helper holding DataSource information for the Cayenne-managed DataSource. + * * @since 5.0 */ public class DataSourceDescriptor implements Serializable, XMLSerializable { @@ -111,11 +112,11 @@ public class DataSourceDescriptor implements Serializable, XMLSerializable { .start("driver").attribute("value", jdbcDriver).end() .start("url").attribute("value", dataSourceUrl).end() .start("connectionPool") - .attribute("min", minConnections) - .attribute("max", maxConnections).end() + .attribute("min", minConnections) + .attribute("max", maxConnections).end() .start("login") - .attribute("userName", userName) - .attribute("password", password).end() + .attribute("userName", userName) + .attribute("password", password).end() .end(); } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DataSourceFactory.java index 692c80b15..0cab18655 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DataSourceFactory.java @@ -18,18 +18,17 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -import javax.sql.DataSource; - import org.apache.cayenne.configuration.DataNodeDescriptor; +import javax.sql.DataSource; + /** * @since 3.1 */ public interface DataSourceFactory { /** - * Returns DataSource object based on the configuration provided in the - * "nodeDescriptor". + * Returns DataSource object based on the configuration provided in the "nodeDescriptor". */ - DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception; + DataSource getDataSource(DataNodeDescriptor nodeDescriptor); } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DbAdapterFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DbAdapterFactory.java index bd1b035d7..36f1ddd51 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DbAdapterFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DbAdapterFactory.java @@ -18,19 +18,18 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -import javax.sql.DataSource; - import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.dba.DbAdapter; +import javax.sql.DataSource; + /** * @since 3.1 */ public interface DbAdapterFactory { /** - * Returns an instance of DbAdapter if the factory detects that it knows how to handle - * the database. + * Returns an instance of DbAdapter if the factory detects that it knows how to handle the database. */ - DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource) throws Exception; + DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource); } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDataNodeFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDataNodeFactory.java index 56d1062f8..9f43a3f23 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDataNodeFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDataNodeFactory.java @@ -64,7 +64,7 @@ public class DefaultDataNodeFactory implements DataNodeFactory { protected SQLTemplateProcessor sqlTemplateProcessor; @Override - public DataNode createDataNode(DataNodeDescriptor nodeDescriptor) throws Exception { + public DataNode createDataNode(DataNodeDescriptor nodeDescriptor) { DataNode dataNode = new DataNode(nodeDescriptor.getName()); diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDbAdapterFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDbAdapterFactory.java index 56a1bfada..aaada0212 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDbAdapterFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DefaultDbAdapterFactory.java @@ -18,14 +18,6 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; -import java.util.List; -import java.util.Objects; - -import javax.sql.DataSource; - import org.apache.cayenne.CayenneRuntimeException; import org.apache.cayenne.configuration.Constants; import org.apache.cayenne.configuration.DataNodeDescriptor; @@ -38,6 +30,13 @@ import org.apache.cayenne.di.Inject; import org.apache.cayenne.di.Injector; import org.apache.cayenne.log.JdbcEventLogger; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import java.util.List; +import java.util.Objects; + /** * A factory of DbAdapters that either loads user-provided adapter or guesses * the adapter type from the database metadata. @@ -68,7 +67,7 @@ public class DefaultDbAdapterFactory implements DbAdapterFactory { } @Override - public DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, final DataSource dataSource) { + public DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource) { String adapterType = null; diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DelegatingDataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DelegatingDataSourceFactory.java index 848a7a105..1374e80fd 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DelegatingDataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/DelegatingDataSourceFactory.java @@ -64,9 +64,8 @@ public class DelegatingDataSourceFactory implements DataSourceFactory { } @Override - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { - DataSource dataSource = getDataSourceFactory(nodeDescriptor).getDataSource( - nodeDescriptor); + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { + DataSource dataSource = getDataSourceFactory(nodeDescriptor).getDataSource(nodeDescriptor); attachToScope(dataSource); return dataSource; } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactory.java index 04bd36437..b7da7db66 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactory.java @@ -18,29 +18,29 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.sql.DataSource; - import org.apache.cayenne.CayenneRuntimeException; import org.apache.cayenne.configuration.DataNodeDescriptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + /** * Locates DataSource mapped via JNDI. * * @since 3.1 - * @deprecated since 5.0, unused by Cayenne + * @deprecated unused by Cayenne */ -@Deprecated(since = "5.0") +@Deprecated(since = "5.0", forRemoval = true) public class JNDIDataSourceFactory implements DataSourceFactory { private static final Logger LOGGER = LoggerFactory.getLogger(JNDIDataSourceFactory.class); @Override - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { String location = getLocation(nodeDescriptor); @@ -48,7 +48,7 @@ public class JNDIDataSourceFactory implements DataSourceFactory { return lookupViaJNDI(location); } catch (Exception e) { LOGGER.info("*** failed JNDI lookup of DataSource at location: " + location, e); - throw e; + throw new RuntimeException(e); } } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/PropertyDataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/PropertyDataSourceFactory.java index 81260f04e..622dd1ed6 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/PropertyDataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/PropertyDataSourceFactory.java @@ -18,10 +18,6 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -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; @@ -31,6 +27,9 @@ import org.apache.cayenne.datasource.UnmanagedPoolingDataSource; import org.apache.cayenne.di.AdhocObjectFactory; import org.apache.cayenne.di.Inject; +import javax.sql.DataSource; +import java.sql.Driver; + /** * A DataSourceFactrory that creates a DataSource based on system properties. * Properties can be set per domain/node name or globally, applying to all nodes @@ -45,54 +44,59 @@ import org.apache.cayenne.di.Inject; * </ul> * At least url and driver properties must be specified for this factory to * return a valid DataSource. - * + * * @since 3.1 */ public class PropertyDataSourceFactory implements DataSourceFactory { - @Inject - protected RuntimeProperties properties; + @Inject + protected RuntimeProperties properties; - @Inject - private AdhocObjectFactory objectFactory; + @Inject + private AdhocObjectFactory objectFactory; - @Override - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + @Override + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { - String suffix = "." + nodeDescriptor.getDataChannelDescriptor().getName() + "." + nodeDescriptor.getName(); + String suffix = "." + nodeDescriptor.getDataChannelDescriptor().getName() + "." + nodeDescriptor.getName(); - 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); - long maxQueueWaitTime = properties.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, - UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT); - String validationQuery = properties.get(Constants.JDBC_VALIDATION_QUERY_PROPERTY); + 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); + long maxQueueWaitTime = properties.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, + UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT); + String validationQuery = properties.get(Constants.JDBC_VALIDATION_QUERY_PROPERTY); - Driver driver = objectFactory.<Driver>getJavaClass(driverClass).getDeclaredConstructor().newInstance(); - return DataSourceBuilder.url(url).driver(driver).userName(username).password(password) - .pool(minConnections, maxConnections).maxQueueWaitTime(maxQueueWaitTime) - .validationQuery(validationQuery).build(); - } + Driver driver = objectFactory.newInstance(Driver.class, driverClass, true); + return DataSourceBuilder + .url(url) + .driver(driver) + .userName(username) + .password(password) + .pool(minConnections, maxConnections) + .maxQueueWaitTime(maxQueueWaitTime) + .validationQuery(validationQuery).build(); + } - protected int getIntProperty(String propertyName, String suffix, int defaultValue) { - String string = getProperty(propertyName, suffix); + protected int getIntProperty(String propertyName, String suffix, int defaultValue) { + String string = getProperty(propertyName, suffix); - if (string == null) { - return defaultValue; - } + if (string == null) { + return defaultValue; + } - try { - return Integer.parseInt(string); - } catch (NumberFormatException e) { - throw new ConfigurationException("Invalid int property '%s': '%s'", propertyName, string); - } - } + try { + return Integer.parseInt(string); + } catch (NumberFormatException e) { + throw new ConfigurationException("Invalid int property '%s': '%s'", propertyName, string); + } + } - protected String getProperty(String propertyName, String suffix) { - String value = properties.get(propertyName + suffix); - return value != null ? value : properties.get(propertyName); - } + protected String getProperty(String propertyName, String suffix) { + String value = properties.get(propertyName + suffix); + return value != null ? value : properties.get(propertyName); + } } diff --git a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/XMLPoolingDataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/XMLPoolingDataSourceFactory.java index 3616b861e..77f3c4818 100644 --- a/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/XMLPoolingDataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/configuration/runtime/XMLPoolingDataSourceFactory.java @@ -18,10 +18,6 @@ ****************************************************************/ package org.apache.cayenne.configuration.runtime; -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; @@ -34,47 +30,48 @@ import org.apache.cayenne.di.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.sql.DataSource; +import java.sql.Driver; + /** * A {@link DataSourceFactory} that loads JDBC connection information from an * XML resource associated with the DataNodeDescriptor, returning a DataSource * with simple connection pooling. - * + * * @since 3.1 */ // TODO: this factory does not read XML anymore, should we rename it to something else? public class XMLPoolingDataSourceFactory implements DataSourceFactory { - private static final Logger logger = LoggerFactory.getLogger(XMLPoolingDataSourceFactory.class); - - @Inject - private RuntimeProperties properties; + private static final Logger logger = LoggerFactory.getLogger(XMLPoolingDataSourceFactory.class); - @Inject - private AdhocObjectFactory objectFactory; + @Inject + private RuntimeProperties properties; - @Override - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + @Inject + private AdhocObjectFactory objectFactory; - DataSourceDescriptor descriptor = nodeDescriptor.getDataSourceDescriptor(); - if (descriptor == null) { - String message = "Null dataSourceDescriptor for nodeDescriptor '" + nodeDescriptor.getName() + "'"; - logger.info(message); - throw new ConfigurationException(message); - } + @Override + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { - long maxQueueWaitTime = properties - .getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT); + DataSourceDescriptor descriptor = nodeDescriptor.getDataSourceDescriptor(); + if (descriptor == null) { + String message = "Null dataSourceDescriptor for nodeDescriptor '" + nodeDescriptor.getName() + "'"; + logger.info(message); + throw new ConfigurationException(message); + } - Driver driver = objectFactory.<Driver>getJavaClass(descriptor.getJdbcDriver()) - .getDeclaredConstructor().newInstance(); + long maxQueueWaitTime = properties + .getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT); - return DataSourceBuilder.url(descriptor.getDataSourceUrl()) - .driver(driver) - .userName(descriptor.getUserName()) - .password(descriptor.getPassword()) - .pool(descriptor.getMinConnections(), descriptor.getMaxConnections()) - .maxQueueWaitTime(maxQueueWaitTime) - .build(); - } + Driver driver = objectFactory.newInstance(Driver.class, descriptor.getJdbcDriver(), true); + return DataSourceBuilder.url(descriptor.getDataSourceUrl()) + .driver(driver) + .userName(descriptor.getUserName()) + .password(descriptor.getPassword()) + .pool(descriptor.getMinConnections(), descriptor.getMaxConnections()) + .maxQueueWaitTime(maxQueueWaitTime) + .build(); + } } diff --git a/cayenne/src/main/java/org/apache/cayenne/runtime/FixedDataSourceFactory.java b/cayenne/src/main/java/org/apache/cayenne/runtime/FixedDataSourceFactory.java index 33077609d..9b187aa75 100644 --- a/cayenne/src/main/java/org/apache/cayenne/runtime/FixedDataSourceFactory.java +++ b/cayenne/src/main/java/org/apache/cayenne/runtime/FixedDataSourceFactory.java @@ -35,7 +35,7 @@ class FixedDataSourceFactory implements DataSourceFactory { } @Override - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { return dataSource; } } diff --git a/cayenne/src/test/java/org/apache/cayenne/configuration/mock/MockDataSourceFactory1.java b/cayenne/src/test/java/org/apache/cayenne/configuration/mock/MockDataSourceFactory1.java index ab9ced46c..0c78766d1 100644 --- a/cayenne/src/test/java/org/apache/cayenne/configuration/mock/MockDataSourceFactory1.java +++ b/cayenne/src/test/java/org/apache/cayenne/configuration/mock/MockDataSourceFactory1.java @@ -18,19 +18,19 @@ ****************************************************************/ package org.apache.cayenne.configuration.mock; -import javax.sql.DataSource; - import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.configuration.runtime.DataSourceFactory; import org.apache.cayenne.di.Inject; import org.apache.cayenne.di.Injector; +import javax.sql.DataSource; + public class MockDataSourceFactory1 implements DataSourceFactory { @Inject protected Injector injector; - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { return null; } diff --git a/cayenne/src/test/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactoryIT.java b/cayenne/src/test/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactoryIT.java index 7c3438e69..d09e8598c 100644 --- a/cayenne/src/test/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactoryIT.java +++ b/cayenne/src/test/java/org/apache/cayenne/configuration/runtime/JNDIDataSourceFactoryIT.java @@ -19,13 +19,13 @@ package org.apache.cayenne.configuration.runtime; import org.apache.cayenne.configuration.DataNodeDescriptor; +import org.apache.cayenne.unit.CayenneTestsEnv; import org.apache.cayenne.unit.jdbc.TestDataSource; import org.apache.cayenne.unit.runtime.CayenneProjects; -import org.apache.cayenne.unit.CayenneTestsEnv; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + import javax.naming.InitialContext; -import javax.naming.NameNotFoundException; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -91,6 +91,6 @@ public class JNDIDataSourceFactoryIT { JNDISetup.doSetup(); JNDIDataSourceFactory factory = new JNDIDataSourceFactory(); - assertThrows(NameNotFoundException.class, () -> factory.getDataSource(descriptor)); + assertThrows(RuntimeException.class, () -> factory.getDataSource(descriptor)); } } diff --git a/cayenne/src/test/java/org/apache/cayenne/unit/runtime/RuntimeCaseSharedDataSourceFactory.java b/cayenne/src/test/java/org/apache/cayenne/unit/runtime/RuntimeCaseSharedDataSourceFactory.java index 56d2a3059..17cfc66ea 100644 --- a/cayenne/src/test/java/org/apache/cayenne/unit/runtime/RuntimeCaseSharedDataSourceFactory.java +++ b/cayenne/src/test/java/org/apache/cayenne/unit/runtime/RuntimeCaseSharedDataSourceFactory.java @@ -33,7 +33,7 @@ public class RuntimeCaseSharedDataSourceFactory implements DataSourceFactory { this.factory = factory; } - public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception { + public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) { return factory.sharedDataSource(); } }
