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.ui;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.InvocationTargetException;
021: import java.net.URL;
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.SQLException;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import org.eclipse.core.runtime.IProgressMonitor;
032: import org.eclipse.jface.operation.IRunnableWithProgress;
033: import org.eclipse.jface.viewers.ISelection;
034: import org.eclipse.jface.viewers.IStructuredSelection;
035: import org.eclipse.jface.viewers.StructuredSelection;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.custom.CCombo;
038: import org.eclipse.swt.events.FocusEvent;
039: import org.eclipse.swt.events.ModifyEvent;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Event;
042: import org.eclipse.swt.widgets.Group;
043: import org.eclipse.swt.widgets.Text;
044: import org.eclipse.ui.PlatformUI;
045: import org.geotools.data.DataStoreFactorySpi.Param;
046: import org.geotools.data.oracle.OracleConnectionFactory;
047: import org.geotools.data.oracle.OracleDataStoreFactory;
048:
049: import net.refractions.udig.catalog.IResolve;
050: import net.refractions.udig.catalog.IService;
051: import net.refractions.udig.catalog.internal.oracle.OracleServiceExtension;
052: import net.refractions.udig.catalog.oracle.internal.Messages;
053: import net.refractions.udig.catalog.ui.UDIGConnectionPage;
054: import net.refractions.udig.catalog.ui.preferences.AbstractProprietaryDatastoreWizardPage;
055: import net.refractions.udig.catalog.ui.preferences.AbstractProprietaryJarPreferencePage;
056: import net.refractions.udig.ui.PlatformGIS;
057:
058: /**
059: * Enter Oracle connection parameters.
060: *
061: * @author dzwiers
062: * @since 0.3
063: */
064: public class OracleSpatialWizardPage extends
065: AbstractProprietaryDatastoreWizardPage implements
066: UDIGConnectionPage {
067:
068: public OracleSpatialWizardPage() {
069: super (Messages.OracleSpatialWizardPage_wizardTitle);
070: }
071:
072: @Override
073: protected AbstractProprietaryJarPreferencePage getPreferencePage() {
074: return new OracleSpatialPreferences();
075: }
076:
077: public String getId() {
078: return "net.refractions.udig.catalog.ui.oracle"; //$NON-NLS-1$
079: }
080:
081: @Override
082: protected String getRestartMessage() {
083: return Messages.OracleSpatialWizardPage_restart;
084: }
085:
086: @Override
087: protected String getDriversMessage() {
088: return Messages.OracleSpatialWizardPage_drivers;
089: }
090:
091: @Override
092: protected void doCreateWizardPage(Composite parent) {
093:
094: // For Drag 'n Drop as well as for general selections
095: // look for a url as part of the selction
096: ISelection tmpSelection = PlatformUI.getWorkbench()
097: .getActiveWorkbenchWindow().getActivePage()
098: .getSelection();
099: IStructuredSelection selection = null;
100: if (tmpSelection == null
101: || !(tmpSelection instanceof IStructuredSelection)) {
102: selection = new StructuredSelection();
103: } else {
104: selection = (IStructuredSelection) tmpSelection;
105: }
106:
107: String selectedText = null;
108: for (Iterator itr = selection.iterator(); itr.hasNext();) {
109: Object o = itr.next();
110: if (o instanceof URL || o instanceof String) {
111: selectedText = (String) o;
112: // jdbc:postgresql://host:port/database
113: // jdbc:oracle:thin:@host:port:instance
114: if (selectedText.contains("jdbc:oracle:thin:@")) { //$NON-NLS-1$
115: break;
116: }
117: selectedText = null;
118: }
119: }
120: if (selectedText != null) {
121: int startindex = selectedText.indexOf("@"); //$NON-NLS-1$
122: int hostEnd = selectedText.indexOf(":", startindex); //$NON-NLS-1$
123: int portEnd = selectedText.indexOf(":", hostEnd); //$NON-NLS-1$
124: int databaseEnd = selectedText.indexOf(":", portEnd); //$NON-NLS-1$
125: String the_host = selectedText.substring(startindex,
126: hostEnd);
127: ((Text) host).setText(the_host);
128:
129: String the_port = selectedText.substring(hostEnd, portEnd);
130: if (!the_port.equalsIgnoreCase("")) { //$NON-NLS-1$
131: port.setText(the_port);
132: }
133: String the_database = selectedText.substring(portEnd,
134: databaseEnd);
135: ((CCombo) database).add(the_database);
136: }
137: }
138:
139: public boolean canProcess(Object object) {
140: return getOracleURL(object) != null;
141: }
142:
143: protected String getOracleURL(Object data) {
144: String url = null;
145: if (data instanceof String) {
146: String[] strings = ((String) data).split("\n"); //$NON-NLS-1$
147: url = strings[0];
148: if (!url.toLowerCase().contains("jdbc:oracle")) { //$NON-NLS-1$
149: url = null;
150: }
151: }
152:
153: return url;
154: }
155:
156: /**
157: * TODO summary sentence for createAdvancedControl ...
158: *
159: */
160: protected Group createAdvancedControl(Composite arg0) {
161: port.setText("1521"); //$NON-NLS-1$
162: return null;
163: }
164:
165: /*
166: * dbtype host port user passwd instance schema namespace
167: */
168:
169: private static OracleDataStoreFactory factory = new OracleDataStoreFactory();
170:
171: protected OracleDataStoreFactory getDataStoreFactorySpi() {
172: return factory;
173: }
174:
175: public Map<String, Serializable> getParams() {
176: if (!OracleSpatialPreferences.isInstalled())
177: return null;
178:
179: if (getHostText() == null || getPortText() == null
180: || getUserText() == null || getDBText() == null
181: || getSchema() == null)
182: return null;
183:
184: Map<String, Serializable> params = new HashMap<String, Serializable>();
185: Param[] dbParams = factory.getParametersInfo();
186:
187: params.put(dbParams[0].key, "oracle"); //$NON-NLS-1$
188: params.put(dbParams[1].key, getHostText());
189:
190: String port1 = getPortText();
191: params.put(dbParams[2].key, port1);
192:
193: String user1 = getUserText();
194: params.put(dbParams[3].key, user1);
195:
196: String pass1 = getPassText();
197: params.put(dbParams[4].key, pass1);
198:
199: String db = getDBText();
200: params.put(dbParams[5].key, db);
201:
202: String schema1 = getSchema();
203: params.put(dbParams[6].key, schema1);
204:
205: // not sureabout this line
206: // params.put(dbParams[7].key,"MAPINFO"); //$NON-NLS-1$
207:
208: return params;
209: }
210:
211: /**
212: *
213: * @return
214: */
215: protected String getPortText() {
216: return this .port.getText();
217: }
218:
219: /**
220: * TODO summary sentence for getConnection ...
221: *
222: * @see net.refractions.udig.catalog.internal.ui.datastore.DataBaseRegistryWizardPage#getConnection()
223: * @return
224: */
225: protected Connection getConnection() {
226: try {
227: String db1 = ((Text) database).getText();
228: db1 = "".equals(db1) ? null : db1; //$NON-NLS-1$
229: final String db = db1;
230: final String host1 = ((Text) host).getText();
231: final String port1 = port.getText();
232: final String user1 = user.getText();
233: final String pass1 = pass.getText();
234: if (db == null) {
235: //don't bother
236: return null;
237: }
238: getContainer().run(true, true, new IRunnableWithProgress() {
239:
240: public void run(IProgressMonitor monitor)
241: throws InvocationTargetException,
242: InterruptedException {
243: PlatformGIS.runBlockingOperation(
244: new IRunnableWithProgress() {
245:
246: public void run(IProgressMonitor monitor)
247: throws InvocationTargetException,
248: InterruptedException {
249: monitor
250: .beginTask(
251: Messages.OracleSpatialWizardPage_connectionTask,
252: IProgressMonitor.UNKNOWN);
253: if (OracleSpatialWizardPage.this .connection != null)
254: try {
255: OracleSpatialWizardPage.this .connection
256: .close();
257: } catch (SQLException e1) {
258: // it's dead anyhow
259: }
260:
261: OracleConnectionFactory connFac = new OracleConnectionFactory(
262: host1, port1, db);
263: connFac.setLogin(user1, pass1);
264: DriverManager.setLoginTimeout(3);
265:
266: try {
267: OracleSpatialWizardPage.this .connection = connFac
268: .getConnectionPool()
269: .getConnection();
270: if (OracleSpatialWizardPage.this .connection != null) {
271: OracleSpatialWizardPage.this .database
272: .getDisplay()
273: .asyncExec(
274: new Runnable() {
275: public void run() {
276: OracleSpatialWizardPage.this .database
277: .notifyListeners(
278: SWT.FocusIn,
279: new Event());
280:
281: }
282: });
283: }
284: } catch (SQLException e) {
285: throw new InvocationTargetException(
286: e,
287: e.getLocalizedMessage());
288: }
289: }
290:
291: }, monitor);
292: }
293: });
294: } catch (InvocationTargetException e2) {
295: throw new RuntimeException(e2.getLocalizedMessage(), e2);
296: } catch (InterruptedException e2) {
297: // Don't know why this exception doesn't do anything.
298: }
299:
300: return connection;
301: }
302:
303: public void focusGained(FocusEvent e) {
304: if (e.widget != null) {
305: if (e.widget.equals(schema)) {
306: super .focusGained(e);
307: }
308: }
309: }
310:
311: /**
312: * TODO summary sentence for dispose ...
313: *
314: * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
315: *
316: */
317: public void dispose() {
318: super .dispose();
319: if (connection != null)
320: try {
321: connection.close();
322: } catch (SQLException e) {
323: // it's dead anyhow
324: }
325: }
326:
327: private Connection connection = null;
328:
329: public void modifyText(ModifyEvent e) {
330: if (e.widget != null) {
331: if (e.widget.equals(host) || e.widget.equals(port)
332: || e.widget.equals(user) || e.widget.equals(pass)) {
333: setErrorMessage(null);
334: }
335: }
336: super .modifyText(e);
337: }
338:
339: /**
340: * TODO summary sentence for isDBCombo ...
341: *
342: * @see net.refractions.udig.catalog.internal.ui.datastore.DataBaseRegistryWizardPage#isDBCombo()
343: * @return
344: */
345: protected boolean isDBCombo() {
346: return false;
347: }
348:
349: protected boolean isHostCombo() {
350: return false;
351: }
352:
353: /**
354: * TODO summary sentence for hasSchema ...
355: *
356: * @see net.refractions.udig.catalog.internal.ui.datastore.DataBaseRegistryWizardPage#hasSchema()
357: * @return
358: */
359: protected boolean hasSchema() {
360: return true;
361: }
362:
363: protected String getHostText() {
364: String text = ((Text) host).getText();
365: if (text.trim().length() == 0)
366: text = null;
367: return host == null ? null : text;
368: }
369:
370: protected String getDBText() {
371: String text = ((Text) database).getText();
372: if (text.trim().length() == 0)
373: text = null;
374: return database == null ? null : text;
375: }
376:
377: private String getSchema() {
378: String text = schema.getText();
379: if (text.trim().length() == 0)
380: text = null;
381: return schema == null ? null : text;
382: }
383:
384: protected String getUserText() {
385: String text = user.getText();
386: if (text.trim().length() == 0)
387: text = null;
388: return user == null ? null : text;
389: }
390:
391: protected String getPassText() {
392: String text = pass.getText();
393: if (text.trim().length() == 0)
394: text = null;
395: return pass == null ? null : text;
396: }
397:
398: @Override
399: protected boolean doIsPageComplete() {
400: Map p = getParams();
401: if (p == null)
402: return false;
403: boolean r = factory.canProcess(p);
404: return r;
405: }
406:
407: public List<IResolve> getResources(IProgressMonitor monitor)
408: throws Exception {
409: if (!isPageComplete())
410: return null;
411:
412: OracleServiceExtension creator = new OracleServiceExtension();
413: IService service = creator.createService(null, getParams());
414: service.getInfo(monitor); // load
415:
416: List<IResolve> servers = new ArrayList<IResolve>();
417: servers.add(service);
418: return servers;
419: }
420: }
|