cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r494937 - /cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java
Date Wed, 10 Jan 2007 19:22:03 GMT
Author: cziegeler
Date: Wed Jan 10 11:22:02 2007
New Revision: 494937

URL: http://svn.apache.org/viewvc?view=rev&rev=494937
Log:
Make database reader usable with a normal DataSource.

Modified:
    cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java

Modified: cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java?view=diff&rev=494937&r1=494936&r2=494937
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java
(original)
+++ cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/reading/DatabaseReader.java
Wed Jan 10 11:22:02 2007
@@ -26,6 +26,8 @@
 import java.sql.Timestamp;
 import java.util.Map;
 
+import javax.sql.DataSource;
+
 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.configuration.Configurable;
@@ -33,8 +35,6 @@
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.ResourceNotFoundException;
 import org.apache.cocoon.caching.CacheableProcessingComponent;
@@ -57,8 +57,9 @@
 public class DatabaseReader extends ServiceableReader
                             implements Configurable, Disposable, CacheableProcessingComponent
{
 
-    private ServiceSelector datasourceSelector;
-    private DataSourceComponent datasource;
+    private DataSourceComponent dataSourceComponent;
+
+    private DataSource dataSource;
 
     private int typeColumn;
     private boolean defaultCache = true;
@@ -71,10 +72,12 @@
     private long lastModified = System.currentTimeMillis();
     private boolean doCommit;
 
-
-    public void service(final ServiceManager manager) throws ServiceException {
-        super.service(manager);
-        this.datasourceSelector = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE
+ "Selector");
+    /**
+     * Set a data source to be used by this component.
+     * @param dataSource A datasource.
+     */
+    public void setDataSource(DataSource dataSource) {
+        this.dataSource = dataSource;
     }
 
     /**
@@ -84,11 +87,13 @@
     public void configure(Configuration conf) throws ConfigurationException {
         this.defaultCache = conf.getChild("invalidate").getValue("never").equals("always");
 
-        String datasourceName = conf.getChild("use-connection").getValue();
-        try {
-            this.datasource = (DataSourceComponent) datasourceSelector.select(datasourceName);
-        } catch (ServiceException e) {
-            throw new ConfigurationException("Datasource '" + datasourceName + "' is not
available.", e);
+        if ( this.dataSource == null ) {
+            String datasourceName = conf.getChild("use-connection").getValue();
+            try {
+                this.dataSourceComponent = (DataSourceComponent) this.manager.lookup(DataSourceComponent.ROLE
+ '/' + datasourceName);
+            } catch (ServiceException e) {
+                throw new ConfigurationException("Datasource '" + datasourceName + "' is
not available.", e);
+            }
         }
     }
 
@@ -101,7 +106,11 @@
         super.setup(resolver, objectModel, src, par);
 
         try {
-            this.connection = datasource.getConnection();
+            if ( this.dataSource != null ) {
+                this.connection = this.dataSource.getConnection();
+            } else {
+                this.connection = dataSourceComponent.getConnection();
+            }
             if (this.connection.getAutoCommit()) {
                 this.connection.setAutoCommit(false);
             }
@@ -324,6 +333,9 @@
         }
     }
 
+    /**
+     * @see org.apache.cocoon.reading.AbstractReader#recycle()
+     */
     public void recycle() {
         super.recycle();
         this.resource = null;
@@ -331,19 +343,19 @@
         this.mimeType = null;
         this.typeColumn = 0;
 
-        try {
-            if (resultSet != null) {
+        if (resultSet != null) {
+            try {
                 resultSet.close();
-            }
-        } catch (SQLException e) { /* ignored */ }
-        resultSet = null;
+            } catch (SQLException e) { /* ignored */ }
+            resultSet = null;
+        }
 
-        try {
-            if (statement != null) {
+        if (statement != null) {
+            try {
                 statement.close();
-            }
-        } catch (SQLException e) { /* ignored */ }
-        statement = null;
+            } catch (SQLException e) { /* ignored */ }
+            statement = null;
+        }
 
         if (this.connection != null) {
             try {
@@ -363,19 +375,21 @@
         }
     }
 
+    /**
+     * @see org.apache.avalon.framework.activity.Disposable#dispose()
+     */
     public void dispose() {
-        recycle();
-        if (datasource != null) {
-            datasourceSelector.release(datasource);
-            datasource = null;
-        }
-        if (datasourceSelector != null) {
-            manager.release(datasourceSelector);
-            datasourceSelector = null;
+        this.recycle();
+        if (manager != null) {
+            this.manager.release(dataSourceComponent);
+            dataSourceComponent = null;
+            this.manager = null;
         }
-        manager = null;
     }
 
+    /**
+     * @see org.apache.cocoon.reading.AbstractReader#getMimeType()
+     */
     public String getMimeType() {
         if (mimeType != null) {
             return mimeType;



Mime
View raw message