org.compass.spring.device.hibernate.dep.SpringHibernate3GpsDevice Maven / Gradle / Ivy
Show all versions of compass Show documentation
/*
* Copyright 2004-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.compass.spring.device.hibernate.dep;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
import org.compass.gps.device.hibernate.dep.Hibernate3GpsDevice;
import org.hibernate.SessionFactory;
/**
* A special Hibernate3GpsDevice
that handled cases when spring
* proxies the SessionFactory
(like when
* exposeTransactionAwareSessionFactory
is set to
* true
, which is the default from spring 1.2.X).
*
* Use this hibernate gps device instead of
* org.compass.gps.device.hibernate.dep.Hibernate3GpsDevice
if you
* are using Hibernate 3 and Spring.
*
* @author kimchy
*/
public class SpringHibernate3GpsDevice extends Hibernate3GpsDevice {
public static SessionFactory getNativeSessionFactory(SessionFactory sessionFactory) {
if (Proxy.isProxyClass(sessionFactory.getClass())) {
InvocationHandler invocationHandler = Proxy.getInvocationHandler(sessionFactory);
try {
Field target = invocationHandler.getClass().getDeclaredField("target");
target.setAccessible(true);
sessionFactory = (SessionFactory) target.get(invocationHandler);
} catch (Exception e) {
throw new HibernateGpsDeviceException("Failed to fetch actual session factory, " +
"sessionFactory[" + sessionFactory.getClass().getName() + "], " +
"invocationHandler[" + invocationHandler.getClass().getName() + "]", e);
}
}
return sessionFactory;
}
/**
* Returns the actual SessionFactory
in case it is proxied by
* spring.
*/
protected SessionFactory doGetActualSessionFactory() {
return getNativeSessionFactory(super.doGetActualSessionFactory());
}
}