View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.mailrepository;
19  
20  import org.apache.avalon.cornerstone.services.store.StreamRepository;
21  import org.apache.james.core.MimeMessageSource;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  public class MimeMessageAvalonSource extends MimeMessageSource {
27  
28      //Define how to get to the data
29      
30      /***
31       * The stream repository used by this data source.
32       */
33      StreamRepository sr = null;
34  
35      /***
36       * The name of the repository
37       */
38      String repositoryName = null;
39  
40      /***
41       * The key for the particular stream in the stream repository
42       * to be used by this data source.
43       */
44      String key = null;
45  
46      private long size = -1;
47  
48      public MimeMessageAvalonSource(StreamRepository sr, String repositoryName, String key) {
49          this.sr = sr;
50          this.repositoryName = repositoryName;
51          this.key = key;
52      }
53  
54      /***
55       * Returns a unique String ID that represents the location from where 
56       * this source is loaded.  This will be used to identify where the data 
57       * is, primarily to avoid situations where this data would get overwritten.
58       *
59       * @return the String ID
60       */
61      public String getSourceId() {
62          StringBuffer sourceIdBuffer =
63              new StringBuffer(128)
64                      .append(repositoryName)
65                      .append("/")
66                      .append(key);
67          return sourceIdBuffer.toString();
68      }
69  
70      public InputStream getInputStream() throws IOException {
71          return sr.get(key);
72      }
73  
74      public long getMessageSize() throws IOException {
75          if (size == -1) {
76              if (sr instanceof org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) {
77                  size = ((org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) sr).getSize(key);
78              } else size = super.getMessageSize();
79          }
80          return size;
81      }
82  }