001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
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: package org.geotools.data.hsql;
017:
018: import java.io.PrintWriter;
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.SQLException;
022:
023: /**
024: * Shell for JDBC transactions of all types. This provides a base class to the
025: * database transactional classes.
026: *
027: * @author Amr Alam, Refractions Research
028: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/hsql/src/main/java/org/geotools/data/hsql/HsqlConnection.java $
029: */
030: public class HsqlConnection implements javax.sql.DataSource {
031: //private static Category _log = Category.getInstance(MysqlConnection.class.getName());
032:
033: /** The Mysql-specific JDBC driver class using mm.mysql */
034: private static final String HSQL_DRIVER_CLASS = "org.hsqldb.jdbcDriver";
035:
036: /** The Mysql-specific JDBC driver path. */
037: private static final String HSQL_DRIVER_PATH = "jdbc:hsql";
038:
039: /** the computer where the database to connect to resides */
040: private String host;
041:
042: /** The port to connect on */
043: private String port;
044:
045: /** The name of the database to connect to */
046: private String dbName;
047:
048: /** The name of the user to log in to the database */
049: private String user = null;
050:
051: /** The password of the user to log in to the database */
052: private String password = null;
053:
054: /**
055: * Constructor with all internal database driver classes, driver paths and
056: * database types.
057: *
058: * @param host The driver class; should be passed from the
059: * database-specific subclass.
060: * @param port The driver path; should be passed from the database-specific
061: * subclass.
062: * @param dbName The database type; should be passed from the
063: * database-specific subclass.
064: */
065: public HsqlConnection(String host, String port, String dbName) {
066: this .host = host;
067: this .port = port;
068: this .dbName = dbName;
069: }
070:
071: /**
072: * Sets the user and password strings of the login to be used when
073: * connecting to the Mysql database.
074: *
075: * @param user The string of the user to connect to the database with.
076: * @param password The string of the password of user.
077: */
078: public void setLogin(String user, String password) {
079: this .user = user;
080: this .password = password;
081: }
082:
083: /**
084: * An accessor function to get the user to log in to the db.
085: *
086: * @return the user.
087: */
088: public String getLoginUser() {
089: return user;
090: }
091:
092: /**
093: * An accessor function to get the password to log in to the db.
094: *
095: * @return a string of the password.
096: */
097: public String getLoginPassword() {
098: return password;
099: }
100:
101: /**
102: * Retrieves a connection to the Mysql database, using the current user and
103: * password;
104: *
105: * @return An open SQL connection with the database.
106: *
107: * @throws SQLException if there are any database problems.
108: */
109: public Connection getConnection() throws SQLException {
110: return getConnection(user, password);
111: }
112:
113: /**
114: * Retrieves a connection to the Mysql database, specifying a user and
115: * password.
116: *
117: * @param user The string of the user to connect to the database with.
118: * @param password The string of the corresponding password of user.
119: *
120: * @return An open SQL connection with the database.
121: *
122: * @throws SQLException if there are any database problems.
123: */
124: public Connection getConnection(String user, String password)
125: throws SQLException {
126: // creates the string to connect with
127: String connectionPath = HSQL_DRIVER_PATH + "://" + host + ":"
128: + port + "/" + dbName;
129: Connection dbConnection = null;
130:
131: // Instantiate the driver classes
132: try {
133: Class.forName(HSQL_DRIVER_CLASS);
134: dbConnection = DriverManager.getConnection(connectionPath,
135: user, password);
136: } catch (ClassNotFoundException e) {
137: throw new SQLException("Hsql driver was not found.");
138: }
139:
140: return dbConnection;
141: }
142:
143: /**
144: * An accessor function to get the length of timeout.
145: *
146: * @return the time out.
147: *
148: * @task TODO: implement this.
149: */
150: public int getLoginTimeout() {
151: return 10;
152: }
153:
154: /**
155: * A setter function to get the length of timeout.
156: *
157: * @param seconds the length of the time out.
158: *
159: * @task TODO: implement this.
160: */
161: public void setLoginTimeout(int seconds) {
162: }
163:
164: /**
165: * An accessor function to get the log writer.
166: *
167: * @return a writer
168: *
169: * @task TODO: implement this.
170: */
171: public PrintWriter getLogWriter() {
172: return new PrintWriter(System.out);
173: }
174:
175: /**
176: * An setter method to set the log writer.
177: *
178: * @param out the writer to use for logging.
179: *
180: * @task TODO: implement this.
181: */
182: public void setLogWriter(PrintWriter out) {
183: }
184: }
|