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