001: package net.sourceforge.squirrel_sql.client.mainframe.action;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell and Johan Compagner
005: * colbell@users.sourceforge.net
006: * jcompagner@j-com.nl
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: */
022: import java.sql.SQLException;
023:
024: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
025: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
026: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
027: import net.sourceforge.squirrel_sql.fw.sql.SQLConnection;
028: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverManager;
029: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
030: import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
031: import net.sourceforge.squirrel_sql.fw.util.BaseException;
032: import net.sourceforge.squirrel_sql.fw.util.ICommand;
033:
034: import net.sourceforge.squirrel_sql.client.IApplication;
035:
036: /**
037: * This <CODE>ICommand</CODE> allows the user to connect to
038: * an <TT>ISQLAlias</TT>.
039: *
040: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
041: */
042: public class OpenConnectionCommand implements ICommand {
043: /** Application API. */
044: private IApplication _app;
045:
046: /** The <TT>ISQLAlias</TT> to connect to. */
047: private ISQLAlias _sqlAlias;
048:
049: private final String _userName;
050: private final String _password;
051: private final SQLDriverPropertyCollection _props;
052:
053: private SQLConnection _conn;
054:
055: /**
056: * Ctor.
057: *
058: * @param app The <TT>IApplication</TT> that defines app API.
059: * @param alias The <TT>ISQLAlias</TT> to connect to.
060: * @param userName The user to connect as.
061: * @param password Password for userName.
062: * @param props Connection properties.
063: *
064: * @throws IllegalArgumentException
065: * Thrown if a <TT>null</TT> <TT>IApplication</TT> or <TT>ISQLAlias</TT> passed.
066: */
067: public OpenConnectionCommand(IApplication app, ISQLAlias sqlAlias,
068: String userName, String password,
069: SQLDriverPropertyCollection props) {
070: super ();
071: if (app == null) {
072: throw new IllegalArgumentException(
073: "Null IApplication passed");
074: }
075: if (sqlAlias == null) {
076: throw new IllegalArgumentException("Null ISQLAlias passed");
077: }
078: _app = app;
079: _sqlAlias = sqlAlias;
080: _userName = userName;
081: _password = password;
082: _props = props;
083: }
084:
085: /**
086: * Display connection internal frame.
087: */
088: public void execute() throws BaseException {
089: _conn = null;
090: final IIdentifier driverID = _sqlAlias.getDriverIdentifier();
091: final ISQLDriver sqlDriver = _app.getDataCache().getDriver(
092: driverID);
093: final SQLDriverManager mgr = _app.getSQLDriverManager();
094: try {
095: _conn = mgr.getConnection(sqlDriver, _sqlAlias, _userName,
096: _password, _props);
097: } catch (SQLException ex) {
098: throw new WrappedSQLException(ex);
099: } catch (Throwable th) {
100: throw new BaseException(th);
101: }
102: }
103:
104: /**
105: * Retrieve the newly opened connection.
106: *
107: * @return The <TT>SQLConnection</T>.
108: */
109: public SQLConnection getSQLConnection() {
110: return _conn;
111: }
112:
113: }
|