1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.messaging;
16  
17  /**
18   * <a href="InvokerMessageListener.java.html"><b><i>View Source</i></b></a>
19   *
20   * @author Michael C. Han
21   */
22  public class InvokerMessageListener implements MessageListener {
23  
24      public InvokerMessageListener(MessageListener messageListener) {
25          this(
26              messageListener,
27              Thread.currentThread().getContextClassLoader());
28      }
29  
30      public InvokerMessageListener(
31          MessageListener messageListener, ClassLoader classLoader) {
32  
33          _messageListener = messageListener;
34          _classLoader = classLoader;
35      }
36  
37      public boolean equals(Object obj) {
38          InvokerMessageListener messageListenerInvoker =
39              (InvokerMessageListener)obj;
40  
41          return _messageListener.equals(
42              messageListenerInvoker.getMessageListener());
43      }
44  
45      public ClassLoader getClassLoader() {
46          return _classLoader;
47      }
48  
49      public MessageListener getMessageListener() {
50          return _messageListener;
51      }
52  
53      public int hashCode() {
54          return _messageListener.hashCode();
55      }
56  
57      public void receive(Message message) {
58          Thread currentThread = Thread.currentThread();
59  
60          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
61  
62          currentThread.setContextClassLoader(_classLoader);
63  
64          try {
65              _messageListener.receive(message);
66          }
67          finally {
68              currentThread.setContextClassLoader(contextClassLoader);
69          }
70      }
71  
72      private MessageListener _messageListener;
73      private ClassLoader _classLoader;
74  
75  }