001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.folder.outbox;
017:
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Vector;
021:
022: import org.columba.mail.composer.SendableMessage;
023:
024: /**
025: * Keeps a list of {@SenableMessage} objects.
026: *
027: *
028: * @author fdietz
029: */
030: public class SendListManager {
031:
032: private static final java.util.logging.Logger LOG = java.util.logging.Logger
033: .getLogger("org.columba.mail.folder.outbox"); //$NON-NLS-1$
034:
035: private List sendAccounts;
036: private int counter;
037: private boolean mutex;
038:
039: public SendListManager() {
040: sendAccounts = new Vector();
041: counter = 0;
042:
043: mutex = false;
044: }
045:
046: private synchronized void getMutex() {
047: while (mutex) {
048: try {
049: wait();
050: } catch (InterruptedException e) {
051: }
052: }
053:
054: mutex = true;
055: }
056:
057: private synchronized void releaseMutex() {
058: mutex = false;
059: notify();
060: }
061:
062: public void add(SendableMessage message) {
063: getMutex();
064:
065: SendList actList;
066: counter++;
067:
068: LOG.info("SMTP_SEND::Adding message in sendlistManager"); //$NON-NLS-1$
069:
070: for (Iterator it = sendAccounts.iterator(); it.hasNext();) {
071: actList = (SendList) it.next();
072:
073: // for( int i=0; i<sendAccounts.size(); i++)
074: // {
075: // actList = (SendList) sendAccounts.get(i);
076: if (message.getAccountUid() == actList.getAccountUid()) {
077: actList.add(message);
078: releaseMutex();
079:
080: return;
081: }
082: }
083:
084: sendAccounts.add(new SendList(message));
085:
086: releaseMutex();
087: }
088:
089: public boolean hasMoreMessages() {
090: getMutex();
091:
092: boolean output = (counter > 0);
093:
094: releaseMutex();
095:
096: return output;
097: }
098:
099: public int count() {
100: int output;
101:
102: LOG.info("DEBUG"); //$NON-NLS-1$
103:
104: getMutex();
105:
106: output = counter;
107:
108: releaseMutex();
109:
110: return output;
111: }
112:
113: public Object getNextUid() {
114: getMutex();
115:
116: SendList actList = (SendList) sendAccounts.get(0);
117: Object output = actList.getFirst().getUID();
118:
119: counter--;
120:
121: if (actList.count() == 0) {
122: sendAccounts.remove(0);
123: }
124:
125: releaseMutex();
126:
127: return output;
128: }
129:
130: public SendableMessage getNextMessage() {
131: getMutex();
132:
133: SendList actList = (SendList) sendAccounts.get(0);
134: SendableMessage output = actList.getFirst();
135:
136: counter--;
137:
138: if (actList.count() == 0) {
139: sendAccounts.remove(0);
140: }
141:
142: releaseMutex();
143:
144: return output;
145: }
146: }
147:
148: class SendList {
149: private Vector messages;
150: private int accountUid;
151:
152: public SendList(SendableMessage message) {
153: this .accountUid = message.getAccountUid();
154:
155: messages = new Vector();
156: messages.add(message);
157: }
158:
159: public int getAccountUid() {
160: return accountUid;
161: }
162:
163: public void add(SendableMessage message) {
164: messages.add(message);
165: }
166:
167: public SendableMessage getFirst() {
168: return (SendableMessage) messages.remove(0);
169: }
170:
171: public SendableMessage get(int index) {
172: return (SendableMessage) messages.get(index);
173: }
174:
175: public int count() {
176: return messages.size();
177: }
178: }
|