1 21 22 package org.armedbear.j.mail; 23 24 import java.io.BufferedInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.OutputStream ; 30 import java.io.Serializable ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.List ; 34 import java.util.Properties ; 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 42 { 43 private static Properties catalog; 44 45 private transient ImapMailbox mailbox; 46 private final String mailboxName; 47 private final int uidValidity; 48 private final ArrayList entries; 49 50 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 (mailbox.getEntries()); 59 } 60 61 private final void setMailbox(ImapMailbox mailbox) 62 { 63 this.mailbox = mailbox; 64 } 65 66 public final List getEntries() 67 { 68 return entries; 69 } 70 71 public void writeCache() 72 { 73 Runnable r = new Runnable () { 74 public void run() 75 { 76 writeCacheInternal(); 77 } 78 }; 79 new Thread (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 objectOut = 90 new ObjectOutputStream (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 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 in = null; 108 try { 109 in = new ObjectInputStream (new BufferedInputStream ( 110 file.getInputStream())); 111 ImapMailboxCache cache = (ImapMailboxCache) in.readObject(); 112 cache.setMailbox(mb); 113 return cache; 114 } 115 catch (Exception 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 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 (); 159 try { 161 if (catalogFile.isFile()) { 162 InputStream in = catalogFile.getInputStream(); 163 catalog.load(in); 164 in.close(); 165 } 166 } 167 catch (IOException e) { 168 Log.error(e); 169 } 170 Properties temp = new Properties (); 172 Enumeration keys = catalog.keys(); 173 while (keys.hasMoreElements()) { 174 String key = (String ) keys.nextElement(); 175 String value = (String ) catalog.get(key); 176 if (key.indexOf('@') < 0 || key.indexOf(':') < 0) { 178 Log.debug("removing obsolete entry " + key + '=' + value); 179 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 mailboxName = mb.getUrl().getCanonicalName(); 191 final String 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 out = catalogFile.getOutputStream(); 203 catalog.save(out, null); 204 out.flush(); 205 out.close(); 206 } 207 catch (IOException e) { 208 Log.error(e); 209 } 210 } 211 return file; 212 } 213 } 214 | Popular Tags |