KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > outbox > SendListManager


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.folder.outbox;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.columba.mail.composer.SendableMessage;
23
24
25 /**
26  * Keeps a list of {@SenableMessage} objects.
27  *
28  *
29  * @author fdietz
30  */

31 public class SendListManager {
32     
33     private static final java.util.logging.Logger JavaDoc LOG =
34         java.util.logging.Logger.getLogger("org.columba.mail.folder.outbox"); //$NON-NLS-1$
35

36     private List JavaDoc sendAccounts;
37     private int counter;
38     private boolean mutex;
39
40     public SendListManager() {
41         sendAccounts = new Vector JavaDoc();
42         counter = 0;
43
44         mutex = false;
45     }
46
47     private synchronized void getMutex() {
48         while (mutex) {
49             try {
50                 wait();
51             } catch (InterruptedException JavaDoc e) {
52             }
53         }
54
55         mutex = true;
56     }
57
58     private synchronized void releaseMutex() {
59         mutex = false;
60         notify();
61     }
62
63     public void add(SendableMessage message) {
64         getMutex();
65
66         SendList actList;
67         counter++;
68
69         LOG.info("SMTP_SEND::Adding message in sendlistManager"); //$NON-NLS-1$
70

71         for (Iterator JavaDoc it = sendAccounts.iterator(); it.hasNext();) {
72             actList = (SendList) it.next();
73
74             // for( int i=0; i<sendAccounts.size(); i++)
75
// {
76
// actList = (SendList) sendAccounts.get(i);
77
if (message.getAccountUid() == actList.getAccountUid()) {
78                 actList.add(message);
79                 releaseMutex();
80
81                 return;
82             }
83         }
84
85         sendAccounts.add(new SendList(message));
86
87         releaseMutex();
88     }
89
90     public boolean hasMoreMessages() {
91         getMutex();
92
93         boolean output = (counter > 0);
94
95         releaseMutex();
96
97         return output;
98     }
99
100     public int count() {
101         int output;
102
103         LOG.info("DEBUG"); //$NON-NLS-1$
104

105         getMutex();
106
107         output = counter;
108
109         releaseMutex();
110
111         return output;
112     }
113
114     public Object JavaDoc getNextUid() {
115         getMutex();
116
117         SendList actList = (SendList) sendAccounts.get(0);
118         Object JavaDoc output = actList.getFirst().getUID();
119
120         counter--;
121
122         if (actList.count() == 0) {
123             sendAccounts.remove(0);
124         }
125
126         releaseMutex();
127
128         return output;
129     }
130
131     public SendableMessage getNextMessage() {
132         getMutex();
133
134         SendList actList = (SendList) sendAccounts.get(0);
135         SendableMessage output = actList.getFirst();
136
137         counter--;
138
139         if (actList.count() == 0) {
140             sendAccounts.remove(0);
141         }
142
143         releaseMutex();
144
145         return output;
146     }
147 }
148
149
150 class SendList {
151     private Vector JavaDoc messages;
152     private int accountUid;
153
154     public SendList(SendableMessage message) {
155         this.accountUid = message.getAccountUid();
156
157         messages = new Vector JavaDoc();
158         messages.add(message);
159     }
160
161     public int getAccountUid() {
162         return accountUid;
163     }
164
165     public void add(SendableMessage message) {
166         messages.add(message);
167     }
168
169     public SendableMessage getFirst() {
170         return (SendableMessage) messages.remove(0);
171     }
172
173     public SendableMessage get(int index) {
174         return (SendableMessage) messages.get(index);
175     }
176
177     public int count() {
178         return messages.size();
179     }
180 }
181
Popular Tags