You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by st...@apache.org on 2011/11/03 14:05:16 UTC

svn commit: r1197092 - in /myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api: ./ src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/

Author: struberg
Date: Thu Nov  3 13:05:16 2011
New Revision: 1197092

URL: http://svn.apache.org/viewvc?rev=1197092&view=rev
Log:
EXTCDI-236 initial version of ConfigurableDataSource

we should also provide a default implmentation of DataSourceConfig 
and write unit tests for it.

Added:
    myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/
    myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java   (with props)
    myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java   (with props)
Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/pom.xml

Modified: myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/pom.xml?rev=1197092&r1=1197091&r2=1197092&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/pom.xml (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/pom.xml Thu Nov  3 13:05:16 2011
@@ -36,6 +36,18 @@
             <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
             <artifactId>myfaces-extcdi-core-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>commons-beanutils</groupId>
+            <artifactId>commons-beanutils-core</artifactId>
+            <version>1.8.3</version>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>commons-logging</groupId>
+            <artifactId>commons-logging</artifactId>
+            <version>1.1.1</version>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 
     <build>
@@ -54,6 +66,39 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <createSourcesJar>true</createSourcesJar>
+                    <createDependencyReducedPom>false</createDependencyReducedPom>
+                    <minimizeJar>true</minimizeJar>
+                    <artifactSet>
+                        <includes>
+                            <include>commons-beanutils:commons-beanutils</include>
+                            <include>commons-logging:commons-logging</include>
+                        </includes>
+                    </artifactSet>
+                    <relocations>
+                        <relocation>
+                            <pattern>org.apache.commons.beanutils</pattern>
+                            <shadedPattern>org.apache.myfaces.codi.shaded.commons.beanutils</shadedPattern>
+                        </relocation>
+                        <relocation>
+                            <pattern>org.apache.commons.logging</pattern>
+                            <shadedPattern>org.apache.myfaces.codi.shaded.commons.logging</shadedPattern>
+                        </relocation>
+                    </relocations>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 

Added: myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java?rev=1197092&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java Thu Nov  3 13:05:16 2011
@@ -0,0 +1,221 @@
+/*
+ * 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.myfaces.extensions.cdi.jpa.api.datasource;
+
+import org.apache.commons.beanutils.BeanUtils;
+import org.apache.myfaces.extensions.cdi.core.api.provider.BeanManagerProvider;
+
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Map;
+import java.util.logging.Logger;
+import javax.naming.Context;
+
+/**
+ * <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>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 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;
+
+    /**
+     *
+     */
+    private volatile DataSource wrappedDataSource;
+
+    public ConfigurableDataSource()
+    {
+        loaded = false;
+        dataSourceConfig = BeanManagerProvider.getInstance().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 (wrappedDataSource == null)
+        {
+            initDataSource();
+        }
+
+        if (userName == null && password == null)
+        {
+            return wrappedDataSource.getConnection();
+        }
+        return wrappedDataSource.getConnection(userName, password);
+    }
+
+
+    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();
+    }
+
+    /**
+     *
+     */
+    protected void initDataSource() throws SQLException
+    {
+        // double check lock idiom on volatile member is ok as of Java5
+        if (wrappedDataSource != null)
+        {
+            return;
+        }
+
+        String jndiLookupName = dataSourceConfig.getJndiResourceName(connectionId);
+        if (jndiLookupName != null && jndiLookupName.length() > 0)
+        {
+            wrappedDataSource = resolveDataSourceViaJndi(jndiLookupName);
+            return;
+        }
+
+
+        // no JNDI, so we take the direct JDBC route.
+        String jdbcDriverClass = dataSourceConfig.getDriverClassName(connectionId);
+        if (jdbcDriverClass == null && jdbcDriverClass.length() == 0)
+        {
+            throw new SQLException("Neither a JNDI location nor a JDBC driver class name is configured!");
+        }
+
+        try
+        {
+            Class clazz =  Class.forName(jdbcDriverClass);
+
+            // the given driver classname must be a DataSource
+            if (!DataSource.class.isAssignableFrom(clazz))
+            {
+                throw new SQLException("Configured DriverClassName is not a javax.sql.DataSource: "
+                                       + jdbcDriverClass);
+            }
+
+            wrappedDataSource = (DataSource) clazz.newInstance();
+
+            Map<String, String> config = dataSourceConfig.getConnectionProperties(connectionId);
+            for (Map.Entry<String, String> configOption : config.entrySet())
+            {
+                BeanUtils.setProperty(wrappedDataSource, configOption.getKey(), configOption.getValue());
+            }
+        }
+        catch (Exception e)
+        {
+            wrappedDataSource = null;
+
+            if (e instanceof SQLException)
+            {
+                throw (SQLException) e;
+            }
+            throw new SQLException(e);
+        }
+    }
+
+    protected DataSource resolveDataSourceViaJndi(String jndiLookupName)
+    {
+        DataSource ds = null;
+        try
+        {
+            Context jndiContext = new InitialContext();
+            ds = (DataSource) jndiContext.lookup(jndiLookupName);
+        }
+        catch (Exception e)
+        {
+            throw new IllegalArgumentException("Could not lookup DataSource from JNDI using " + jndiLookupName);
+        }
+
+        return ds;
+    }
+
+}

Propchange: myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/ConfigurableDataSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java?rev=1197092&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java Thu Nov  3 13:05:16 2011
@@ -0,0 +1,79 @@
+/*
+ * 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.myfaces.extensions.cdi.jpa.api.datasource;
+
+import org.apache.myfaces.extensions.cdi.core.api.config.CodiConfig;
+
+import java.util.Map;
+
+/**
+ * <p>Configuration for the DataSource.
+ * The <code>connectionId</code> parameter can be used to distinguish
+ * between different databases.</p>
+ *
+ * <p>There are 2 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 JDBC connecion properties - This will be used if {@link #getJndiResourceName(String)}
+ *         returns <code>null</code>. In this case you must specify the {@link #getDriverClassName(String)}
+ *         and {@link #getConnectionProperties(String)}.
+ *     </li>
+ * </ol>
+ *
+ * </p>
+ *
+ *
+ */
+public interface DataSourceConfig extends CodiConfig
+{
+
+    /**
+     * 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 #getDriverClassName(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.
+     */
+    public 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
+     */
+    public String getDriverClassName(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.
+     */
+    public Map<String, String> getConnectionProperties(String connectionId);
+
+
+}

Propchange: myfaces/extensions/cdi/trunk/jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/datasource/DataSourceConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native