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 package org.apache.james.mailrepository;
21
22 import org.apache.avalon.cornerstone.services.store.StreamRepository;
23 import org.apache.james.core.MimeMessageSource;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 public class MimeMessageAvalonSource extends MimeMessageSource {
29
30
31
32 /***
33 * The stream repository used by this data source.
34 */
35 StreamRepository sr = null;
36
37 /***
38 * The name of the repository
39 */
40 String repositoryName = null;
41
42 /***
43 * The key for the particular stream in the stream repository
44 * to be used by this data source.
45 */
46 String key = null;
47
48 private long size = -1;
49
50 public MimeMessageAvalonSource(StreamRepository sr, String repositoryName, String key) {
51 this.sr = sr;
52 this.repositoryName = repositoryName;
53 this.key = key;
54 }
55
56 /***
57 * Returns a unique String ID that represents the location from where
58 * this source is loaded. This will be used to identify where the data
59 * is, primarily to avoid situations where this data would get overwritten.
60 *
61 * @return the String ID
62 */
63 public String getSourceId() {
64 StringBuffer sourceIdBuffer =
65 new StringBuffer(128)
66 .append(repositoryName)
67 .append("/")
68 .append(key);
69 return sourceIdBuffer.toString();
70 }
71
72 public InputStream getInputStream() throws IOException {
73 return sr.get(key);
74 }
75
76 public long getMessageSize() throws IOException {
77 if (size == -1) {
78 if (sr instanceof org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) {
79 size = ((org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) sr).getSize(key);
80 } else size = super.getMessageSize();
81 }
82 return size;
83 }
84 }