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
41 public class POPServerUtil {
42
43 public static void addListener(MessageListener listener)
44 throws Exception {
45
46 _instance._addListener(listener);
47 }
48
49 public static void deleteListener(MessageListener listener)
50 throws Exception {
51
52 _instance._deleteListener(listener);
53 }
54
55 public static List<MessageListener> getListeners() throws Exception {
56 return _instance._getListeners();
57 }
58
59 public static void start() {
60 _instance._start();
61 }
62
63 public static void stop() {
64 _instance._stop();
65 }
66
67 private POPServerUtil() {
68 }
69
70 private void _addListener(MessageListener listener) {
71 if (listener == null) {
72 if (_log.isDebugEnabled()) {
73 _log.debug("Do not add null listener");
74 }
75
76 return;
77 }
78
79 if (_log.isDebugEnabled()) {
80 _log.debug("Add listener " + listener.getClass().getName());
81 }
82
83 MessageListenerWrapper messageListenerWrapper =
84 new MessageListenerWrapper(listener);
85
86 _deleteListener(messageListenerWrapper);
87
88 _listeners.add(messageListenerWrapper);
89
90 if (_log.isDebugEnabled()) {
91 _log.debug("Listeners size " + _listeners.size());
92 }
93 }
94
95 private void _deleteListener(MessageListenerWrapper listener) {
96 Iterator<MessageListener> itr = _listeners.iterator();
97
98 while (itr.hasNext()) {
99 MessageListenerWrapper curListener =
100 (MessageListenerWrapper)itr.next();
101
102 if (curListener.equals(listener)) {
103 itr.remove();
104 }
105 }
106 }
107
108 private void _deleteListener(MessageListener listener) {
109 if (listener == null) {
110 if (_log.isDebugEnabled()) {
111 _log.debug("Do not delete null listener");
112 }
113
114 return;
115 }
116
117 if (_log.isDebugEnabled()) {
118 _log.debug("Delete listener " + listener.getClass().getName());
119 }
120
121 MessageListenerWrapper messageListenerWrapper =
122 new MessageListenerWrapper(listener);
123
124 _deleteListener(messageListenerWrapper);
125
126 if (_log.isDebugEnabled()) {
127 _log.debug("Listeners size " + _listeners.size());
128 }
129 }
130
131 private List<MessageListener> _getListeners() {
132 if (_log.isDebugEnabled()) {
133 _log.debug("Listeners size " + _listeners.size());
134 }
135
136 return new UnmodifiableList(_listeners);
137 }
138
139 private void _start() {
140 if (_log.isDebugEnabled()) {
141 _log.debug("Start");
142 }
143
144 try {
145 _popNotificationsJob = new POPNotificationsJob();
146
147 JobSchedulerUtil.schedule(_popNotificationsJob);
148
149 }
151 catch (Exception e) {
152 _log.error(e, e);
153 }
154 }
155
156 private void _stop() {
157 if (_log.isDebugEnabled()) {
158 _log.debug("Stop");
159 }
160
161 try {
162 if (_popNotificationsJob != null) {
163 JobSchedulerUtil.unschedule(_popNotificationsJob);
164 }
165 }
166 catch (Exception e) {
167 _log.error(e, e);
168 }
169 }
170
171 private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
172
173 private static POPServerUtil _instance = new POPServerUtil();
174
175 private IntervalJob _popNotificationsJob;
176 private List<MessageListener> _listeners = new ArrayList<MessageListener>();
177
178 }