001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.dir;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.geotools.data.AbstractFileDataStore;
029: import org.geotools.data.DataStore;
030: import org.geotools.data.FeatureLock;
031: import org.geotools.data.FeatureReader;
032: import org.geotools.data.FeatureSource;
033: import org.geotools.data.FeatureWriter;
034: import org.geotools.data.FileDataStoreFinder;
035: import org.geotools.data.LockingManager;
036: import org.geotools.data.Query;
037: import org.geotools.data.Transaction;
038: import org.geotools.data.view.DefaultView;
039: import org.geotools.feature.FeatureType;
040: import org.geotools.feature.SchemaException;
041: import org.opengis.filter.Filter;
042:
043: /**
044: * This datastore represents methods of reading an enture directory. It
045: * propagates actual reading / writing of the data to the dataStore which
046: * reads / writes the requested format.
047: * </p>
048: *
049: * @author David Zwiers, Refractions Research, Inc.
050: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/directory/src/main/java/org/geotools/data/dir/DirectoryDataStore.java $
051: */
052: public class DirectoryDataStore implements DataStore, LockingManager {
053: // the directory for this ds
054: private File dir;
055:
056: // map of featureTypes to dataStore instances
057: private Map dataStores;
058:
059: // suffix order to attempt to store new featureTypes
060: private String[] createOrder;
061:
062: /** List<TypeEntry> subclass control provided by createContents.
063: * <p>
064: * Access via entries(), creation by createContents.
065: */
066: private List contents = null;
067:
068: // should not be used
069: private DirectoryDataStore() {
070: }
071:
072: // This is the *better* implementation of getview from AbstractDataStore
073: public FeatureSource getView(final Query query) throws IOException,
074: SchemaException {
075: return new DefaultView(this .getFeatureSource(query
076: .getTypeName()), query);
077: }
078:
079: /**
080: * Creates a new DirectoryDataStore object.
081: *
082: * @param f File the directory
083: * @param co list of file suffixes in order of preference for creating new
084: * FTs
085: *
086: * @throws MalformedURLException
087: * @throws IOException
088: */
089: public DirectoryDataStore(File f, String[] co)
090: throws MalformedURLException, IOException {
091: dir = f;
092: createOrder = co;
093: dataStores = new HashMap();
094:
095: // load list of dataStores by typeName
096: File[] children = f.listFiles();
097:
098: for (int i = 0; i < children.length; i++) {
099: if (children[i].isFile()) {
100: AbstractFileDataStore afds = (AbstractFileDataStore) FileDataStoreFinder
101: .getDataStore(children[i].toURL());
102:
103: if (afds != null) {
104: dataStores.put(afds.getTypeNames()[0], afds);
105: }
106: }
107: }
108: }
109:
110: /**
111: * @see org.geotools.data.DataStore#getTypeNames()
112: */
113: public String[] getTypeNames() throws IOException {
114: Set l = new HashSet();
115: Iterator i = dataStores.values().iterator();
116:
117: while (i.hasNext()) {
118: AbstractFileDataStore afds = (AbstractFileDataStore) i
119: .next();
120: String[] strs = afds.getTypeNames();
121:
122: if (strs != null) {
123: for (int j = 0; j < strs.length; j++)
124: l.add(strs[j]);
125: }
126: }
127:
128: return (String[]) l.toArray(new String[l.size()]);
129: }
130:
131: /**
132: * @see org.geotools.data.DataStore#getSchema(java.lang.String)
133: */
134: public FeatureType getSchema(String typeName) throws IOException {
135: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
136: .get(typeName);
137:
138: if (afds != null) {
139: return afds.getSchema();
140: }
141:
142: return null;
143: }
144:
145: /**
146: * @see org.geotools.data.DataStore#createSchema(org.geotools.feature.FeatureType)
147: */
148: public void createSchema(FeatureType featureType)
149: throws IOException {
150: boolean notDone = true;
151: int i = 0;
152:
153: while (notDone && (i < createOrder.length)) {
154: File f = new File(dir, featureType.getTypeName()
155: + createOrder[i]);
156:
157: if (!f.exists()) {
158: AbstractFileDataStore afds = (AbstractFileDataStore) FileDataStoreFinder
159: .getDataStore(f.toURL());
160:
161: if (afds != null) {
162: afds.createSchema(featureType);
163: dataStores.put(featureType.getTypeName(), afds);
164: notDone = false;
165: }
166: }
167:
168: i++;
169: }
170: }
171:
172: /**
173: * @see org.geotools.data.DataStore#updateSchema(java.lang.String,
174: * org.geotools.feature.FeatureType)
175: */
176: public void updateSchema(String typeName, FeatureType featureType)
177: throws IOException {
178: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
179: .get(typeName);
180:
181: if (afds != null) {
182: afds.updateSchema(featureType);
183: }
184: }
185:
186: /**
187: * @see org.geotools.data.DataStore#getFeatureSource(java.lang.String)
188: */
189: public FeatureSource getFeatureSource(String typeName)
190: throws IOException {
191: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
192: .get(typeName);
193:
194: if (afds != null) {
195: return afds.getFeatureSource();
196: }
197:
198: return null;
199: }
200:
201: /**
202: * @see org.geotools.data.DataStore#getFeatureReader(org.geotools.data.Query,
203: * org.geotools.data.Transaction)
204: */
205: public FeatureReader getFeatureReader(Query query,
206: Transaction transaction) throws IOException {
207: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
208: .get(query.getTypeName());
209:
210: if (afds != null) {
211: return afds.getFeatureReader(query, transaction);
212: }
213:
214: return null;
215: }
216:
217: /**
218: * @see org.geotools.data.DataStore#getFeatureWriter(java.lang.String,
219: * org.geotools.filter.Filter, org.geotools.data.Transaction)
220: */
221: public FeatureWriter getFeatureWriter(String typeName,
222: Filter filter, Transaction transaction) throws IOException {
223: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
224: .get(typeName);
225:
226: if (afds != null) {
227: return afds.getFeatureWriter(filter, transaction);
228: }
229:
230: return null;
231: }
232:
233: /**
234: * @see org.geotools.data.DataStore#getFeatureWriter(java.lang.String,
235: * org.geotools.data.Transaction)
236: */
237: public FeatureWriter getFeatureWriter(String typeName,
238: Transaction transaction) throws IOException {
239: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
240: .get(typeName);
241:
242: if (afds != null) {
243: return afds.getFeatureWriter(transaction);
244: }
245:
246: return null;
247: }
248:
249: /**
250: * @see org.geotools.data.DataStore#getFeatureWriterAppend(java.lang.String,
251: * org.geotools.data.Transaction)
252: */
253: public FeatureWriter getFeatureWriterAppend(String typeName,
254: Transaction transaction) throws IOException {
255: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
256: .get(typeName);
257:
258: if (afds != null) {
259: return afds.getFeatureWriterAppend(transaction);
260: }
261:
262: return null;
263: }
264:
265: /**
266: * @see org.geotools.data.DataStore#getLockingManager()
267: */
268: public LockingManager getLockingManager() {
269: return this ;
270: }
271:
272: /**
273: * @see org.geotools.data.LockingManager#exists(java.lang.String)
274: */
275: public boolean exists(String authID) {
276: Iterator i = dataStores.values().iterator();
277:
278: while (i.hasNext()) {
279: AbstractFileDataStore afds = (AbstractFileDataStore) i
280: .next();
281:
282: if ((afds.getLockingManager() != null)
283: && afds.getLockingManager().exists(authID)) {
284: return true;
285: }
286: }
287:
288: return false;
289: }
290:
291: /**
292: * @see org.geotools.data.LockingManager#release(java.lang.String,
293: * org.geotools.data.Transaction)
294: */
295: public boolean release(String authID, Transaction transaction)
296: throws IOException {
297: Iterator i = dataStores.values().iterator();
298:
299: while (i.hasNext()) {
300: AbstractFileDataStore afds = (AbstractFileDataStore) i
301: .next();
302:
303: if ((afds.getLockingManager() != null)
304: && afds.getLockingManager().exists(authID)) {
305: return afds.getLockingManager().release(authID,
306: transaction);
307: }
308: }
309:
310: return false;
311: }
312:
313: /**
314: * @see org.geotools.data.LockingManager#refresh(java.lang.String,
315: * org.geotools.data.Transaction)
316: */
317: public boolean refresh(String authID, Transaction transaction)
318: throws IOException {
319: Iterator i = dataStores.values().iterator();
320:
321: while (i.hasNext()) {
322: AbstractFileDataStore afds = (AbstractFileDataStore) i
323: .next();
324:
325: if ((afds.getLockingManager() != null)
326: && afds.getLockingManager().exists(authID)) {
327: return afds.getLockingManager().refresh(authID,
328: transaction);
329: }
330: }
331:
332: return false;
333: }
334:
335: /**
336: * @see org.geotools.data.LockingManager#unLockFeatureID(java.lang.String,
337: * java.lang.String, org.geotools.data.Transaction,
338: * org.geotools.data.FeatureLock)
339: */
340: public void unLockFeatureID(String typeName, String authID,
341: Transaction transaction, FeatureLock featureLock)
342: throws IOException {
343: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
344: .get(typeName);
345:
346: if ((afds != null) && (afds.getLockingManager() != null)) {
347: afds.getLockingManager().unLockFeatureID(typeName, authID,
348: transaction, featureLock);
349: }
350: }
351:
352: /**
353: * @see org.geotools.data.LockingManager#lockFeatureID(java.lang.String,
354: * java.lang.String, org.geotools.data.Transaction,
355: * org.geotools.data.FeatureLock)
356: */
357: public void lockFeatureID(String typeName, String authID,
358: Transaction transaction, FeatureLock featureLock)
359: throws IOException {
360: AbstractFileDataStore afds = (AbstractFileDataStore) dataStores
361: .get(typeName);
362:
363: if ((afds != null) && (afds.getLockingManager() != null)) {
364: afds.getLockingManager().lockFeatureID(typeName, authID,
365: transaction, featureLock);
366: }
367: }
368:
369: /**
370: * Will dispose all child datastores. After this call the {@link DirectoryDataStore} will
371: * be unusable.
372: */
373: public void dispose() {
374: if (dataStores != null) {
375: for (Iterator it = dataStores.values().iterator(); it
376: .hasNext();) {
377: DataStore ds = (DataStore) it.next();
378: ds.dispose();
379: }
380: dataStores = null;
381: }
382: }
383: }
|