001    /**
002     * Copyright (c) 2000-2011 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            @Override
067            public boolean equals(Object obj) {
068                    if (obj == null) {
069                            return false;
070                    }
071    
072                    MessageListenerWrapper listener = null;
073    
074                    try {
075                            listener = (MessageListenerWrapper)obj;
076                    }
077                    catch (ClassCastException cce) {
078                            return false;
079                    }
080    
081                    String id = listener.getId();
082    
083                    return getId().equals(id);
084            }
085    
086            @Override
087            public int hashCode() {
088                    return _listener.getId().hashCode();
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(
092                    MessageListenerWrapper.class);
093    
094            private MessageListener _listener;
095    
096    }