01: package org.compass.spring.device.hibernate;
02:
03: import java.lang.reflect.Field;
04: import java.lang.reflect.InvocationHandler;
05: import java.lang.reflect.Proxy;
06:
07: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
08: import org.compass.gps.device.hibernate.NativeHibernateExtractor;
09: import org.hibernate.SessionFactory;
10:
11: /**
12: * A Spring hibernate native SessionFactory extractor.
13: *
14: * @author kimchy
15: */
16: public class SpringNativeHibernateExtractor implements
17: NativeHibernateExtractor {
18:
19: public SessionFactory extractNative(SessionFactory sessionFactory)
20: throws HibernateGpsDeviceException {
21: if (Proxy.isProxyClass(sessionFactory.getClass())) {
22: InvocationHandler invocationHandler = Proxy
23: .getInvocationHandler(sessionFactory);
24: try {
25: Field target = invocationHandler.getClass()
26: .getDeclaredField("target");
27: target.setAccessible(true);
28: sessionFactory = (SessionFactory) target
29: .get(invocationHandler);
30: } catch (Exception e) {
31: throw new HibernateGpsDeviceException(
32: "Failed to fetch actual session factory, "
33: + "sessionFactory["
34: + sessionFactory.getClass().getName()
35: + "], "
36: + "invocationHandler["
37: + invocationHandler.getClass()
38: .getName() + "]", e);
39: }
40: }
41: return sessionFactory;
42:
43: }
44: }
|