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.kernel.messaging;
016    
017    /**
018     * @author Michael C. Han
019     */
020    public class InvokerMessageListener implements MessageListener {
021    
022            public InvokerMessageListener(MessageListener messageListener) {
023                    this(
024                            messageListener,
025                            Thread.currentThread().getContextClassLoader());
026            }
027    
028            public InvokerMessageListener(
029                    MessageListener messageListener, ClassLoader classLoader) {
030    
031                    _messageListener = messageListener;
032                    _classLoader = classLoader;
033            }
034    
035            @Override
036            public boolean equals(Object obj) {
037                    InvokerMessageListener messageListenerInvoker =
038                            (InvokerMessageListener)obj;
039    
040                    return _messageListener.equals(
041                            messageListenerInvoker.getMessageListener());
042            }
043    
044            public ClassLoader getClassLoader() {
045                    return _classLoader;
046            }
047    
048            public MessageListener getMessageListener() {
049                    return _messageListener;
050            }
051    
052            @Override
053            public int hashCode() {
054                    return _messageListener.hashCode();
055            }
056    
057            public void receive(Message message) throws MessageListenerException {
058                    Thread currentThread = Thread.currentThread();
059    
060                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
061    
062                    currentThread.setContextClassLoader(_classLoader);
063    
064                    try {
065                            _messageListener.receive(message);
066                    }
067                    finally {
068                            currentThread.setContextClassLoader(contextClassLoader);
069                    }
070            }
071    
072            private MessageListener _messageListener;
073            private ClassLoader _classLoader;
074    
075    }