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.job.JobScheduler;
26  import com.liferay.portal.kernel.pop.MessageListener;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class POPServerUtil {
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 List getListeners() throws Exception {
57          return _instance._getListeners();
58      }
59  
60      public static void start() {
61          _instance._start();
62      }
63  
64      public static void stop() {
65          _instance._stop();
66      }
67  
68      private POPServerUtil() {
69      }
70  
71      private void _addListener(MessageListener listener) {
72          if (listener == null) {
73              if (_log.isDebugEnabled()) {
74                  _log.debug("Do not add null listener");
75              }
76  
77              return;
78          }
79  
80          if (_log.isDebugEnabled()) {
81              _log.debug("Add listener " + listener.getClass().getName());
82          }
83  
84          MessageListenerWrapper messageListenerWrapper =
85              new MessageListenerWrapper(listener);
86  
87          _deleteListener(messageListenerWrapper);
88  
89          _listeners.add(messageListenerWrapper);
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Listeners size " + _listeners.size());
93          }
94      }
95  
96      private void _deleteListener(MessageListenerWrapper listener) {
97          Iterator itr = _listeners.iterator();
98  
99          while (itr.hasNext()) {
100             MessageListenerWrapper curListener =
101                 (MessageListenerWrapper)itr.next();
102 
103             if (curListener.equals(listener)) {
104                 itr.remove();
105             }
106         }
107     }
108 
109     private void _deleteListener(MessageListener listener) {
110         if (listener == null) {
111             if (_log.isDebugEnabled()) {
112                 _log.debug("Do not delete null listener");
113             }
114 
115             return;
116         }
117 
118         if (_log.isDebugEnabled()) {
119             _log.debug("Delete listener " + listener.getClass().getName());
120         }
121 
122         MessageListenerWrapper messageListenerWrapper =
123             new MessageListenerWrapper(listener);
124 
125         _deleteListener(messageListenerWrapper);
126 
127         if (_log.isDebugEnabled()) {
128             _log.debug("Listeners size " + _listeners.size());
129         }
130     }
131 
132     private List _getListeners() {
133         if (_log.isDebugEnabled()) {
134             _log.debug("Listeners size " + _listeners.size());
135         }
136 
137         return Collections.unmodifiableList(_listeners);
138     }
139 
140     private void _start() {
141         if (_log.isDebugEnabled()) {
142             _log.debug("Start");
143         }
144 
145         try {
146             POPNotificationsJob popNotificationsJob = new POPNotificationsJob();
147 
148             JobScheduler.schedule(popNotificationsJob);
149 
150             //popNotificationsJob.pollPopServer();
151         }
152         catch (Exception e) {
153             _log.error(e, e);
154         }
155     }
156 
157     private void _stop() {
158         if (_log.isDebugEnabled()) {
159             _log.debug("Stop");
160         }
161 
162         try {
163             JobScheduler.unscheduleJob(POPNotificationsJob.class.getName());
164         }
165         catch (Exception e) {
166             _log.error(e, e);
167         }
168     }
169 
170     private static Log _log = LogFactory.getLog(POPServerUtil.class);
171 
172     private static POPServerUtil _instance = new POPServerUtil();
173 
174     private List _listeners = new ArrayList();
175 
176 }