You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/12/11 12:17:23 UTC

svn commit: r485610 - in /cocoon/branches/BRANCH_2_1_X: ./ src/blocks/databases/java/org/apache/cocoon/databases/ src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ src/blocks/databases/mocks/com/ibatis/ src/blocks/databases/mocks/com/ibatis...

Author: cziegeler
Date: Mon Dec 11 03:17:22 2006
New Revision: 485610

URL: http://svn.apache.org/viewvc?view=rev&rev=485610
Log:
Add support for iBatis to use configured Excalibur data sources in iBatis.

Added:
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java   (with props)
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java   (with props)
Modified:
    cocoon/branches/BRANCH_2_1_X/status.xml

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java?view=auto&rev=485610
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java Mon Dec 11 03:17:22 2006
@@ -0,0 +1,116 @@
+/*
+ * 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.cocoon.databases.ibatis;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
+import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.component.ComponentException;
+import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.ComponentSelector;
+import org.apache.cocoon.components.CocoonComponentManager;
+
+import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
+
+/**
+ * This is an implementation if iBatis DataSourceFactory allowing to use
+ * a reference to an excalibur data source in the iBatis configuration.
+ * The configuration in the iBatis sqlMapConfig looks like this:
+ * <dataSource type="org.apache.cocoon.databases.ibatis.ExcaliburDataSourceFactory">
+ *   <property name="connection" value="Name of the Excalibur data source from the cocoon.xconf"/>
+ * </dataSource>
+ *
+ * @version $Id$
+ * @since 2.1.10
+ */
+public class ExcaliburDataSourceFactory implements DataSourceFactory {
+
+    protected DataSourceComponent datasource;
+
+    public DataSource getDataSource() {
+        return new DataSourceWrapper(this.datasource);
+    }
+
+    /**
+     * @see com.ibatis.sqlmap.engine.datasource.DataSourceFactory#initialize(java.util.Map)
+     */
+    public void initialize(Map values) {
+        final String connection = (String)values.get("connection");
+        if ( connection == null ) {
+            throw new RuntimeException("Connection configuration is missing for " + this.getClass().getName() + "." +
+                    " Have a look at the iBatis sqlMapConfig and check the 'connection' property for the data source.");
+        }
+        // get the component manager
+        final ComponentManager manager = CocoonComponentManager.getSitemapComponentManager();
+        if ( manager == null ) {
+            throw new RuntimeException("CocoonComponentManager is not available for " + this.getClass().getName() + "." +
+            " Make sure that you're initializing iBatis during an active request and not on startup.");            
+        }
+        try {
+            final ComponentSelector selector = (ComponentSelector)manager.lookup(DataSourceComponent.ROLE + "Selector");
+            try {
+                this.datasource = (DataSourceComponent)selector.select(connection);
+            } catch (ComponentException e) {
+                throw new CascadingRuntimeException("Unable to lookup data source with name " + connection + "." +
+                        " Check the cocoon.xconf and the iBatis sqlMapConfig.", e);                
+            }
+        } catch (ComponentException e) {
+            throw new CascadingRuntimeException("Unable to lookup datasource selector.", e);
+        }
+    }
+
+    protected static final class DataSourceWrapper implements DataSource {
+
+        protected final DataSourceComponent datasource;
+        protected PrintWriter writer = new PrintWriter(System.out);
+        protected int timeout = 0;
+
+        public DataSourceWrapper(DataSourceComponent d) {
+            this.datasource = d;
+        }
+
+        public Connection getConnection() throws SQLException {
+            return this.datasource.getConnection();
+        }
+
+        public Connection getConnection(String username, String password) throws SQLException {
+            return null;
+        }
+
+        public int getLoginTimeout() throws SQLException {
+            return this.timeout;
+        }
+
+        public PrintWriter getLogWriter() throws SQLException {
+            return this.writer;
+        }
+
+        public void setLoginTimeout(int seconds) throws SQLException {
+            this.timeout = seconds;
+        }
+
+        public void setLogWriter(PrintWriter out) throws SQLException {
+            this.writer = out;
+        }       
+    }
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/databases/ibatis/ExcaliburDataSourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java?view=auto&rev=485610
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java Mon Dec 11 03:17:22 2006
@@ -0,0 +1,34 @@
+/*
+ * 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 com.ibatis.sqlmap.engine.datasource;
+
+import javax.sql.DataSource;
+import java.util.Map;
+
+/**
+ * Mock class providing the declarations required to compile the Cocoon code when
+ * the actual library is not present.
+ *
+ * @version $Id$
+ * @since 2.1.10
+ */
+public interface DataSourceFactory {
+
+    void initialize(Map map);
+
+    DataSource getDataSource();
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/mocks/com/ibatis/sqlmap/engine/datasource/DataSourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?view=diff&rev=485610&r1=485609&r2=485610
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Mon Dec 11 03:17:22 2006
@@ -184,6 +184,9 @@
   <release version="@version@" date="@date@">
 -->
   <release version="2.1.10" date="TBD">
+   <action dev="CZ" type="add">
+     Databases: Add support for iBatis to use configured Excalibur data sources in iBatis.
+   </action>
    <action dev="AS" type="fix" fixes-bug="COCOON-1965" due-to="Jeroen Reijn">
       CForms: The filterfield widget was not defined for the forms manager.
    </action>