001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Refractions Research Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.catalog.defaults;
018:
019: import org.geotools.catalog.Catalog;
020: import org.geotools.catalog.Service;
021: import org.geotools.catalog.ServiceFactory;
022: import org.geotools.catalog.ServiceFinder;
023: import org.geotools.factory.FactoryRegistry;
024:
025: import java.net.URI;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: /**
033: * A default service factory which uses the SPI classpath plugin lookup
034: * mechanism to locate service extension plugins.
035: *
036: * @since 0.6
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/catalog/defaults/DefaultServiceFinder.java $
038: */
039: public class DefaultServiceFinder implements ServiceFinder {
040: private Catalog catalog;
041:
042: public DefaultServiceFinder(Catalog catalog) {
043: this .catalog = catalog;
044: }
045:
046: public List aquire(URI id, Map params) {
047: List services = new ArrayList();
048: List extensions = getServiceFactories();
049:
050: for (Iterator itr = extensions.iterator(); itr.hasNext();) {
051: ServiceFactory se = (ServiceFactory) itr.next();
052:
053: try {
054: //protect ourselves from plugins
055: Service service = se.createService(catalog, id, params);
056:
057: if (service != null) {
058: services.add(service);
059: }
060: } catch (Throwable t) {
061: //do nothing
062: }
063: }
064:
065: return services;
066: }
067:
068: public List aquire(Map params) {
069: return aquire(null, params);
070: }
071:
072: /**
073: * Aquire IService handles generated by all ServiceExtentions that think
074: * they can handle the provided target url.
075: *
076: * <p>
077: * Note: Just because a target is created does NOT mean it will actually
078: * work. You can check the handles in the usual manner (ask for their
079: * info) after you get back this list.
080: * </p>
081: *
082: * @param target
083: *
084: *
085: * @see net.refractions.udig.catalog.IServiceFactory#aquire(java.net.URL)
086: */
087: public List aquire(URI target) {
088: List maps = new LinkedList();
089: List extensions = getServiceFactories();
090:
091: for (Iterator itr = extensions.iterator(); itr.hasNext();) {
092: ServiceFactory se = (ServiceFactory) itr.next();
093:
094: try {
095: if (!se.canProcess(target))
096: continue;
097:
098: Map m = se.createParams(target);
099:
100: if (m != null) {
101: maps.add(m);
102: }
103: } catch (Throwable t) {
104: //do nothing
105: }
106: }
107:
108: List services = new LinkedList();
109: Iterator i = maps.iterator();
110:
111: while (i.hasNext()) {
112: List o = aquire(target, (Map) i.next());
113:
114: if ((o != null) && !o.isEmpty()) {
115: services.addAll(o);
116: }
117: }
118:
119: return services;
120: }
121:
122: /**
123: * This method uses the current classpath to look for instances of {@link
124: * ServiceFactory} using the factory spi mechanism.
125: * <p>
126: * Sublcasses should overide this method if they wish to use a diffent plug-in mechanism.
127: * </p>
128: *
129: * @return A list of ServiceFactory plugins, or an empty list if none
130: * could be found.
131: *
132: * @see FactoryRegistry
133: */
134: public List getServiceFactories() {
135: Iterator f = FactoryRegistry
136: .lookupProviders(ServiceFactory.class);
137:
138: ArrayList factories = new ArrayList();
139: while (f.hasNext())
140: factories.add(f.next());
141:
142: return factories;
143: }
144: }
|