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 java.net.URI;
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.Iterator;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * A default service factory which uses the SPI classpath plugin lookup
033: * mechanism to locate service extension plugins.
034: *
035: * @since 0.6
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/catalog/defaults/DefaultServiceFactory.java $
037: */
038: public class DefaultServiceFactory implements ServiceFinder {
039: private Catalog catalog;
040:
041: public DefaultServiceFactory(Catalog catalog) {
042: this .catalog = catalog;
043: }
044:
045: public List aquire(URI id, Map params) {
046: List services = new ArrayList();
047: List extensions = getServiceExtensions();
048:
049: for (Iterator itr = extensions.iterator(); itr.hasNext();) {
050: ServiceFactory se = (ServiceFactory) itr.next();
051:
052: try {
053: //protect ourselves from plugins
054: Service service = se.createService(catalog, id, params);
055:
056: if (service != null) {
057: services.add(service);
058: }
059: } catch (Throwable t) {
060: //do nothing
061: }
062: }
063:
064: return services;
065: }
066:
067: public List aquire(Map params) {
068: return aquire(null, params);
069: }
070:
071: /**
072: * Aquire IService handles generated by all ServiceExtentions that think
073: * they can handle the provided target url.
074: *
075: * <p>
076: * Note: Just because a target is created does NOT mean it will actually
077: * work. You can check the handles in the usual manner (ask for their
078: * info) after you get back this list.
079: * </p>
080: *
081: * @param target
082: *
083: *
084: * @see net.refractions.udig.catalog.IServiceFactory#aquire(java.net.URL)
085: */
086: public List aquire(URI target) {
087: List maps = new LinkedList();
088: List extensions = getServiceExtensions();
089:
090: for (Iterator itr = extensions.iterator(); itr.hasNext();) {
091: ServiceFactory se = (ServiceFactory) itr.next();
092:
093: try {
094: Map m = se.createParams(target);
095:
096: if (m != null) {
097: maps.add(m);
098: }
099: } catch (Throwable t) {
100: //do nothing
101: }
102: }
103:
104: List services = new LinkedList();
105: Iterator i = maps.iterator();
106:
107: while (i.hasNext()) {
108: List o = aquire((Map) i.next());
109:
110: if ((o != null) && !o.isEmpty()) {
111: services.addAll(o);
112: }
113: }
114:
115: return services;
116: }
117:
118: /**
119: * This method uses the current classpath to look for instances of {@link
120: * ServiceExtension}. Sublcasses should overide this method if they wish
121: * to use a diffent plug-in mechanism.
122: *
123: * @return A list of ServiceExtension plugins, or an empty list if none
124: * could be found.
125: */
126: protected List getServiceExtensions() {
127: //TODO: default implementation should use classpath lookup mechanism
128: return Collections.EMPTY_LIST;
129: }
130: }
|