1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.smtp;
24  
25  import com.liferay.portal.kernel.smtp.MessageListener;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import org.subethamail.smtp.server.SMTPServer;
35  
36  /**
37   * <a href="SMTPServerUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class SMTPServerUtil {
43  
44      public static void addListener(MessageListener listener)
45          throws Exception {
46  
47          _instance._addListener(listener);
48      }
49  
50      public static void deleteListener(MessageListener listener)
51          throws Exception {
52  
53          _instance._deleteListener(listener);
54      }
55  
56      public static void setPort(int port) {
57          _instance._setPort(port);
58      }
59  
60      public static void start() {
61          _instance._start();
62      }
63  
64      public static void stop() {
65          _instance._stop();
66      }
67  
68      private SMTPServerUtil() {
69          _smtpServer = new SMTPServer(_listeners);
70      }
71  
72      private void _addListener(MessageListener listener) {
73          if (listener == null) {
74              if (_log.isDebugEnabled()) {
75                  _log.debug("Do not add null listener");
76              }
77  
78              return;
79          }
80  
81          if (_log.isDebugEnabled()) {
82              _log.debug("Add listener " + listener.getClass().getName());
83          }
84  
85          SubEthaMessageListenerImpl subEthaListener =
86              new SubEthaMessageListenerImpl(listener);
87  
88          _deleteListener(subEthaListener);
89  
90          _listeners.add(subEthaListener);
91  
92          if (_log.isDebugEnabled()) {
93              _log.debug("Listeners size " + _listeners.size());
94          }
95      }
96  
97      private void _deleteListener(SubEthaMessageListenerImpl listener) {
98          Iterator itr = _listeners.iterator();
99  
100         while (itr.hasNext()) {
101             SubEthaMessageListenerImpl curListener =
102                 (SubEthaMessageListenerImpl)itr.next();
103 
104             if (curListener.equals(listener)) {
105                 itr.remove();
106             }
107         }
108     }
109 
110     private void _deleteListener(MessageListener listener) {
111         if (listener == null) {
112             if (_log.isDebugEnabled()) {
113                 _log.debug("Do not delete null listener");
114             }
115 
116             return;
117         }
118 
119         if (_log.isDebugEnabled()) {
120             _log.debug("Delete listener " + listener.getClass().getName());
121         }
122 
123         SubEthaMessageListenerImpl subEthaListener =
124             new SubEthaMessageListenerImpl(listener);
125 
126         _deleteListener(subEthaListener);
127 
128         if (_log.isDebugEnabled()) {
129             _log.debug("Listeners size " + _listeners.size());
130         }
131     }
132 
133     private void _setPort(int port) {
134         if (_log.isDebugEnabled()) {
135             _log.debug("Listen on port " + port);
136         }
137 
138         _smtpServer.setPort(port);
139     }
140 
141     private void _start() {
142         if (_log.isDebugEnabled()) {
143             _log.debug("Start");
144         }
145 
146         _smtpServer.start();
147     }
148 
149     private void _stop() {
150         if (_log.isDebugEnabled()) {
151             _log.debug("Stop");
152         }
153 
154         _smtpServer.stop();
155     }
156 
157     private static Log _log = LogFactory.getLog(SMTPServerUtil.class);
158 
159     private static SMTPServerUtil _instance = new SMTPServerUtil();
160 
161     private SMTPServer _smtpServer;
162     private List _listeners = new ArrayList();
163 
164 }