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.jdo;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import javax.jdo.PersistenceManager;
023: import javax.jdo.PersistenceManagerFactory;
024: import javax.jdo.Query;
025: import javax.jdo.Transaction;
026:
027: import org.compass.core.CompassSession;
028: import org.compass.core.spi.InternalCompass;
029: import org.compass.core.mapping.ResourceMapping;
030: import org.compass.core.mapping.osem.ClassMapping;
031: import org.compass.gps.CompassGpsException;
032: import org.compass.gps.device.AbstractGpsDevice;
033:
034: /**
035: * A JDO device, provides support for using jdo and jdo mapping files to index a
036: * database. The path can be views as: Database <-> JDO <-> Objects <->
037: * Compass::Gps <-> Compass::Core (Search Engine). What it means is that for
038: * every object that has both jdo and compass mappings, you will be able to
039: * index it's data, as well as real time mirroring of data changes.
040: * <p/>
041: * The <code>persistenceManagerFactory</code> must be set in order to perform
042: * the index operation.
043: * <p/>
044: * Note: Real time monitoring is only supported with JDO 2, please see
045: * {@link Jdo2GpsDevice} for more details.
046: *
047: * @author kimchy
048: */
049: public class JdoGpsDevice extends AbstractGpsDevice {
050:
051: protected PersistenceManagerFactory persistenceManagerFactory;
052:
053: public JdoGpsDevice() {
054:
055: }
056:
057: public JdoGpsDevice(String name,
058: PersistenceManagerFactory persistenceManagerFactory) {
059: setName(name);
060: this .persistenceManagerFactory = persistenceManagerFactory;
061: }
062:
063: protected void doStart() throws CompassGpsException {
064: if (persistenceManagerFactory == null) {
065: throw new IllegalArgumentException(
066: buildMessage("persistenceManagerFactory must be set"));
067: }
068: }
069:
070: protected void doIndex(CompassSession session)
071: throws CompassGpsException {
072: if (log.isInfoEnabled()) {
073: log.info(buildMessage("Indexing the database"));
074: }
075: // TODO is there a meta data option in jdo so we can get the persistent
076: // classes (similar to hibernate) instead of exception level handling
077: // based on the OSEM mappings?
078: final ResourceMapping[] resourceMappings = ((InternalCompass) compassGps
079: .getIndexCompass()).getMapping().getRootMappings();
080:
081: for (int i = 0; i < resourceMappings.length; i++) {
082: if (!(resourceMappings[i] instanceof ClassMapping)) {
083: continue;
084: }
085:
086: final ClassMapping classMapping = (ClassMapping) resourceMappings[i];
087: final Class clazz = classMapping.getClazz();
088: if (isFilteredForIndex(clazz.getName())) {
089: continue;
090: }
091: PersistenceManager pm = persistenceManagerFactory
092: .getPersistenceManager();
093: Transaction tr = pm.currentTransaction();
094: try {
095: tr.begin();
096: Collection datas = null;
097: try {
098: Query query = pm.newQuery(clazz);
099: datas = (Collection) query.execute();
100: } catch (Exception e) {
101: // no mapping for the class in jdo
102: }
103: if (datas == null || datas.size() == 0) {
104: continue;
105: }
106: if (log.isDebugEnabled()) {
107: log.debug(buildMessage("Indexing alias ["
108: + classMapping.getAlias()
109: + "] with object count [" + datas.size()
110: + "]"));
111: }
112: for (Iterator it = datas.iterator(); it.hasNext();) {
113: session.create(it.next());
114: }
115: tr.commit();
116: } catch (Exception e) {
117: log.error(buildMessage("Failed to index the database"),
118: e);
119: throw new JdoGpsDeviceException(
120: buildMessage("Failed to index the database"), e);
121: } finally {
122: if (tr.isActive()) {
123: tr.rollback();
124: }
125: pm.close();
126: }
127: }
128:
129: if (log.isInfoEnabled()) {
130: log.info(buildMessage("Finished indexing the database"));
131: }
132: }
133:
134: public PersistenceManagerFactory getPersistenceManagerFactory() {
135: return persistenceManagerFactory;
136: }
137:
138: public void setPersistenceManagerFactory(
139: PersistenceManagerFactory persistenceManagerFactory) {
140: this.persistenceManagerFactory = persistenceManagerFactory;
141: }
142: }
|