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.oracle;
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.catalog.oracle.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.geotools.data.oracle.OracleDataStore;
046: import org.geotools.data.oracle.OracleDataStoreFactory;
047:
048: /**
049: * Provides ...TODO summary sentence
050: * <p>
051: * TODO Description
052: * </p>
053: * @author David Zwiers, Refractions Research
054: * @since 0.6
055: */
056: public class OracleServiceImpl extends IService {
057:
058: private URL url = null;
059: private Map<String, Serializable> params = null;
060:
061: public OracleServiceImpl(URL arg1, Map<String, Serializable> arg2) {
062: url = arg1;
063: params = arg2;
064: checkPort(params);
065: }
066:
067: /*
068: * Required adaptions:
069: * <ul>
070: * <li>IServiceInfo.class
071: * <li>List.class <IGeoResource>
072: * </ul>
073: *
074: * @see net.refractions.udig.catalog.IService#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
075: */
076: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
077: throws IOException {
078: if (monitor == null)
079: monitor = new NullProgressMonitor();
080:
081: if (adaptee == null) {
082: throw new NullPointerException("No adaptor specified"); //$NON-NLS-1$
083: }
084:
085: if (adaptee.isAssignableFrom(OracleDataStore.class)) {
086: return adaptee.cast(getDS(monitor));
087: }
088: return super .resolve(adaptee, monitor);
089: }
090:
091: /*
092: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
093: */
094: public <T> boolean canResolve(Class<T> adaptee) {
095: if (adaptee == null)
096: return false;
097: return (adaptee.isAssignableFrom(OracleDataStore.class))
098: || super .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<OracleGeoResource> members(IProgressMonitor monitor)
126: throws IOException {
127:
128: if (members == null) {
129: rLock.lock();
130: try {
131: if (members == null) {
132: getDS(monitor); // load ds
133: members = new LinkedList<OracleGeoResource>();
134: String[] typenames = ds.getTypeNames();
135: if (typenames != null)
136: for (int i = 0; i < typenames.length; i++) {
137: members.add(new OracleGeoResource(this ,
138: typenames[i]));
139: }
140: }
141: } finally {
142: rLock.unlock();
143: }
144: }
145: return members;
146: }
147:
148: private volatile List<OracleGeoResource> 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 IServiceOracleInfo(ds);
161: }
162: } finally {
163: rLock.unlock();
164: }
165: IResolveDelta delta = new ResolveDelta(this ,
166: IResolveDelta.Kind.CHANGED);
167: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
168: .fire(new ResolveChangeEvent(this ,
169: IResolveChangeEvent.Type.POST_CHANGE, delta));
170: }
171: return info;
172: }
173:
174: private volatile IServiceInfo info = null;
175:
176: /*
177: * @see net.refractions.udig.catalog.IService#getConnectionParams()
178: */
179: public Map<String, Serializable> getConnectionParams() {
180: return params;
181: }
182:
183: private Throwable msg = null;
184: private volatile OracleDataStore ds = null;
185: protected Lock rLock = new UDIGDisplaySafeLock();
186: private static final Lock dsLock = new UDIGDisplaySafeLock();
187:
188: OracleDataStore getDS(IProgressMonitor monitor) throws IOException {
189: if (ds == null) {
190: dsLock.lock();
191: try {
192: if (ds == null) {
193: OracleDataStoreFactory dsf = new OracleDataStoreFactory();
194: checkPort(params);
195: assert params.get("port") instanceof String; //$NON-NLS-1$
196: if (dsf.canProcess(params)) {
197: try {
198: ds = (OracleDataStore) dsf
199: .createDataStore(params);
200: } catch (IOException e) {
201: msg = e;
202: throw e;
203: }
204: }
205: }
206: } finally {
207: dsLock.unlock();
208: }
209: IResolveDelta delta = new ResolveDelta(this ,
210: IResolveDelta.Kind.CHANGED);
211: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
212: .fire(new ResolveChangeEvent(this ,
213: IResolveChangeEvent.Type.POST_CHANGE, delta));
214: }
215: return ds;
216: }
217:
218: /*
219: * @see net.refractions.udig.catalog.IResolve#getStatus()
220: */
221: public Status getStatus() {
222: return msg != null ? Status.BROKEN
223: : ds == null ? Status.NOTCONNECTED : Status.CONNECTED;
224: }
225:
226: /*
227: * @see net.refractions.udig.catalog.IResolve#getMessage()
228: */
229: public Throwable getMessage() {
230: return msg;
231: }
232:
233: /*
234: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
235: */
236: public URL getIdentifier() {
237: return url;
238: }
239:
240: private class IServiceOracleInfo extends IServiceInfo {
241:
242: IServiceOracleInfo(OracleDataStore resource) {
243: super ();
244: String[] tns = null;
245: try {
246: tns = resource.getTypeNames();
247: } catch (IOException e) {
248: OraclePlugin.log(null, e);
249: tns = new String[0];
250: }
251: keywords = new String[tns.length + 1];
252: System.arraycopy(tns, 0, keywords, 1, tns.length);
253: keywords[0] = "oracle"; //$NON-NLS-1$
254:
255: try {
256: schema = new URI("jdbc://oracle/gml"); //$NON-NLS-1$
257: } catch (URISyntaxException e) {
258: OraclePlugin.log(null, e);
259: }
260: }
261:
262: public String getDescription() {
263: return getIdentifier().toString();
264: }
265:
266: public URL getSource() {
267: return getIdentifier();
268: }
269:
270: public String getTitle() {
271: return Messages.OracleServiceImpl_oracle_spatial
272: + getIdentifier().getHost();
273: }
274: }
275:
276: /**
277: * Checks to make sure the port hasn't been switched from String to Integer and corrects if necessary.
278: *
279: * @param params Parameters object
280: */
281: private void checkPort(Map<String, Serializable> params) {
282: String portKey = OracleServiceExtension.getFactory()
283: .getParametersInfo()[2].key;
284: if (params != null && params.containsKey(portKey)
285: && params.get(portKey) instanceof Integer) {
286: Integer val = (Integer) params.get(portKey);
287: params.remove(portKey);
288: params.put(portKey, val.toString());
289: }
290: }
291: }
|