You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2014/06/12 23:18:08 UTC
git commit: DELTASPIKE-610 remove ConfigurableDataSource
Repository: deltaspike
Updated Branches:
refs/heads/master 8fc6b38bb -> 406913cba
DELTASPIKE-610 remove ConfigurableDataSource
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/406913cb
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/406913cb
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/406913cb
Branch: refs/heads/master
Commit: 406913cbae58424fb289535ba002933b5576f75c
Parents: 8fc6b38
Author: Mark Struberg <st...@apache.org>
Authored: Thu Jun 12 23:16:52 2014 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Jun 12 23:16:52 2014 +0200
----------------------------------------------------------------------
.../jpa/api/datasource/DataSourceConfig.java | 135 ---------
.../impl/datasource/ConfigurableDataSource.java | 303 -------------------
.../datasource/ConfigurableDataSourceTest.java | 81 -----
.../jpa/datasource/LocalDatabaseConfig.java | 58 ----
4 files changed, 577 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/406913cb/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/datasource/DataSourceConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/datasource/DataSourceConfig.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/datasource/DataSourceConfig.java
deleted file mode 100644
index 4b695fc..0000000
--- a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/datasource/DataSourceConfig.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.jpa.api.datasource;
-
-import org.apache.deltaspike.core.api.config.DeltaSpikeConfig;
-
-import java.util.Properties;
-
-/**
- * <h3>Configuration for a dynamic DataSource.</h3>
- * <p>If you use the ConfigurableDataSource then this interface needs
- * to be implemented in customer projects to return
- * the proper values to connect to the database.</p>
- *
- * <p>The <code>connectionId</code> parameter can be used to distinguish
- * between different databases.</p>
- *
- * <p>There are 3 ways to configure a DataSource
- *
- * <ol>
- * <li>
- * via JNDI lookup - specify the JNDI resource location for the DataSource via
- * {@link #getJndiResourceName(String)}
- * </li>
- * <li>
- * via a DataSource class name plus properties - This will be used if {@link #getJndiResourceName(String)}
- * returns <code>null</code>. In this case you must specify the {@link #getConnectionClassName(String)}
- * to contain the class name of a DataSource, e.g.
- * <code>"";com.mchange.v2.c3p0.ComboPooledDataSource"";</code>
- * and return additional configuration via {@link #getConnectionProperties(String)}.
- * </li>
- * <li>
- * via a JDBC Driver class name plus properties - This will be used if {@link #getJndiResourceName(String)}
- * returns <code>null</code>. In this case you must specify the {@link #getConnectionClassName(String)}
- * to contain the class name of a javax.sql.Driver, e.g.
- * <code>"";org.hsqldb.jdbcDriver"";</code>
- * and return additional configuration via {@link #getConnectionProperties(String)}.
- * </li>
- * </ol>
- * </p>
- *
- * <h3>Usage</h3>
- * <p>Instead of configuring any hardcoded DataSource provider, JDBC driver
- * or JNDI location of the DataSource you just configure our <i>ConfigurableDataSource</i>
- * in your persistence.xml. This class is an implementation of DataSource and acts as
- * kind of a proxy to determine the underlying database configuration for your usage
- * scenarios.</p>
- * <p>A possible persistence.xml configuration would look like the following:
- * <pre>
- * <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- * xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
- * http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- * version="1.0">
- *
- * <persistence-unit name="test" >
- * <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
- *
- * <class>org.apache.deltaspike.jpa.test.TestEntity</class>
- *
- * <properties>
- * <property name="openjpa.ConnectionDriverName"
- * value="org.apache.deltaspike.jpa.impl.datasource.ConfigurableDataSource"/>
- * <property name="openjpa.ConnectionProperties"
- * value="connectionId=core"/>
- * </properties>
- *
- * </persistence-unit>
- * </persistence>
- * </pre>
- *
- * </p>
- *
- */
-public interface DataSourceConfig extends DeltaSpikeConfig
-{
-
- /**
- * Return the JNDI resource name if the DataSource should get retrieved via JNDI.
- * If a native JDBC connection should get used, this method must return <code>null</code>.
- * And the JDBC connection properties must get set via
- * {@link #getConnectionClassName(String)} and {@link #getConnectionProperties(String)}.
- *
- * @param connectionId used to distinguish between different databases.
- *
- * @return the JNDI lookup for the DataSource or <code>null</code> if a native
- * JDBC connection should get used.
- */
- String getJndiResourceName(String connectionId);
-
- /**
- * @param connectionId used to distinguish between different databases.
- *
- * @return the fully qualified class name of the JDBC driver for the underlying connection
- * or <code>null</code> if {@link #getJndiResourceName(String)} is not being used
- */
- String getConnectionClassName(String connectionId);
-
- /**
- * @param connectionId used to distinguish between different databases.
- *
- * @return allows to configure additional connection properties which will
- * get applied to the underlying JDBC driver or <code>null</code>
- * if {@link #getJndiResourceName(String)} is not being used
- */
- Properties getConnectionProperties(String connectionId);
-
- /**
- * This will only get used if {@link #getConnectionClassName(String)} is a javax.sql.Driver.
- * Foor Datasources, the underlying connection url must get configured via
- * {@link #getConnectionProperties(String)}.
- *
- * @param connectionId used to distinguish between different databases.
- *
- * @return the connection url, e.g. "jdbc://..."
- * or <code>null</code> if {@link #getJndiResourceName(String)} is not being used
- */
- String getJdbcConnectionUrl(String connectionId);
-}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/406913cb/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/datasource/ConfigurableDataSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/datasource/ConfigurableDataSource.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/datasource/ConfigurableDataSource.java
deleted file mode 100644
index fa5159c..0000000
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/datasource/ConfigurableDataSource.java
+++ /dev/null
@@ -1,303 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.jpa.impl.datasource;
-
-
-import javax.sql.DataSource;
-import java.io.PrintWriter;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.Driver;
-import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
-import java.util.Map;
-import java.util.Properties;
-import java.util.logging.Logger;
-
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-import org.apache.deltaspike.core.impl.util.JndiUtils;
-import org.apache.deltaspike.jpa.api.datasource.DataSourceConfig;
-
-/**
- * <p>This class can be used instead of a real DataSource.
- * It is a simple wrapper to hide any database configuration details
- * and make it configurable via CDI.</p>
- * <p>See {@link DataSourceConfig} on how to configure it!</p>
- *
- * <p>The configuration itself will be provided via CDI mechanics.
- * To distinguish different databases, users can specify a
- * <code>connectionId</code>. If no <code>connectionId</code> is set,
- * the String <code>default</code> will be used</p>
- */
-public class ConfigurableDataSource implements DataSource
-{
- /**
- * config and settings are loaded only once.
- */
- private volatile boolean loaded;
-
- /**
- * The connectionId allows to configure multiple databases.
- * This can e.g. be used to distinguish between a 'customer' and 'admin'
- * database.
- */
- private String connectionId = "default";
-
- /**
- * The underlying configuration of the datasource
- */
- private DataSourceConfig dataSourceConfig;
-
- /**
- * In case of an underlying JDBC connection, we need to provide the connection URL;
- */
- private String jdbcConnectionURL;
-
- /**
- * In case of an underlying JDBC connection, we need to provide some configured properties
- */
- private Properties connectionProperties;
-
- /**
- * The underlying 'real' DataSource if we got a DataSource either via JNDI
- * or as class name.
- */
- private DataSource wrappedDataSource = null;
-
- /**
- * The underlying jdbcDriver if configured.
- */
- private Driver wrappedJdbcDriver = null;
-
-
- public ConfigurableDataSource()
- {
- loaded = false;
- dataSourceConfig = BeanProvider.getContextualReference(DataSourceConfig.class);
- }
-
- public void setConnectionId(String connectionId)
- {
- if (loaded)
- {
- throw new IllegalStateException("connectionId must not get changed after the DataSource was established");
- }
- this.connectionId = connectionId;
- }
-
- public Connection getConnection() throws SQLException
- {
- return getConnection(null, null);
- }
-
- public Connection getConnection(String userName, String password) throws SQLException
- {
- if (!loaded)
- {
- initDataSource();
- }
-
- if (wrappedDataSource != null)
- {
- // if we got a DataSource as underlying connector
- if (userName == null && password == null )
- {
- return wrappedDataSource.getConnection();
- }
- return wrappedDataSource.getConnection(userName, password);
- }
- else if (wrappedJdbcDriver != null)
- {
- // if we got a native JDBC Driver class as underlying connector
- return wrappedJdbcDriver.connect(jdbcConnectionURL, connectionProperties);
- }
-
- return null;
- }
-
-
- public PrintWriter getLogWriter() throws SQLException
- {
- return null;
- }
-
- public void setLogWriter(PrintWriter printWriter) throws SQLException
- {
- }
-
- public void setLoginTimeout(int loginTimeout) throws SQLException
- {
- }
-
- public int getLoginTimeout() throws SQLException
- {
- return 0;
- }
-
- public <T> T unwrap(Class<T> iface) throws SQLException
- {
- if (isWrapperFor(iface))
- {
- return (T) this;
- }
- else
- {
- return null;
- }
- }
-
- public boolean isWrapperFor(Class<?> iface) throws SQLException
- {
- return iface.isAssignableFrom(ConfigurableDataSource.class);
- }
-
- /**
- * NEW JDK1.7 signature.
- * This makes sure that CODI can also get compiled using java-7.
- * This method is not actively used though.
- */
- public Logger getParentLogger() throws SQLFeatureNotSupportedException
- {
- throw new SQLFeatureNotSupportedException();
- }
-
- /**
- * Initialize the DataSource either from JNDI or via JDBC Driver.
- * This method does not actually create a connection yet.
- */
- protected synchronized void initDataSource() throws SQLException
- {
- // double check lock idiom on volatile member is ok as of Java5
- if (loaded)
- {
- return;
- }
- loaded = true;
-
- String jndiLookupName = dataSourceConfig.getJndiResourceName(connectionId);
- if (jndiLookupName != null && jndiLookupName.length() > 0)
- {
- wrappedDataSource = JndiUtils.lookup(jndiLookupName, DataSource.class);
- return;
- }
-
- // no JNDI, so we take the direct JDBC route.
- String dataSourceClass = dataSourceConfig.getConnectionClassName(connectionId);
-
- if (dataSourceClass == null || dataSourceClass.length() == 0)
- {
- throw new SQLException("Neither a JNDI location nor a JDBC driver class name is configured!");
- }
-
- connectionProperties = dataSourceConfig.getConnectionProperties(connectionId);
-
- try
- {
- // we explicitely use class.forName and NOT the ThreadContextClassLoader!
- Class clazz = Class.forName(dataSourceClass);
-
- // the given driver classname must be a DataSource
- if (DataSource.class.isAssignableFrom(clazz))
- {
- wrappedDataSource = (DataSource) clazz.newInstance();
-
- for (Map.Entry configOption : connectionProperties.entrySet())
- {
- String name = (String) configOption.getKey();
- String value = (String) configOption.getValue();
- setProperty(wrappedDataSource, name, value);
- }
- }
- else if (Driver.class.isAssignableFrom(clazz))
- {
- // if we have a javax.sql.Driver then we also need an explicite connection URL
- jdbcConnectionURL = dataSourceConfig.getJdbcConnectionUrl(connectionId);
- if (jdbcConnectionURL == null)
- {
- throw new SQLException("Neither a JNDI location nor a JDBC connection URL is configured!");
- }
-
- wrappedJdbcDriver = (Driver) clazz.newInstance();
- }
- else
- {
- throw new SQLException("Configured DriverClassName is not a javax.sql.DataSource "
- + "nor a javax.sql.Driver: "
- + dataSourceClass);
- }
- }
- catch (RuntimeException e)
- {
- wrappedDataSource = null;
- throw e;
- }
- catch (SQLException e)
- {
- wrappedDataSource = null;
- throw e;
- }
- catch (Exception e)
- {
- wrappedDataSource = null;
- throw new RuntimeException(e);
- }
- }
-
- protected void setProperty(Object instance, String key, String value)
- throws InvocationTargetException, IllegalAccessException
- {
- if (key.length() == 0)
- {
- throw new IllegalArgumentException("property name must not be empty!");
- }
-
- String setterName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1);
- Method setter = null;
- try
- {
- setter = instance.getClass().getMethod(setterName, String.class);
- }
- catch (NoSuchMethodException e)
- {
- try
- {
- setter = instance.getClass().getMethod(setterName, Object.class);
- }
- catch (NoSuchMethodException e1)
- {
- //X TODO probably search for fields to set
-
- }
- }
-
- if (setter == null)
- {
- return;
- }
-
- if (!setter.isAccessible())
- {
- setter.setAccessible(true);
- }
-
- setter.invoke(instance, value);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/406913cb/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/ConfigurableDataSourceTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/ConfigurableDataSourceTest.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/ConfigurableDataSourceTest.java
deleted file mode 100644
index 8c68912..0000000
--- a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/ConfigurableDataSourceTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.test.jpa.datasource;
-
-import org.apache.deltaspike.jpa.api.datasource.DataSourceConfig;
-import org.apache.deltaspike.jpa.impl.datasource.ConfigurableDataSource;
-import org.apache.deltaspike.jpa.impl.transaction.context.TransactionContextExtension;
-import org.apache.deltaspike.test.category.SeCategory;
-import org.apache.deltaspike.test.util.ArchiveUtils;
-import org.jboss.arquillian.container.test.api.Deployment;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.asset.EmptyAsset;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.jboss.shrinkwrap.api.spec.WebArchive;
-import org.junit.experimental.categories.Category;
-import org.junit.runner.RunWith;
-import org.junit.Test;
-import org.junit.Assert;
-
-import javax.enterprise.inject.spi.Extension;
-import javax.inject.Inject;
-import java.sql.Connection;
-
-@RunWith(Arquillian.class)
-@Category(SeCategory.class)
-public class ConfigurableDataSourceTest
-{
-
- @Deployment
- public static WebArchive deploy()
- {
- JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "defaultInjectionTest.jar")
- .addPackage(ArchiveUtils.SHARED_PACKAGE)
- .addPackage(LocalDatabaseConfig.class.getPackage().getName())
- .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
-
- return ShrinkWrap.create(WebArchive.class)
- .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive())
- .addAsLibraries(testJar)
- .addAsServiceProvider(Extension.class, TransactionContextExtension.class)
- .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml");
- }
-
- @Inject
- private DataSourceConfig dataSourceConfig;
-
- @Test
- public void testLocalDataSource() throws Exception
- {
- Assert.assertNull(dataSourceConfig.getJndiResourceName(null));
- Assert.assertEquals("org.apache.deltaspike.test.jpa.datasource.DummyJdbcDriver",
- dataSourceConfig.getConnectionClassName(null));
- }
-
- @Test
- public void testConfigurableDataSource() throws Exception
- {
- // we do not use @Inject as this is normally instantiated
- // via newInstance()
- ConfigurableDataSource cds = new ConfigurableDataSource();
- Connection connection = cds.getConnection();
- Assert.assertNotNull(connection);
- }
-}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/406913cb/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/LocalDatabaseConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/LocalDatabaseConfig.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/LocalDatabaseConfig.java
deleted file mode 100644
index dee291f..0000000
--- a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/datasource/LocalDatabaseConfig.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.test.jpa.datasource;
-
-
-import org.apache.deltaspike.jpa.api.datasource.DataSourceConfig;
-
-import javax.enterprise.context.ApplicationScoped;
-import java.util.Properties;
-
-/**
- * {@link DataSourceConfig} for our test database.
- */
-@ApplicationScoped
-public class LocalDatabaseConfig implements DataSourceConfig
-{
- public String getJndiResourceName(String connectionId)
- {
- return null;
- }
-
- public String getConnectionClassName(String connectionId)
- {
- return "org.apache.deltaspike.test.jpa.datasource.DummyJdbcDriver";
- }
-
- public String getJdbcConnectionUrl(String connectionId)
- {
- return "jdbc:dummy:mem:test";
- }
-
- public Properties getConnectionProperties(String connectionId)
- {
- Properties props = new Properties();
-
- props.put("userName", "sa");
- props.put("", "");
-
- return props;
- }
-
-}