001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.folder.mh;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.List;
027: import java.util.logging.Logger;
028:
029: import org.columba.core.io.StreamUtils;
030: import org.columba.mail.folder.AbstractLocalFolder;
031: import org.columba.mail.folder.FolderInconsistentException;
032: import org.columba.mail.folder.IDataStorage;
033: import org.columba.ristretto.io.FileSource;
034: import org.columba.ristretto.io.Source;
035:
036: /**
037: * MH-style local mailbox {@link DataStorage}
038: * <p>
039: * Every message is saved in a single file, which is contrary to
040: * the mbox-style format where a complete mailbox is saved in
041: * one file.
042: * <p>
043: * Following the mh-mailbox standard, we use the message UID,
044: * consisting of numbers, to name the message files.
045: * <p>
046: * This data storage ignores every file starting with a "."
047: * <p>
048: * Note, that headercache is stored in the file ".headercache".
049: *
050: * @author fdietz
051: */
052: public class MHDataStorage implements IDataStorage {
053:
054: /** JDK 1.4+ logging framework logger, used for logging. */
055: private static final Logger LOG = Logger
056: .getLogger("org.columba.mail.folder.mh");
057:
058: protected AbstractLocalFolder folder;
059:
060: public MHDataStorage(AbstractLocalFolder folder) {
061: this .folder = folder;
062: }
063:
064: public boolean exists(Object uid) throws Exception {
065: File file = new File(folder.getDirectoryFile() + File.separator
066: + ((Integer) uid).toString());
067:
068: return file.exists();
069: }
070:
071: public void removeMessage(Object uid) throws Exception {
072: File file = new File(folder.getDirectoryFile() + File.separator
073: + ((Integer) uid).toString());
074:
075: //delete the file containing the message in the file system
076: if (!file.delete()) {
077: // Could not delete the file - possibly someone has a lock on it
078: LOG.warning("Could not delete " + file.getAbsolutePath()
079: + ". Will try to delete it on exit");
080:
081: // ... delete it when Columba exists instead
082: file.deleteOnExit();
083: } else {
084: LOG.info(file.getAbsolutePath() + " deleted successfully");
085: }
086: }
087:
088: public int getMessageCount() {
089: File[] list = folder.getDirectoryFile().listFiles(
090: MHMessageFileFilter.getInstance());
091:
092: return list.length;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.columba.mail.folder.IDataStorage#getMessages()
097: */
098: public Object[] getMessageUids() {
099: File[] list = folder.getDirectoryFile().listFiles(
100: MHMessageFileFilter.getInstance());
101:
102: // A list of all files that seem to be messages (only numbers in the name)
103: List result = new ArrayList(list.length); //new Object[list.length];
104:
105: for (int i = 0; i < list.length; i++) {
106: result.add(i, new Integer(list[i].getName()));
107: }
108:
109: Collections.sort(result);
110:
111: return result.toArray();
112: }
113:
114: /* (non-Javadoc)
115: * @see org.columba.mail.folder.IDataStorage#getFileSource(java.lang.Object)
116: */
117: public Source getMessageSource(Object uid) throws Exception {
118: File file = new File(folder.getDirectoryFile() + File.separator
119: + ((Integer) uid).toString());
120:
121: try {
122: return new FileSource(file);
123: } catch (IOException e) {
124: throw new FolderInconsistentException();
125: }
126: }
127:
128: /* (non-Javadoc)
129: * @see org.columba.mail.folder.IDataStorage#saveInputStream(java.lang.Object, java.io.InputStream)
130: */
131: public void saveMessage(Object uid, InputStream source)
132: throws IOException {
133: File file = new File(folder.getDirectoryFile() + File.separator
134: + (Integer) uid);
135:
136: OutputStream out = new FileOutputStream(file);
137:
138: StreamUtils.streamCopy(source, out);
139:
140: source.close();
141: out.close();
142: }
143:
144: /**
145: * @see org.columba.mail.folder.IDataStorage#getMessageStream(java.lang.Object)
146: */
147: public InputStream getMessageStream(Object uid) throws Exception {
148:
149: return new FileInputStream(new File(folder.getDirectoryFile()
150: + File.separator + ((Integer) uid).toString()));
151: }
152: }
|