KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > thread > MessageLoader


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 JavaDoc;
13 import javax.swing.Action JavaDoc;
14 import javax.swing.AbstractAction JavaDoc;
15
16 /**
17  * This class does the actual loading of the header information from
18  * the messages in the folder. It also is set up to communicate with
19  * a JProgessBar to show how far the loading has gotten.
20  *
21  * More specifically, this class takes an array of Messages and a
22  * Vector of Strings which are the column values which are to be put
23  * into the table. It then loads the values into a Vector of Vectors,
24  * each of which contains the information for the Table for a group
25  * of Messages. It then throws a ChangeEvent to the listening
26  * FolderTableModel. The FolderTableModel can then get the information
27  * using the getNewMessages() function.
28  */

29
30 public class MessageLoader extends Thread JavaDoc {
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   /**
46    * The Action that will get added to the ActionQueue when there
47    * are messages to be loaded.
48    */

49   public Action JavaDoc mAction = null;
50
51   /**
52    * This creates a new MessageLoader from a FolderInfo object.
53    */

54   public MessageLoader(FolderInfo newFolderInfo) {
55     folderInfo = newFolderInfo;
56     mAction = new AbstractAction JavaDoc("Load Messages - " + newFolderInfo.getFolderID()) {
57     public void actionPerformed(ActionEvent JavaDoc ae) {
58       loadNextBatch();
59     }
60       };
61   }
62   
63   /**
64    * Loads the messages in the queue.
65    */

66   public void loadNextBatch() {
67     MessageProxy mp;
68     
69     // start this load section.
70
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     // get the batch sizes.
76
int fetchBatchSize = 50;
77     try {
78       fetchBatchSize = Integer.parseInt(Pooka.getProperty("Pooka.fetchBatchSize", "50"));
79     } catch (NumberFormatException JavaDoc nfe) {
80     }
81     
82     FetchProfile fetchProfile = getFolderInfo().getFetchProfile();
83     
84     // get the next batch.
85
List messageProxies = retrieveNextBatch(fetchBatchSize);
86     List toFetchInfos = new LinkedList();
87     
88     // go through and find all of the messages that need to be fetched
89
// or refetched, and add them to the toFetchInfos list.
90
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     // now load each individual messageproxy.
111
// and refresh each message.
112
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 JavaDoc 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 JavaDoc 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   /**
159    * Fires a new MessageLoadedEvent to each registered MessageLoadedListener.
160    */

161   public void fireMessageLoadedEvent(int type, int numMessages, int max) {
162     /*
163     for (int i = 0; i < messageLoadedListeners.size(); i ++) {
164       ((MessageLoadedListener)messageLoadedListeners.get(i)).handleMessageLoaded(new MessageLoadedEvent(this, type, numMessages, max));
165     }
166     */

167   }
168   
169   /**
170    * Adds a MessageLoadedListener to the messageLoadedListener list.
171    */

172   public void addMessageLoadedListener(MessageLoadedListener newListener) {
173     if (messageLoadedListeners.indexOf(newListener) == -1)
174       messageLoadedListeners.add(newListener);
175   }
176   
177   /**
178    * Removes a MessageLoadedListener from the messageLoadedListener list,
179    * if it's in the list.
180    */

181   public void removeMessageLoadedListener(MessageLoadedListener remListener) {
182     if (messageLoadedListeners.indexOf(remListener) > -1)
183       messageLoadedListeners.remove(remListener);
184   }
185   
186   /**
187    * Adds the MessageProxy(s) to the loadQueue.
188    */

189   public synchronized void loadMessages(MessageProxy mp) {
190     loadMessages(mp, NORMAL);
191   }
192
193   /**
194    * Adds the MessageProxy(s) to the loadQueue.
195    */

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   /**
211    * Adds the MessageProxy(s) to the loadQueue.
212    */

213   public synchronized void loadMessages(MessageProxy[] mp) {
214     loadMessages(mp, NORMAL);
215   }
216
217   /**
218    * Adds the MessageProxy(s) to the loadQueue.
219    */

220   public synchronized void loadMessages(MessageProxy[] mp, int pPriority) {
221     loadMessages(Arrays.asList(mp), pPriority);
222   }
223   
224   /**
225    * Adds the MessageProxy(s) to the loadQueue.
226    */

227   public synchronized void loadMessages(List mp) {
228     loadMessages(mp, NORMAL);
229   }
230
231   /**
232    * Adds the MessageProxy(s) to the loadQueue.
233    */

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   /**
250    * Adds the MessageProxy(s) to the cacheQueue.
251    */

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   /**
261    * Adds the MessageProxy(s) to the cacheQueue.
262    */

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   /**
272    * retrieves all the messages from the loadQueue, and resets that
273    * List to 0 (an empty List).
274    *
275    * generally, use retrieveNextBatch() instead.
276    */

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   /**
287    * Adds all of the entries in toAdd to targetList, in reversed order.
288    */

289   private void addUniqueReversed(List targetList, List toAdd) {
290     for (int i = toAdd.size() - 1; i >= 0; i--) {
291       Object JavaDoc current = toAdd.get(i);
292       if (current != null && ! targetList.contains(current))
293     targetList.add(current);
294     }
295   }
296
297   /**
298    * Retrieves the next pCount messages from the queue, or returns null
299    * if there are no entries in the queue.
300    */

301   public synchronized List retrieveNextBatch(int pCount) {
302     int plqLength = priorityLoadQueue.size();
303     int lqLength = loadQueue.size();
304
305     // check to see if we actually have anything in the queue.
306
if (plqLength + lqLength > 0) {
307       List returnValue = new LinkedList();
308
309       // adding the priority queue first
310
if (plqLength > 0) {
311     // if the priority queue is larger than (or the same size as) the
312
// requested count, then just return it.
313
if (plqLength >= pCount) {
314       List subList = priorityLoadQueue.subList(0, pCount);
315       returnValue.addAll(subList);
316       subList.clear();
317       return returnValue;
318     } else {
319       // just add of the priority queue, and go on.
320
returnValue.addAll(priorityLoadQueue);
321       priorityLoadQueue.clear();
322     }
323       }
324
325       // add in the normal queue now.
326
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   /**
345    * Adds the action for this MessageLoader to the FolderThread of the
346    * Folder.
347    */

348   public synchronized void enqueue() {
349     //FIXME
350
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   /**
363    * Returns the total amount left in the queue.
364    */

365   public synchronized int getQueueSize() {
366     return loadQueue.size() + priorityLoadQueue.size();
367   }
368
369   /**
370    * Returns the total amount left in the cache queue.
371    */

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   /**
393    * Stops the thread.
394    */

395   public void stopLoading() {
396     stopped = true;
397   }
398 }
399
400
401
402
403
404
405
Popular Tags