You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by se...@apache.org on 2001/08/03 05:37:24 UTC

cvs commit: jakarta-james/proposals/noparse-mimemessage/java/org/apache/james/mailrepository MimeMessageJDBCSource.java

serge       01/08/02 20:37:24

  Modified:    proposals/noparse-mimemessage/java/org/apache/james/mailrepository
                        MimeMessageJDBCSource.java
  Log:
  Added implementation of getSize method.  This will check for an optional SQL query to get the size of a message (and will optionally add the file size to this).  Also added some close statements to the actual message stream query.
  
  Revision  Changes    Path
  1.4       +59 -2     jakarta-james/proposals/noparse-mimemessage/java/org/apache/james/mailrepository/MimeMessageJDBCSource.java
  
  Index: MimeMessageJDBCSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/proposals/noparse-mimemessage/java/org/apache/james/mailrepository/MimeMessageJDBCSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MimeMessageJDBCSource.java	2001/08/01 03:49:23	1.3
  +++ MimeMessageJDBCSource.java	2001/08/03 03:37:24	1.4
  @@ -32,6 +32,7 @@
       StreamRepository sr = null;
   
       String retrieveMessageBodySQL = null;
  +    String retrieveMessageBodySizeSQL = null;
   
       /**
        * Construct a MimeMessageSource based on a JDBC repository, a key, and a
  @@ -50,6 +51,7 @@
           this.sr = sr;
   
           retrieveMessageBodySQL = repository.sqlQueries.getProperty("retrieveMessageBodySQL");
  +        retrieveMessageBodySizeSQL = repository.sqlQueries.getProperty("retrieveMessageBodySizeSQL");
       }
   
       /**
  @@ -73,11 +75,66 @@
               }
   
               byte[] headers = rsRetrieveMessageStream.getBytes(1);
  +            rsRetrieveMessageStream.close();
  +            retrieveMessageStream.close();
  +            conn.close();
  +
               InputStream in = new ByteArrayInputStream(headers);
  -            if (sr != null) {
  -                in = new SequenceInputStream(in, sr.get(key));
  +            try {
  +                if (sr != null) {
  +                    in = new SequenceInputStream(in, sr.get(key));
  +                }
  +            } catch (Exception e) {
  +                //ignore this... either sr is null, or the file does not exist
  +                // or something else
               }
               return in;
  +        } catch (SQLException sqle) {
  +            throw new IOException(sqle.toString());
  +        }
  +    }
  +
  +    /**
  +     * Runs a custom SQL statement to check the size of the message body
  +     */
  +    public synchronized long getSize() throws IOException {
  +        if (retrieveMessageBodySizeSQL == null) {
  +            //There was no SQL statement for this repository... figure it out the hard way
  +            return super.getSize();
  +        }
  +
  +        try {
  +            Connection conn = repository.getConnection();
  +
  +            PreparedStatement retrieveMessageSize = conn.prepareStatement(retrieveMessageBodySizeSQL);
  +            retrieveMessageSize.setString(1, key);
  +            retrieveMessageSize.setString(2, repository.repositoryName);
  +            ResultSet rsRetrieveMessageSize = retrieveMessageSize.executeQuery();
  +
  +            if (!rsRetrieveMessageSize.next()) {
  +                throw new IOException("Could not find message");
  +            }
  +
  +            long size = rsRetrieveMessageSize.getLong(1);
  +            rsRetrieveMessageSize.close();
  +            retrieveMessageSize.close();
  +            conn.close();
  +
  +            try {
  +                if (sr != null) {
  +                    InputStream in = sr.get(key);
  +                    int len = 0;
  +                    byte[] block = new byte[1024];
  +                    while ((len = in.read(block)) > -1) {
  +                        size += len;
  +                    }
  +                    in.close();
  +                }
  +            } catch (Exception e) {
  +                //ignore this... either sr is null, or the file does not exist
  +                // or something else
  +            }
  +            return size;
           } catch (SQLException sqle) {
               throw new IOException(sqle.toString());
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org