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