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.wfs;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.net.URI;
022: import java.net.URL;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.concurrent.locks.Lock;
027:
028: import net.refractions.udig.catalog.CatalogPlugin;
029: import net.refractions.udig.catalog.IResolve;
030: import net.refractions.udig.catalog.IResolveChangeEvent;
031: import net.refractions.udig.catalog.IResolveDelta;
032: import net.refractions.udig.catalog.IService;
033: import net.refractions.udig.catalog.IServiceInfo;
034: import net.refractions.udig.catalog.internal.CatalogImpl;
035: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
036: import net.refractions.udig.catalog.internal.ResolveDelta;
037: import net.refractions.udig.catalog.internal.wfs.UDIGWFSDataStoreFactory.UDIGWFSDataStore;
038: import net.refractions.udig.catalog.wfs.internal.Messages;
039: import net.refractions.udig.ui.ErrorManager;
040: import net.refractions.udig.ui.UDIGDisplaySafeLock;
041:
042: import org.eclipse.core.runtime.IProgressMonitor;
043: import org.eclipse.core.runtime.NullProgressMonitor;
044: import org.eclipse.core.runtime.SubProgressMonitor;
045: import org.eclipse.jface.resource.ImageDescriptor;
046: import org.eclipse.ui.plugin.AbstractUIPlugin;
047: import org.geotools.data.ows.WFSCapabilities;
048: import org.geotools.data.wfs.WFSDataStore;
049: import org.geotools.xml.wfs.WFSSchema;
050:
051: /**
052: * Handle for a WFS service.
053: *
054: * @author David Zwiers, Refractions Research
055: * @since 0.6
056: */
057: public class WFSServiceImpl extends IService {
058:
059: private URL url = null;
060: private Map<String, Serializable> params = null;
061: protected Lock rLock = new UDIGDisplaySafeLock();
062:
063: public WFSServiceImpl(URL arg1, Map<String, Serializable> arg2) {
064: url = arg1;
065: params = arg2;
066: }
067:
068: /*
069: * Required adaptions:
070: * <ul>
071: * <li>IServiceInfo.class
072: * <li>List.class <IGeoResource>
073: * </ul>
074: *
075: * @see net.refractions.udig.catalog.IService#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
076: */
077: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
078: throws IOException {
079: if (adaptee == null)
080: return null;
081: if (adaptee.isAssignableFrom(WFSDataStore.class)) {
082: return adaptee.cast(getDS(monitor));
083: }
084: return super .resolve(adaptee, monitor);
085: }
086:
087: /*
088: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
089: */
090: public <T> boolean canResolve(Class<T> adaptee) {
091: if (adaptee == null)
092: return false;
093: return adaptee.isAssignableFrom(UDIGWFSDataStore.class)
094: || super .canResolve(adaptee);
095: }
096:
097: public void dispose(IProgressMonitor monitor) {
098: if (members == null)
099: return;
100:
101: int steps = (int) ((double) 99 / (double) members.size());
102: for (IResolve resolve : members) {
103: try {
104: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
105: monitor, steps);
106: resolve.dispose(subProgressMonitor);
107: subProgressMonitor.done();
108: } catch (Throwable e) {
109: ErrorManager
110: .get()
111: .displayException(
112: e,
113: "Error disposing members of service: " + getIdentifier(), CatalogPlugin.ID); //$NON-NLS-1$
114: }
115: }
116: }
117:
118: /*
119: * @see net.refractions.udig.catalog.IResolve#members(org.eclipse.core.runtime.IProgressMonitor)
120: */
121: public List<WFSGeoResourceImpl> members(IProgressMonitor monitor)
122: throws IOException {
123:
124: if (members == null) {
125: rLock.lock();
126: try {
127: if (members == null) {
128: getDS(monitor); // load ds
129: members = new LinkedList<WFSGeoResourceImpl>();
130: String[] typenames = ds.getTypeNames();
131: if (typenames != null)
132: for (int i = 0; i < typenames.length; i++) {
133: try {
134: members.add(new WFSGeoResourceImpl(
135: this , typenames[i]));
136: } catch (Exception e) {
137: WfsPlugin.log("", e); //$NON-NLS-1$
138: }
139: }
140: }
141: } finally {
142: rLock.unlock();
143: }
144: }
145: return members;
146: }
147:
148: private volatile List<WFSGeoResourceImpl> members = null;
149:
150: /*
151: * @see net.refractions.udig.catalog.IService#getInfo(org.eclipse.core.runtime.IProgressMonitor)
152: */
153: public IServiceInfo getInfo(IProgressMonitor monitor)
154: throws IOException {
155: getDS(monitor); // load ds
156: if (info == null && ds != null) {
157: rLock.lock();
158: try {
159: if (info == null) {
160: info = new IServiceWFSInfo(ds);
161: IResolveDelta delta = new ResolveDelta(this ,
162: IResolveDelta.Kind.CHANGED);
163: ((CatalogImpl) CatalogPlugin.getDefault()
164: .getLocalCatalog())
165: .fire(new ResolveChangeEvent(
166: this ,
167: IResolveChangeEvent.Type.POST_CHANGE,
168: delta));
169: }
170: } finally {
171: rLock.unlock();
172: }
173: }
174: return info;
175: }
176:
177: private volatile IServiceInfo info = null;
178:
179: /*
180: * @see net.refractions.udig.catalog.IService#getConnectionParams()
181: */
182: public Map<String, Serializable> getConnectionParams() {
183: return params;
184: }
185:
186: private Throwable msg = null;
187: private volatile UDIGWFSDataStore ds = null;
188: private static final Lock dsLock = new UDIGDisplaySafeLock();
189:
190: UDIGWFSDataStore getDS(IProgressMonitor monitor) throws IOException {
191: if (ds == null) {
192: if (monitor == null)
193: monitor = new NullProgressMonitor();
194: monitor.beginTask(Messages.WFSServiceImpl_task_name, 3);
195: dsLock.lock();
196: monitor.worked(1);
197: try {
198: if (ds == null) {
199: UDIGWFSDataStoreFactory dsf = new UDIGWFSDataStoreFactory();
200: monitor.worked(1);
201: if (dsf.canProcess(params)) {
202: monitor.worked(1);
203: try {
204: ds = (UDIGWFSDataStore) dsf
205: .createDataStore(params);
206: monitor.worked(1);
207: } catch (IOException e) {
208: msg = e;
209: throw e;
210: }
211: }
212: }
213: } finally {
214: dsLock.unlock();
215: monitor.done();
216: }
217: IResolveDelta delta = new ResolveDelta(this ,
218: IResolveDelta.Kind.CHANGED);
219: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
220: .fire(new ResolveChangeEvent(this ,
221: IResolveChangeEvent.Type.POST_CHANGE, delta));
222: }
223: return ds;
224: }
225:
226: /*
227: * @see net.refractions.udig.catalog.IResolve#getStatus()
228: */
229: public Status getStatus() {
230: return msg != null ? Status.BROKEN
231: : ds == null ? Status.NOTCONNECTED : Status.CONNECTED;
232: }
233:
234: /*
235: * @see net.refractions.udig.catalog.IResolve#getMessage()
236: */
237: public Throwable getMessage() {
238: return msg;
239: }
240:
241: /*
242: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
243: */
244: public URL getIdentifier() {
245: return url;
246: }
247:
248: private class IServiceWFSInfo extends IServiceInfo {
249:
250: private WFSCapabilities caps = null;
251:
252: IServiceWFSInfo(UDIGWFSDataStore resource) {
253: super ();
254:
255: try {
256: caps = resource.getCapabilities();
257: } catch (Throwable t) {
258: t.printStackTrace();
259: caps = null;
260: }
261: }
262:
263: /*
264: * @see net.refractions.udig.catalog.IServiceInfo#getAbstract()
265: */
266: public String getAbstract() {
267: return caps == null ? null
268: : caps.getService() == null ? null : caps
269: .getService().get_abstract();
270: }
271:
272: /*
273: * @see net.refractions.udig.catalog.IServiceInfo#getIcon()
274: */
275: public ImageDescriptor getIcon() {
276: //return CatalogUIPlugin.getDefault().getImages().getImageDescriptor( ISharedImages.WFS_OBJ );
277: return AbstractUIPlugin.imageDescriptorFromPlugin(
278: WfsPlugin.ID, "icons/obj16/wfs_obj.16"); //$NON-NLS-1$
279: }
280:
281: /*
282: * @see net.refractions.udig.catalog.IServiceInfo#getKeywords()
283: */
284: public String[] getKeywords() {
285: return caps == null ? null
286: : caps.getService() == null ? null : caps
287: .getService().getKeywordList();
288: }
289:
290: /*
291: * @see net.refractions.udig.catalog.IServiceInfo#getSchema()
292: */
293: public URI getSchema() {
294: return WFSSchema.NAMESPACE;
295: }
296:
297: public String getDescription() {
298: return getIdentifier().toString();
299: }
300:
301: public URL getSource() {
302: return getIdentifier();
303: }
304:
305: public String getTitle() {
306: return (caps == null || caps.getService() == null) ? (getIdentifier() == null ? Messages.WFSServiceImpl_broken
307: : getIdentifier().toString())
308: : caps.getService().getTitle();
309: }
310: }
311: }
|