001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 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.Arrays;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import org.geotools.factory.FactoryCreator;
028: import org.geotools.factory.FactoryRegistry;
029: import org.geotools.factory.Hints;
030: import org.geotools.resources.LazySet;
031:
032: public class DataAccessFinder {
033: protected static final Logger LOGGER = org.geotools.util.logging.Logging
034: .getLogger("org.geotools.data");
035:
036: /**
037: * The service registry for this manager.
038: * Will be initialized only when first needed.
039: */
040: private static FactoryRegistry registry;
041:
042: /**
043: * Returns the service registry. The registry will be created the first
044: * time this method is invoked.
045: */
046: private static FactoryRegistry getServiceRegistry() {
047: assert Thread.holdsLock(DataAccessFinder.class);
048:
049: if (registry == null) {
050: registry = new FactoryCreator(Arrays
051: .asList(new Class[] { DataAccessFactory.class }));
052: }
053:
054: return registry;
055: }
056:
057: /**
058: * Returns a set of all available implementations for the {@link DataAccessFactory} interface.
059: *
060: * @param hints An optional map of hints, or {@code null} if none.
061: * @return Set<DataAccessFactory> of available factory implementations.
062: */
063: public static synchronized Set getDataAccessFactories(
064: final Hints hints) {
065: return new LazySet(getServiceRegistry().getServiceProviders(
066: DataAccessFactory.class, null, hints));
067: }
068:
069: /**
070: * Finds all implemtaions of DataStoreFactory which have registered using
071: * the services mechanism, and that have the appropriate libraries on the
072: * classpath.
073: *
074: * @return An iterator over all discovered datastores which have registered
075: * factories, and whose available method returns true.
076: */
077: public static Iterator getAvailableDataStores() {
078: Set availableDS = new HashSet();
079:
080: Iterator it = getDataAccessFactories(null).iterator();
081:
082: while (it.hasNext()) {
083: DataAccessFactory dsFactory = (DataAccessFactory) it.next();
084:
085: if (dsFactory.isAvailable()) {
086: availableDS.add(dsFactory);
087: }
088: }
089:
090: return availableDS.iterator();
091: }
092:
093: public static DataAccess createAccess(Object bean)
094: throws IOException {
095: Iterator ps = getAvailableDataStores();
096:
097: while (ps.hasNext()) {
098: DataAccessFactory fac = (DataAccessFactory) ps.next();
099:
100: try {
101: if (fac.canAccess(bean)) {
102: return fac.createAccess(bean);
103: }
104: } catch (Throwable t) {
105: LOGGER.log(Level.WARNING, "Could not acquire "
106: + fac.getName() + ":" + t, t);
107:
108: continue;
109: }
110: }
111:
112: return null;
113: }
114:
115: public static DataAccess createAccess(Map properties)
116: throws IOException {
117: Iterator ps = getAvailableDataStores();
118:
119: while (ps.hasNext()) {
120: DataAccessFactory fac = (DataAccessFactory) ps.next();
121: Object bean = fac.createAccessBean();
122:
123: try {
124: if (fac.canAccess(properties)) {
125: return fac.createAccess(properties);
126: }
127: } catch (Throwable t) {
128: LOGGER.log(Level.WARNING, "Could not acquire "
129: + fac.getName() + ":" + t, t);
130:
131: continue;
132: }
133: }
134:
135: return null;
136: }
137: }
|