001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.hibernate.embedded;
018:
019: import java.util.Properties;
020:
021: import org.compass.core.Compass;
022: import org.compass.core.CompassTemplate;
023: import org.compass.gps.CompassGps;
024: import org.compass.gps.device.hibernate.HibernateGpsDevice;
025: import org.compass.gps.impl.SingleCompassGps;
026: import org.hibernate.HibernateException;
027: import org.hibernate.Session;
028: import org.hibernate.SessionFactory;
029: import org.hibernate.engine.SessionImplementor;
030: import org.hibernate.event.PostInsertEventListener;
031: import org.hibernate.impl.SessionFactoryImpl;
032:
033: /**
034: * @author kimchy
035: */
036: public abstract class HibernateHelper {
037:
038: private HibernateHelper() {
039: }
040:
041: public static Compass getCompass(Session session) {
042: return findEventListener(session).getCompass();
043: }
044:
045: public static CompassTemplate getCompassTempalte(Session session) {
046: return new CompassTemplate(findEventListener(session)
047: .getCompass());
048: }
049:
050: public static Compass getCompass(SessionFactory sessionFactory) {
051: return findEventListener(sessionFactory).getCompass();
052: }
053:
054: public static CompassTemplate getCompassTempalte(
055: SessionFactory sessionFactory) {
056: return new CompassTemplate(findEventListener(sessionFactory)
057: .getCompass());
058: }
059:
060: public static Properties getIndexSettings(Session session) {
061: return findEventListener(session).getIndexSettings();
062: }
063:
064: public static Properties getIndexSettings(
065: SessionFactory sessionFactory) {
066: return findEventListener(sessionFactory).getIndexSettings();
067: }
068:
069: public static CompassGps getCompassGps(SessionFactory sessionFactory) {
070: HibernateGpsDevice device = new HibernateGpsDevice("hibernate",
071: sessionFactory);
072: return getCompassGps(device);
073: }
074:
075: public static CompassGps getCompassGps(HibernateGpsDevice device) {
076: SingleCompassGps gps = new SingleCompassGps(getCompass(device
077: .getSessionFactory()));
078: device.setMirrorDataChanges(false);
079: gps.setIndexProperties(getIndexSettings(device
080: .getSessionFactory()));
081: gps.addGpsDevice(device);
082: gps.start();
083: return gps;
084: }
085:
086: private static CompassEventListener findEventListener(
087: SessionFactory sessionFactory) {
088: PostInsertEventListener[] listeners = ((SessionFactoryImpl) sessionFactory)
089: .getEventListeners().getPostInsertEventListeners();
090: return findEventListener(listeners);
091: }
092:
093: private static CompassEventListener findEventListener(
094: Session session) {
095: PostInsertEventListener[] listeners = ((SessionImplementor) session)
096: .getListeners().getPostInsertEventListeners();
097: return findEventListener(listeners);
098: }
099:
100: private static CompassEventListener findEventListener(
101: PostInsertEventListener[] listeners) {
102: for (PostInsertEventListener candidate : listeners) {
103: if (candidate instanceof CompassEventListener) {
104: return (CompassEventListener) candidate;
105: }
106: }
107: throw new HibernateException(
108: "Compass Event listeners not configured, please check the reference documentation and the "
109: + "application's hibernate.cfg.xml");
110: }
111: }
|