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.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            public boolean equals(Object obj) {
036                    InvokerMessageListener messageListenerInvoker =
037                            (InvokerMessageListener)obj;
038    
039                    return _messageListener.equals(
040                            messageListenerInvoker.getMessageListener());
041            }
042    
043            public ClassLoader getClassLoader() {
044                    return _classLoader;
045            }
046    
047            public MessageListener getMessageListener() {
048                    return _messageListener;
049            }
050    
051            public int hashCode() {
052                    return _messageListener.hashCode();
053            }
054    
055            public void receive(Message message) {
056                    Thread currentThread = Thread.currentThread();
057    
058                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
059    
060                    currentThread.setContextClassLoader(_classLoader);
061    
062                    try {
063                            _messageListener.receive(message);
064                    }
065                    finally {
066                            currentThread.setContextClassLoader(contextClassLoader);
067                    }
068            }
069    
070            private MessageListener _messageListener;
071            private ClassLoader _classLoader;
072    
073    }