1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.spring.hibernate;
16  
17  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
18  
19  import java.lang.Object;
20  import java.lang.String;
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  
25  import org.hibernate.classic.Session;
26  
27  /**
28   * <a href="SessionInvocationHandler.java.html"><b><i>View Source</i></b></a>
29   *
30   * <p>
31   * See http://issues.liferay.com/browse/LEP-2996.
32   * </p>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class SessionInvocationHandler implements InvocationHandler {
37  
38      public SessionInvocationHandler(Session session) {
39          _session = session;
40      }
41  
42      public Session getSession() {
43          return _session;
44      }
45  
46      @SuppressWarnings("deprecation")
47      public Object invoke(Object proxy, Method method, Object[] args)
48          throws Throwable {
49  
50          String methodName = method.getName();
51  
52          if (methodName.equals(_CONNECTION)) {
53              Thread currentThread = Thread.currentThread();
54  
55              ClassLoader contextClassLoader =
56                  currentThread.getContextClassLoader();
57  
58              try {
59                  ClassLoader portalClassLoader =
60                      PortalClassLoaderUtil.getClassLoader();
61  
62                  currentThread.setContextClassLoader(portalClassLoader);
63  
64                  return _session.connection();
65              }
66              finally {
67                  currentThread.setContextClassLoader(contextClassLoader);
68              }
69          }
70          else if (methodName.equals(_EQUALS)) {
71              if (proxy == args[0]) {
72                  return Boolean.TRUE;
73              }
74              else {
75                  return Boolean.FALSE;
76              }
77          }
78          else if (methodName.equals(_HASHCODE)) {
79              return new Integer(hashCode());
80          }
81  
82          try {
83              return method.invoke(_session, args);
84          }
85          catch (InvocationTargetException ite) {
86              throw ite.getTargetException();
87          }
88      }
89  
90      private static final String _CONNECTION = "connection";
91  
92      private static final String _EQUALS = "equals";
93  
94      private static final String _HASHCODE = "hashCode";
95  
96      private final Session _session;
97  
98  }