| SessionFactoryInvocationHandler.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights 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.dao.orm.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 public class SessionFactoryInvocationHandler implements InvocationHandler {
49
50 public SessionFactoryInvocationHandler(SessionFactory sessionFactory) {
51 _sessionFactory = sessionFactory;
52 }
53
54 public Object invoke(Object proxy, Method method, Object[] args)
55 throws Throwable {
56
57 String methodName = method.getName();
58
59 if (methodName.equals("getCurrentSession")) {
60 try {
61 Session session = (Session)SessionFactoryUtils.doGetSession(
62 (SessionFactory)proxy, false);
63
64 return wrapLiferaySession(session);
65 }
66 catch (IllegalStateException ise) {
67 throw new HibernateException(ise.getMessage());
68 }
69 }
70 else if (methodName.equals("openSession")) {
71 Session session = (Session)method.invoke(_sessionFactory, args);
72
73 return wrapLiferaySession(session);
74 }
75 else if (methodName.equals("equals")) {
76 if (proxy == args[0]) {
77 return Boolean.TRUE;
78 }
79 else {
80 return Boolean.FALSE;
81 }
82 }
83 else if (methodName.equals("hashCode")) {
84 return new Integer(hashCode());
85 }
86
87 try {
88 return method.invoke(_sessionFactory, args);
89 }
90 catch (InvocationTargetException ite) {
91 throw ite.getTargetException();
92 }
93 }
94
95 protected Session wrapLiferaySession(Session session) {
96 if (session.getClass().getName().equals(
97 LiferayClassicSession.class.getName())) {
98
99 return session;
100 }
101 else {
102 return new LiferayClassicSession(session);
103 }
104 }
105
106 private final SessionFactory _sessionFactory;
107
108 }