1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.hibernate;
21  
22  import com.liferay.portal.dao.orm.hibernate.LiferayClassicSession;
23  
24  import java.lang.Object;
25  import java.lang.reflect.InvocationHandler;
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  
29  import org.hibernate.HibernateException;
30  import org.hibernate.SessionFactory;
31  import org.hibernate.classic.Session;
32  
33  import org.springframework.orm.hibernate3.SessionFactoryUtils;
34  
35  /**
36   * <a href="SessionFactoryInvocationHandler.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * <p>
40   * See http://support.liferay.com/browse/LEP-2996.
41   * </p>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SessionFactoryInvocationHandler implements InvocationHandler {
47  
48      public SessionFactoryInvocationHandler(SessionFactory sessionFactory) {
49          _sessionFactory = sessionFactory;
50      }
51  
52      public Object invoke(Object proxy, Method method, Object[] args)
53          throws Throwable {
54  
55          String methodName = method.getName();
56  
57          if (methodName.equals("getCurrentSession")) {
58              try {
59                  Session session = (Session)SessionFactoryUtils.doGetSession(
60                      (SessionFactory)proxy, false);
61  
62                  return wrapLiferaySession(session);
63              }
64              catch (IllegalStateException ise) {
65                  throw new HibernateException(ise.getMessage());
66              }
67          }
68          else if (methodName.equals("openSession")) {
69              Session session = (Session)method.invoke(_sessionFactory, args);
70  
71              return wrapLiferaySession(session);
72          }
73          else if (methodName.equals("equals")) {
74              if (proxy == args[0]) {
75                  return Boolean.TRUE;
76              }
77              else {
78                  return Boolean.FALSE;
79              }
80          }
81          else if (methodName.equals("hashCode")) {
82              return new Integer(hashCode());
83          }
84  
85          try {
86              return method.invoke(_sessionFactory, args);
87          }
88          catch (InvocationTargetException ite) {
89              throw ite.getTargetException();
90          }
91      }
92  
93      protected Session wrapLiferaySession(Session session) {
94          if (session.getClass().getName().equals(
95                  LiferayClassicSession.class.getName())) {
96  
97              return session;
98          }
99          else {
100             return new LiferayClassicSession(session);
101         }
102     }
103 
104     private final SessionFactory _sessionFactory;
105 
106 }