1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.pop;
21  
22  import com.liferay.portal.kernel.job.IntervalJob;
23  import com.liferay.portal.kernel.job.JobSchedulerUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.pop.MessageListener;
27  import com.liferay.portal.kernel.util.UnmodifiableList;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class POPServerUtil {
40  
41      public static void addListener(MessageListener listener)
42          throws Exception {
43  
44          _instance._addListener(listener);
45      }
46  
47      public static void deleteListener(MessageListener listener)
48          throws Exception {
49  
50          _instance._deleteListener(listener);
51      }
52  
53      public static List<MessageListener> getListeners() throws Exception {
54          return _instance._getListeners();
55      }
56  
57      public static void start() {
58          _instance._start();
59      }
60  
61      public static void stop() {
62          _instance._stop();
63      }
64  
65      private POPServerUtil() {
66      }
67  
68      private void _addListener(MessageListener listener) {
69          if (listener == null) {
70              if (_log.isDebugEnabled()) {
71                  _log.debug("Do not add null listener");
72              }
73  
74              return;
75          }
76  
77          if (_log.isDebugEnabled()) {
78              _log.debug("Add listener " + listener.getClass().getName());
79          }
80  
81          MessageListenerWrapper messageListenerWrapper =
82              new MessageListenerWrapper(listener);
83  
84          _deleteListener(messageListenerWrapper);
85  
86          _listeners.add(messageListenerWrapper);
87  
88          if (_log.isDebugEnabled()) {
89              _log.debug("Listeners size " + _listeners.size());
90          }
91      }
92  
93      private void _deleteListener(MessageListenerWrapper listener) {
94          Iterator<MessageListener> itr = _listeners.iterator();
95  
96          while (itr.hasNext()) {
97              MessageListenerWrapper curListener =
98                  (MessageListenerWrapper)itr.next();
99  
100             if (curListener.equals(listener)) {
101                 itr.remove();
102             }
103         }
104     }
105 
106     private void _deleteListener(MessageListener listener) {
107         if (listener == null) {
108             if (_log.isDebugEnabled()) {
109                 _log.debug("Do not delete null listener");
110             }
111 
112             return;
113         }
114 
115         if (_log.isDebugEnabled()) {
116             _log.debug("Delete listener " + listener.getClass().getName());
117         }
118 
119         MessageListenerWrapper messageListenerWrapper =
120             new MessageListenerWrapper(listener);
121 
122         _deleteListener(messageListenerWrapper);
123 
124         if (_log.isDebugEnabled()) {
125             _log.debug("Listeners size " + _listeners.size());
126         }
127     }
128 
129     private List<MessageListener> _getListeners() {
130         if (_log.isDebugEnabled()) {
131             _log.debug("Listeners size " + _listeners.size());
132         }
133 
134         return new UnmodifiableList(_listeners);
135     }
136 
137     private void _start() {
138         if (_log.isDebugEnabled()) {
139             _log.debug("Start");
140         }
141 
142         try {
143             _popNotificationsJob = new POPNotificationsJob();
144 
145             JobSchedulerUtil.schedule(_popNotificationsJob);
146 
147             //_popNotificationsJob.pollPopServer();
148         }
149         catch (Exception e) {
150             _log.error(e, e);
151         }
152     }
153 
154     private void _stop() {
155         if (_log.isDebugEnabled()) {
156             _log.debug("Stop");
157         }
158 
159         try {
160             if (_popNotificationsJob != null) {
161                 JobSchedulerUtil.unschedule(_popNotificationsJob);
162             }
163         }
164         catch (Exception e) {
165             _log.error(e, e);
166         }
167     }
168 
169     private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
170 
171     private static POPServerUtil _instance = new POPServerUtil();
172 
173     private IntervalJob _popNotificationsJob;
174     private List<MessageListener> _listeners = new ArrayList<MessageListener>();
175 
176 }