1   /**
2    * Copyright (c) 2000-2008 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.pop;
24  
25  import com.liferay.portal.kernel.job.IntervalJob;
26  import com.liferay.portal.kernel.job.JobSchedulerUtil;
27  import com.liferay.portal.kernel.pop.MessageListener;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class POPServerUtil {
44  
45      public static void addListener(MessageListener listener)
46          throws Exception {
47  
48          _instance._addListener(listener);
49      }
50  
51      public static void deleteListener(MessageListener listener)
52          throws Exception {
53  
54          _instance._deleteListener(listener);
55      }
56  
57      public static List<MessageListener> getListeners() throws Exception {
58          return _instance._getListeners();
59      }
60  
61      public static void start() {
62          _instance._start();
63      }
64  
65      public static void stop() {
66          _instance._stop();
67      }
68  
69      private POPServerUtil() {
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          MessageListenerWrapper messageListenerWrapper =
86              new MessageListenerWrapper(listener);
87  
88          _deleteListener(messageListenerWrapper);
89  
90          _listeners.add(messageListenerWrapper);
91  
92          if (_log.isDebugEnabled()) {
93              _log.debug("Listeners size " + _listeners.size());
94          }
95      }
96  
97      private void _deleteListener(MessageListenerWrapper listener) {
98          Iterator<MessageListener> itr = _listeners.iterator();
99  
100         while (itr.hasNext()) {
101             MessageListenerWrapper curListener =
102                 (MessageListenerWrapper)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         MessageListenerWrapper messageListenerWrapper =
124             new MessageListenerWrapper(listener);
125 
126         _deleteListener(messageListenerWrapper);
127 
128         if (_log.isDebugEnabled()) {
129             _log.debug("Listeners size " + _listeners.size());
130         }
131     }
132 
133     private List<MessageListener> _getListeners() {
134         if (_log.isDebugEnabled()) {
135             _log.debug("Listeners size " + _listeners.size());
136         }
137 
138         return Collections.unmodifiableList(_listeners);
139     }
140 
141     private void _start() {
142         if (_log.isDebugEnabled()) {
143             _log.debug("Start");
144         }
145 
146         try {
147             _popNotificationsJob = new POPNotificationsJob();
148 
149             JobSchedulerUtil.schedule(_popNotificationsJob);
150 
151             //_popNotificationsJob.pollPopServer();
152         }
153         catch (Exception e) {
154             _log.error(e, e);
155         }
156     }
157 
158     private void _stop() {
159         if (_log.isDebugEnabled()) {
160             _log.debug("Stop");
161         }
162 
163         try {
164             if (_popNotificationsJob != null) {
165                 JobSchedulerUtil.unschedule(_popNotificationsJob);
166             }
167         }
168         catch (Exception e) {
169             _log.error(e, e);
170         }
171     }
172 
173     private static Log _log = LogFactory.getLog(POPServerUtil.class);
174 
175     private static POPServerUtil _instance = new POPServerUtil();
176 
177     private IntervalJob _popNotificationsJob;
178     private List<MessageListener> _listeners = new ArrayList<MessageListener>();
179 
180 }