001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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: */
017: package net.refractions.udig.catalog.internal;
018:
019: import java.io.Serializable;
020: import java.net.URL;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.concurrent.locks.Lock;
026: import java.util.concurrent.locks.ReentrantLock;
027:
028: import net.refractions.udig.catalog.CatalogPlugin;
029: import net.refractions.udig.catalog.IService;
030: import net.refractions.udig.catalog.IServiceFactory;
031: import net.refractions.udig.catalog.ServiceExtension;
032: import net.refractions.udig.core.internal.ExtensionPointProcessor;
033: import net.refractions.udig.core.internal.ExtensionPointUtil;
034:
035: import org.eclipse.core.runtime.IConfigurationElement;
036: import org.eclipse.core.runtime.IExtension;
037:
038: /**
039: * Provides ...TODO summary sentence
040: * <p>
041: * TODO Description
042: * </p>
043: *
044: * @author David Zwiers, Refractions Research
045: * @since 0.6
046: */
047: public class ServiceFactoryImpl implements IServiceFactory {
048: /**
049: * @deprecated use acquire(URL, Map)
050: */
051: public List<IService> aquire(final URL id,
052: final Map<String, Serializable> params) {
053: return acquire(id, params);
054: }
055:
056: /**
057: * Aquire an IService for the provided connection.
058: * <p>
059: * Please note: often connection parameters can be handled by more then one service
060: * (especially when they describe an Open Web Service that speaks several service protocols).
061: *
062: * @see net.refractions.udig.catalog.IServiceFactory#acquire(java.net.URI, java.util.Map)
063: * @param id identifier
064: * @param params Connection parameters
065: * @return List of services able to handle provided params
066: */
067: public List<IService> acquire(final URL id,
068: final Map<String, Serializable> params) {
069: final List<IService> services = new LinkedList<IService>();
070:
071: // load services
072: ExtensionPointUtil.process(CatalogPlugin.getDefault(),
073: ServiceExtension.EXTENSION_ID,
074: new ExtensionPointProcessor() {
075: /**
076: * Attempt to construct a service, and add to the list if available.
077: * <p>
078: * Note: ExtentionPointUtil will log exceptions against provided plugin.
079: * </p>
080: *
081: * @see net.refractions.udig.core.internal.ExtensionPointProcessor#process(org.eclipse.core.runtime.IExtension,
082: * org.eclipse.core.runtime.IConfigurationElement)
083: * @param extension
084: * @param element
085: */
086: public void process(IExtension extension,
087: IConfigurationElement element)
088: throws Exception {
089: ServiceExtension se = (ServiceExtension) element
090: .createExecutableExtension("class"); //$NON-NLS-1$
091: IService service = se.createService(id, params);
092: if (service != null) {
093: services.add(service);
094: }
095: }
096: });
097:
098: return services;
099: }
100:
101: /**
102: * @deprecated use acquire(Map)
103: */
104: public List<IService> aquire(Map<String, Serializable> params) {
105: return acquire(params);
106: }
107:
108: /**
109: * TODO summary sentence for acquire ...
110: *
111: * @see net.refractions.udig.catalog.IServiceFactory#acquire(java.util.Map)
112: * @param params
113: * @return
114: */
115: public List<IService> acquire(Map<String, Serializable> params) {
116: return acquire(null, params);
117: }
118:
119: List<Map<String, Serializable>> maps;
120: private Lock lock = new ReentrantLock();
121:
122: /**
123: * @deprecated use acquire(URL)
124: */
125: public List<IService> aquire(final URL target) {
126: return acquire(target);
127: }
128:
129: /**
130: * Acquire IService handles generated by all ServiceExtentions that think they can handle the
131: * provided target url.
132: * <p>
133: * Note: Just because a target is created does *NOT* mean it will actually work. You can check
134: * the handles in the usual manner (ask for their info) after you get back this list.
135: * </p>
136: *
137: * @see net.refractions.udig.catalog.IServiceFactory#acquire(java.net.URL)
138: * @param target
139: * @return
140: */
141: public List<IService> acquire(final URL target) {
142: lock.lock();
143: try {
144: maps = new LinkedList<Map<String, Serializable>>();
145: // load maps
146: ExtensionPointUtil
147: .process(
148: CatalogPlugin.getDefault(),
149: "net.refractions.udig.catalog.ServiceExtension", new ExtensionPointProcessor() { //$NON-NLS-1$
150: public void process(
151: IExtension extension,
152: IConfigurationElement element)
153: throws Exception {
154: ServiceExtension se = (ServiceExtension) element
155: .createExecutableExtension("class"); //$NON-NLS-1$
156: Map<String, Serializable> m = se
157: .createParams(target);
158: if (m != null)
159: maps.add(m);
160: }
161: });
162: } finally {
163: lock.unlock();
164: }
165:
166: List<IService> r = new LinkedList<IService>();
167: Iterator<Map<String, Serializable>> i = maps.iterator();
168: while (i.hasNext()) {
169: List<IService> o = acquire(i.next());
170: if (o != null && !o.isEmpty())
171: r.addAll(o);
172: }
173: return r;
174: }
175: }
|