1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.hibernate;
24  
25  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
26  
27  import java.lang.Object;
28  import java.lang.String;
29  import java.lang.reflect.InvocationHandler;
30  import java.lang.reflect.InvocationTargetException;
31  import java.lang.reflect.Method;
32  
33  import org.hibernate.classic.Session;
34  
35  /**
36   * <a href="SessionInvocationHandler.java.html"><b><i>View Source</i></b></a>
37   *
38   * <p>
39   * See http://support.liferay.com/browse/LEP-2996.
40   * </p>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class SessionInvocationHandler implements InvocationHandler {
45  
46      public SessionInvocationHandler(Session session) {
47          _session = session;
48      }
49  
50      public Session getSession() {
51          return _session;
52      }
53  
54      @SuppressWarnings("deprecation")
55      public Object invoke(Object proxy, Method method, Object[] args)
56          throws Throwable {
57  
58          String methodName = method.getName();
59  
60          if (methodName.equals(_CONNECTION)) {
61              Thread currentThread = Thread.currentThread();
62  
63              ClassLoader contextClassLoader =
64                  currentThread.getContextClassLoader();
65  
66              try {
67                  ClassLoader portalClassLoader =
68                      PortalClassLoaderUtil.getClassLoader();
69  
70                  currentThread.setContextClassLoader(portalClassLoader);
71  
72                  return _session.connection();
73              }
74              finally {
75                  currentThread.setContextClassLoader(contextClassLoader);
76              }
77          }
78          else if (methodName.equals(_EQUALS)) {
79              if (proxy == args[0]) {
80                  return Boolean.TRUE;
81              }
82              else {
83                  return Boolean.FALSE;
84              }
85          }
86          else if (methodName.equals(_HASHCODE)) {
87              return new Integer(hashCode());
88          }
89  
90          try {
91              return method.invoke(_session, args);
92          }
93          catch (InvocationTargetException ite) {
94              throw ite.getTargetException();
95          }
96      }
97  
98      private static final String _CONNECTION = "connection";
99  
100     private static final String _EQUALS = "equals";
101 
102     private static final String _HASHCODE = "hashCode";
103 
104     private final Session _session;
105 
106 }