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.Iterator;
020: import java.util.List;
021:
022: import org.compass.core.CompassSession;
023: import org.compass.gps.CompassGpsException;
024: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
025: import org.compass.gps.device.support.parallel.AbstractParallelGpsDevice;
026: import org.compass.gps.device.support.parallel.IndexEntitiesIndexer;
027: import org.compass.gps.device.support.parallel.IndexEntity;
028:
029: /**
030: * <p>An abstract hibernate device support. Aimed to provide the base operations
031: * required by both Hiberante 2 and Hibernate 3 - the index operation.
032: *
033: * <p>Extends the abstract parallel gps device providing parallel index execution
034: * support.
035: *
036: * @author kimchy
037: */
038: public abstract class AbstractHibernateGpsDevice extends
039: AbstractParallelGpsDevice {
040:
041: public static interface HibernateSessionWrapper {
042: void open() throws HibernateGpsDeviceException;
043:
044: void close();
045:
046: void closeOnError();
047: }
048:
049: protected int fetchCount = 200;
050:
051: public void setFetchCount(int fetchCount) {
052: this .fetchCount = fetchCount;
053: }
054:
055: protected IndexEntity[] doGetIndexEntities()
056: throws CompassGpsException {
057: return doGetHibernateEntitiesInfo();
058: }
059:
060: protected IndexEntitiesIndexer doGetIndexEntitiesIndexer() {
061: return new HibernateIndexEntitiesIndexer();
062: }
063:
064: /**
065: * Returns all the hibernate entity info. Called when the device starts up.
066: *
067: * @return Hibernate class informtion
068: * @throws HibernateGpsDeviceException
069: */
070: protected abstract HibernateEntityInfo[] doGetHibernateEntitiesInfo()
071: throws HibernateGpsDeviceException;
072:
073: /**
074: * Returns the data that maps to the given class info, paginated with from
075: * and count.
076: */
077: protected abstract List doGetObjects(HibernateEntityInfo info,
078: int from, int count, HibernateSessionWrapper sessionWrapper)
079: throws HibernateGpsDeviceException;
080:
081: protected abstract HibernateSessionWrapper doGetHibernateSessionWrapper();
082:
083: private class HibernateIndexEntitiesIndexer implements
084: IndexEntitiesIndexer {
085:
086: public void performIndex(CompassSession session,
087: IndexEntity[] entities) {
088:
089: for (int i = 0; i < entities.length; i++) {
090: HibernateEntityInfo entityInfo = (HibernateEntityInfo) entities[i];
091: int current = 0;
092: while (true) {
093: if (!isRunning()) {
094: return;
095: }
096: HibernateSessionWrapper sessionWrapper = doGetHibernateSessionWrapper();
097: try {
098: sessionWrapper.open();
099: final List values = doGetObjects(entityInfo,
100: current, fetchCount, sessionWrapper);
101: if (log.isDebugEnabled()) {
102: log.debug(buildMessage("Indexing entity ["
103: + entityInfo.getName()
104: + "] range [" + current + "-"
105: + (current + fetchCount) + "]"));
106: }
107: current += fetchCount;
108: for (Iterator it = values.iterator(); it
109: .hasNext();) {
110: session.create(it.next());
111: }
112: session.evictAll();
113: sessionWrapper.close();
114: if (values.size() < fetchCount) {
115: break;
116: }
117: } catch (Exception e) {
118: log
119: .error(
120: buildMessage("Failed to index the database"),
121: e);
122: sessionWrapper.closeOnError();
123: if (!(e instanceof HibernateGpsDeviceException)) {
124: throw new HibernateGpsDeviceException(
125: buildMessage("Failed to index the database"),
126: e);
127: }
128: throw (HibernateGpsDeviceException) e;
129: }
130: }
131: }
132:
133: }
134: }
135: }
|