cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r452159 - /cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java
Date Mon, 02 Oct 2006 18:20:46 GMT
Author: vgritsenko
Date: Mon Oct  2 11:20:45 2006
New Revision: 452159

URL: http://svn.apache.org/viewvc?view=rev&rev=452159
Log:
COCOON-1441: Improve database reader

Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java?view=diff&rev=452159&r1=452158&r2=452159
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/databases/java/org/apache/cocoon/reading/DatabaseReader.java
Mon Oct  2 11:20:45 2006
@@ -5,9 +5,9 @@
  * 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.
@@ -56,23 +56,26 @@
  * @version CVS $Id$
  */
 public class DatabaseReader extends ServiceableReader
-    implements Configurable, Disposable, CacheableProcessingComponent
-{
+                            implements Configurable, Disposable, CacheableProcessingComponent
{
 
-    private ServiceSelector dbselector;
-    private String dsn;
-    private long lastModified = System.currentTimeMillis();
-    private InputStream resource = null; // because HSQL doesn't yet implement getBlob()
-    private Connection con = null;
-    private DataSourceComponent datasource = null;
-    private String mimeType = null;
-    private int typeColumn = 0;
-    private boolean doCommit = false;
+    private ServiceSelector datasourceSelector;
+    private DataSourceComponent datasource;
+
+    private int typeColumn;
     private boolean defaultCache = true;
 
+    private Connection connection;
+    private PreparedStatement statement;
+    private ResultSet resultSet;
+    private InputStream resource; // because HSQL doesn't yet implement getBlob()
+    private String mimeType;
+    private long lastModified = System.currentTimeMillis();
+    private boolean doCommit;
+
+
     public void service(final ServiceManager manager) throws ServiceException {
         super.service(manager);
-        this.dbselector = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE + "Selector");
+        this.datasourceSelector = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE
+ "Selector");
     }
 
     /**
@@ -80,8 +83,14 @@
      * for all instances.
      */
     public void configure(Configuration conf) throws ConfigurationException {
-        this.dsn = conf.getChild("use-connection").getValue();
         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);
+        }
     }
 
     /**
@@ -89,49 +98,41 @@
      * the source and sitemap <code>Parameters</code> used to process the request.
      */
     public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
-        throws ProcessingException, SAXException, IOException {
+    throws ProcessingException, SAXException, IOException {
         super.setup(resolver, objectModel, src, par);
 
-        PreparedStatement statement = null;
         try {
-            this.datasource = (DataSourceComponent) dbselector.select(dsn);
-            this.con = datasource.getConnection();
-
-            if (this.con.getAutoCommit()) {
-                this.con.setAutoCommit(false);
+            this.connection = datasource.getConnection();
+            if (this.connection.getAutoCommit()) {
+                this.connection.setAutoCommit(false);
             }
 
-            statement = con.prepareStatement(getQuery());
+            statement = connection.prepareStatement(getQuery());
             statement.setString(1, this.source);
-            ResultSet set = statement.executeQuery();
-            if (!set.next()) throw new ResourceNotFoundException("There is no resource with
that key");
+
+            resultSet = statement.executeQuery();
+            if (!resultSet.next()) {
+                throw new ResourceNotFoundException("There is no resource with that key");
+            }
 
             Response response = ObjectModelHelper.getResponse(objectModel);
             Request request = ObjectModelHelper.getRequest(objectModel);
 
-            if (this.modifiedSince(set, request, response)) {
-                this.resource = set.getBinaryStream(1);
-                if (this.typeColumn != 0) {
-                    this.mimeType = set.getString(this.typeColumn);
-                }
-
+            if (modifiedSince(resultSet, request, response)) {
+                this.resource = resultSet.getBinaryStream(1);
                 if (this.resource == null) {
                     throw new ResourceNotFoundException("There is no resource with that key");
                 }
+
+                if (this.typeColumn != 0) {
+                    this.mimeType = resultSet.getString(this.typeColumn);
+                }
             }
 
             this.doCommit = true;
         } catch (Exception e) {
             this.doCommit = false;
             throw new ResourceNotFoundException("DatabaseReader error:", e);
-        } finally {
-            try {
-                if (statement != null) {
-                    statement.close();
-                }
-            } catch(SQLException sqle) {
-                throw new ResourceNotFoundException("Cannot close connection", sqle);
-            }
         }
     }
 
@@ -167,14 +168,12 @@
     public void generate() throws ProcessingException, SAXException, IOException {
         try {
             Response response = ObjectModelHelper.getResponse(objectModel);
-            this.serialize(response);
-        } catch (IOException ioe) {
+            serialize(response);
+        } catch (IOException e) {
             getLogger().warn("Assuming client reset stream");
-
             this.doCommit = false;
         } catch (Exception e) {
             this.doCommit = false;
-
             throw new ResourceNotFoundException("DatabaseReader error:", e);
         }
     }
@@ -184,14 +183,13 @@
      * If the required parameters do not exist, then we cannot build a
      * correct query.
      */
-    public String getQuery() throws Exception {
+    protected String getQuery() throws ProcessingException {
         String table = this.parameters.getParameter("table", null);
         String column = this.parameters.getParameter("image", null);
         String key = this.parameters.getParameter("key", null);
         String where = this.parameters.getParameter("where", null);
         String orderBy = this.parameters.getParameter("order-by", null);
         String typeColumn = this.parameters.getParameter("type-column", null);
-        int columnNo = 1;
 
         if (table == null || column == null || key==null) {
             throw new ProcessingException("We are missing a required parameter.  Please include
'table', 'image', and 'key'");
@@ -200,6 +198,7 @@
         String date = this.parameters.getParameter("last-modified", null);
         StringBuffer query = new StringBuffer("SELECT ");
 
+        int columnNo = 1;
         query.append(column);
         columnNo++;
 
@@ -222,7 +221,6 @@
         if (null != typeColumn) {
             query.append(", ").append(typeColumn);
             this.typeColumn = columnNo;
-            columnNo++;
         }
 
         query.append(" FROM ").append(table);
@@ -251,10 +249,9 @@
      * more prone to change than filesystems, and don't have intrinsic
      * timestamps on column updates.
      */
-    public boolean modifiedSince(ResultSet set, Request request, Response response)
+    protected boolean modifiedSince(ResultSet set, Request request, Response response)
     throws SQLException {
         String lastModified = this.parameters.getParameter("last-modified", null);
-
         if (lastModified != null) {
             Timestamp modified = set.getTimestamp(lastModified, null);
             if (null != modified) {
@@ -264,7 +261,6 @@
             }
 
             response.setDateHeader("Last-Modified", this.lastModified);
-
             return this.lastModified > request.getDateHeader("if-modified-since");
         }
 
@@ -285,7 +281,6 @@
         InputStream is = new BufferedInputStream(this.resource);
 
         long expires = parameters.getParameterAsInteger("expires", -1);
-
         if (expires > 0) {
             response.setDateHeader("Expires", System.currentTimeMillis() + expires);
         }
@@ -293,8 +288,7 @@
         response.setHeader("Accept-Ranges", "bytes");
 
         byte[] buffer = new byte[8192];
-        int length = -1;
-
+        int length;
         while ((length = is.read(buffer)) > -1) {
             out.write(buffer, 0, length);
         }
@@ -337,44 +331,56 @@
         this.mimeType = null;
         this.typeColumn = 0;
 
-        if (this.con != null) {
+        try {
+            if (resultSet != null) {
+                resultSet.close();
+            }
+        } catch (SQLException e) { /* ignored */ }
+        resultSet = null;
+
+        try {
+            if (statement != null) {
+                statement.close();
+            }
+        } catch (SQLException e) { /* ignored */ }
+        statement = null;
+
+        if (this.connection != null) {
             try {
                 if (this.doCommit) {
-                    this.con.commit();
+                    this.connection.commit();
                 } else {
-                    this.con.rollback();
+                    this.connection.rollback();
                 }
-            } catch (SQLException se) {
-                getLogger().warn("Could not commit or rollback connection", se);
+            } catch (SQLException e) {
+                getLogger().warn("Could not commit or rollback connection", e);
             }
 
             try {
-                this.con.close();
-            } catch (SQLException se) {
-                getLogger().warn("Could not close connection", se);
-            }
-
-            this.con = null;
-        }
-
-        if (this.datasource != null) {
-            this.dbselector.release(this.datasource);
-            this.datasource = null;
+                this.connection.close();
+            } catch (SQLException e) { /* ignored */ }
+            this.connection = null;
         }
     }
 
-    /**
-     * dispose()
-     */
-    public void dispose()
-    {
-        this.manager.release(this.dbselector);
+    public void dispose() {
+        recycle();
+        if (datasource != null) {
+            datasourceSelector.release(datasource);
+            datasource = null;
+        }
+        if (datasourceSelector != null) {
+            manager.release(datasourceSelector);
+            datasourceSelector = null;
+        }
+        manager = null;
     }
 
     public String getMimeType() {
-        return (this.mimeType != null ? 
-                this.mimeType : 
-                this.parameters.getParameter("content-type", super.getMimeType()));
-    }
+        if (mimeType != null) {
+            return mimeType;
+        }
 
+        return this.parameters.getParameter("content-type", super.getMimeType());
+    }
 }



Mime
View raw message