KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > mh > MHDataStorage


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.folder.mh;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29
30 import org.columba.core.io.StreamUtils;
31 import org.columba.mail.folder.AbstractLocalFolder;
32 import org.columba.mail.folder.FolderInconsistentException;
33 import org.columba.mail.folder.IDataStorage;
34 import org.columba.ristretto.io.FileSource;
35 import org.columba.ristretto.io.Source;
36
37
38 /**
39  * MH-style local mailbox {@link DataStorage}
40  * <p>
41  * Every message is saved in a single file, which is contrary to
42  * the mbox-style format where a complete mailbox is saved in
43  * one file.
44  * <p>
45  * Following the mh-mailbox standard, we use the message UID,
46  * consisting of numbers, to name the message files.
47  * <p>
48  * This data storage ignores every file starting with a "."
49  * <p>
50  * Note, that headercache is stored in the file ".headercache".
51  *
52  * @author fdietz
53  */

54 public class MHDataStorage implements IDataStorage {
55
56     /** JDK 1.4+ logging framework logger, used for logging. */
57     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.mail.folder.mh");
58
59     protected AbstractLocalFolder folder;
60
61     public MHDataStorage(AbstractLocalFolder folder) {
62         this.folder = folder;
63     }
64
65     public boolean exists(Object JavaDoc uid) throws Exception JavaDoc {
66         File JavaDoc file = new File JavaDoc(folder.getDirectoryFile() + File.separator
67                 + ((Integer JavaDoc) uid).toString());
68
69         return file.exists();
70     }
71
72     public void removeMessage(Object JavaDoc uid) throws Exception JavaDoc {
73         File JavaDoc file = new File JavaDoc(folder.getDirectoryFile() + File.separator
74                 + ((Integer JavaDoc) uid).toString());
75
76         //delete the file containing the message in the file system
77
if (!file.delete()) {
78             // Could not delete the file - possibly someone has a lock on it
79
LOG.warning("Could not delete " + file.getAbsolutePath()
80                     + ". Will try to delete it on exit");
81
82             // ... delete it when Columba exists instead
83
file.deleteOnExit();
84         } else {
85             LOG.info(file.getAbsolutePath() + " deleted successfully");
86         }
87     }
88
89     public int getMessageCount() {
90         File JavaDoc[] list = folder.getDirectoryFile().listFiles(MHMessageFileFilter.getInstance());
91
92         return list.length;
93     }
94
95     /* (non-Javadoc)
96      * @see org.columba.mail.folder.IDataStorage#getMessages()
97      */

98     public Object JavaDoc[] getMessageUids() {
99         File JavaDoc[] list = folder.getDirectoryFile().listFiles(MHMessageFileFilter.getInstance());
100
101         // A list of all files that seem to be messages (only numbers in the name)
102
List JavaDoc result = new ArrayList JavaDoc(list.length); //new Object[list.length];
103

104         for (int i = 0; i < list.length; i++) {
105             result.add(i, new Integer JavaDoc(list[i].getName()));
106         }
107
108         Collections.sort(result);
109
110         return result.toArray();
111     }
112
113     /* (non-Javadoc)
114      * @see org.columba.mail.folder.IDataStorage#getFileSource(java.lang.Object)
115      */

116     public Source getMessageSource(Object JavaDoc uid) throws Exception JavaDoc {
117         File JavaDoc file = new File JavaDoc(folder.getDirectoryFile() + File.separator + ((Integer JavaDoc) uid).toString());
118
119         try {
120             return new FileSource(file);
121         } catch (IOException JavaDoc e) {
122             throw new FolderInconsistentException();
123         }
124     }
125
126     /* (non-Javadoc)
127      * @see org.columba.mail.folder.IDataStorage#saveInputStream(java.lang.Object, java.io.InputStream)
128      */

129     public void saveMessage(Object JavaDoc uid, InputStream JavaDoc source)
130         throws IOException JavaDoc {
131         File JavaDoc file = new File JavaDoc(folder.getDirectoryFile() + File.separator + (Integer JavaDoc) uid);
132
133         OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
134
135         StreamUtils.streamCopy(source, out);
136
137         source.close();
138         out.close();
139     }
140
141     /**
142      * @see org.columba.mail.folder.IDataStorage#getMessageStream(java.lang.Object)
143      */

144     public InputStream JavaDoc getMessageStream(Object JavaDoc uid) throws Exception JavaDoc {
145         
146         return new FileInputStream JavaDoc( new File JavaDoc(folder.getDirectoryFile() + File.separator + ((Integer JavaDoc) uid).toString()));
147     }
148 }
149
Popular Tags