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;
018:
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.compass.core.CompassCallbackWithoutResult;
025: import org.compass.core.CompassException;
026: import org.compass.core.CompassSession;
027: import org.compass.core.spi.InternalCompassSession;
028: import org.compass.gps.CompassGps;
029: import org.compass.gps.CompassGpsDevice;
030: import org.compass.gps.CompassGpsException;
031: import org.compass.gps.MirrorDataChangesGpsDevice;
032: import org.compass.gps.spi.CompassGpsInterfaceDevice;
033:
034: /**
035: * A general abstract device which can be used by all types of devices.
036: * <p/>
037: * Provides support for device name, state management
038: * {@link AbstractGpsDevice#isRunning()}, as well as general helper methods.
039: *
040: * @author kimchy
041: */
042: public abstract class AbstractGpsDevice implements CompassGpsDevice {
043:
044: protected Log log = LogFactory.getLog(getClass());
045:
046: private String name;
047:
048: protected CompassGpsInterfaceDevice compassGps;
049:
050: private volatile boolean started = false;
051:
052: private boolean internalMirrorDataChanges = false;
053:
054: private String[] filteredEntitiesForIndex;
055:
056: private Set filteredEntitiesLookupForIndex;
057:
058: public String getName() {
059: return name;
060: }
061:
062: public void setName(String name) {
063: this .name = name;
064: }
065:
066: public CompassGps getGps() {
067: return compassGps;
068: }
069:
070: public void injectGps(CompassGps compassGps) {
071: this .compassGps = (CompassGpsInterfaceDevice) compassGps;
072: }
073:
074: public void setFilteredEntitiesForIndex(
075: String[] filteredEntitiesForIndex) {
076: this .filteredEntitiesForIndex = filteredEntitiesForIndex;
077: }
078:
079: public String buildMessage(String message) {
080: return "{" + name + "}: " + message;
081: }
082:
083: public boolean isFilteredForIndex(String entity) {
084: return (filteredEntitiesLookupForIndex != null && filteredEntitiesLookupForIndex
085: .contains(entity));
086: }
087:
088: public synchronized void index() throws CompassGpsException {
089: if (!isRunning()) {
090: throw new IllegalStateException(
091: buildMessage("must be running in order to perform the index operation"));
092: }
093: compassGps.executeForIndex(new CompassCallbackWithoutResult() {
094: protected void doInCompassWithoutResult(
095: CompassSession session) throws CompassException {
096: doIndex(session);
097: ((InternalCompassSession) session).flush();
098: }
099: });
100: }
101:
102: /**
103: * Derived devices must implement the method to perform the actual indexing
104: * operation.
105: */
106: protected abstract void doIndex(CompassSession session)
107: throws CompassGpsException;
108:
109: public synchronized void start() throws CompassGpsException {
110: if (name == null) {
111: throw new IllegalArgumentException(
112: "Must set the name for the device");
113: }
114: if (compassGps == null) {
115: throw new IllegalArgumentException(
116: buildMessage("Must set the compassGps for the device, or add it to an active CompassGps instance"));
117: }
118: if (!started) {
119: if (this instanceof MirrorDataChangesGpsDevice) {
120: internalMirrorDataChanges = ((MirrorDataChangesGpsDevice) this )
121: .isMirrorDataChanges();
122: }
123: // build the filtered enteties map
124: if (filteredEntitiesForIndex != null) {
125: filteredEntitiesLookupForIndex = new HashSet();
126: for (int i = 0; i < filteredEntitiesForIndex.length; i++) {
127: filteredEntitiesLookupForIndex
128: .add(filteredEntitiesForIndex[i]);
129: }
130: }
131: doStart();
132: started = true;
133: }
134: }
135:
136: /**
137: * Derived devices can implement it in case of start event notification.
138: *
139: * @throws CompassGpsException
140: */
141: protected void doStart() throws CompassGpsException {
142:
143: }
144:
145: public synchronized void stop() throws CompassGpsException {
146: if (started) {
147: doStop();
148: started = false;
149: }
150: }
151:
152: /**
153: * Derived devices can implement it in case of stop event notification.
154: *
155: * @throws CompassGpsException
156: */
157: protected void doStop() throws CompassGpsException {
158:
159: }
160:
161: public boolean isRunning() {
162: return started;
163: }
164:
165: public boolean isPerformingIndexOperation() {
166: return compassGps.isPerformingIndexOperation();
167: }
168:
169: public boolean shouldMirrorDataChanges() {
170: if (!internalMirrorDataChanges) {
171: return false;
172: }
173: return isRunning();
174: }
175: }
|