1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.pop;
16  
17  import com.liferay.portal.kernel.job.IntervalJob;
18  import com.liferay.portal.kernel.job.JobSchedulerUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.pop.MessageListener;
22  import com.liferay.portal.kernel.util.UnmodifiableList;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  /**
29   * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class POPServerUtil {
34  
35      public static void addListener(MessageListener listener)
36          throws Exception {
37  
38          _instance._addListener(listener);
39      }
40  
41      public static void deleteListener(MessageListener listener)
42          throws Exception {
43  
44          _instance._deleteListener(listener);
45      }
46  
47      public static List<MessageListener> getListeners() throws Exception {
48          return _instance._getListeners();
49      }
50  
51      public static void start() {
52          _instance._start();
53      }
54  
55      public static void stop() {
56          _instance._stop();
57      }
58  
59      private POPServerUtil() {
60      }
61  
62      private void _addListener(MessageListener listener) {
63          if (listener == null) {
64              if (_log.isDebugEnabled()) {
65                  _log.debug("Do not add null listener");
66              }
67  
68              return;
69          }
70  
71          if (_log.isDebugEnabled()) {
72              _log.debug("Add listener " + listener.getClass().getName());
73          }
74  
75          MessageListenerWrapper messageListenerWrapper =
76              new MessageListenerWrapper(listener);
77  
78          _deleteListener(messageListenerWrapper);
79  
80          _listeners.add(messageListenerWrapper);
81  
82          if (_log.isDebugEnabled()) {
83              _log.debug("Listeners size " + _listeners.size());
84          }
85      }
86  
87      private void _deleteListener(MessageListenerWrapper listener) {
88          Iterator<MessageListener> itr = _listeners.iterator();
89  
90          while (itr.hasNext()) {
91              MessageListenerWrapper curListener =
92                  (MessageListenerWrapper)itr.next();
93  
94              if (curListener.equals(listener)) {
95                  itr.remove();
96              }
97          }
98      }
99  
100     private void _deleteListener(MessageListener listener) {
101         if (listener == null) {
102             if (_log.isDebugEnabled()) {
103                 _log.debug("Do not delete null listener");
104             }
105 
106             return;
107         }
108 
109         if (_log.isDebugEnabled()) {
110             _log.debug("Delete listener " + listener.getClass().getName());
111         }
112 
113         MessageListenerWrapper messageListenerWrapper =
114             new MessageListenerWrapper(listener);
115 
116         _deleteListener(messageListenerWrapper);
117 
118         if (_log.isDebugEnabled()) {
119             _log.debug("Listeners size " + _listeners.size());
120         }
121     }
122 
123     private List<MessageListener> _getListeners() {
124         if (_log.isDebugEnabled()) {
125             _log.debug("Listeners size " + _listeners.size());
126         }
127 
128         return new UnmodifiableList<MessageListener>(_listeners);
129     }
130 
131     private void _start() {
132         if (_log.isDebugEnabled()) {
133             _log.debug("Start");
134         }
135 
136         try {
137             _popNotificationsJob = new POPNotificationsJob();
138 
139             JobSchedulerUtil.schedule(_popNotificationsJob);
140 
141             //_popNotificationsJob.pollPopServer();
142         }
143         catch (Exception e) {
144             _log.error(e, e);
145         }
146     }
147 
148     private void _stop() {
149         if (_log.isDebugEnabled()) {
150             _log.debug("Stop");
151         }
152 
153         try {
154             if (_popNotificationsJob != null) {
155                 JobSchedulerUtil.unschedule(_popNotificationsJob);
156             }
157         }
158         catch (Exception e) {
159             _log.error(e, e);
160         }
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
164 
165     private static POPServerUtil _instance = new POPServerUtil();
166 
167     private IntervalJob _popNotificationsJob;
168     private List<MessageListener> _listeners = new ArrayList<MessageListener>();
169 
170 }