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 2002/04/18 16:49:15 UTC

cvs commit: jakarta-james/src/java/org/apache/james/mailrepository AvalonMailRepository.java JDBCMailRepository.java MimeMessageAvalonSource.java MimeMessageJDBCSource.java

serge       02/04/18 07:49:15

  Modified:    src/java/org/apache/james/core
                        MimeMessageInputStreamSource.java
                        MimeMessageSource.java MimeMessageWrapper.java
               src/java/org/apache/james/mailrepository
                        AvalonMailRepository.java JDBCMailRepository.java
                        MimeMessageAvalonSource.java
                        MimeMessageJDBCSource.java
  Log:
  Introduced the concept of a "sourceId" on MimeMessageSource.  This should be a unique ID that represents where this source is retrieving this message.  In theory equals() should be written to use this (if we needed that method).
  
  This then allows the AvalonMailRepository to see where it is retrieving a message compared to where it is saving it.  If it sees a message where it is trying to save to the same place it would be retrieving from, it will not do so unless the message has changed.  If the message has changed, the message has in fact been loaded into memory and is also logical for the message to be saved.
  
  This should allow the mail repository to handle saves reliably rather than worrying about calling store() on a mail object that hasn't changed.
  
  Revision  Changes    Path
  1.5       +34 -30    jakarta-james/src/java/org/apache/james/core/MimeMessageInputStreamSource.java
  
  Index: MimeMessageInputStreamSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/core/MimeMessageInputStreamSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MimeMessageInputStreamSource.java	18 Jan 2002 02:48:35 -0000	1.4
  +++ MimeMessageInputStreamSource.java	18 Apr 2002 14:49:14 -0000	1.5
  @@ -10,13 +10,15 @@
   import java.io.*;
   
   /**
  - * MimeMessageInputStreamSource.java
  + * Takes an input stream and creates a repeatable input stream source
  + * for a MimeMessageWrapper.  It does this by completely reading the
  + * input stream and saving that to a temporary file that should delete on exit,
  + * or when this object is GC'd.
    *
  + * @see MimeMessageWrapper
    *
  - * Created:
    *
  - * @author
  - * @version
  + * @author <a href="mailto:sergek@lokitech.com>">Serge Knystautas</a>
    *
    * Modified by <a href="mailto:okidz@pindad.com">Oki DZ</a>
    * Thu Oct  4 15:15:27 WIT 2001
  @@ -24,35 +26,13 @@
    */
   public class MimeMessageInputStreamSource extends MimeMessageSource {
   
  -    String key = null;
  -    InputStream in = null;
       File file = null;
  -
  -    //If you try to access this size first, it will load it into a temp file
  -    //  and work from there.
  +    String sourceId = null;
   
       public MimeMessageInputStreamSource(String key, InputStream in) {
  -        this.key = key;
  -        this.in = in;
  -    }
  -
  -    /**
  -     * Return an input stream to the data
  -     */
  -    public synchronized InputStream getInputStream() throws IOException {
  -        if (file == null) {
  -            return in;
  -        } else {
  -            return new BufferedInputStream(new FileInputStream(file));
  -        }
  -    }
  -
  -    /**
  -     * If not already, read the stream into a temp file
  -     */
  -    public synchronized long getMessageSize() throws IOException {
  -        if (file == null) {
  -            //Create a temp file and channel the input stream into it
  +        //We want to immediately read this into a temporary file
  +        //Create a temp file and channel the input stream into it
  +        try {
               file = File.createTempFile(key, ".m64");
               OutputStream fout = new BufferedOutputStream(new FileOutputStream(file));
               int b = -1;
  @@ -62,7 +42,31 @@
               fout.close();
               in.close();
               file.deleteOnExit();
  +
  +            sourceId = file.getCanonicalPath();
  +        } catch (IOException ioe) {
  +            throw new RuntimeException("Unable to retrieve the data: " + ioe.getMessage());
           }
  +    }
  +
  +    /**
  +     * Returns the unique identifier of this input stream source
  +     */
  +    public String getSourceId() {
  +        return sourceId;
  +    }
  +
  +    /**
  +     * Return an input stream to the data
  +     */
  +    public synchronized InputStream getInputStream() throws IOException {
  +        return new BufferedInputStream(new FileInputStream(file));
  +    }
  +
  +    /**
  +     * Return the size of the temp file
  +     */
  +    public long getMessageSize() throws IOException {
           return file.length();
       }
   
  
  
  
  1.4       +12 -1     jakarta-james/src/java/org/apache/james/core/MimeMessageSource.java
  
  Index: MimeMessageSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/core/MimeMessageSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MimeMessageSource.java	5 Dec 2001 22:11:48 -0000	1.3
  +++ MimeMessageSource.java	18 Apr 2002 14:49:14 -0000	1.4
  @@ -11,9 +11,20 @@
   import java.io.InputStream;
   
   /**
  - * A source of a MimeMessage.
  + * This defines a reusable datasource that can supply an input stream with
  + * MimeMessage data.  This allows a MimeMessageWrapper or other classes to
  + * grab the underlying data.
  + *
  + * @see MimeMessageWrapper
    */
   public abstract class MimeMessageSource {
  +    /**
  +     * Returns a unique String ID that represents where this file is loaded
  +     * from.  This will be used to identify where the data is, primarily to
  +     * avoid situations where this data would get overwritten.
  +     */
  +    public abstract String getSourceId();
  +
       /**
        * Return an input stream to the data
        */
  
  
  
  1.9       +7 -3      jakarta-james/src/java/org/apache/james/core/MimeMessageWrapper.java
  
  Index: MimeMessageWrapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/core/MimeMessageWrapper.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- MimeMessageWrapper.java	4 Feb 2002 15:34:11 -0000	1.8
  +++ MimeMessageWrapper.java	18 Apr 2002 14:49:14 -0000	1.9
  @@ -49,9 +49,13 @@
           this.source = source;
       }
   
  -    public MimeMessageWrapper(Session session, MimeMessageSource source) {
  -        super(session);
  -        this.source = source;
  +    /**
  +     * Returns the source ID of the MimeMessageSource that is supplying this
  +     * with data.
  +     * @see MimeMessageSource
  +     */
  +    public String getSourceId() {
  +        return source.getSourceId();
       }
   
       /**
  
  
  
  1.14      +24 -7     jakarta-james/src/java/org/apache/james/mailrepository/AvalonMailRepository.java
  
  Index: AvalonMailRepository.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/mailrepository/AvalonMailRepository.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- AvalonMailRepository.java	1 Mar 2002 15:58:39 -0000	1.13
  +++ AvalonMailRepository.java	18 Apr 2002 14:49:15 -0000	1.14
  @@ -47,7 +47,7 @@
       implements MailRepository, Component, Configurable, Composable, Initializable {
   
       private Lock lock;
  -    protected static boolean DEEP_DEBUG = false;
  +    protected final static boolean DEEP_DEBUG = false;
       private static final String TYPE = "MAIL";
       private Store store;
       private StreamRepository sr;
  @@ -175,9 +175,26 @@
                   if (!keys.contains(key)) {
                       keys.add(key);
                   }
  -                OutputStream out = sr.put(key);
  -                mc.writeMessageTo(out);
  -                out.close();
  +                boolean saveStream = true;
  +
  +                if (mc.getMessage() instanceof MimeMessageWrapper) {
  +                    MimeMessageWrapper wrapper = (MimeMessageWrapper) mc.getMessage();
  +                    System.out.println("Retrieving from: " + wrapper.getSourceId());
  +                    System.out.println("Saving to:       " + destination + "/" + mc.getName());
  +                    System.out.println("Modified: " + wrapper.isModified());
  +                    if (wrapper.getSourceId().equals(destination + "/" + mc.getName()) && !wrapper.isModified()) {
  +                        //We're trying to save to the same place, and it's not modified... we shouldn't save.
  +                        //More importantly, if we try to save, we will create a 0-byte file since we're
  +                        //retrying to retrieve from a file we'll be overwriting.
  +                        saveStream = false;
  +                    }
  +                }
  +                if (saveStream) {
  +                    OutputStream out = sr.put(key);
  +                    mc.writeMessageTo(out);
  +                    out.close();
  +                }
  +                //Always save the header information
                   or.put(key, mc);
               } finally {
                   if (!wasLocked) {
  @@ -186,7 +203,7 @@
                   }
               }
   
  -            if(DEEP_DEBUG) {
  +            if (DEEP_DEBUG) {
                   getLogger().debug("Mail " + key + " stored." );
               }
   
  @@ -201,7 +218,7 @@
       }
   
       public MailImpl retrieve(String key) {
  -        if(DEEP_DEBUG) {
  +        if (DEEP_DEBUG) {
               getLogger().debug("Retrieving mail: " + key);
           }
           try {
  @@ -213,7 +230,7 @@
                   remove(key);
                   return null;
               }
  -            MimeMessageAvalonSource source = new MimeMessageAvalonSource(sr, key);
  +            MimeMessageAvalonSource source = new MimeMessageAvalonSource(sr, destination, key);
               mc.setMessage(new MimeMessageWrapper(source));
   
               return mc;
  
  
  
  1.18      +13 -2     jakarta-james/src/java/org/apache/james/mailrepository/JDBCMailRepository.java
  
  Index: JDBCMailRepository.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/mailrepository/JDBCMailRepository.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- JDBCMailRepository.java	27 Feb 2002 04:05:11 -0000	1.17
  +++ JDBCMailRepository.java	18 Apr 2002 14:49:15 -0000	1.18
  @@ -60,8 +60,11 @@
   public class JDBCMailRepository
       extends AbstractLogEnabled
       implements MailRepository, Component, Contextualizable, Composable, Configurable, Initializable {
  -    protected Context context;
   
  +    private static final boolean DEEP_DEBUG = false;
  +
  +
  +    protected Context context;
       private Lock lock;
   
       // Configuration elements
  @@ -429,16 +432,24 @@
       }
   
       public MailImpl retrieve(String key) {
  -        //System.err.println("retrieving " + key);
  +        if (DEEP_DEBUG) {
  +            System.err.println("retrieving " + key);
  +        }
           Connection conn = null;
           try {
               conn = datasource.getConnection();
  +            if (DEEP_DEBUG) {
  +                System.err.println("got a conn " + key);
  +            }
   
               PreparedStatement retrieveMessage =
                   conn.prepareStatement(sqlQueries.getSqlString("retrieveMessageSQL", true));
               retrieveMessage.setString(1, key);
               retrieveMessage.setString(2, repositoryName);
               ResultSet rsMessage = retrieveMessage.executeQuery();
  +            if (DEEP_DEBUG) {
  +                System.err.println("ran the query " + key);
  +            }
               if (!rsMessage.next()) {
                   throw new RuntimeException("Did not find a record " + key + " in " + repositoryName);
               }
  
  
  
  1.3       +7 -1      jakarta-james/src/java/org/apache/james/mailrepository/MimeMessageAvalonSource.java
  
  Index: MimeMessageAvalonSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/mailrepository/MimeMessageAvalonSource.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MimeMessageAvalonSource.java	18 Jan 2002 02:48:36 -0000	1.2
  +++ MimeMessageAvalonSource.java	18 Apr 2002 14:49:15 -0000	1.3
  @@ -17,11 +17,17 @@
   
       //Define how to get to the data
       StreamRepository sr = null;
  +    String repositoryName = null;
       String key = null;
   
  -    public MimeMessageAvalonSource(StreamRepository sr, String key) {
  +    public MimeMessageAvalonSource(StreamRepository sr, String repositoryName, String key) {
           this.sr = sr;
  +        this.repositoryName = repositoryName;
           this.key = key;
  +    }
  +
  +    public String getSourceId() {
  +        return repositoryName + "/" + key;
       }
   
       public InputStream getInputStream() throws IOException {
  
  
  
  1.5       +24 -9     jakarta-james/src/java/org/apache/james/mailrepository/MimeMessageJDBCSource.java
  
  Index: MimeMessageJDBCSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/mailrepository/MimeMessageJDBCSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MimeMessageJDBCSource.java	18 Jan 2002 02:48:36 -0000	1.4
  +++ MimeMessageJDBCSource.java	18 Apr 2002 14:49:15 -0000	1.5
  @@ -56,6 +56,10 @@
               repository.sqlQueries.getSqlString("retrieveMessageBodySizeSQL");
       }
   
  +    public String getSourceId() {
  +        return repository.repositoryName + "/" + key;
  +    }
  +
       /**
        * Return the input stream to the database field and then the file stream.  This should
        * be smart enough to work even if the file does not exist.  This is to support
  @@ -63,20 +67,30 @@
        */
       public synchronized InputStream getInputStream() throws IOException {
           try {
  +            new Throwable().printStackTrace();
               Connection conn = repository.getConnection();
   
  -            PreparedStatement retrieveMessageStream = conn.prepareStatement(retrieveMessageBodySQL);
  -            retrieveMessageStream.setString(1, key);
  -            retrieveMessageStream.setString(2, repository.repositoryName);
  -            ResultSet rsRetrieveMessageStream = retrieveMessageStream.executeQuery();
  +            byte[] headers = null;
  +
  +            for (int i = 0; i < 10; i++) {
  +                long start = System.currentTimeMillis();
  +                System.err.println("starting");
  +                PreparedStatement retrieveMessageStream = conn.prepareStatement(retrieveMessageBodySQL);
  +                retrieveMessageStream.setString(1, key);
  +                retrieveMessageStream.setString(2, repository.repositoryName);
  +                ResultSet rsRetrieveMessageStream = retrieveMessageStream.executeQuery();
  +
  +                if (!rsRetrieveMessageStream.next()) {
  +                    throw new IOException("Could not find message");
  +                }
   
  -            if (!rsRetrieveMessageStream.next()) {
  -                throw new IOException("Could not find message");
  +                headers = rsRetrieveMessageStream.getBytes(1);
  +                rsRetrieveMessageStream.close();
  +                retrieveMessageStream.close();
  +                System.err.println("stopping");
  +                System.err.println(System.currentTimeMillis() - start);
               }
   
  -            byte[] headers = rsRetrieveMessageStream.getBytes(1);
  -            rsRetrieveMessageStream.close();
  -            retrieveMessageStream.close();
               conn.close();
   
               InputStream in = new ByteArrayInputStream(headers);
  @@ -100,6 +114,7 @@
       public synchronized long getMessageSize() throws IOException {
           if (retrieveMessageBodySizeSQL == null) {
               //There was no SQL statement for this repository... figure it out the hard way
  +            System.err.println("no SQL statement to find size");
               return super.getMessageSize();
           }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>