1
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.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.pop.MessageListener;
30 import com.liferay.portal.kernel.util.UnmodifiableList;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36
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<MessageListener> 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<MessageListener> 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<MessageListener> _getListeners() {
133 if (_log.isDebugEnabled()) {
134 _log.debug("Listeners size " + _listeners.size());
135 }
136
137 return new UnmodifiableList(_listeners);
138 }
139
140 private void _start() {
141 if (_log.isDebugEnabled()) {
142 _log.debug("Start");
143 }
144
145 try {
146 _popNotificationsJob = new POPNotificationsJob();
147
148 JobSchedulerUtil.schedule(_popNotificationsJob);
149
150 }
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 if (_popNotificationsJob != null) {
164 JobSchedulerUtil.unschedule(_popNotificationsJob);
165 }
166 }
167 catch (Exception e) {
168 _log.error(e, e);
169 }
170 }
171
172 private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
173
174 private static POPServerUtil _instance = new POPServerUtil();
175
176 private IntervalJob _popNotificationsJob;
177 private List<MessageListener> _listeners = new ArrayList<MessageListener>();
178
179 }