cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r452458 - in /cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon: acting/AbstractDatabaseAction.java components/source/impl/BlobSource.java util/JDBCTypeConversions.java
Date Tue, 03 Oct 2006 12:47:28 GMT
Author: vgritsenko
Date: Tue Oct  3 05:47:27 2006
New Revision: 452458

URL: http://svn.apache.org/viewvc?view=rev&rev=452458
Log:
copy from branch

Modified:
    cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java
    cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/components/source/impl/BlobSource.java
    cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/util/JDBCTypeConversions.java

Modified: cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java?view=diff&rev=452458&r1=452457&r2=452458
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java
(original)
+++ cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java
Tue Oct  3 05:47:27 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.
@@ -169,15 +169,19 @@
  *
  * @version $Id$
  */
-public abstract class AbstractDatabaseAction extends AbstractComplementaryConfigurableAction
implements Disposable {
+public abstract class AbstractDatabaseAction extends AbstractComplementaryConfigurableAction
+                                             implements Disposable {
+
     protected Map files = new HashMap();
     protected static final Map typeConstants;
     protected ServiceSelector dbselector;
 
     static {
-        /** Initialize the map of type names to jdbc column types.
-            Note that INTEGER, BLOB, and VARCHAR column types map to more than
-            one type name. **/
+        /*
+         * Initialize the map of type names to jdbc column types.
+         * Note that INTEGER, BLOB, and VARCHAR column types map to more than
+         * one type name.
+         */
         Map constants = new HashMap();
         constants.put("ascii", new Integer(Types.CLOB));
         constants.put("big-decimal", new Integer(Types.BIGINT));
@@ -216,6 +220,7 @@
         super.service(manager);
         this.dbselector = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE + "Selector");
     }
+
     /**
      * Get the Datasource we need.
      */
@@ -248,13 +253,17 @@
         switch (type.intValue()) {
             case Types.CLOB:
                 Clob dbClob = set.getClob(dbcol);
-                int length = (int) dbClob.length();
-                InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
-                byte[] buffer = new byte[length];
-                asciiStream.read(buffer);
-                String str = new String(buffer);
-                asciiStream.close();
-                value = str;
+                if (dbClob != null) {
+                    int length = (int) dbClob.length();
+                    InputStream is = new BufferedInputStream(dbClob.getAsciiStream());
+                    try {
+                        byte[] buffer = new byte[length];
+                        length = is.read(buffer);
+                        value = new String(buffer, 0, length);
+                    } finally {
+                        is.close();
+                    }
+                }
                 break;
             case Types.BIGINT:
                 value = set.getBigDecimal(dbcol);
@@ -693,7 +702,7 @@
     }
 
     /**
-     * Build a separed list with the Values of a Configuration Array  
+     * Build a separed list with the Values of a Configuration Array
      * @param values - build the list from
      * @param separator - Put a separator between the values of the list
      * @return - an StringBuffer with the builded List
@@ -712,7 +721,7 @@
     }
 
     /**
-     * Build a separed list with the Values of a Configuration Array  
+     * Build a separed list with the Values of a Configuration Array
      * @param values - build the list from
      * @param begin - Initial index
      * @return - an StringBuffer with the builded List

Modified: cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/components/source/impl/BlobSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/components/source/impl/BlobSource.java?view=diff&rev=452458&r1=452457&r2=452458
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/components/source/impl/BlobSource.java
(original)
+++ cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/components/source/impl/BlobSource.java
Tue Oct  3 05:47:27 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.
@@ -30,19 +30,18 @@
 import java.sql.Types;
 import java.util.Iterator;
 
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.avalon.framework.service.Serviceable;
-
-import org.apache.avalon.excalibur.datasource.DataSourceComponent;
-
-import org.apache.cocoon.CascadingIOException;
-
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceNotFoundException;
 import org.apache.excalibur.source.SourceValidity;
 
+import org.apache.cocoon.CascadingIOException;
+
 /**
  * A <code>Source</code> that takes its content in a single JDBC column. Any
  * kind of column can be used (clob, blob, varchar, etc), but "Blob" means
@@ -61,14 +60,16 @@
  *
  * @version $Id$
  */
-public class BlobSource extends AbstractLogEnabled implements Source, Serviceable {
+public class BlobSource extends AbstractLogEnabled
+                        implements Source, Serviceable {
+
+    private static final String URL_PREFIX = "blob:/";
+    private static final int URL_PREFIX_LEN = URL_PREFIX.length();
 
     /** The ServiceManager instance */
-    private ServiceManager manager = null;
+    private ServiceManager manager;
 
-    /**
-     * The system ID for this source
-     */
+    /** The system ID for this source */
     private String systemId;
 
     private String datasourceName;
@@ -79,23 +80,18 @@
 
     private String condition;
 
-    private final static String URL_PREFIX = "blob:/";
-    private final static int URL_PREFIX_LEN = URL_PREFIX.length();
-
     /**
      * Create a file source from a 'blob:' url and a component manager.
      * <p>The url is of the form "blob:/datasource/table/column[condition]
      */
     public BlobSource(String url) throws MalformedURLException {
-
         this.systemId = url;
 
         // Parse the url
         int start = URL_PREFIX_LEN;
-        int end;
 
         // Datasource
-        end = url.indexOf('/', start);
+        int end = url.indexOf('/', start);
         if (end == -1) {
             throw new MalformedURLException("Malformed blob source (cannot find datasource)
: " + url);
         }
@@ -163,12 +159,11 @@
             );
         }
 
-        Connection cnx = null;
+        Connection conn = null;
         Statement stmt = null;
-
         try {
-            cnx = getConnection();
-            stmt = cnx.createStatement();
+            conn = getConnection();
+            stmt = conn.createStatement();
 
             StringBuffer selectBuf = new StringBuffer("SELECT ").append(this.columnName).
                 append(" FROM ").append(this.tableName);
@@ -183,43 +178,50 @@
             }
 
             ResultSet rs = stmt.executeQuery(select);
-            rs.next();
+            if (!rs.next()) {
+                throw new SourceNotFoundException("Source not found.");
+            }
 
             int colType = rs.getMetaData().getColumnType(1);
-
             switch(colType) {
                 case Types.BLOB :
                     Blob blob = rs.getBlob(1);
-                    return new JDBCInputStream(blob.getBinaryStream(), cnx);
-                //break;
+                    if (blob != null) {
+                        return new JDBCInputStream(blob.getBinaryStream(), conn);
+                    }
+                    break;
 
                 case Types.CLOB :
                     Clob clob = rs.getClob(1);
-                    return new JDBCInputStream(clob.getAsciiStream(), cnx);
-                //break;
+                    if (clob != null) {
+                        return new JDBCInputStream(clob.getAsciiStream(), conn);
+                    }
+                    break;
 
                 default :
                     String value = rs.getString(1);
-                    stmt.close();
-                    rs.close();
-                    cnx.close();
-                    return new ByteArrayInputStream(value.getBytes());
+                    if (value != null) {
+                        return new ByteArrayInputStream(value.getBytes());
+                    }
             }
-        } catch(SQLException sqle) {
-            String msg = "Cannot retrieve content from " + this.systemId;
-            getLogger().error(msg, sqle);
 
+            return new ByteArrayInputStream(new byte[0]);
+        } catch (SQLException e) {
+            String msg = "Cannot retrieve content from " + this.systemId;
+            getLogger().error(msg, e);
+            // IOException would be more adequate, but SourceException is cascaded...
+            throw new SourceException(msg, e);
+        } finally {
             try {
-                if (cnx != null) {
-                    cnx.close();
+                if (stmt != null) {
+                    stmt.close();
                 }
-            } catch(SQLException sqle2) {
-                // PITA
-                throw new SourceException("Cannot close connection", sqle2);
-            }
-
-            // IOException would be more adequate, but ProcessingException is cascaded...
-            throw new SourceException(msg, sqle);
+            } catch (SQLException e) { /* ignored */ }
+            try {
+                if (conn != null) {
+                    conn.close();
+                }
+            } catch (SQLException e) { /* ignored */ }
         }
     }
 
@@ -232,23 +234,23 @@
     public SourceValidity getValidity() {
         return null;
     }
-    
+
     /**
      * Refresh the content of this object after the underlying data
      * content has changed.
      */
     public void refresh() {
     }
-    
+
     /**
-     * 
+     *
      * @see org.apache.excalibur.source.Source#exists()
      */
     public boolean exists() {
         // FIXME
         return true;
     }
-    
+
     /**
      * The mime-type of the content described by this object.
      * If the source is not able to determine the mime-type by itself
@@ -257,7 +259,7 @@
     public String getMimeType() {
         return null;
     }
-    
+
     /**
      * Return the content length of the content or -1 if the length is
      * unknown
@@ -302,7 +304,7 @@
         return new EmptyIterator();
     }
 
-    class EmptyIterator implements Iterator {
+    static class EmptyIterator implements Iterator {
         public boolean hasNext() { return false; }
         public Object next() { return null; }
         public void remove() {}
@@ -319,7 +321,7 @@
 
                 datasource = (DataSourceComponent)selector.select(this.datasourceName);
 
-            } catch(Exception e) {
+            } catch (Exception e) {
                 String msg = "Cannot get datasource '" + this.datasourceName + "'";
                 getLogger().error(msg);
                 throw new SourceException(msg, e);
@@ -327,7 +329,7 @@
 
             try {
                 return datasource.getConnection();
-            } catch(Exception e) {
+            } catch (Exception e) {
                 String msg = "Cannot get connection for datasource '" + this .datasourceName
+ "'";
                 getLogger().error(msg);
                 throw new SourceException(msg, e);
@@ -346,18 +348,18 @@
      */
     private class JDBCInputStream extends FilterInputStream {
 
-        Connection cnx;
+        private Connection cnx;
 
-        private final void closeCnx() throws IOException {
+        private void closeCnx() throws IOException {
             if (this.cnx != null) {
                 try {
-                    Connection tmp = cnx;
-                    cnx = null;
-                    tmp.close();
-                } catch(Exception e) {
+                    cnx.close();
+                } catch (Exception e) {
                     String msg = "Error closing the connection for " + BlobSource.this.getURI();
                     BlobSource.this.getLogger().warn(msg, e);
                     throw new CascadingIOException(msg + " : " + e.getMessage(), e);
+                } finally {
+                    cnx = null;
                 }
             }
         }
@@ -412,4 +414,3 @@
         }
     }
 }
-

Modified: cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/util/JDBCTypeConversions.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/util/JDBCTypeConversions.java?view=diff&rev=452458&r1=452457&r2=452458
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/util/JDBCTypeConversions.java
(original)
+++ cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/util/JDBCTypeConversions.java
Tue Oct  3 05:47:27 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.
@@ -21,6 +21,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.io.Reader;
 import java.math.BigDecimal;
 import java.sql.Array;
 import java.sql.Blob;
@@ -39,9 +40,9 @@
 
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.cocoon.servlet.multipart.Part;
+import org.apache.commons.lang.BooleanUtils;
 import org.apache.excalibur.source.Source;
 
-
 /**
  * Provide some utility methods to read from JDBC result sets or store
  * them to JDBC statements. Largely copied from
@@ -145,30 +146,35 @@
         }
         return object;
     }
-                    
-      
 
     /**
      * Get the Statement column so that the results are mapped correctly.
      * (this has been copied from AbstractDatabaseAction and modified slightly)
      */
-    public static Object getColumn(ResultSet set, Configuration column ) throws Exception
{
+    public static Object getColumn(ResultSet set, Configuration column)
+    throws Exception {
 
         Integer type = (Integer) JDBCTypeConversions.typeConstants.get(column.getAttribute("type"));
         String dbcol = column.getAttribute("name");
-        Object value = null;
+        Object value;
 
         switch (type.intValue()) {
         case Types.CLOB:
         case Types.CHAR:
             Clob dbClob = set.getClob(dbcol);
-            int length = (int) dbClob.length();
-            InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
-            byte[] buffer = new byte[length];
-            asciiStream.read(buffer);
-            String str = new String(buffer);
-            asciiStream.close();
-            value = str;
+            if (dbClob != null) {
+                int length = (int) dbClob.length();
+                char[] buffer = new char[length];
+                Reader r = dbClob.getCharacterStream();
+                try {
+                    length = r.read(buffer);
+                    value = new String(buffer, 0, length);
+                } finally {
+                    r.close();
+                }
+            } else {
+                value = null;
+            }
             break;
         case Types.BIGINT:
             value = set.getBigDecimal(dbcol);
@@ -207,7 +213,7 @@
             value = set.getArray(dbcol); // new Integer(set.getInt(dbcol));
             break;
         case Types.BIT:
-            value = new Boolean(set.getBoolean(dbcol));
+            value = BooleanUtils.toBooleanObject(set.getBoolean(dbcol));
             break;
         case Types.STRUCT:
             value = set.getObject(dbcol);
@@ -261,16 +267,16 @@
             }
         }
 
-        File file = null;
-        int length = -1;
-        InputStream asciiStream = null;
+        File file;
+        int length;
+        InputStream asciiStream;
 
         //System.out.println("========================================================================");
         //System.out.println("JDBCTypeConversions: setting type "+typeObject.intValue());
         switch (typeObject.intValue()) {
         case Types.CLOB:
             //System.out.println("CLOB");
-            Clob clob = null;
+            Clob clob;
             if (value instanceof Clob) {
                 clob = (Clob) value;
             } else if (value instanceof File) {
@@ -297,13 +303,13 @@
                 length = asciiText.length();
                 clob = new ClobHelper(asciiStream, length);
             }
-            
+
             statement.setClob(position, clob);
             break;
         case Types.CHAR:
             // simple large object, e.g. Informix's TEXT
             //System.out.println("CHAR");
-            
+
             if (value instanceof File) {
                 File asciiFile = (File) value;
                 asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
@@ -324,13 +330,13 @@
                 asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes()));
                 length = asciiText.length();
             }
-            
+
             statement.setAsciiStream(position, asciiStream, length);
             break;
         case Types.BIGINT:
             //System.out.println("BIGINT");
-            BigDecimal bd = null;
-            
+            BigDecimal bd;
+
             if (value instanceof BigDecimal) {
                 bd = (BigDecimal) value;
             } else if (value instanceof Number) {
@@ -338,13 +344,12 @@
             } else {
                 bd = new BigDecimal(value.toString());
             }
-            
+
             statement.setBigDecimal(position, bd);
             break;
         case Types.TINYINT:
             //System.out.println("TINYINT");
-            Byte b = null;
-            
+            Byte b;
             if (value instanceof Byte) {
                 b = (Byte) value;
             } else if (value instanceof Number) {
@@ -352,13 +357,12 @@
             } else {
                 b = new Byte(value.toString());
             }
-            
+
             statement.setByte(position, b.byteValue());
             break;
         case Types.DATE:
             //System.out.println("DATE");
-            Date d = null;
-            
+            Date d;
             if (value instanceof Date) {
                 d = (Date) value;
             } else if (value instanceof java.util.Date) {
@@ -368,13 +372,13 @@
             } else {
                 d = Date.valueOf(value.toString());
             }
-            
+
             statement.setDate(position, d);
             break;
         case Types.DOUBLE:
             //System.out.println("DOUBLE");
             double db;
-            
+
             if (value instanceof Number) {
                 db = (((Number) value).doubleValue());
             } else {
@@ -385,7 +389,7 @@
         case Types.FLOAT:
             //System.out.println("FLOAT");
             float f;
-            
+
             if (value instanceof Number) {
                 f = (((Number) value).floatValue());
             } else {
@@ -396,19 +400,18 @@
         case Types.NUMERIC:
             //System.out.println("NUMERIC");
             long l;
-            
+
             if (value instanceof Number) {
                 l = (((Number) value).longValue());
             } else {
                 l = Long.parseLong(value.toString());
             }
-            
+
             statement.setLong(position, l);
             break;
         case Types.SMALLINT:
             //System.out.println("SMALLINT");
-            Short s = null;
-            
+            Short s;
             if (value instanceof Short) {
                 s = (Short) value;
             } else if (value instanceof Number) {
@@ -416,13 +419,12 @@
             } else {
                 s = new Short(value.toString());
             }
-            
+
             statement.setShort(position, s.shortValue());
             break;
         case Types.TIME:
             //System.out.println("TIME");
-            Time t = null;
-            
+            Time t;
             if (value instanceof Time) {
                 t = (Time) value;
             } else if (value instanceof java.util.Date){
@@ -430,13 +432,12 @@
             } else {
                 t = Time.valueOf(value.toString());
             }
-            
+
             statement.setTime(position, t);
             break;
         case Types.TIMESTAMP:
             //System.out.println("TIMESTAMP");
-            Timestamp ts = null;
-            
+            Timestamp ts;
             if (value instanceof Time) {
                 ts = (Timestamp) value;
             } else if (value instanceof java.util.Date) {
@@ -444,7 +445,7 @@
             } else {
                 ts = Timestamp.valueOf(value.toString());
             }
-            
+
             statement.setTimestamp(position, ts);
             break;
         case Types.ARRAY:
@@ -472,7 +473,7 @@
             } else if (value instanceof Source){
                 statement.setBinaryStream(position, ((Source)value).getInputStream(), (int)((Source)value).getContentLength());
             } else {
-                Blob blob = null;
+                Blob blob;
                 if (value instanceof Blob) {
                     blob = (Blob) value;
                 } else if( value instanceof File) {
@@ -500,7 +501,7 @@
             } else if (value instanceof Part) {
                 statement.setBinaryStream(position, ((Part)value).getInputStream(), ((Part)value).getSize());
             } else {
-                if (value instanceof File) {           
+                if (value instanceof File) {
                    file = (File)value;
                } else if (value instanceof String) {
                    file = new File((String)value);
@@ -514,11 +515,11 @@
             break;
         case Types.INTEGER:
             //System.out.println("INTEGER");
-            Integer i = null;
+            Integer i;
             if (value instanceof Integer) {
                 i = (Integer) value;
-            } else if (value instanceof java.lang.Number) {
-                i = new Integer(((java.lang.Number) value).intValue());
+            } else if (value instanceof Number) {
+                i = new Integer(((Number) value).intValue());
             } else {
                 i = new Integer(value.toString());
             }
@@ -526,23 +527,21 @@
             break;
         case Types.BIT:
             //System.out.println("BIT");
-            Boolean bo = null;
+            Boolean bo;
             if (value instanceof Boolean) {
-                bo = (Boolean) value;
-            } else if (value instanceof java.lang.Number) {
-                bo = new Boolean(((java.lang.Number) value).intValue()==1);
+                bo = (Boolean)value;
+            } else if (value instanceof Number) {
+                bo = BooleanUtils.toBooleanObject(((Number) value).intValue()==1);
             } else {
-                bo = new Boolean(value.toString());
+                bo = BooleanUtils.toBooleanObject(value.toString());
             }
             statement.setBoolean(position, bo.booleanValue());
             break;
-            
+
         default:
             //System.out.println("default");
             throw new SQLException("Impossible exception - invalid type ");
         }
         //System.out.println("========================================================================");
     }
-    
-    
 }



Mime
View raw message