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.dep;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import net.sf.hibernate.HibernateException;
025: import net.sf.hibernate.Query;
026: import net.sf.hibernate.Session;
027: import net.sf.hibernate.SessionFactory;
028: import net.sf.hibernate.Transaction;
029: import net.sf.hibernate.cfg.Configuration;
030: import org.compass.core.mapping.ResourceMapping;
031: import org.compass.gps.CompassGpsException;
032: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
033:
034: /**
035: * A {@link org.compass.gps.device.hibernate.HibernateGpsDevice} which works with hibernate 2.
036: * <p/>
037: * You must either set the Hibernate <code>Configuration</code> or the
038: * <code>SessionFactory</code> to be used by the device. Note that if the
039: * <code>Configuration</code> is supplied, when the device <code>start</code>
040: * is called, a new <code>SessionFactory</code> will be built.
041: * <p/>
042: * Note: Only provides the <code>index()</code> operation. No real time index
043: * updated are performed since the Hiberante <code>Interceptor</code> provides
044: * null values for newly created objects with generated ids.
045: *
046: * @author kimchy
047: */
048: public class Hibernate2GpsDevice extends AbstractHibernateGpsDevice {
049:
050: private SessionFactory sessionFactory;
051:
052: private Configuration configuration;
053:
054: private class Hibernate2SessionWrapper implements
055: HibernateSessionWrapper {
056:
057: private SessionFactory sessionFactory;
058:
059: private Session session;
060:
061: private Transaction tr;
062:
063: public Hibernate2SessionWrapper(SessionFactory sessionFactory) {
064: this .sessionFactory = sessionFactory;
065: }
066:
067: public Session getSession() {
068: return session;
069: }
070:
071: public void open() throws HibernateGpsDeviceException {
072: try {
073: session = sessionFactory.openSession();
074: } catch (HibernateException e) {
075: throw new HibernateGpsDeviceException(
076: buildMessage("Failed to open session to fetch data"),
077: e);
078: }
079: try {
080: tr = session.beginTransaction();
081: } catch (HibernateException e) {
082: throw new HibernateGpsDeviceException(
083: buildMessage("Failed to begin transaction to fetch data"),
084: e);
085: }
086: }
087:
088: public void close() {
089: if (tr != null) {
090: try {
091: tr.commit();
092: } catch (HibernateException e1) {
093: log
094: .error(
095: buildMessage("Failed to rollback hibernate transaction"),
096: e1);
097: }
098: }
099: try {
100: session.close();
101: } catch (HibernateException e) {
102: log
103: .error(
104: buildMessage("Failed to close Hibernate session"),
105: e);
106: }
107: }
108:
109: public void closeOnError() {
110: if (tr != null) {
111: try {
112: tr.rollback();
113: } catch (HibernateException e1) {
114: log
115: .error(
116: buildMessage("Failed to rollback hibernate transaction"),
117: e1);
118: }
119: }
120: try {
121: session.close();
122: } catch (HibernateException e) {
123: log
124: .error(
125: buildMessage("Failed to close Hibernate session"),
126: e);
127: }
128: }
129: }
130:
131: public Hibernate2GpsDevice() {
132:
133: }
134:
135: public Hibernate2GpsDevice(String name,
136: SessionFactory sessionFactory) {
137: setName(name);
138: this .sessionFactory = sessionFactory;
139: }
140:
141: public Hibernate2GpsDevice(String name, Configuration configuration) {
142: setName(name);
143: this .configuration = configuration;
144: }
145:
146: public void setSessionFactory(SessionFactory sessionFactory) {
147: this .sessionFactory = sessionFactory;
148: }
149:
150: public void setConfiguration(Configuration configuration) {
151: this .configuration = configuration;
152: }
153:
154: protected void doStart() throws CompassGpsException {
155: super .doStart();
156: if (sessionFactory == null) {
157: if (configuration == null) {
158: throw new HibernateGpsDeviceException(
159: buildMessage("Must set configuration or sessionFactory"));
160: }
161: try {
162: sessionFactory = configuration.buildSessionFactory();
163: } catch (HibernateException e) {
164: throw new HibernateGpsDeviceException(
165: buildMessage("Failed to create session factory"),
166: e);
167: }
168: }
169: }
170:
171: protected void doStop() throws CompassGpsException {
172: }
173:
174: protected HibernateSessionWrapper doGetHibernateSessionWrapper() {
175: return new Hibernate2SessionWrapper(sessionFactory);
176: }
177:
178: protected HibernateEntityInfo[] doGetHibernateEntitiesInfo()
179: throws HibernateGpsDeviceException {
180: ArrayList infos = new ArrayList();
181: try {
182: Map allClassMetaData = sessionFactory.getAllClassMetadata();
183: for (Iterator it = allClassMetaData.keySet().iterator(); it
184: .hasNext();) {
185: Class clazz = (Class) it.next();
186: if (isFilteredForIndex(clazz.getName())) {
187: continue;
188: }
189: if (compassGps.hasMappingForEntityForIndex(clazz)) {
190: ResourceMapping resourceMapping = compassGps
191: .getMappingForEntityForIndex(clazz);
192: HibernateEntityInfo info = new HibernateEntityInfo(
193: clazz.getName(), "from " + clazz.getName(),
194: resourceMapping.getSubIndexHash()
195: .getSubIndexes());
196: infos.add(info);
197: }
198: }
199: } catch (Exception e) {
200: throw new HibernateGpsDeviceException(
201: buildMessage("Failed to fetch all class meta data"),
202: e);
203: }
204:
205: return (HibernateEntityInfo[]) infos
206: .toArray(new HibernateEntityInfo[infos.size()]);
207: }
208:
209: protected List doGetObjects(HibernateEntityInfo info, int from,
210: int count, HibernateSessionWrapper sessionWrapper)
211: throws HibernateGpsDeviceException {
212: List values;
213: Session session = ((Hibernate2SessionWrapper) sessionWrapper)
214: .getSession();
215: try {
216: Query query = session.createQuery(info.getSelectQuery())
217: .setFirstResult(from).setMaxResults(count);
218: values = query.list();
219: } catch (Exception e) {
220: throw new HibernateGpsDeviceException(
221: buildMessage("Failed to open session to fetch data for class ["
222: + info.getName() + "]"), e);
223: }
224: return values;
225: }
226: }
|