1
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
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 String methodName = method.getName();
59
60 if (methodName.equals("getCurrentSession")) {
61 try {
62 Session session = (Session)SessionFactoryUtils.doGetSession(
63 (SessionFactory)proxy, false);
64
65 return wrapLiferaySession(session);
66 }
67 catch (IllegalStateException ise) {
68 throw new HibernateException(ise.getMessage());
69 }
70 }
71 else if (methodName.equals("openSession")) {
72 Session session = (Session)method.invoke(_sessionFactory, args);
73
74 return wrapLiferaySession(session);
75 }
76 else if (methodName.equals("equals")) {
77 if (proxy == args[0]) {
78 return Boolean.TRUE;
79 }
80 else {
81 return Boolean.FALSE;
82 }
83 }
84 else if (methodName.equals("hashCode")) {
85 return new Integer(hashCode());
86 }
87
88 try {
89 return method.invoke(_sessionFactory, args);
90 }
91 catch (InvocationTargetException ite) {
92 throw ite.getTargetException();
93 }
94 }
95
96 protected Session wrapLiferaySession(Session session) {
97 if (session.getClass().getName().equals(
98 LiferayClassicSession.class.getName())) {
99
100 return session;
101 }
102 else {
103 return new LiferayClassicSession(session);
104 }
105 }
106
107 private final SessionFactory _sessionFactory;
108
109 }