001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.spring.hibernate;
022:
023: import com.liferay.util.dao.hibernate.LiferayClassicSession;
024:
025: import java.lang.Object;
026: import java.lang.reflect.InvocationHandler;
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029:
030: import org.hibernate.HibernateException;
031: import org.hibernate.SessionFactory;
032: import org.hibernate.classic.Session;
033:
034: import org.springframework.orm.hibernate3.SessionFactoryUtils;
035:
036: /**
037: * <a href="SessionFactoryInvocationHandler.java.html"><b><i>View Source</i></b>
038: * </a>
039: *
040: * <p>
041: * See http://support.liferay.com/browse/LEP-2996.
042: * </p>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class SessionFactoryInvocationHandler implements
048: InvocationHandler {
049:
050: public SessionFactoryInvocationHandler(SessionFactory sessionFactory) {
051: _sessionFactory = sessionFactory;
052: }
053:
054: public Object invoke(Object proxy, Method method, Object[] args)
055: throws Throwable {
056:
057: String methodName = method.getName();
058:
059: if (methodName.equals("getCurrentSession")) {
060: try {
061: Session session = (Session) SessionFactoryUtils
062: .doGetSession((SessionFactory) proxy, false);
063:
064: return wrapLiferaySession(session);
065: } catch (IllegalStateException ise) {
066: throw new HibernateException(ise.getMessage());
067: }
068: } else if (methodName.equals("openSession")) {
069: Session session = (Session) method.invoke(_sessionFactory,
070: args);
071:
072: return wrapLiferaySession(session);
073: } else if (methodName.equals("equals")) {
074: if (proxy == args[0]) {
075: return Boolean.TRUE;
076: } else {
077: return Boolean.FALSE;
078: }
079: } else if (methodName.equals("hashCode")) {
080: return new Integer(hashCode());
081: }
082:
083: try {
084: return method.invoke(_sessionFactory, args);
085: } catch (InvocationTargetException ite) {
086: throw ite.getTargetException();
087: }
088: }
089:
090: protected Session wrapLiferaySession(Session session) {
091: if (session.getClass().getName().equals(
092: LiferayClassicSession.class.getName())) {
093:
094: return session;
095: } else {
096: return new LiferayClassicSession(session);
097: }
098: }
099:
100: private final SessionFactory _sessionFactory;
101:
102: }
|