001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020: import java.util.Set;
021:
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureIterator;
024: import org.opengis.filter.Filter;
025:
026: /**
027: * A Starting point for your own FeatureLocking implementations.
028: *
029: * <p>
030: * This class extends AbstractFeatureSource and depends on getDataStore().
031: * </p>
032: * The implementation of the following functions depends on
033: * getDataStore().getLockingManger() not being <code>null</code>:
034: *
035: * <ul>
036: * <li>
037: * lockFeatures( Query )
038: * </li>
039: * <li>
040: * unLockFeatures( Query )
041: * </li>
042: * <li>
043: * releaseLock( AuthorizationID )
044: * </li>
045: * <li>
046: * refreshLock( AuthorizationID )
047: * </li>
048: * </ul>
049: *
050: * <p>
051: * FeatureStores that have provided their own locking to will need to override
052: * the above methods, or provide a custom LockingManger.
053: * </p>
054: *
055: * @author Jody Garnett, Refractions Research
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/AbstractFeatureLocking.java $
057: */
058: public abstract class AbstractFeatureLocking extends
059: AbstractFeatureStore implements FeatureLocking {
060: FeatureLock featureLock = FeatureLock.TRANSACTION;
061:
062: public AbstractFeatureLocking() {
063: // just to keep the default constructor around
064: }
065:
066: /**
067: * This constructors allows to set the supported hints
068: * @param hints
069: */
070: public AbstractFeatureLocking(Set hints) {
071: super (hints);
072: }
073:
074: /**
075: * Provide a FeatureLock for locking opperations to opperate against.
076: *
077: * <p>
078: * Initial Transactional duration locks can be restored with
079: * setFeatureLock( FetaureLock.TRANSACTION )
080: * </p>
081: *
082: * @param lock FeatureLock (or FeatureLock.TRANSACTION );
083: *
084: * @throws NullPointerException If lock was <code>null</code>
085: *
086: * @see org.geotools.data.FeatureLocking#setFeatureLock(org.geotools.data.FeatureLock)
087: */
088: public void setFeatureLock(FeatureLock lock) {
089: if (lock == null) {
090: throw new NullPointerException(
091: "A FeatureLock is required - did you mean FeatureLock.TRANSACTION?");
092: }
093:
094: featureLock = lock;
095: }
096:
097: /**
098: * Lock all Features
099: *
100: * @return Number of Locked features
101: *
102: * @throws IOException
103: *
104: * @see org.geotools.data.FeatureLocking#lockFeatures()
105: */
106: public int lockFeatures() throws IOException {
107: return lockFeatures(Filter.INCLUDE);
108: }
109:
110: /**
111: * Lock features matching <code>filter</code>.
112: *
113: * @param filter
114: *
115: * @return Number of locked Features
116: *
117: * @throws IOException
118: */
119: public int lockFeatures(Filter filter) throws IOException {
120: return lockFeatures(new DefaultQuery(getSchema().getTypeName(),
121: filter));
122: }
123:
124: /**
125: * Lock features matching Query.
126: *
127: * <p>
128: * FeatureStores that have provided their own locking to will need to
129: * override this method.
130: * </p>
131: *
132: * @param query
133: *
134: * @return Number of locked Features
135: *
136: * @throws IOException If we could not determine which feature to lock
137: * based on Query
138: * @throws UnsupportedOperationException When DataStore does not provide a
139: * LockingManager
140: * @throws DataSourceException If feature to be locked does not exist
141: *
142: * @see org.geotools.data.FeatureLocking#lockFeatures(org.geotools.data.Query)
143: */
144: public int lockFeatures(Query query) throws IOException {
145: LockingManager lockingManager = getDataStore()
146: .getLockingManager();
147:
148: if (lockingManager == null) {
149: throw new UnsupportedOperationException(
150: "DataStore not using lockingManager, must provide alternate implementation");
151: }
152:
153: // Could we reduce the Query to only return the FetureID here?
154: //
155: FeatureIterator reader = getFeatures(query).features();
156: String typeName = query.getTypeName();
157: Feature feature;
158: int count = 0;
159:
160: try {
161: while (reader.hasNext()) {
162: try {
163: feature = reader.next();
164: lockingManager.lockFeatureID(typeName, feature
165: .getID(), getTransaction(), featureLock);
166: count++;
167: } catch (FeatureLockException locked) {
168: // could not aquire - don't increment count
169: } catch (NoSuchElementException nosuch) {
170: throw new DataSourceException("Problem with "
171: + query.getHandle() + " while locking",
172: nosuch);
173: }
174: }
175: } finally {
176: reader.close();
177: }
178:
179: return count;
180: }
181:
182: /**
183: * Unlock all Features.
184: *
185: * @throws IOException
186: *
187: * @see org.geotools.data.FeatureLocking#unLockFeatures()
188: */
189: public void unLockFeatures() throws IOException {
190: unLockFeatures(Filter.INCLUDE);
191: }
192:
193: /**
194: * Unlock Features specified by <code>filter</code>.
195: *
196: * @param filter
197: *
198: * @throws IOException
199: */
200: public void unLockFeatures(Filter filter) throws IOException {
201: unLockFeatures(new DefaultQuery(getSchema().getTypeName(),
202: filter));
203: }
204:
205: /**
206: * Unlock features specified by the <code>query</code>.
207: *
208: * <p>
209: * FeatureStores that have provided their own locking to will need to
210: * override this method.
211: * </p>
212: *
213: * @param query
214: *
215: * @throws IOException
216: * @throws UnsupportedOperationException If lockingManager is not provided
217: * by DataStore subclass
218: * @throws DataSourceException Filter describes an unlocked Feature, or
219: * authorization not held
220: *
221: * @see org.geotools.data.FeatureLocking#unLockFeatures(org.geotools.data.Query)
222: */
223: public void unLockFeatures(Query query) throws IOException {
224: LockingManager lockingManager = getDataStore()
225: .getLockingManager();
226:
227: if (lockingManager == null) {
228: throw new UnsupportedOperationException(
229: "DataStore not using lockingManager, must provide alternate implementation");
230: }
231:
232: // Could we reduce the Query to only return the FetureID here?
233: //
234: FeatureIterator reader = getFeatures(query).features();
235: String typeName = query.getTypeName();
236: Feature feature;
237:
238: try {
239: while (reader.hasNext()) {
240: try {
241: feature = reader.next();
242: lockingManager.unLockFeatureID(typeName, feature
243: .getID(), getTransaction(), featureLock);
244: } catch (NoSuchElementException nosuch) {
245: throw new DataSourceException("Problem with "
246: + query.getHandle() + " while locking",
247: nosuch);
248: }
249: }
250: } finally {
251: reader.close();
252: }
253: }
254: }
|