Source Code Cross Referenced for OutgoingMailServerManager.java in  » Mail-Clients » pooka » net » suberic » pooka » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Mail Clients » pooka » net.suberic.pooka 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.suberic.pooka;
002:
003:        import java.util.*;
004:
005:        import javax.mail.*;
006:        import javax.mail.internet.*;
007:
008:        import net.suberic.util.*;
009:
010:        /**
011:         * <p>An object which manages OutgoingMailServer resources.</p>
012:         *
013:         * @author Allen Petersen
014:         * @version $Revision: 1.5 $
015:         */
016:        public class OutgoingMailServerManager implements  ItemCreator,
017:                ItemListChangeListener {
018:
019:            private ItemManager manager;
020:            private LinkedList listenerList = new LinkedList();
021:
022:            private DefaultOutgoingMailServer defaultServer = null;
023:
024:            /**
025:             * <p>Creates a new OutgoingMailServerManager.</p>
026:             */
027:            public OutgoingMailServerManager() {
028:                createOutgoingMailServerList();
029:            }
030:
031:            //-----------------------
032:            // public interface.
033:
034:            /**
035:             * This listens for ItemListChangeEvents, which result from changes to the 
036:             * "OutgoingMailServer" property.  The result is that refreshOutgoingMailServers() is called,
037:             * and then the event is passed to listeners to this object.
038:             */
039:            public void itemListChanged(ItemListChangeEvent e) {
040:                fireItemListChanged(e);
041:            }
042:
043:            /**
044:             * This returns a Vector with all the currently registered OutgoingMailServer
045:             * objects.
046:             */
047:            public java.util.Vector getOutgoingMailServerList() {
048:                return manager.getItems();
049:            }
050:
051:            /**
052:             * This adds the OutgoingMailServer with the given OutgoingMailServerName to the 
053:             * allOutgoingMailServers list.
054:             */
055:            public void addOutgoingMailServer(String OutgoingMailServerName) {
056:                manager.addItem(OutgoingMailServerName);
057:            }
058:
059:            /**
060:             * This adds the OutgoingMailServers with the given OutgoingMailServerNames to the allOutgoingMailServers list.
061:             */
062:            public void addOutgoingMailServer(String[] OutgoingMailServerName) {
063:                manager.addItem(OutgoingMailServerName);
064:            }
065:
066:            /**
067:             * This removes the OutgoingMailServer with the given OutgoingMailServerName.
068:             */
069:            public void removeOutgoingMailServer(String OutgoingMailServerName) {
070:                manager.removeItem(OutgoingMailServerName);
071:            }
072:
073:            /**
074:             * This removes the OutgoingMailServers with the given OutgoingMailServerNames.
075:             */
076:            public void removeOutgoingMailServer(
077:                    String[] OutgoingMailServerNames) {
078:                manager.removeItem(OutgoingMailServerNames);
079:            }
080:
081:            /**
082:             * This removes the given OutgoingMailServer.
083:             */
084:            public void removeOutgoingMailServer(
085:                    OutgoingMailServer OutgoingMailServer) {
086:                manager.removeItem(OutgoingMailServer);
087:            }
088:
089:            /**
090:             * This removes the given OutgoingMailServers.
091:             */
092:            public void removeOutgoingMailServer(
093:                    OutgoingMailServer[] OutgoingMailServers) {
094:                manager.removeItem(OutgoingMailServers);
095:            }
096:
097:            /**
098:             * This returns the NetwordOutgoingMailServer with the given OutgoingMailServerName if it 
099:             * exists; otherwise, returns null.
100:             */
101:            public OutgoingMailServer getOutgoingMailServer(
102:                    String OutgoingMailServerID) {
103:                return (OutgoingMailServer) manager
104:                        .getItem(OutgoingMailServerID);
105:            }
106:
107:            /**
108:             * This returns the NetworkOutgoingMailServer with the given 
109:             * OutgoingMailServerName if it 
110:             * exists; otherwise, returns null.
111:             */
112:            public OutgoingMailServer getDefaultOutgoingMailServer() {
113:                if (defaultServer == null) {
114:                    String defaultId = Pooka.getProperty(
115:                            "OutgoingServer._default", "");
116:                    if (defaultId != "") {
117:                        defaultServer = new DefaultOutgoingMailServer(defaultId);
118:                    }
119:                }
120:
121:                return defaultServer;
122:            }
123:
124:            /**
125:             * This adds a ItemListChangeListener to the local listener list.
126:             */
127:            public void addItemListChangeListener(ItemListChangeListener ilcl) {
128:                if (!listenerList.contains(ilcl))
129:                    listenerList.add(ilcl);
130:            }
131:
132:            /**
133:             * This removes a ItemListChangeListener from the local listener list.
134:             */
135:            public void removeItemListChangeListener(ItemListChangeListener ilcl) {
136:                listenerList.remove(ilcl);
137:            }
138:
139:            /**
140:             * This notifies all listeners that the OutgoingMailServerList has changed.
141:             */
142:            public void fireItemListChanged(ItemListChangeEvent e) {
143:                for (int i = 0; i < listenerList.size(); i++)
144:                    ((ItemListChangeListener) listenerList.get(i))
145:                            .itemListChanged(e);
146:            }
147:
148:            /**
149:             * This creates a new OutgoingMailServer.
150:             */
151:            public Item createItem(VariableBundle sourceBundle,
152:                    String resourceString, String itemID) {
153:                return new OutgoingMailServer(itemID);
154:            }
155:
156:            //---------------------------
157:            // the background stuff.
158:
159:            /**
160:             * This loads and creates all the OutgoingMailServers using the "OutgoingServer" 
161:             * property of the main Pooka VariableBundle.
162:             */
163:            private void createOutgoingMailServerList() {
164:                manager = new ItemManager("OutgoingServer", Pooka
165:                        .getResources(), this );
166:
167:                manager.addItemListChangeListener(this );
168:            }
169:
170:            /**
171:             * Tells all of the FolderInfos that are outboxes that they are, in fact,
172:             * outboxes.
173:             */
174:            public void loadOutboxFolders() {
175:                Vector v = getOutgoingMailServerList();
176:
177:                for (int i = 0; i < v.size(); i++) {
178:                    OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
179:                    oms.loadOutboxFolder();
180:                }
181:            }
182:
183:            /**
184:             * Stops all mail server threads.
185:             */
186:            public void stopServers() {
187:                Vector v = getOutgoingMailServerList();
188:                VariableBundle resources = Pooka.getResources();
189:                for (int i = 0; i < v.size(); i++) {
190:                    OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
191:                    oms.stopThread();
192:                    resources.removeValueChangeListener(oms);
193:                }
194:                resources.removeValueChangeListener(manager);
195:            }
196:
197:            /**
198:             * A special OutgoingMailServer that represents the 'default' value.
199:             */
200:            class DefaultOutgoingMailServer extends OutgoingMailServer {
201:                String defaultServerId = null;
202:                OutgoingMailServer underlyingServer;
203:                String outboxID = null;
204:
205:                /**
206:                 * <p>Creates a new DefaultOutgoingMailServer using the given 
207:                 * String as the default server id.
208:                 */
209:                public DefaultOutgoingMailServer(String newDefaultServerId) {
210:                    super (newDefaultServerId);
211:                }
212:
213:                /**
214:                 * <p>Configures this mail server.</p>
215:                 */
216:                protected void configure() {
217:                    defaultServerId = id;
218:                    id = Pooka.getProperty("OutgoingServer._default.label",
219:                            "*default*");
220:                    propertyName = "OutgoingServer._default";
221:
222:                    // changing because we get a NPE here for some reason...
223:                    //underlyingServer = getOutgoingMailServer(defaultServerId);
224:
225:                    underlyingServer = Pooka.getOutgoingMailManager()
226:                            .getOutgoingMailServer(defaultServerId);
227:                    outboxID = Pooka.getProperty("OutgoingServer."
228:                            + defaultServerId + ".outbox", "");
229:
230:                    mailServerThread = new net.suberic.util.thread.ActionThread(
231:                            "default - smtp thread");
232:                    mailServerThread.start();
233:                }
234:
235:                /**
236:                 * Sends all available messages.
237:                 *
238:                 * What this will do actually is send all of the default server's
239:                 * messages, and then send the underlying server's messages, if any.
240:                 */
241:                protected void internal_sendAll()
242:                        throws javax.mail.MessagingException {
243:
244:                    NetworkConnection connection = getConnection();
245:
246:                    if (connection.getStatus() == NetworkConnection.DISCONNECTED) {
247:                        connection.connect();
248:                    }
249:
250:                    if (connection.getStatus() != NetworkConnection.CONNECTED) {
251:                        throw new MessagingException(Pooka.getProperty(
252:                                "error.connectionDown",
253:                                "Connection down for Mail Server:  ")
254:                                + getItemID());
255:                    }
256:
257:                    Transport sendTransport = Pooka.getDefaultSession()
258:                            .getTransport(underlyingServer.getSendMailURL());
259:                    try {
260:                        sendTransport.connect();
261:
262:                        FolderInfo outbox = getOutbox();
263:
264:                        Message[] msgs;
265:
266:                        if (outbox != null) {
267:                            msgs = outbox.getFolder().getMessages();
268:
269:                            try {
270:                                for (int i = 0; i < msgs.length; i++) {
271:                                    Message m = msgs[i];
272:                                    if (!m.isSet(Flags.Flag.DRAFT)) {
273:                                        sendTransport.sendMessage(m, m
274:                                                .getAllRecipients());
275:                                        m.setFlag(Flags.Flag.DELETED, true);
276:                                    }
277:                                }
278:                            } finally {
279:                                outbox.getFolder().expunge();
280:                            }
281:                        }
282:
283:                        FolderInfo underlyingOutbox = underlyingServer
284:                                .getOutbox();
285:
286:                        if (underlyingOutbox != null
287:                                && underlyingOutbox != outbox) {
288:                            msgs = underlyingOutbox.getFolder().getMessages();
289:
290:                            try {
291:                                for (int i = 0; i < msgs.length; i++) {
292:                                    Message m = msgs[i];
293:                                    if (!m.isSet(Flags.Flag.DRAFT)) {
294:                                        sendTransport.sendMessage(m, m
295:                                                .getAllRecipients());
296:                                        m.setFlag(Flags.Flag.DELETED, true);
297:                                    }
298:                                }
299:                            } finally {
300:                                underlyingOutbox.getFolder().expunge();
301:                            }
302:                        }
303:                    } finally {
304:                        sendTransport.close();
305:                    }
306:                }
307:
308:                /**
309:                 * <p>The NetworkConnection that this OutgoingMailServer depends on.
310:                 */
311:                public NetworkConnection getConnection() {
312:                    return underlyingServer.getConnection();
313:                }
314:
315:                /**
316:                 * <p>The FolderInfo where messages for this MailServer are
317:                 * stored until they're ready to be sent.
318:                 */
319:                public FolderInfo getOutbox() {
320:                    if (outboxID != null && outboxID != "") {
321:                        return Pooka.getStoreManager().getFolder(outboxID);
322:                    } else {
323:                        return null;
324:                    }
325:                }
326:
327:                /**
328:                 * Returns the sendMailURL.
329:                 */
330:                public URLName getSendMailURL() {
331:                    return underlyingServer.getSendMailURL();
332:                }
333:            }
334:
335:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.