001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.pop.MessageListenerException;
021    
022    import javax.mail.Message;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class MessageListenerWrapper implements MessageListener {
028    
029            public MessageListenerWrapper(MessageListener listener) {
030                    _listener = listener;
031            }
032    
033            public boolean accept(String from, String recipient, Message message) {
034                    if (_log.isDebugEnabled()) {
035                            _log.debug("Listener " + _listener.getClass().getName());
036                            _log.debug("From " + from);
037                            _log.debug("Recipient " + recipient);
038                    }
039    
040                    boolean value = _listener.accept(from, recipient, message);
041    
042                    if (_log.isDebugEnabled()) {
043                            _log.debug("Accept " + value);
044                    }
045    
046                    return value;
047            }
048    
049            public void deliver(String from, String recipient, Message message)
050                    throws MessageListenerException {
051    
052                    if (_log.isDebugEnabled()) {
053                            _log.debug("Listener " + _listener.getClass().getName());
054                            _log.debug("From " + from);
055                            _log.debug("Recipient " + recipient);
056                            _log.debug("Message " + message);
057                    }
058    
059                    _listener.deliver(from, recipient, message);
060            }
061    
062            public String getId() {
063                    return _listener.getId();
064            }
065    
066            public boolean equals(Object obj) {
067                    if (obj == null) {
068                            return false;
069                    }
070    
071                    MessageListenerWrapper listener = null;
072    
073                    try {
074                            listener = (MessageListenerWrapper)obj;
075                    }
076                    catch (ClassCastException cce) {
077                            return false;
078                    }
079    
080                    String id = listener.getId();
081    
082                    return getId().equals(id);
083            }
084    
085            public int hashCode() {
086                    return _listener.getId().hashCode();
087            }
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    MessageListenerWrapper.class);
091    
092            private MessageListener _listener;
093    
094    }