1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.pop;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.pop.MessageListener;
20  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
21  import com.liferay.portal.kernel.scheduler.SchedulerEntry;
22  import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
23  import com.liferay.portal.kernel.scheduler.TimeUnit;
24  import com.liferay.portal.kernel.scheduler.TriggerType;
25  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
26  import com.liferay.portal.kernel.util.UnmodifiableList;
27  import com.liferay.portal.pop.messaging.POPNotificationsMessageListener;
28  import com.liferay.portal.util.PropsValues;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
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<MessageListener>(_listeners);
135     }
136 
137     private void _start() {
138         if (_log.isDebugEnabled()) {
139             _log.debug("Start");
140         }
141 
142         try {
143             _schedulerEntry = new SchedulerEntryImpl();
144 
145             _schedulerEntry.setEventListenerClass(
146                 POPNotificationsMessageListener.class.getName());
147             _schedulerEntry.setTimeUnit(TimeUnit.MINUTE);
148             _schedulerEntry.setTriggerType(TriggerType.SIMPLE);
149             _schedulerEntry.setTriggerValue(
150                 PropsValues.POP_SERVER_NOTIFICATIONS_INTERVAL);
151 
152             SchedulerEngineUtil.schedule(
153                 _schedulerEntry, PortalClassLoaderUtil.getClassLoader());
154         }
155         catch (Exception e) {
156             _log.error(e, e);
157         }
158     }
159 
160     private void _stop() {
161         if (_log.isDebugEnabled()) {
162             _log.debug("Stop");
163         }
164 
165         try {
166             if (_schedulerEntry != null) {
167                 SchedulerEngineUtil.unschedule(_schedulerEntry);
168             }
169         }
170         catch (Exception e) {
171             _log.error(e, e);
172         }
173     }
174 
175     private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
176 
177     private static POPServerUtil _instance = new POPServerUtil();
178 
179     private List<MessageListener> _listeners = new ArrayList<MessageListener>();
180     private SchedulerEntry _schedulerEntry;
181 
182 }