001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.jdbcdatastore;
024:
025: import java.sql.Connection;
026: import java.sql.DriverManager;
027: import java.sql.SQLException;
028: import java.util.MissingResourceException;
029: import java.util.ResourceBundle;
030:
031: import org.eclipse.core.runtime.Platform;
032: import org.eclipse.jface.dialogs.IDialogConstants;
033: import org.eclipse.jface.dialogs.MessageDialog;
034: import org.eclipse.ui.IWorkbenchWindow;
035: import org.eclipse.ui.plugin.AbstractUIPlugin;
036: import org.osgi.framework.BundleContext;
037:
038: /**
039: * The main plugin class to be used in the desktop.
040: *
041: * @author Nigel Westbury
042: */
043: public class JDBCDatastorePlugin extends AbstractUIPlugin {
044:
045: public static final boolean DEBUG = "true"
046: .equalsIgnoreCase(Platform
047: .getDebugOption("net.sf.jmoney.jdbcdatastore/debug"));
048:
049: //The shared instance.
050: private static JDBCDatastorePlugin plugin;
051: //Resource bundle.
052: private ResourceBundle resourceBundle;
053:
054: /**
055: * The constructor.
056: */
057: public JDBCDatastorePlugin() {
058: super ();
059: plugin = this ;
060: try {
061: resourceBundle = ResourceBundle
062: .getBundle("net.sf.jmoney.jdbcdatastore.Language");
063: } catch (MissingResourceException x) {
064: resourceBundle = null;
065: }
066: }
067:
068: /**
069: * This method is called upon plug-in activation
070: */
071: @Override
072: public void start(BundleContext context) throws Exception {
073: super .start(context);
074: }
075:
076: /**
077: * This method is called when the plug-in is stopped
078: */
079: @Override
080: public void stop(BundleContext context) throws Exception {
081: super .stop(context);
082: }
083:
084: /**
085: * Returns the shared instance.
086: */
087: public static JDBCDatastorePlugin getDefault() {
088: return plugin;
089: }
090:
091: /**
092: * Returns the string from the plugin's resource bundle,
093: * or 'key' if not found.
094: */
095: public static String getResourceString(String key) {
096: ResourceBundle bundle = JDBCDatastorePlugin.getDefault()
097: .getResourceBundle();
098: try {
099: return (bundle != null) ? bundle.getString(key) : key;
100: } catch (MissingResourceException e) {
101: return key;
102: }
103: }
104:
105: /**
106: * Returns the plugin's resource bundle,
107: */
108: public ResourceBundle getResourceBundle() {
109: return resourceBundle;
110: }
111:
112: /**
113: * @param window
114: * @return
115: */
116: public SessionManager readSession(IWorkbenchWindow window) {
117: SessionManager result = null;
118:
119: // The following lines cannot return a null value because if
120: // no value is set then the default value set in
121: // the above initializeDefaultPreferences method will be returned.
122: String driver = getPreferenceStore().getString("driver");
123: String subprotocol = getPreferenceStore().getString(
124: "subProtocol");
125: String subprotocolData = getPreferenceStore().getString(
126: "subProtocolData");
127:
128: String url = "jdbc:" + subprotocol + ":" + subprotocolData;
129:
130: String user = getPreferenceStore().getString("user");
131: String password = getPreferenceStore().getString("password");
132:
133: if (getPreferenceStore().getBoolean("promptEachTime")) {
134: // TODO: Put up a dialog box so the user can change
135: // the connection options for this connection only.
136: }
137:
138: try {
139: Class.forName(driver).newInstance();
140: } catch (InstantiationException e2) {
141: // TODO Auto-generated catch block
142: e2.printStackTrace();
143: } catch (IllegalAccessException e2) {
144: // TODO Auto-generated catch block
145: e2.printStackTrace();
146: } catch (ClassNotFoundException e2) {
147: String title = JDBCDatastorePlugin
148: .getResourceString("errorTitle");
149: String message = JDBCDatastorePlugin
150: .getResourceString("driverNotFound");
151: MessageDialog waitDialog = new MessageDialog(window
152: .getShell(), title,
153: null, // accept the default window icon
154: message, MessageDialog.ERROR,
155: new String[] { IDialogConstants.OK_LABEL }, 0);
156: waitDialog.open();
157: return null;
158: }
159:
160: try {
161: Connection connection = DriverManager.getConnection(url,
162: user, password);
163: result = new SessionManager(connection);
164: } catch (SQLException e3) {
165: if (e3.getSQLState().equals("08000")) {
166: // A connection error which means the database server is probably not running.
167: String title = JDBCDatastorePlugin
168: .getResourceString("errorTitle");
169: String message = JDBCDatastorePlugin
170: .getResourceString("connectionFailed");
171: MessageDialog waitDialog = new MessageDialog(window
172: .getShell(), title,
173: null, // accept the default window icon
174: message, MessageDialog.ERROR,
175: new String[] { IDialogConstants.OK_LABEL }, 0);
176: waitDialog.open();
177: } else {
178: e3.printStackTrace();
179: throw new RuntimeException(e3.getMessage());
180: }
181: }
182:
183: return result;
184: }
185:
186: }
|