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.jdbc;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020:
021: import org.geotools.data.DataSourceException;
022: import org.geotools.data.DefaultQuery;
023: import org.geotools.data.FeatureLock;
024: import org.geotools.data.FeatureLockException;
025: import org.geotools.data.FeatureLocking;
026: import org.geotools.data.LockingManager;
027: import org.geotools.data.Query;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureIterator;
030: import org.geotools.feature.FeatureType;
031: import org.opengis.filter.Filter;
032:
033: /**
034: * A Starting point for your own FeatureLocking implementations.
035: *
036: * <p>
037: * This class extends JDBCFeatureSource and depends on getDataStore().
038: * </p>
039: * The implementation of the following functions depends on
040: * getDataStore().getLockingManger() not being <code>null</code>:
041: *
042: * <ul>
043: * <li>
044: * lockFeatures( Query )
045: * </li>
046: * <li>
047: * unLockFeatures( Query )
048: * </li>
049: * <li>
050: * releaseLock( AuthorizationID )
051: * </li>
052: * <li>
053: * refreshLock( AuthorizationID )
054: * </li>
055: * </ul>
056: *
057: * <p>
058: * JDBCFeatureLocking that have provided their own locking to will need to
059: * override the above methods, or provide a custom LockingManger.
060: * </p>
061: *
062: * @author Jody Garnett, Refractions Research
063: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/JDBCFeatureLocking.java $
064: */
065: public class JDBCFeatureLocking extends JDBCFeatureStore implements
066: FeatureLocking {
067: FeatureLock featureLock = FeatureLock.TRANSACTION;
068:
069: public JDBCFeatureLocking(JDBC1DataStore jdbcDataStore,
070: FeatureType featureType) {
071: super (jdbcDataStore, featureType);
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 DOCUMENT ME!
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: //GEOT-1192
125: //public int lockFeatures(org.geotools.filter.Filter filter) throws IOException {
126: // return lockFeatures(new DefaultQuery(getSchema().getTypeName(), filter));
127: //}
128: /**
129: * Lock features matching Query.
130: *
131: * <p>
132: * FeatureStores that have provided their own locking to will need to
133: * override this method.
134: * </p>
135: *
136: * @param query
137: *
138: * @return Number of locked Features
139: *
140: * @throws IOException DOCUMENT ME!
141: * @throws UnsupportedOperationException DOCUMENT ME!
142: * @throws DataSourceException If we have any lock conflicts
143: *
144: * @see org.geotools.data.FeatureLocking#lockFeatures(org.geotools.data.Query)
145: */
146: public int lockFeatures(Query query) throws IOException {
147: LockingManager lockingManager = getDataStore()
148: .getLockingManager();
149:
150: if (lockingManager == null) {
151: throw new UnsupportedOperationException(
152: "DataStore not using lockingManager, must provide alternate implementation");
153: }
154:
155: // Could we reduce the Query to only return the FetureID here?
156: //
157: FeatureIterator reader = getFeatures(query).features();
158: String typeName = query.getTypeName();
159: Feature feature;
160: int count = 0;
161:
162: try {
163: while (reader.hasNext()) {
164: try {
165: feature = reader.next();
166: lockingManager.lockFeatureID(typeName, feature
167: .getID(), getTransaction(), featureLock);
168: count++;
169: } catch (FeatureLockException locked) {
170: // could not aquire - don't increment count
171: } catch (NoSuchElementException nosuch) {
172: throw new DataSourceException("Problem with "
173: + query.getHandle() + " while locking",
174: nosuch);
175: }
176: }
177: } finally {
178: reader.close();
179: }
180:
181: return count;
182: }
183:
184: /**
185: * Unlock all Features.
186: *
187: * @throws IOException
188: *
189: * @see org.geotools.data.FeatureLocking#unLockFeatures()
190: */
191: public void unLockFeatures() throws IOException {
192: unLockFeatures(Filter.INCLUDE);
193: }
194:
195: /**
196: * Unlock Features specified by <code>filter</code>.
197: *
198: * @param filter
199: *
200: * @throws IOException
201: */
202: public void unLockFeatures(Filter filter) throws IOException {
203: unLockFeatures(new DefaultQuery(getSchema().getTypeName(),
204: filter));
205: }
206:
207: /**
208: * Unlock features specified by the <code>query</code>.
209: *
210: * <p>
211: * FeatureStores that have provided their own locking to will need to
212: * override this method.
213: * </p>
214: *
215: * @param query
216: *
217: * @throws IOException
218: * @throws UnsupportedOperationException DOCUMENT ME!
219: * @throws DataSourceException DOCUMENT ME!
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: }
|