001: //The contents of this file are subject to the Mozilla Public License Version
002: //1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: //Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.mail.folder.imap;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028:
029: import org.columba.core.config.DefaultConfigDirectory;
030: import org.columba.core.io.StreamCache;
031: import org.columba.core.shutdown.ShutdownManager;
032: import org.columba.ristretto.message.MimeTree;
033:
034: public class IMAPCache implements Runnable {
035:
036: //TODO:add a configuration of the cache size
037:
038: private StreamCache cache;
039: private static IMAPCache instance = new IMAPCache();
040:
041: protected IMAPCache() {
042: File configDir = DefaultConfigDirectory.getInstance()
043: .getCurrentPath();
044: cache = new StreamCache(new File(configDir, "imap_cache"));
045:
046: ShutdownManager.getInstance().register(this );
047: }
048:
049: public static IMAPCache getInstance() {
050: return instance;
051: }
052:
053: public void addMimeTree(IMAPFolder folder, Object uid,
054: MimeTree mimeTree) throws IOException {
055: cache.activeAdd(createMimeTreeKey(folder, uid),
056: convertToStream(mimeTree));
057: }
058:
059: public MimeTree getMimeTree(IMAPFolder folder, Object uid)
060: throws IOException {
061: InputStream in = cache.get(createMimeTreeKey(folder, uid));
062: if (in != null) {
063: try {
064: return (MimeTree) new ObjectInputStream(in)
065: .readObject();
066: } catch (Exception e) {
067: return null;
068: }
069:
070: } else {
071: return null;
072: }
073: }
074:
075: private InputStream convertToStream(Object o) throws IOException {
076: ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
077: new ObjectOutputStream(out).writeObject(o);
078:
079: return new ByteArrayInputStream(out.toByteArray());
080:
081: }
082:
083: public InputStream addMimeBody(IMAPFolder folder, Object uid,
084: Integer[] address, InputStream data) throws IOException {
085:
086: return cache.passiveAdd(
087: createMimeBodyKey(folder, uid, address), data);
088: }
089:
090: public InputStream getMimeBody(IMAPFolder folder, Object uid,
091: Integer[] address) {
092: return cache.get(createMimeBodyKey(folder, uid, address));
093: }
094:
095: protected String createMimeBodyKey(IMAPFolder folder, Object uid,
096: Integer[] address) {
097: return folder.getId() + uid.toString()
098: + addressToString(address);
099: }
100:
101: protected String createMimeTreeKey(IMAPFolder folder, Object uid) {
102: return folder.getId() + uid.toString();
103: }
104:
105: protected String addressToString(Integer[] address) {
106: String result = address[0].toString();
107: for (int i = 1; i < address.length; i++) {
108: result += "." + address[i];
109: }
110:
111: return result;
112: }
113:
114: public void run() {
115: //cache.clear();
116: try {
117: cache.persist();
118: } catch (IOException e) {
119: cache.clear();
120: }
121: }
122: }
|