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.arcsde;
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:
028: import net.refractions.udig.catalog.CatalogPlugin;
029: import net.refractions.udig.catalog.IResolveChangeEvent;
030: import net.refractions.udig.catalog.IResolveDelta;
031: import net.refractions.udig.catalog.IService;
032: import net.refractions.udig.catalog.IServiceInfo;
033: import net.refractions.udig.catalog.internal.CatalogImpl;
034: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
035: import net.refractions.udig.catalog.internal.ResolveDelta;
036:
037: import org.eclipse.core.runtime.IProgressMonitor;
038: import org.eclipse.core.runtime.NullProgressMonitor;
039: import org.eclipse.jface.resource.ImageDescriptor;
040: import org.eclipse.ui.plugin.AbstractUIPlugin;
041: import org.geotools.data.DataStore;
042: import org.geotools.data.arcsde.ArcSDEDataStoreFactory;
043:
044: /**
045: * Connect to ArcSDE.
046: *
047: * @author David Zwiers, Refractions Research
048: * @since 0.6
049: */
050: public class ArcServiceImpl extends IService {
051:
052: private URL url = null;
053: private Map<String, Serializable> params = null;
054:
055: /**
056: * Construct <code>PostGISServiceImpl</code>.
057: *
058: * @param arg1
059: * @param arg2
060: */
061: public ArcServiceImpl(URL arg1, Map<String, Serializable> arg2) {
062: url = arg1;
063: params = arg2;
064: }
065:
066: /*
067: * Required adaptions: <ul> <li>IServiceInfo.class <li>List.class <IGeoResource> </ul>
068: *
069: * @see net.refractions.udig.catalog.IService#resolve(java.lang.Class,
070: * org.eclipse.core.runtime.IProgressMonitor)
071: */
072: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
073: throws IOException {
074: if (monitor == null)
075: monitor = new NullProgressMonitor();
076:
077: if (adaptee == null) {
078: throw new NullPointerException("No adaptor specified"); //$NON-NLS-1$
079: }
080: if (adaptee.isAssignableFrom(DataStore.class)) {
081: return adaptee.cast(getDS(monitor));
082: }
083: return super .resolve(adaptee, monitor);
084: }
085:
086: /*
087: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
088: */
089: public <T> boolean canResolve(Class<T> adaptee) {
090: return adaptee != null
091: && (adaptee.isAssignableFrom(IServiceInfo.class)
092: || adaptee.isAssignableFrom(List.class) || adaptee
093: .isAssignableFrom(DataStore.class));
094: }
095:
096: /*
097: * @see net.refractions.udig.catalog.IResolve#members(org.eclipse.core.runtime.IProgressMonitor)
098: */
099: public List<ArcGeoResource> members(IProgressMonitor monitor)
100: throws IOException {
101: if (members == null) {
102: synchronized (getDS(monitor)) {
103: if (members == null) {
104: getDS(null); // load ds
105: members = new LinkedList<ArcGeoResource>();
106: String[] typenames = ds.getTypeNames();
107: if (typenames != null)
108: for (int i = 0; i < typenames.length; i++) {
109: members.add(new ArcGeoResource(this ,
110: typenames[i]));
111: }
112: }
113: }
114: }
115: return members;
116: }
117:
118: private volatile List<ArcGeoResource> members = null;
119:
120: /*
121: * @see net.refractions.udig.catalog.IService#getInfo(org.eclipse.core.runtime.IProgressMonitor)
122: */
123: public IServiceInfo getInfo(IProgressMonitor monitor)
124: throws IOException {
125: getDS(monitor); // load ds
126: if (info == null && ds != null) {
127: synchronized (ds) {
128: if (info == null) {
129: info = new IServiceArcSDEInfo(ds);
130: }
131: }
132: IResolveDelta delta = new ResolveDelta(this ,
133: IResolveDelta.Kind.CHANGED);
134: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
135: .fire(new ResolveChangeEvent(this ,
136: IResolveChangeEvent.Type.POST_CHANGE, delta));
137: }
138: return info;
139: }
140:
141: private volatile IServiceInfo info = null;
142:
143: /*
144: * @see net.refractions.udig.catalog.IService#getConnectionParams()
145: */
146: public Map<String, Serializable> getConnectionParams() {
147: return params;
148: }
149:
150: private Throwable msg = null;
151: private volatile DataStore ds = null;
152:
153: DataStore getDS(IProgressMonitor monitor) throws IOException {
154: if (ds == null) {
155: synchronized (ArcSDEDataStoreFactory.class) {
156: // please copy a better example from WFS
157: if (ds == null) {
158: ArcSDEDataStoreFactory dsf = new ArcSDEDataStoreFactory();
159: if (dsf.canProcess(params)) {
160: try {
161: ds = dsf.createDataStore(params);
162: } catch (IOException e) {
163: msg = e;
164: throw e;
165: }
166: }
167: }
168: }
169: IResolveDelta delta = new ResolveDelta(this ,
170: IResolveDelta.Kind.CHANGED);
171: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
172: .fire(new ResolveChangeEvent(this ,
173: IResolveChangeEvent.Type.POST_CHANGE, delta));
174: }
175: return ds;
176: }
177:
178: /*
179: * @see net.refractions.udig.catalog.IResolve#getStatus()
180: */
181: public Status getStatus() {
182: return msg != null ? Status.BROKEN
183: : ds == null ? Status.NOTCONNECTED : Status.CONNECTED;
184: }
185:
186: /*
187: * @see net.refractions.udig.catalog.IResolve#getMessage()
188: */
189: public Throwable getMessage() {
190: return msg;
191: }
192:
193: /*
194: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
195: */
196: public URL getIdentifier() {
197: return url;
198: }
199:
200: private class IServiceArcSDEInfo extends IServiceInfo {
201:
202: IServiceArcSDEInfo(DataStore resource) {
203: super ();
204: String[] tns = null;
205: try {
206: tns = resource.getTypeNames();
207: } catch (IOException e) {
208: ArcsdePlugin.log(null, e);
209: tns = new String[0];
210: }
211: keywords = new String[tns.length + 1];
212: System.arraycopy(tns, 0, keywords, 1, tns.length);
213: keywords[0] = "postgis"; //$NON-NLS-1$
214:
215: try {
216: schema = new URI("jdbc://arcsde/gml"); //$NON-NLS-1$
217: } catch (URISyntaxException e) {
218: ArcsdePlugin.log(null, e);
219: }
220: }
221:
222: public String getDescription() {
223: return getIdentifier().toString();
224: }
225:
226: public URL getSource() {
227: return getIdentifier();
228: }
229:
230: public String getTitle() {
231: return "ARCSDE " + getIdentifier().getHost(); //$NON-NLS-1$
232: }
233:
234: /*
235: * @see net.refractions.udig.catalog.IServiceInfo#getIcon()
236: */
237: public ImageDescriptor getIcon() {
238: return AbstractUIPlugin.imageDescriptorFromPlugin(
239: ArcsdePlugin.ID, "icons/obj16/arcsde_obj.gif"); //$NON-NLS-1$
240: }
241: }
242: }
|