001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.net.URL;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.geotools.factory.CommonFactoryFinder;
027:
028: /**
029: * <p>
030: * Most of this code was copied from DataStoreFinder. See the Documentation
031: * there for details.
032: * </p>
033: *
034: * <p>
035: * This searches for DataStores which support a singular file parsed in a
036: * particular file format.
037: * </p>
038: *
039: * @author dzwiers
040: *
041: * @see DataStoreFinder
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/FileDataStoreFinder.java $
043: */
044: public class FileDataStoreFinder {
045: /** The logger for the filter module. */
046: protected static final Logger LOGGER = org.geotools.util.logging.Logging
047: .getLogger("org.geotools.data");
048:
049: private FileDataStoreFinder() {
050: }
051:
052: /**
053: * Checks each available datasource implementation in turn and returns the
054: * first one which claims to support the resource identified by the params
055: * object.
056: *
057: * @param url A Map object which contains a defenition of the resource to
058: * connect to. for file based resources the property 'url' should
059: * be set within this Map.
060: *
061: * @return The first datasource which claims to process the required
062: * resource, returns null if none can be found.
063: *
064: * @throws IOException If a suitable loader can be found, but it can not be
065: * attached to the specified resource without errors.
066: */
067: public static DataStore getDataStore(URL url) throws IOException {
068: Iterator ps = getAvailableDataStores();
069:
070: while (ps.hasNext()) {
071: FileDataStoreFactorySpi fac = (FileDataStoreFactorySpi) ps
072: .next();
073:
074: try {
075: if (fac.canProcess(url)) {
076: return fac.createDataStore(url);
077: }
078: } catch (Throwable t) {
079: /**
080: * The logger for the filter module.
081: */
082: LOGGER.log(Level.WARNING, "Could not aquire "
083: + fac.getDescription() + ":" + t, t);
084:
085: // Protect against DataStores that don't carefully
086: // code canProcess
087: continue;
088: }
089: }
090:
091: return null;
092: }
093:
094: /**
095: * Returns an iterator of FileDataStoreFactorySpi to allow for the easy
096: * creation of a FileDataStore
097: *
098: *
099: * @see FileDataStoreFactorySpi
100: * @see FileDataStore
101: */
102: public static Iterator getAvailableDataStores() {
103: Set availableDS = new HashSet();
104:
105: Set all = CommonFactoryFinder.getFileDataStoreFactories(null);
106:
107: for (Iterator it = all.iterator(); it.hasNext();) {
108: FileDataStoreFactorySpi dsFactory = (FileDataStoreFactorySpi) it
109: .next();
110:
111: if (dsFactory.isAvailable()) {
112: availableDS.add(dsFactory);
113: }
114: }
115:
116: return availableDS.iterator();
117: }
118: }
|