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.referencing.factory.epsg;
017:
018: // J2SE dependencies
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.FileInputStream;
023: import java.util.Properties;
024: import java.sql.Connection;
025: import java.sql.SQLException;
026:
027: // Geotools dependencies
028: import org.geotools.util.logging.Logging;
029: import org.geotools.factory.Hints;
030: import org.geotools.referencing.factory.AbstractAuthorityFactory;
031:
032: // PostgreSQL dependencies
033: import org.postgresql.jdbc3.Jdbc3SimpleDataSource;
034:
035: /**
036: * Connection to the EPSG database in PostgreSQL database engine using JDBC. The EPSG
037: * database can be downloaded from <A HREF="http://www.epsg.org">http://www.epsg.org</A>.
038: * It should have been imported into a PostgreSQL database, which doesn't need to be on
039: * the local machine.
040: * <p>
041: * <h3>Connection parameters</h3>
042: * The preferred way to specify connection parameters is through the JNDI interface.
043: * However, this datasource provides the following alternative as a convenience: if an
044: * {@value #CONFIGURATION_FILE} file is found in current directory or in the user's home
045: * directory, then the following properties are fetch. Note that the default value may change
046: * in a future version if a public server become available.
047: * <P>
048: * <TABLE BORDER="1">
049: * <TR>
050: * <TH>Property</TH>
051: * <TH>Type</TH>
052: * <TH>Description</TH>
053: * <TH>Geotools Default</TH>
054: * </TR>
055: * <TR>
056: * <TD>{@code serverName}</TD>
057: * <TD>String</TD>
058: * <TD>PostgreSQL database server host name</TD>
059: * <TD>{@code localhost}</TD>
060: * </TR>
061: * <TR>
062: * <TD>{@code databaseName}</TD>
063: * <TD>String</TD>
064: * <TD>PostgreSQL database name</TD>
065: * <TD>{@code EPSG}</TD>
066: * </TR>
067: * <TR>
068: * <TD>{@code schema}</TD>
069: * <TD>String</TD>
070: * <TD>The schema for the EPSG tables</TD>
071: * <TD></TD>
072: * </TR>
073: * <TR>
074: * <TD>{@code portNumber}</TD>
075: * <TD>int</TD>
076: * <TD>TCP port which the PostgreSQL database server is listening on</TD>
077: * <TD>{@code 5432}</TD>
078: * </TR>
079: * <TR>
080: * <TD>{@code user}</TD>
081: * <TD>String</TD>
082: * <TD>User used to make database connections</TD>
083: * <TD>{@code GeoTools}</TD>
084: * </TR>
085: * <TR>
086: * <TD>{@code password}</TD>
087: * <TD>String</TD>
088: * <TD>Password used to make database connections</TD>
089: * <TD>{@code GeoTools}</TD></TR>
090: * </TABLE>
091: * <P>
092: * The database version is given in the
093: * {@linkplain org.opengis.metadata.citation.Citation#getEdition edition attribute}
094: * of the {@linkplain org.opengis.referencing.AuthorityFactory#getAuthority authority}.
095: * The postgreSQL database should be read only.
096: * <P>
097: * Just having this class accessible in the classpath, together with the registration in
098: * the {@code META-INF/services/} directory, is suffisient to get a working EPSG authority
099: * factory backed by this database. Vendors can create a copy of this class, modify it and
100: * bundle it with their own distribution if they want to connect their users to an other
101: * database.
102: *
103: * @since 2.2
104: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/epsg-postgresql/src/main/java/org/geotools/referencing/factory/epsg/PostgreDataSource.java $
105: * @version $Id: PostgreDataSource.java 27848 2007-11-12 13:10:32Z desruisseaux $
106: * @author Didier Richard
107: * @author Martin Desruisseaux
108: *
109: * @tutorial http://docs.codehaus.org/display/GEOTOOLS/How+to+install+the+EPSG+database+in+PostgreSQL
110: *
111: * @deprecated Replaced by {@link FactoryOnPostgreSQL}.
112: */
113: public class PostgreDataSource extends Jdbc3SimpleDataSource implements
114: DataSource {
115: /**
116: * The user configuration file. This class search first for the first file found in the
117: * following directories:
118: * <ul>
119: * <li>The current directory</li>
120: * <li>The user's home directory</li>
121: * </ul>
122: */
123: public static final String CONFIGURATION_FILE = "EPSG-DataSource.properties";
124:
125: /**
126: * The schema name, or {@code null} if none.
127: */
128: private final String schema;
129:
130: /**
131: * Creates a new instance of this data source.
132: */
133: public PostgreDataSource() {
134: this ("localhost", "EPSG", null, "Geotools", "Geotools");
135: }
136:
137: /**
138: * Creates a new instance of this data source with the specified default parameters.
139: * If a {@linkplain #CONFIGURATION_FILE configuration file} has been found, then the
140: * user setting will override the arguments supplied to this constructor.
141: *
142: * @param server The server name.
143: * @param database The database name.
144: * @param schema The schema name, or {@code null} if none.
145: * @param user The user name.
146: * @param password The password.
147: */
148: public PostgreDataSource(final String server,
149: final String database, final String schema,
150: final String user, final String password) {
151: final Properties p = new Properties();
152: try {
153: load(p);
154: } catch (IOException exception) {
155: Logging.unexpectedException(
156: "org.geotools.referencing.factory",
157: PostgreDataSource.class, "<init>", exception);
158: }
159: int port;
160: try {
161: port = Integer
162: .parseInt(p.getProperty("portNumber", "5432"));
163: } catch (NumberFormatException exception) {
164: port = 5432;
165: Logging.unexpectedException(
166: "org.geotools.referencing.factory",
167: PostgreDataSource.class, "<init>", exception);
168: }
169: setPortNumber(port);
170: setServerName(p.getProperty("serverName", server));
171: setDatabaseName(p.getProperty("databaseName", database));
172: setUser(p.getProperty("user", user));
173: setPassword(p.getProperty("password", password));
174: this .schema = (p.getProperty("schema", schema));
175: }
176:
177: /**
178: * Loads the {@linkplain #CONFIGURATION_FILE configuration file}.
179: *
180: * @param The properties in which to stores the configuration informations.
181: * @throws IOException if the configuration file was found, but an error occured while
182: * reading it.
183: */
184: private static void load(final Properties p) throws IOException {
185: File file = new File(CONFIGURATION_FILE);
186: if (!file.isFile()) {
187: file = new File(System.getProperty("user.home", "."),
188: CONFIGURATION_FILE);
189: if (!file.isFile()) {
190: return;
191: }
192: }
193: final InputStream in = new FileInputStream(file);
194: p.load(in);
195: in.close();
196: return;
197: }
198:
199: /**
200: * Returns the priority for this data source. This priority is set to a lower value than
201: * the {@linkplain AccessDataSource}'s one in order to give the priority to any "official"
202: * database installed locally by the user, when available.
203: */
204: public int getPriority() {
205: return NORMAL_PRIORITY - 50;
206: }
207:
208: /**
209: * Open a connection and creates an {@linkplain DirectEpsgFactory EPSG factory} for it.
210: *
211: * @param hints A map of hints, including the low-level factories to use for CRS creation.
212: * @return The EPSG factory using PostgreSQL syntax.
213: * @throws SQLException if connection to the database failed.
214: */
215: public AbstractAuthorityFactory createFactory(final Hints hints)
216: throws SQLException {
217: final FactoryUsingAnsiSQL factory = new FactoryUsingAnsiSQL(
218: hints, getConnection());
219: if (schema != null) {
220: factory.setSchema(schema);
221: }
222: return factory;
223: }
224:
225: /**
226: * For compilation with Java 6.
227: */
228: public boolean isWrapperFor(Class type) {
229: return false;
230: }
231:
232: /**
233: * For compilation with Java 6.
234: */
235: public Object unwrap(Class type) throws SQLException {
236: throw new SQLException();
237: }
238: }
|