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.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  }