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.IOException;
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023: import java.util.Properties;
024:
025: /**
026: * This creates connections for the HSQL datasource to make
027: * its transactions. To create a HsqlDataStore, create a
028: * HsqlConnectionFactory, and pass that connection factory
029: * to the HsqlDataStore constructor.
030: *
031: * @author Amr Alam, Refractions Research
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/hsql/src/main/java/org/geotools/data/hsql/HsqlConnectionFactory.java $
033: */
034: public class HsqlConnectionFactory {
035:
036: /** Standard logging instance */
037: // private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geotools.data.hsql");
038: /** Creates Hsql-specific JDBC driver class. */
039: private static final String DRIVER_CLASS = "org.hsqldb.jdbcDriver";
040: private String _username = "";
041: private String _password = "";
042: /** An alternate character set to use. */
043: private String charSet;
044:
045: private String _dbFileName = null;
046:
047: /**
048: * Creates a new HsqlConnectionFactory object from a DB filename, a username
049: * and a password.
050: * @param dbFileName the HSQL database filename
051: * @param user the HSQL database username
052: * @param password the HSQL database password for user
053: */
054: public HsqlConnectionFactory(String dbFileName, String user,
055: String password) {
056: _dbFileName = dbFileName;
057: _username = user;
058: _password = password;
059: }
060:
061: /**
062: *
063: * @return an HSQL Connection object
064: * @throws SQLException if an error occurs connecting to the HSQL database
065: */
066: public Connection getConnection() throws SQLException {
067: return getConnection(_dbFileName, _username, _password);
068: }
069:
070: public Connection getConnection(String dbFileName)
071: throws SQLException {
072: _dbFileName = dbFileName;
073: return getConnection(_dbFileName, _username, _password);
074: }
075:
076: /**
077: * Creates and returns a HSQL Connection based upon the username
078: * and password parameters passed to this
079: * method. This is shorthand for the following two calls:<br>
080: * <br>
081: * connPool.setLogin(username, password);<br>
082: * connPool.setDBFileName(filename);<br>
083: * connPool.getConnection();<br>
084: * @param username the HSQL username
085: * @param password the password corresponding to <code>username</code>
086: * @return an HSQL Connection object
087: * @throws SQLException if an error occurs connecting to the HSQL database
088: */
089: public Connection getConnection(String username, String password)
090: throws SQLException {
091: setLogin(username, password);
092: return getConnection(_dbFileName, username, password);
093: }
094:
095: /**
096: * Creates and returns a HSQL Connection based upon the username
097: * and password parameters passed to this
098: * method. This is shorthand for the following two calls:<br>
099: * <br>
100: * connPool.setLogin(username, password);<br>
101: * connPool.setDBFileName(filename);<br>
102: * connPool.getConnection();<br>
103: *
104: * @param dbFileName the filename to use for the new database connection.
105: * @param user the name of the user connect to connect to the pgsql db.
106: * @param password the password for the user.
107: *
108: * @return the sql Connection object to the database.
109: *
110: * @throws SQLException if the postgis sql driver could not be found
111: */
112: public Connection getConnection(String dbFileName, String user,
113: String password) throws SQLException {
114: _dbFileName = dbFileName;
115: _username = user;
116: _password = password;
117:
118: Properties props = new Properties();
119: props.put("user", user);
120: props.put("password", password);
121:
122: if (charSet != null) {
123: props.put("charSet", charSet);
124: }
125:
126: return getConnection(dbFileName, props);
127: }
128:
129: /**
130: * Creates a database connection method to initialize a given database for
131: * feature extraction with the given Properties.
132: *
133: * @param dbFileName the filename to use for the new database connection.
134: * @param props Should contain at a minimum the user and password. Additional
135: * properties, such as charSet, can also be added.
136: *
137: * @return the sql Connection object to the database.
138: *
139: * @throws SQLException if the postgis sql driver could not be found
140: */
141: public Connection getConnection(String dbFileName, Properties props)
142: throws SQLException {
143: _dbFileName = dbFileName;
144: Connection dbConnection = null;
145: try {
146: //Load the HSQL Database Engine JDBC driver
147: // hsqldb.jar should be in the class path or made part of the current jar
148: Class.forName(DRIVER_CLASS);
149:
150: //connect to the database. This will load the db files and start the
151: // database if it is not alread running.
152: // dbFileName is used to open or create files that hold the state
153: // of the db.
154: // It can contain directory names relative to the
155: // current working directory
156: dbConnection = DriverManager.getConnection("jdbc:hsqldb:"//"jdbc:hsqldb:"
157: + dbFileName, // filenames
158: props); // properties (include user/password)
159: setSpatialAliases(dbConnection);
160: } catch (ClassNotFoundException e) {
161: // TODO Auto-generated catch block
162: e.printStackTrace();
163: } catch (IOException e) {
164: // TODO Auto-generated catch block
165: e.printStackTrace();
166: } catch (SQLException e) {
167: // TODO Auto-generated catch block
168: e.printStackTrace();
169: }
170: return dbConnection;
171: }
172:
173: /**
174: * Sets the HSQL database login credentials.
175: * @param username the username
176: * @param password the password
177: */
178: public void setLogin(String username, String password) {
179: _username = username;
180: _password = password;
181: }
182:
183: /**
184: * Sets the HSQL database filename.
185: * @param dbFileName the filename to use for this database
186: */
187: public void setDBFileName(String dbFileName) {
188: _dbFileName = dbFileName;
189: }
190:
191: /**
192: * Sets a different character set for the hsql driver to use.
193: *
194: * @param charSet the string of a valid charset name.
195: */
196: public void setCharSet(String charSet) {
197: this .charSet = charSet;
198: }
199:
200: /**
201: * Sets up all the spatial DB in a box aliases
202: * @param conn The connection we've just created
203: * @throws IOException
204: * @throws SQLException
205: */
206: private void setSpatialAliases(Connection conn) throws IOException,
207: SQLException {
208: Statement st = conn.createStatement();
209: st
210: .execute("CREATE ALIAS equals FOR \"org.openplans.spatialDBbox.StaticGeometry.equals\";"
211: + "CREATE ALIAS toString FOR \"org.openplans.spatialDBbox.StaticGeometry.toString\";"
212: + "CREATE ALIAS contains FOR \"org.openplans.spatialDBbox.StaticGeometry.contains\";"
213: + "CREATE ALIAS isEmpty FOR \"org.openplans.spatialDBbox.StaticGeometry.isEmpty\";"
214: + "CREATE ALIAS length FOR \"org.openplans.spatialDBbox.StaticGeometry.getLength\";"
215: + "CREATE ALIAS intersects FOR \"org.openplans.spatialDBbox.StaticGeometry.intersects\";"
216: + "CREATE ALIAS geomFromWKT FOR \"org.openplans.spatialDBbox.StaticGeometry.geomFromWKT\";"
217: + "CREATE ALIAS isValid FOR \"org.openplans.spatialDBbox.StaticGeometry.isValid\";"
218: + "CREATE ALIAS geometryType FOR \"org.openplans.spatialDBbox.StaticGeometry.getGeometryType\";"
219: + "CREATE ALIAS sRID FOR \"org.openplans.spatialDBbox.StaticGeometry.getSRID\";"
220: + "CREATE ALIAS numPoints FOR \"org.openplans.spatialDBbox.StaticGeometry.getNumPoints\";"
221: + "CREATE ALIAS isSimple FOR \"org.openplans.spatialDBbox.StaticGeometry.isSimple\";"
222: + "CREATE ALIAS distance FOR \"org.openplans.spatialDBbox.StaticGeometry.distance\";"
223: + "CREATE ALIAS isWithinDistance FOR \"org.openplans.spatialDBbox.StaticGeometry.isWithinDistance\";"
224: + "CREATE ALIAS area FOR \"org.openplans.spatialDBbox.StaticGeometry.getArea\";"
225: + "CREATE ALIAS centroid FOR \"org.openplans.spatialDBbox.StaticGeometry.getCentroid\";"
226: + "CREATE ALIAS interiorPoint FOR \"org.openplans.spatialDBbox.StaticGeometry.getInteriorPoint\";"
227: + "CREATE ALIAS dimension FOR \"org.openplans.spatialDBbox.StaticGeometry.getDimension\";"
228: + "CREATE ALIAS boundary FOR \"org.openplans.spatialDBbox.StaticGeometry.getBoundary\";"
229: + "CREATE ALIAS boundaryDimension FOR \"org.openplans.spatialDBbox.StaticGeometry.getBoundaryDimension\";"
230: + "CREATE ALIAS envelope FOR \"org.openplans.spatialDBbox.StaticGeometry.getEnvelope\";"
231: + "CREATE ALIAS disjoint FOR \"org.openplans.spatialDBbox.StaticGeometry.disjoint\";"
232: + "CREATE ALIAS touches FOR \"org.openplans.spatialDBbox.StaticGeometry.touches\";"
233: + "CREATE ALIAS crosses FOR \"org.openplans.spatialDBbox.StaticGeometry.crosses\";"
234: + "CREATE ALIAS within FOR \"org.openplans.spatialDBbox.StaticGeometry.within\";"
235: + "CREATE ALIAS overlaps FOR \"org.openplans.spatialDBbox.StaticGeometry.overlaps\";"
236: + "CREATE ALIAS relatePattern FOR \"org.openplans.spatialDBbox.StaticGeometry.relatePattern\";"
237: + "CREATE ALIAS relate FOR \"org.openplans.spatialDBbox.StaticGeometry.relate\";"
238: + "CREATE ALIAS toText FOR \"org.openplans.spatialDBbox.StaticGeometry.toText\";"
239: + "CREATE ALIAS buffer_with_segments FOR \"org.openplans.spatialDBbox.StaticGeometry.buffer_with_segments\";"
240: + "CREATE ALIAS buffer FOR \"org.openplans.spatialDBbox.StaticGeometry.buffer\";"
241: + "CREATE ALIAS convexHull FOR \"org.openplans.spatialDBbox.StaticGeometry.convexHull\";"
242: + "CREATE ALIAS intersection FOR \"org.openplans.spatialDBbox.StaticGeometry.intersection\";"
243: + "CREATE ALIAS unionGeom FOR \"org.openplans.spatialDBbox.StaticGeometry.unionGeom\";"
244: + "CREATE ALIAS difference FOR \"org.openplans.spatialDBbox.StaticGeometry.difference\";"
245: + "CREATE ALIAS symDifference FOR \"org.openplans.spatialDBbox.StaticGeometry.symDifference\";"
246: + "CREATE ALIAS equalsExactTolerance FOR \"org.openplans.spatialDBbox.StaticGeometry.equalsExactTolerance\";"
247: + "CREATE ALIAS equalsExact FOR \"org.openplans.spatialDBbox.StaticGeometry.equalsExact\";"
248: + "CREATE ALIAS numGeometries FOR \"org.openplans.spatialDBbox.StaticGeometry.getNumGeometries\";"
249: + "CREATE ALIAS geometryN FOR \"org.openplans.spatialDBbox.StaticGeometry.getGeometryN\";"
250: + "CREATE ALIAS x FOR \"org.openplans.spatialDBbox.StaticGeometry.getX\";"
251: + "CREATE ALIAS y FOR \"org.openplans.spatialDBbox.StaticGeometry.getY\";"
252: + "CREATE ALIAS isClosed FOR \"org.openplans.spatialDBbox.StaticGeometry.isClosed\";"
253: + "CREATE ALIAS pointN FOR \"org.openplans.spatialDBbox.StaticGeometry.getPointN\";"
254: + "CREATE ALIAS startPoint FOR \"org.openplans.spatialDBbox.StaticGeometry.getStartPoint\";"
255: + "CREATE ALIAS endPoint FOR \"org.openplans.spatialDBbox.StaticGeometry.getEndPoint\";"
256: + "CREATE ALIAS isRing FOR \"org.openplans.spatialDBbox.StaticGeometry.isRing\";"
257: + "CREATE ALIAS exteriorRing FOR \"org.openplans.spatialDBbox.StaticGeometry.getExteriorRing\";"
258: + "CREATE ALIAS numInteriorRing FOR \"org.openplans.spatialDBbox.StaticGeometry.getNumInteriorRing\";"
259: + "CREATE ALIAS interiorRingN FOR \"org.openplans.spatialDBbox.StaticGeometry.getInteriorRingN\";");
260: }
261: }
|