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.Collections;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024: import java.util.SortedMap;
025: import java.util.SortedSet;
026: import java.util.TreeMap;
027: import java.util.TreeSet;
028:
029: import org.geotools.feature.FeatureType;
030:
031: /**
032: * Another Quick hack of a DataRepository as a bridge to the Opperations api.
033: *
034: * This time we are storing by FeatureType, not DataStores will be harned in
035: * the configuration of this class.
036: *
037: * @author Jody Garnett
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/FeatureSourceRepository.java $
039: */
040: public class FeatureSourceRepository implements Repository {
041:
042: /** Map of FeatuerSource by dataStoreId:typeName */
043: protected SortedMap featuresources = new TreeMap();
044:
045: /**
046: * All FeatureTypes by dataStoreId:typeName
047: */
048: public SortedMap getFeatureSources() {
049: return Collections.unmodifiableSortedMap(featuresources);
050: }
051:
052: /**
053: * Retrieve prefix set.
054: *
055: * @see org.geotools.data.Catalog#getPrefixes()
056: *
057: * @return Set of namespace prefixes
058: * @throws IOException
059: */
060: public Set getPrefixes() throws IOException {
061: Set prefix = new HashSet();
062: for (Iterator i = featuresources.values().iterator(); i
063: .hasNext();) {
064: FeatureSource fs = (FeatureSource) i.next();
065: FeatureType schema = fs.getSchema();
066: prefix.add(schema.getNamespace().toString());
067: }
068: return prefix;
069: }
070:
071: private SortedSet typeNames() throws IOException {
072: SortedSet typeNames = new TreeSet();
073: for (Iterator i = featuresources.values().iterator(); i
074: .hasNext();) {
075: FeatureSource fs = (FeatureSource) i.next();
076: FeatureType schema = fs.getSchema();
077: typeNames.add(schema.getTypeName());
078: }
079: return typeNames;
080: }
081:
082: /** Map of dataStores by dataStoreId */
083: private Map dataStores() {
084: SortedMap dataStores = new TreeMap();
085: for (Iterator i = featuresources.entrySet().iterator(); i
086: .hasNext();) {
087: Map.Entry entry = (Map.Entry) i.next();
088: String key = (String) entry.getKey();
089: String dataStoreId = key.split(":")[0];
090: FeatureSource fs = (FeatureSource) entry.getValue();
091: dataStores.put(dataStoreId, fs.getDataStore());
092: }
093: return dataStores;
094: }
095:
096: private SortedMap types(DataStore ds) throws IOException {
097: SortedMap map = new TreeMap();
098: String typeNames[] = ds.getTypeNames();
099: for (int i = 0; i < typeNames.length; i++) {
100: try {
101: map.put(typeNames[i], ds.getSchema(typeNames[i]));
102: } catch (IOException ignore) {
103: // ignore broken featureType
104: }
105: }
106: return map;
107: }
108:
109: /**
110: * All FeatureTypes by dataStoreId:typeName
111: */
112: public SortedMap types() {
113: return new TreeMap(featuresources);
114: }
115:
116: /**
117: * Implement lockExists.
118: *
119: * @see org.geotools.data.Catalog#lockExists(java.lang.String)
120: *
121: * @param lockID
122: */
123: public boolean lockExists(String lockID) {
124: if (lockID == null)
125: return false;
126: DataStore store;
127: LockingManager lockManager;
128:
129: for (Iterator i = dataStores().values().iterator(); i.hasNext();) {
130: store = (DataStore) i.next();
131: lockManager = store.getLockingManager();
132: if (lockManager == null)
133: continue; // did not support locking
134: if (lockManager.exists(lockID)) {
135: return true;
136: }
137: }
138: return false;
139: }
140:
141: /**
142: * Implement lockRefresh.
143: * <p>
144: * Currently it is an error if the lockID is not found. Because if
145: * we can't find it we cannot refresh it.
146: * </p>
147: * <p>
148: * Since locks are time sensitive it is impossible to check
149: * if a lockExists and then be sure it will still exist when you try to
150: * refresh it. Nothing we do can protect client code from this fact, they
151: * will need to do with the IOException when (not if) this situation
152: * occurs.
153: * </p>
154: * @see org.geotools.data.Catalog#lockRefresh(java.lang.String, org.geotools.data.Transaction)
155: *
156: * @param lockID Authorizataion of lock to refresh
157: * @param transaction Transaction used to authorize refresh
158: * @throws IOException If opperation encounters problems, or lock not found
159: * @throws IllegalArgumentException if lockID is <code>null</code>
160: */
161: public boolean lockRefresh(String lockID, Transaction transaction)
162: throws IOException {
163: if (lockID == null) {
164: throw new IllegalArgumentException("lockID required");
165: }
166: if (transaction == null
167: || transaction == Transaction.AUTO_COMMIT) {
168: throw new IllegalArgumentException(
169: "Tansaction required (with authorization for "
170: + lockID + ")");
171: }
172:
173: DataStore store;
174: LockingManager lockManager;
175:
176: boolean refresh = false;
177: for (Iterator i = dataStores().values().iterator(); i.hasNext();) {
178: store = (DataStore) i.next();
179: lockManager = store.getLockingManager();
180: if (lockManager == null)
181: continue; // did not support locking
182:
183: if (lockManager.release(lockID, transaction)) {
184: refresh = true;
185: }
186: }
187: return refresh;
188: }
189:
190: /**
191: * Implement lockRelease.
192: * <p>
193: * Currently it is <b>not</b> and error if the lockID is not found, it may
194: * have expired. Since locks are time sensitive it is impossible to check
195: * if a lockExists and then be sure it will still exist when you try to
196: * release it.
197: * </p>
198: * @see org.geotools.data.Catalog#lockRefresh(java.lang.String, org.geotools.data.Transaction)
199: *
200: * @param lockID Authorizataion of lock to refresh
201: * @param transaction Transaction used to authorize refresh
202: * @throws IOException If opperation encounters problems
203: * @throws IllegalArgumentException if lockID is <code>null</code>
204: */
205: public boolean lockRelease(String lockID, Transaction transaction)
206: throws IOException {
207: if (lockID == null) {
208: throw new IllegalArgumentException("lockID required");
209: }
210: if (transaction == null
211: || transaction == Transaction.AUTO_COMMIT) {
212: throw new IllegalArgumentException(
213: "Tansaction required (with authorization for "
214: + lockID + ")");
215: }
216:
217: DataStore store;
218: LockingManager lockManager;
219:
220: boolean release = false;
221: for (Iterator i = dataStores().values().iterator(); i.hasNext();) {
222: store = (DataStore) i.next();
223: lockManager = store.getLockingManager();
224: if (lockManager == null)
225: continue; // did not support locking
226:
227: if (lockManager.release(lockID, transaction)) {
228: release = true;
229: }
230: }
231: return release;
232: }
233:
234: /**
235: * Implement registerDataStore.
236: * <p>
237: * Description ...
238: * </p>
239: * @see org.geotools.data.Catalog#registerDataStore(org.geotools.data.DataStore)
240: *
241: * @param id
242: * @param featureSource
243: *
244: * @throws IOException
245: */
246: public void register(String id, FeatureSource featureSource)
247: throws IOException {
248: featuresources.put(id + ":"
249: + featureSource.getSchema().getTypeName(),
250: featureSource);
251: }
252:
253: /**
254: * Implement getDataStores.
255: * <p>
256: * Description ...
257: * </p>
258: * @see org.geotools.data.Catalog#getDataStores(java.lang.String)
259: *
260: * @param id
261: */
262: public DataStore datastore(String id) {
263: Set prefix = new HashSet();
264: for (Iterator i = featuresources.entrySet().iterator(); i
265: .hasNext();) {
266: Map.Entry entry = (Map.Entry) i.next();
267: String key = (String) entry.getKey();
268: String dataStoreId = key.split(":")[0];
269: if (id.equals(dataStoreId)) {
270: FeatureSource fs = (FeatureSource) entry.getValue();
271: return fs.getDataStore();
272: }
273: }
274: return null;
275: }
276:
277: /**
278: * Access to the set of registered DataStores.
279: * <p>
280: * The provided Set may not be modified :-)
281: * </p>
282: * @see org.geotools.data.Catalog#getDataStores(java.lang.String)
283: *
284: *
285: */
286: public Map getDataStores() {
287: return Collections.unmodifiableMap(dataStores());
288: }
289:
290: public FeatureSource source(String dataStoreId, String typeName)
291: throws IOException {
292: String typeRef = dataStoreId + ":" + typeName;
293: return (FeatureSource) featuresources.get(typeRef);
294: }
295: }
|