001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.pop;
022:
023: import com.liferay.portal.job.JobScheduler;
024: import com.liferay.portal.kernel.pop.MessageListener;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <a href="POPServerUtil.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class POPServerUtil {
041:
042: public static void addListener(MessageListener listener)
043: throws Exception {
044:
045: _instance._addListener(listener);
046: }
047:
048: public static void deleteListener(MessageListener listener)
049: throws Exception {
050:
051: _instance._deleteListener(listener);
052: }
053:
054: public static List getListeners() throws Exception {
055: return _instance._getListeners();
056: }
057:
058: public static void start() {
059: _instance._start();
060: }
061:
062: public static void stop() {
063: _instance._stop();
064: }
065:
066: private POPServerUtil() {
067: }
068:
069: private void _addListener(MessageListener listener) {
070: if (listener == null) {
071: if (_log.isDebugEnabled()) {
072: _log.debug("Do not add null listener");
073: }
074:
075: return;
076: }
077:
078: if (_log.isDebugEnabled()) {
079: _log.debug("Add listener " + listener.getClass().getName());
080: }
081:
082: MessageListenerWrapper messageListenerWrapper = new MessageListenerWrapper(
083: listener);
084:
085: _deleteListener(messageListenerWrapper);
086:
087: _listeners.add(messageListenerWrapper);
088:
089: if (_log.isDebugEnabled()) {
090: _log.debug("Listeners size " + _listeners.size());
091: }
092: }
093:
094: private void _deleteListener(MessageListenerWrapper listener) {
095: Iterator itr = _listeners.iterator();
096:
097: while (itr.hasNext()) {
098: MessageListenerWrapper curListener = (MessageListenerWrapper) itr
099: .next();
100:
101: if (curListener.equals(listener)) {
102: itr.remove();
103: }
104: }
105: }
106:
107: private void _deleteListener(MessageListener listener) {
108: if (listener == null) {
109: if (_log.isDebugEnabled()) {
110: _log.debug("Do not delete null listener");
111: }
112:
113: return;
114: }
115:
116: if (_log.isDebugEnabled()) {
117: _log.debug("Delete listener "
118: + listener.getClass().getName());
119: }
120:
121: MessageListenerWrapper messageListenerWrapper = new MessageListenerWrapper(
122: listener);
123:
124: _deleteListener(messageListenerWrapper);
125:
126: if (_log.isDebugEnabled()) {
127: _log.debug("Listeners size " + _listeners.size());
128: }
129: }
130:
131: private List _getListeners() {
132: if (_log.isDebugEnabled()) {
133: _log.debug("Listeners size " + _listeners.size());
134: }
135:
136: return Collections.unmodifiableList(_listeners);
137: }
138:
139: private void _start() {
140: if (_log.isDebugEnabled()) {
141: _log.debug("Start");
142: }
143:
144: try {
145: POPNotificationsJob popNotificationsJob = new POPNotificationsJob();
146:
147: JobScheduler.schedule(popNotificationsJob);
148:
149: //popNotificationsJob.pollPopServer();
150: } catch (Exception e) {
151: _log.error(e, e);
152: }
153: }
154:
155: private void _stop() {
156: if (_log.isDebugEnabled()) {
157: _log.debug("Stop");
158: }
159:
160: try {
161: JobScheduler.unscheduleJob(POPNotificationsJob.class
162: .getName());
163: } catch (Exception e) {
164: _log.error(e, e);
165: }
166: }
167:
168: private static Log _log = LogFactory.getLog(POPServerUtil.class);
169:
170: private static POPServerUtil _instance = new POPServerUtil();
171:
172: private List _listeners = new ArrayList();
173:
174: }
|