KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > ImapMailboxCache


1 /*
2  * ImapMailboxCache.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ImapMailboxCache.java,v 1.1.1.1 2002/09/24 16:10:11 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Properties JavaDoc;
35 import org.armedbear.j.Debug;
36 import org.armedbear.j.Directories;
37 import org.armedbear.j.File;
38 import org.armedbear.j.Log;
39 import org.armedbear.j.Utilities;
40
41 public final class ImapMailboxCache implements Serializable JavaDoc
42 {
43     private static Properties JavaDoc catalog;
44
45     private transient ImapMailbox mailbox;
46     private final String JavaDoc mailboxName;
47     private final int uidValidity;
48     private final ArrayList JavaDoc entries;
49
50     // Force serialization to be dependent on ImapMailboxEntry.
51
private final ImapMailboxEntry dummy = new ImapMailboxEntry(0);
52
53     public ImapMailboxCache(ImapMailbox mailbox)
54     {
55         this.mailbox = mailbox;
56         mailboxName = mailbox.getName();
57         uidValidity = mailbox.getUidValidity();
58         entries = new ArrayList JavaDoc(mailbox.getEntries());
59     }
60
61     private final void setMailbox(ImapMailbox mailbox)
62     {
63         this.mailbox = mailbox;
64     }
65
66     public final List JavaDoc getEntries()
67     {
68         return entries;
69     }
70
71     public void writeCache()
72     {
73         Runnable JavaDoc r = new Runnable JavaDoc() {
74             public void run()
75             {
76                 writeCacheInternal();
77             }
78         };
79         new Thread JavaDoc(r).start();
80     }
81
82     private void writeCacheInternal()
83     {
84         try {
85             Log.debug("ImapMailboxCache.writeCacheInternal " + entries.size() +
86                 " entries");
87             Debug.assertTrue(uidValidity != 0);
88             File temp = Utilities.getTempFile();
89             ObjectOutputStream JavaDoc objectOut =
90                 new ObjectOutputStream JavaDoc(temp.getOutputStream());
91             objectOut.writeObject(this);
92             objectOut.flush();
93             objectOut.close();
94             Utilities.deleteRename(temp, getCacheFile(mailbox));
95             Log.debug("ImapMailboxCache.writeCacheInternal completed");
96         }
97         catch (IOException JavaDoc e) {
98             Log.error(e);
99         }
100     }
101
102     public static ImapMailboxCache readCache(ImapMailbox mb)
103     {
104         File file = ImapMailboxCache.getCacheFile(mb);
105         if (file == null || !file.isFile())
106             return null;
107         ObjectInputStream JavaDoc in = null;
108         try {
109             in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(
110                 file.getInputStream()));
111             ImapMailboxCache cache = (ImapMailboxCache) in.readObject();
112             cache.setMailbox(mb);
113             return cache;
114         }
115         catch (Exception JavaDoc e) {
116             Log.debug("ImapMailboxCache.readCache returning null");
117             return null;
118         }
119         finally {
120             if (in != null) {
121                 try {
122                     in.close();
123                 }
124                 catch (IOException JavaDoc e) {
125                     Log.error(e);
126                 }
127             }
128         }
129     }
130
131     public boolean isValid()
132     {
133         if (entries == null)
134             return false;
135         if (entries.size() == 0)
136             return false;
137         if (mailboxName == null)
138             return false;
139         if (!mailboxName.equals(mailbox.getName()))
140             return false;
141         return uidValidity == mailbox.getSession().getUidValidity();
142     }
143
144     private static synchronized File getCacheFile(ImapMailbox mb)
145     {
146         File directory =
147             File.getInstance(Directories.getMailDirectory(), "imap");
148         if (!directory.isDirectory()) {
149             directory.mkdirs();
150             if (!directory.isDirectory()) {
151                 Log.error("can't create imap dir");
152                 return null;
153             }
154         }
155         boolean modified = false;
156         File catalogFile = File.getInstance(directory, "catalog");
157         if (catalog == null) {
158             catalog = new Properties JavaDoc();
159             // Load the catalog.
160
try {
161                 if (catalogFile.isFile()) {
162                     InputStream JavaDoc in = catalogFile.getInputStream();
163                     catalog.load(in);
164                     in.close();
165                 }
166             }
167             catch (IOException JavaDoc e) {
168                 Log.error(e);
169             }
170             // Remove obsolete entries from catalog.
171
Properties JavaDoc temp = new Properties JavaDoc();
172             Enumeration JavaDoc keys = catalog.keys();
173             while (keys.hasMoreElements()) {
174                 String JavaDoc key = (String JavaDoc) keys.nextElement();
175                 String JavaDoc value = (String JavaDoc) catalog.get(key);
176                 // Make sure key is canonical name.
177
if (key.indexOf('@') < 0 || key.indexOf(':') < 0) {
178                     Log.debug("removing obsolete entry " + key + '=' + value);
179                     // Not canonical name. Delete corresponding file.
180
File file = File.getInstance(directory, value);
181                     if (file != null && file.isFile())
182                         file.delete();
183                     modified = true;
184                 } else
185                     temp.put(key, value);
186             }
187             if (modified)
188                 catalog = temp;
189         }
190         final String JavaDoc mailboxName = mb.getUrl().getCanonicalName();
191         final String JavaDoc fileName = catalog.getProperty(mailboxName);
192         File file;
193         if (fileName != null)
194             file = File.getInstance(directory, fileName);
195         else {
196             file = Utilities.getTempFile(directory);
197             catalog.put(mailboxName, file.getName());
198             modified = true;
199         }
200         if (modified) {
201             try {
202                 OutputStream JavaDoc out = catalogFile.getOutputStream();
203                 catalog.save(out, null);
204                 out.flush();
205                 out.close();
206             }
207             catch (IOException JavaDoc e) {
208                 Log.error(e);
209             }
210         }
211         return file;
212     }
213 }
214
Popular Tags