1 package net.suberic.pooka.thread; 2 3 import net.suberic.pooka.*; 4 import net.suberic.pooka.event.*; 5 import net.suberic.pooka.gui.LoadMessageTracker; 6 import net.suberic.pooka.gui.MessageProxy; 7 import net.suberic.pooka.gui.FolderInternalFrame; 8 import net.suberic.pooka.cache.*; 9 import javax.mail.*; 10 import javax.mail.internet.*; 11 import java.util.*; 12 import java.awt.event.ActionEvent ; 13 import javax.swing.Action ; 14 import javax.swing.AbstractAction ; 15 16 29 30 public class MessageLoader extends Thread { 31 private FolderInfo folderInfo; 32 private List columnValues; 33 private List loadQueue = new LinkedList(); 34 private List priorityLoadQueue = new LinkedList(); 35 private List cacheQueue = new LinkedList(); 36 private List messageLoadedListeners = new LinkedList(); 37 private int updateMessagesCount = 10; 38 private boolean mEnqueued = false; 39 40 private boolean stopped = false; 41 42 public static int NORMAL = 5; 43 public static int HIGH = 10; 44 45 49 public Action mAction = null; 50 51 54 public MessageLoader(FolderInfo newFolderInfo) { 55 folderInfo = newFolderInfo; 56 mAction = new AbstractAction ("Load Messages - " + newFolderInfo.getFolderID()) { 57 public void actionPerformed(ActionEvent ae) { 58 loadNextBatch(); 59 } 60 }; 61 } 62 63 66 public void loadNextBatch() { 67 MessageProxy mp; 68 69 int queueSize = getQueueSize(); 71 if (! stopped) { 72 if (queueSize > 0) { 73 folderInfo.getLogger().log(java.util.logging.Level.FINE, folderInfo.getFolderID() + " loading " + queueSize + " messages."); 74 75 int fetchBatchSize = 50; 77 try { 78 fetchBatchSize = Integer.parseInt(Pooka.getProperty("Pooka.fetchBatchSize", "50")); 79 } catch (NumberFormatException nfe) { 80 } 81 82 FetchProfile fetchProfile = getFolderInfo().getFetchProfile(); 83 84 List messageProxies = retrieveNextBatch(fetchBatchSize); 86 List toFetchInfos = new LinkedList(); 87 88 for (int i = 0 ; i < messageProxies.size(); i++) { 91 MessageInfo fetchCheckInfo = ((MessageProxy) messageProxies.get(i)).getMessageInfo(); 92 if (! fetchCheckInfo.hasBeenFetched()) { 93 toFetchInfos.add(fetchCheckInfo); 94 } 95 } 96 97 if (toFetchInfos.size() > 0) { 98 try { 99 MessageInfo[] toFetch = new MessageInfo[toFetchInfos.size()]; 100 toFetch = (MessageInfo[]) toFetchInfos.toArray(toFetch); 101 getFolderInfo().fetch(toFetch, fetchProfile); 102 } catch(MessagingException me) { 103 if (folderInfo.getLogger().isLoggable(java.util.logging.Level.WARNING)) { 104 System.out.println("caught error while fetching for folder " + getFolderInfo().getFolderID() + ": " + me); 105 me.printStackTrace(); 106 } 107 } 108 } 109 110 for (int i = 0 ; i < messageProxies.size(); i++) { 113 mp = (MessageProxy) messageProxies.get(i); 114 try { 115 if (! mp.isLoaded()) 116 mp.loadTableInfo(); 117 if (mp.needsRefresh()) { 118 mp.refreshMessage(); 119 } 120 else if (! mp.matchedFilters()) { 121 mp.matchFilters(); 122 } 123 } catch (Exception e) { 124 if (folderInfo.getLogger().isLoggable(java.util.logging.Level.WARNING)) { 125 e.printStackTrace(); 126 } 127 } 128 } 129 130 } else if (getCacheQueueSize() > 0) { 131 try { 132 MessageProxy nextCache = (MessageProxy) cacheQueue.remove(0); 133 if (folderInfo instanceof CachingFolderInfo) { 134 MessageInfo mi = nextCache.getMessageInfo(); 135 MimeMessage mimeMessage = (MimeMessage) mi.getMessage(); 136 CachingFolderInfo cfi = (CachingFolderInfo) folderInfo; 137 if (cfi.getFolderDisplayUI() != null) { 138 cfi.showStatusMessage(cfi.getFolderDisplayUI(), "caching messages, " + getCacheQueueSize() + " remaining..."); 139 } 140 cfi.getCache().cacheMessage(mimeMessage, cfi.getUID(mimeMessage), cfi.getUIDValidity(), SimpleFileCache.MESSAGE, false); 141 if (cfi.getFolderDisplayUI() != null && getCacheQueueSize() == 0) { 142 cfi.clearStatusMessage(cfi.getFolderDisplayUI()); 143 } 144 } 145 } catch (Exception e) { 146 if (folderInfo.getLogger().isLoggable(java.util.logging.Level.WARNING)) { 147 e.printStackTrace(); 148 } 149 150 } 151 } 152 } 153 if (! stopped && (getQueueSize() > 0 || getCacheQueueSize() > 0)) { 154 enqueue(); 155 } 156 } 157 158 161 public void fireMessageLoadedEvent(int type, int numMessages, int max) { 162 167 } 168 169 172 public void addMessageLoadedListener(MessageLoadedListener newListener) { 173 if (messageLoadedListeners.indexOf(newListener) == -1) 174 messageLoadedListeners.add(newListener); 175 } 176 177 181 public void removeMessageLoadedListener(MessageLoadedListener remListener) { 182 if (messageLoadedListeners.indexOf(remListener) > -1) 183 messageLoadedListeners.remove(remListener); 184 } 185 186 189 public synchronized void loadMessages(MessageProxy mp) { 190 loadMessages(mp, NORMAL); 191 } 192 193 196 public synchronized void loadMessages(MessageProxy mp, int pPriority) { 197 if (pPriority > NORMAL) { 198 if (! priorityLoadQueue.contains(mp)) 199 priorityLoadQueue.add(mp); 200 loadQueue.remove(mp); 201 } else { 202 if (! priorityLoadQueue.contains(mp) && ! loadQueue.contains(mp)) 203 loadQueue.add(mp); 204 } 205 206 if (! isEnqueued()) 207 enqueue(); 208 } 209 210 213 public synchronized void loadMessages(MessageProxy[] mp) { 214 loadMessages(mp, NORMAL); 215 } 216 217 220 public synchronized void loadMessages(MessageProxy[] mp, int pPriority) { 221 loadMessages(Arrays.asList(mp), pPriority); 222 } 223 224 227 public synchronized void loadMessages(List mp) { 228 loadMessages(mp, NORMAL); 229 } 230 231 234 public synchronized void loadMessages(List mp, int pPriority) { 235 if (mp != null && mp.size() > 0) { 236 if (pPriority > NORMAL) { 237 loadQueue.removeAll(mp); 238 addUniqueReversed(priorityLoadQueue, mp); 239 } else { 240 List copy = new ArrayList(mp); 241 copy.removeAll(priorityLoadQueue); 242 addUniqueReversed(loadQueue, copy); 243 } 244 } 245 246 enqueue(); 247 } 248 249 252 public synchronized void cacheMessages(List mp) { 253 if (mp != null && mp.size() > 0) { 254 addUniqueReversed(cacheQueue, mp); 255 } 256 257 enqueue(); 258 } 259 260 263 public synchronized void cacheMessages(MessageProxy[] mp) { 264 if (mp != null && mp.length > 0) { 265 addUniqueReversed(cacheQueue, Arrays.asList(mp)); 266 } 267 268 enqueue(); 269 } 270 271 277 public synchronized List retrieveLoadQueue() { 278 List returnValue = new LinkedList(); 279 returnValue.addAll(priorityLoadQueue); 280 returnValue.addAll(loadQueue); 281 loadQueue = new LinkedList(); 282 priorityLoadQueue = new LinkedList(); 283 return returnValue; 284 } 285 286 289 private void addUniqueReversed(List targetList, List toAdd) { 290 for (int i = toAdd.size() - 1; i >= 0; i--) { 291 Object current = toAdd.get(i); 292 if (current != null && ! targetList.contains(current)) 293 targetList.add(current); 294 } 295 } 296 297 301 public synchronized List retrieveNextBatch(int pCount) { 302 int plqLength = priorityLoadQueue.size(); 303 int lqLength = loadQueue.size(); 304 305 if (plqLength + lqLength > 0) { 307 List returnValue = new LinkedList(); 308 309 if (plqLength > 0) { 311 if (plqLength >= pCount) { 314 List subList = priorityLoadQueue.subList(0, pCount); 315 returnValue.addAll(subList); 316 subList.clear(); 317 return returnValue; 318 } else { 319 returnValue.addAll(priorityLoadQueue); 321 priorityLoadQueue.clear(); 322 } 323 } 324 325 if (lqLength > 0) { 327 int newCount = pCount - plqLength; 328 if (lqLength >= newCount) { 329 List subList = loadQueue.subList(0, newCount); 330 returnValue.addAll(subList); 331 subList.clear(); 332 } else { 333 returnValue.addAll(loadQueue); 334 loadQueue.clear(); 335 } 336 } 337 338 return returnValue; 339 } else { 340 return null; 341 } 342 } 343 344 348 public synchronized void enqueue() { 349 if (getFolderInfo() != null && getFolderInfo().getFolderThread() != null && ! getFolderInfo().getFolderThread().getQueue().contains(mAction)) { 351 getFolderInfo().getFolderThread().addToQueue(mAction, null, net.suberic.util.thread.ActionThread.PRIORITY_LOW); 352 } 353 } 354 public int getUpdateMessagesCount() { 355 return updateMessagesCount; 356 } 357 358 public void setUpdateMessagesCount(int newValue) { 359 updateMessagesCount = newValue; 360 } 361 362 365 public synchronized int getQueueSize() { 366 return loadQueue.size() + priorityLoadQueue.size(); 367 } 368 369 372 public synchronized int getCacheQueueSize() { 373 return cacheQueue.size(); 374 } 375 376 public List getColumnValues() { 377 return columnValues; 378 } 379 380 public void setColumnValues(List newValue) { 381 columnValues=newValue; 382 } 383 384 public FolderInfo getFolderInfo() { 385 return folderInfo; 386 } 387 388 public boolean isEnqueued() { 389 return mEnqueued; 390 } 391 392 395 public void stopLoading() { 396 stopped = true; 397 } 398 } 399 400 401 402 403 404 405 | Popular Tags |