001    /**
002     * Copyright (c) 2000-2012 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.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            @Override
063            public boolean equals(Object obj) {
064                    if (obj == null) {
065                            return false;
066                    }
067    
068                    MessageListenerWrapper listener = null;
069    
070                    try {
071                            listener = (MessageListenerWrapper)obj;
072                    }
073                    catch (ClassCastException cce) {
074                            return false;
075                    }
076    
077                    String id = listener.getId();
078    
079                    return getId().equals(id);
080            }
081    
082            public String getId() {
083                    return _listener.getId();
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    }