| SessionFactoryInvocationHandler.java |
1 /**
2 * Copyright (c) 2000-2007 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.util.spring.hibernate;
24
25 import com.liferay.util.dao.hibernate.LiferayClassicSession;
26
27 import java.lang.Object;
28 import java.lang.reflect.InvocationHandler;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31
32 import org.hibernate.HibernateException;
33 import org.hibernate.SessionFactory;
34 import org.hibernate.classic.Session;
35
36 import org.springframework.orm.hibernate3.SessionFactoryUtils;
37
38 /**
39 * <a href="SessionFactoryInvocationHandler.java.html"><b><i>View Source</i></b>
40 * </a>
41 *
42 * <p>
43 * See http://support.liferay.com/browse/LEP-2996.
44 * </p>
45 *
46 * @author Brian Wing Shun Chan
47 *
48 */
49 public class SessionFactoryInvocationHandler implements InvocationHandler {
50
51 public SessionFactoryInvocationHandler(SessionFactory sessionFactory) {
52 _sessionFactory = sessionFactory;
53 }
54
55 public Object invoke(Object proxy, Method method, Object[] args)
56 throws Throwable {
57
58 if (method.getName().equals("getCurrentSession")) {
59 try {
60 Session session = (Session)SessionFactoryUtils.doGetSession(
61 (SessionFactory)proxy, false);
62
63 return wrapLiferaySession(session);
64 }
65 catch (IllegalStateException ise) {
66 throw new HibernateException(ise.getMessage());
67 }
68 }
69 else if (method.getName().equals("openSession")) {
70 Session session = (Session)method.invoke(_sessionFactory, args);
71
72 return wrapLiferaySession(session);
73 }
74 else if (method.getName().equals("equals")) {
75 if (proxy == args[0]) {
76 return Boolean.TRUE;
77 }
78 else {
79 return Boolean.FALSE;
80 }
81 }
82 else if (method.getName().equals("hashCode")) {
83 return new Integer(hashCode());
84 }
85
86 try {
87 return method.invoke(_sessionFactory, args);
88 }
89 catch (InvocationTargetException ite) {
90 throw ite.getTargetException();
91 }
92 }
93
94 protected Session wrapLiferaySession(Session session) {
95 if (session.getClass().getName().equals(
96 LiferayClassicSession.class.getName())) {
97
98 return session;
99 }
100 else {
101 return new LiferayClassicSession(session);
102 }
103 }
104
105 private final SessionFactory _sessionFactory;
106
107 }