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.gml;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.net.URI;
022: import java.net.URISyntaxException;
023: import java.net.URL;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.concurrent.locks.Lock;
028:
029: import net.refractions.udig.catalog.CatalogPlugin;
030: import net.refractions.udig.catalog.IResolve;
031: import net.refractions.udig.catalog.IResolveChangeEvent;
032: import net.refractions.udig.catalog.IResolveDelta;
033: import net.refractions.udig.catalog.IService;
034: import net.refractions.udig.catalog.IServiceInfo;
035: import net.refractions.udig.catalog.internal.CatalogImpl;
036: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
037: import net.refractions.udig.catalog.internal.ResolveDelta;
038: import net.refractions.udig.ui.ErrorManager;
039: import net.refractions.udig.ui.UDIGDisplaySafeLock;
040:
041: import org.eclipse.core.runtime.IProgressMonitor;
042: import org.eclipse.core.runtime.NullProgressMonitor;
043: import org.eclipse.core.runtime.SubProgressMonitor;
044: import org.geotools.data.DataStore;
045: import org.geotools.data.gml.GMLDataStore;
046: import org.geotools.data.gml.GMLDataStoreFactory;
047:
048: /**
049: * Connect to a shapefile
050: *
051: * @author David Zwiers, Refractions Research
052: * @since 0.6
053: */
054: public class GMLServiceImpl extends IService {
055:
056: static final Lock dsInstantationLock = new UDIGDisplaySafeLock();
057: final Lock dsLock = new UDIGDisplaySafeLock();
058: private URL url = null;
059: private Map<String, Serializable> params = null;
060:
061: /**
062: * Construct <code>ShpServiceImpl</code>.
063: *
064: * @param arg1
065: * @param arg2
066: */
067: public GMLServiceImpl(URL arg1, Map<String, Serializable> arg2) {
068: url = arg1;
069: params = arg2;
070: }
071:
072: /*
073: * Required adaptions: <ul> <li>IServiceInfo.class <li>List.class <IGeoResource> </ul>
074: *
075: * @see net.refractions.udig.catalog.IService#resolve(java.lang.Class,
076: * org.eclipse.core.runtime.IProgressMonitor)
077: */
078: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
079: throws IOException {
080: if (monitor == null)
081: monitor = new NullProgressMonitor();
082:
083: if (adaptee == null) {
084: throw new NullPointerException("No adaptor specified"); //$NON-NLS-1$
085: }
086: if (adaptee.isAssignableFrom(GMLDataStore.class)) {
087: return adaptee.cast(getDS(monitor));
088: }
089: return super .resolve(adaptee, monitor);
090: }
091:
092: /*
093: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
094: */
095: public <T> boolean canResolve(Class<T> adaptee) {
096: return adaptee != null
097: && (adaptee.isAssignableFrom(GMLDataStore.class) || super
098: .canResolve(adaptee));
099: }
100:
101: public void dispose(IProgressMonitor monitor) {
102: if (members == null)
103: return;
104:
105: int steps = (int) ((double) 99 / (double) members.size());
106: for (IResolve resolve : members) {
107: try {
108: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
109: monitor, steps);
110: resolve.dispose(subProgressMonitor);
111: subProgressMonitor.done();
112: } catch (Throwable e) {
113: ErrorManager
114: .get()
115: .displayException(
116: e,
117: "Error disposing members of service: " + getIdentifier(), CatalogPlugin.ID); //$NON-NLS-1$
118: }
119: }
120: }
121:
122: /*
123: * @see net.refractions.udig.catalog.IResolve#members(org.eclipse.core.runtime.IProgressMonitor)
124: */
125: public List<GMLGeoResourceImpl> members(IProgressMonitor monitor)
126: throws IOException {
127:
128: if (members == null) {
129: dsLock.lock();
130: try {
131: if (members == null) {
132: members = new LinkedList<GMLGeoResourceImpl>();
133: String[] typenames = ds.getTypeNames();
134: if (typenames != null)
135: for (int i = 0; i < typenames.length; i++) {
136: members.add(new GMLGeoResourceImpl(this ,
137: typenames[i]));
138: }
139: }
140: } finally {
141: dsLock.unlock();
142: }
143: }
144: return members;
145: }
146:
147: private volatile List<GMLGeoResourceImpl> members = null;
148:
149: /*
150: * @see net.refractions.udig.catalog.IService#getInfo(org.eclipse.core.runtime.IProgressMonitor)
151: */
152: public IServiceInfo getInfo(IProgressMonitor monitor)
153: throws IOException {
154: getDS(monitor); // load ds
155: if (info == null && ds != null) {
156: dsLock.lock();
157: try {
158: if (info == null) {
159: info = new IServiceShpInfo();
160: }
161: } finally {
162: dsLock.unlock();
163: }
164: IResolveDelta delta = new ResolveDelta(this ,
165: IResolveDelta.Kind.CHANGED);
166: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
167: .fire(new ResolveChangeEvent(this ,
168: IResolveChangeEvent.Type.POST_CHANGE, delta));
169: }
170: return info;
171: }
172:
173: private volatile IServiceInfo info = null;
174:
175: /*
176: * @see net.refractions.udig.catalog.IService#getConnectionParams()
177: */
178: public Map<String, Serializable> getConnectionParams() {
179: return params;
180: }
181:
182: private Throwable msg = null;
183: private volatile DataStore ds = null;
184:
185: DataStore getDS(IProgressMonitor monitor) throws IOException {
186: if (monitor == null)
187: monitor = new NullProgressMonitor();
188: if (ds == null) {
189: dsInstantationLock.lock();
190: try {
191: if (ds == null) {
192: GMLDataStoreFactory dsf = new GMLDataStoreFactory();
193: if (dsf.canProcess(params)) {
194: try {
195: ds = (DataStore) dsf
196: .createDataStore(params);
197: } catch (IOException e) {
198: msg = e;
199: throw e;
200: }
201: }
202: }
203: } finally {
204: dsInstantationLock.unlock();
205: }
206: IResolveDelta delta = new ResolveDelta(this ,
207: IResolveDelta.Kind.CHANGED);
208: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
209: .fire(new ResolveChangeEvent(this ,
210: IResolveChangeEvent.Type.POST_CHANGE, delta));
211: }
212: return ds;
213: }
214:
215: /*
216: * @see net.refractions.udig.catalog.IResolve#getStatus()
217: */
218: public Status getStatus() {
219: return msg != null ? Status.BROKEN
220: : ds == null ? Status.NOTCONNECTED : Status.CONNECTED;
221: }
222:
223: /*
224: * @see net.refractions.udig.catalog.IResolve#getMessage()
225: */
226: public Throwable getMessage() {
227: return msg;
228: }
229:
230: /*
231: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
232: */
233: public URL getIdentifier() {
234: return url;
235: }
236:
237: private class IServiceShpInfo extends IServiceInfo {
238:
239: IServiceShpInfo() {
240: super ();
241: try {
242: keywords = new String[] { ".shp", "Shapefile", //$NON-NLS-1$ //$NON-NLS-2$
243: ds.getTypeNames()[0] };
244: } catch (IOException ex) {
245: keywords = new String[] { ".shp", "Shapfile" //$NON-NLS-1$//$NON-NLS-2$
246: };
247: }
248:
249: try {
250: schema = new URI("shp://www.opengis.net/gml"); //$NON-NLS-1$
251: } catch (URISyntaxException e) {
252: GmlPlugin.log(null, e);
253: schema = null;
254: }
255: }
256:
257: public String getDescription() {
258: return getIdentifier().toString();
259: }
260:
261: public String getTitle() {
262: return getIdentifier().getFile();
263: }
264: }
265: }
|