001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.pop;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.pop.MessageListener;
020    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
021    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
022    import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
023    import com.liferay.portal.kernel.scheduler.StorageType;
024    import com.liferay.portal.kernel.scheduler.TimeUnit;
025    import com.liferay.portal.kernel.scheduler.TriggerType;
026    import com.liferay.portal.kernel.util.UnmodifiableList;
027    import com.liferay.portal.pop.messaging.POPNotificationsMessageListener;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.util.ArrayList;
031    import java.util.Iterator;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class POPServerUtil {
038    
039            public static void addListener(MessageListener listener) throws Exception {
040                    _instance._addListener(listener);
041            }
042    
043            public static void deleteListener(MessageListener listener)
044                    throws Exception {
045    
046                    _instance._deleteListener(listener);
047            }
048    
049            public static List<MessageListener> getListeners() {
050                    return _instance._getListeners();
051            }
052    
053            public static void start() {
054                    _instance._start();
055            }
056    
057            private POPServerUtil() {
058            }
059    
060            private void _addListener(MessageListener listener) {
061                    if (listener == null) {
062                            if (_log.isDebugEnabled()) {
063                                    _log.debug("Do not add null listener");
064                            }
065    
066                            return;
067                    }
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug("Add listener " + listener.getClass().getName());
071                    }
072    
073                    MessageListenerWrapper messageListenerWrapper =
074                            new MessageListenerWrapper(listener);
075    
076                    _deleteListener(messageListenerWrapper);
077    
078                    _listeners.add(messageListenerWrapper);
079    
080                    if (_log.isDebugEnabled()) {
081                            _log.debug("Listeners size " + _listeners.size());
082                    }
083            }
084    
085            private void _deleteListener(MessageListener listener) {
086                    if (listener == null) {
087                            if (_log.isDebugEnabled()) {
088                                    _log.debug("Do not delete null listener");
089                            }
090    
091                            return;
092                    }
093    
094                    if (_log.isDebugEnabled()) {
095                            _log.debug("Delete listener " + listener.getClass().getName());
096                    }
097    
098                    MessageListenerWrapper messageListenerWrapper =
099                            new MessageListenerWrapper(listener);
100    
101                    _deleteListener(messageListenerWrapper);
102    
103                    if (_log.isDebugEnabled()) {
104                            _log.debug("Listeners size " + _listeners.size());
105                    }
106            }
107    
108            private void _deleteListener(MessageListenerWrapper listener) {
109                    Iterator<MessageListener> itr = _listeners.iterator();
110    
111                    while (itr.hasNext()) {
112                            MessageListenerWrapper curListener =
113                                    (MessageListenerWrapper)itr.next();
114    
115                            if (curListener.equals(listener)) {
116                                    itr.remove();
117                            }
118                    }
119            }
120    
121            private List<MessageListener> _getListeners() {
122                    if (_log.isDebugEnabled()) {
123                            _log.debug("Listeners size " + _listeners.size());
124                    }
125    
126                    return new UnmodifiableList<MessageListener>(_listeners);
127            }
128    
129            private void _start() {
130                    if (_log.isDebugEnabled()) {
131                            _log.debug("Start");
132                    }
133    
134                    try {
135                            SchedulerEntry schedulerEntry = new SchedulerEntryImpl();
136    
137                            schedulerEntry.setEventListenerClass(
138                                    POPNotificationsMessageListener.class.getName());
139                            schedulerEntry.setTimeUnit(TimeUnit.MINUTE);
140                            schedulerEntry.setTriggerType(TriggerType.SIMPLE);
141                            schedulerEntry.setTriggerValue(
142                                    PropsValues.POP_SERVER_NOTIFICATIONS_INTERVAL);
143    
144                            SchedulerEngineHelperUtil.schedule(
145                                    schedulerEntry, StorageType.MEMORY_CLUSTERED, null, 0);
146                    }
147                    catch (Exception e) {
148                            _log.error(e, e);
149                    }
150            }
151    
152            private static Log _log = LogFactoryUtil.getLog(POPServerUtil.class);
153    
154            private static POPServerUtil _instance = new POPServerUtil();
155    
156            private List<MessageListener> _listeners = new ArrayList<MessageListener>();
157    
158    }