/[schmitzm]/branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1327 - (hide annotations)
Sun Dec 5 17:23:22 2010 UTC (14 years, 3 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/io/GtDbServerSettings.java
File MIME type: text/plain
File size: 12237 byte(s)
added "isAvailable" to ServerSettings interface
1 alfonx 1161 package skrueger.geotools.io;
2    
3     import java.awt.Component;
4     import java.net.MalformedURLException;
5 alfonx 1195 import java.sql.Connection;
6     import java.sql.DriverManager;
7     import java.sql.SQLException;
8 alfonx 1161 import java.util.HashMap;
9     import java.util.regex.Pattern;
10    
11     import org.apache.commons.lang.ArrayUtils;
12 alfonx 1327 import org.apache.commons.lang.NotImplementedException;
13 alfonx 1163 import org.apache.commons.lang.StringUtils;
14 alfonx 1195 import org.apache.log4j.Logger;
15 alfonx 1161 import org.geotools.data.postgis.PostgisNGDataStoreFactory;
16     import org.geotools.jdbc.JDBCDataStore;
17     import org.geotools.jdbc.JDBCDataStoreFactory;
18 alfonx 1163 import org.jfree.util.Log;
19 alfonx 1161
20     import schmitzm.swing.BooleanInputOption;
21     import schmitzm.swing.ManualInputOption;
22     import schmitzm.swing.ManualInputOption.Integer;
23     import schmitzm.swing.ManualInputOption.PasswordViewable;
24     import schmitzm.swing.ManualInputOption.Text;
25     import schmitzm.swing.MultipleOptionPane;
26     import schmitzm.swing.SelectionInputOption;
27     import schmitzm.swing.SelectionInputOption.Combo;
28 alfonx 1195 import skrueger.db.PGUtil;
29 alfonx 1161
30     /**
31     * This class describes all settings needed to connect to a WFS server. This
32     * class extends a {@link HashMap} and contains two groups of keys:<br/>
33     * The first group of keys, are all keys provided by Geotools to create a WFS
34     * datastore, like: {@link JDBCDataStore#SCHEMA} .<br/>
35     * The second group are additional keys defined in the enum
36 alfonx 1295 * {@link GtDbServerSettings.Key}.<br/>
37 alfonx 1161 * This class can serialize all important parameters needed to define the
38     * connection into a {@link String} with {@link #toPropertiesString()} and
39     * re-import the String with {@link #parsePropertiesString(String)}.
40     */
41 alfonx 1327 public class GtDbServerSettings extends
42     AbstractGTServerSettings<Object, Object> {
43 alfonx 1161
44 alfonx 1295 Logger log = Logger.getLogger(GtDbServerSettings.class);
45 alfonx 1195
46 alfonx 1164 public enum DbType {
47     postgis("postgresql");
48    
49     private final String protocolString;
50    
51     DbType(String protocolString) {
52     this.protocolString = protocolString;
53     }
54    
55     public String getProtocolString() {
56     return protocolString;
57     };
58     }
59    
60 alfonx 1161 /**
61     * params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");
62     * params.put(JDBCDataStoreFactory.HOST.key, host); // the name or ip
63     * params.put(JDBCDataStoreFactory.PORT.key, port); // the port that
64     *
65     * params.put(JDBCDataStoreFactory.DATABASE.key, database); // the
66     *
67     * // name params.put(JDBCDataStoreFactory.USER.key, username); // the user
68     * to params.put(JDBCDataStoreFactory.PASSWD.key, password); // the
69     *
70     * params.put(JDBCDataStoreFactory.SCHEMA, schema);
71     *
72     * params.put(JDBCDataStoreFactory.EXPOSE_PK.key, true); *
73     */
74    
75     public enum Key {
76     }
77    
78 alfonx 1195 private static Connection dbc;
79    
80 alfonx 1164 /**
81     * Opens a GUI that asks the use define a DB connection.
82     *
83     * @param dbServer
84     * <code>null</code> to create a new instance, or an instance to
85     * edit.
86     * @return <code>null</code> if the user cancelled the creation of a new
87 alfonx 1295 * {@link GtDbServerSettings}, otherwise the edited instance.
88 alfonx 1164 */
89 alfonx 1295 public static GtDbServerSettings createOrEdit(Component owner,
90     GtDbServerSettings dbServer) {
91 alfonx 1164 boolean newCreated = false;
92 alfonx 1161
93 alfonx 1164 if (dbServer == null) {
94     newCreated = true;
95 alfonx 1295 dbServer = new GtDbServerSettings(DbType.postgis);
96 alfonx 1164 }
97 alfonx 1161
98 alfonx 1164 Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>(
99     "Database type", true, DbType.values(), ArrayUtils.indexOf(
100     DbType.values(), dbServer.getDbType()), DbType.values());
101    
102     Text hostInput = new ManualInputOption.Text("Hostname", true,
103     dbServer.getHost());
104    
105     Integer portInput = new ManualInputOption.Integer("Port", true,
106     dbServer.getPort());
107    
108     Text databaseInput = new ManualInputOption.Text("Database", true,
109     dbServer.getDatabase());
110    
111     Text schemaInput = new ManualInputOption.Text("Schema", true,
112     dbServer.getSchema());
113    
114     Text userInput = new ManualInputOption.Text("Username", true,
115     dbServer.getUsername());
116    
117     PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
118     "Password", true, dbServer.getPassword());
119    
120     BooleanInputOption exposePkInput = new BooleanInputOption(
121     "Expose primary keys", dbServer.getExposePrimaryKey());
122    
123     Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
124     "DB Connection paramters", dpTypeInput, hostInput, portInput,
125     databaseInput, schemaInput, userInput, passwdInput,
126     exposePkInput);
127    
128     if (input == null) {
129     if (newCreated)
130     return null;
131     else
132     return dbServer;
133     } else {
134     dbServer.setDbType((DbType) input[0]);
135     dbServer.setHost((String) input[1]);
136     dbServer.setPort((java.lang.Integer) input[2]);
137     dbServer.setDatabase((String) input[3]);
138     dbServer.setSchema((String) input[4]);
139     dbServer.setUsername((String) input[5]);
140     dbServer.setPassword(String.valueOf((char[]) input[6]));
141     dbServer.setExposePrimaryKey((Boolean) input[7]);
142 alfonx 1161 }
143    
144 alfonx 1164 return dbServer;
145    
146 alfonx 1161 }
147    
148 alfonx 1164 /**
149     * @return transforms the settings to a String that can be stored in a
150     * .properties line. @see #parsePropertiesString
151     * @throws MalformedURLException
152     */
153 alfonx 1291 @Override
154 alfonx 1295 public boolean parsePropertiesString(String propString) {
155 alfonx 1164
156     if (propString == null || propString.isEmpty())
157 alfonx 1295 return false;
158 alfonx 1164 try {
159    
160     String[] split = propString.split(Pattern.quote(DELIMITER));
161    
162     int i = 0;
163 alfonx 1295 setDbType(DbType.valueOf(split[i++]));
164     // DbServerSettings dbServer = new DbServerSettings(
165     // DbType.valueOf(split[i++])
166     // );
167 alfonx 1164
168 alfonx 1292 setTitle(split[i++]);
169     setHost(split[i++]);
170     setPort(java.lang.Integer.valueOf(split[i++]));
171     setUsername(StringUtils.stripToNull(split[i++]));
172     setPassword(split[i++]);
173     setDatabase(split[i++]);
174     setExposePrimaryKey(Boolean.valueOf(split[i++]));
175     setSchema(stringOrNull(split[i++]));
176 alfonx 1164
177 alfonx 1292 return true;
178 alfonx 1164 } catch (Exception e) {
179     Log.warn("couldn't parse " + propString, e);
180 alfonx 1292 return false;
181 alfonx 1164 }
182 alfonx 1161 }
183    
184 alfonx 1164 private String[] cachedTypeNames = null;
185 alfonx 1195 private String[] cachedGeometryTableNames;
186 alfonx 1164
187 alfonx 1295 public GtDbServerSettings() {
188 alfonx 1165 this(DbType.postgis);
189 alfonx 1295
190 alfonx 1161 }
191    
192 alfonx 1295 public GtDbServerSettings(DbType dbType) {
193 alfonx 1164 setDbType(dbType);
194     setHost("localhost");
195     setSchema("public");
196 alfonx 1165 put(JDBCDataStoreFactory.PK_METADATA_TABLE.key, null);
197 alfonx 1161 }
198    
199 alfonx 1295 public GtDbServerSettings(String propertiesString) {
200     parsePropertiesString(propertiesString);
201 alfonx 1291 }
202    
203 alfonx 1164 public String[] getCachedTypeNames() {
204     return cachedTypeNames;
205 alfonx 1161 }
206    
207 alfonx 1164 public String getDatabase() {
208     return (String) get(JDBCDataStoreFactory.DATABASE.key);
209 alfonx 1161 }
210    
211 alfonx 1164 public DbType getDbType() {
212     String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);
213     if (dbt != null)
214     return DbType.valueOf(dbt);
215     return null;
216     }
217    
218 alfonx 1161 public Boolean getExposePrimaryKey() {
219     Boolean expk = (Boolean) get(JDBCDataStoreFactory.EXPOSE_PK.key);
220     if (expk == null)
221     return (Boolean) JDBCDataStoreFactory.EXPOSE_PK.sample;
222     return expk;
223     }
224    
225     public String getHost() {
226     return (String) get(JDBCDataStoreFactory.HOST.key);
227     }
228    
229     public String getPassword() {
230     return (String) get(JDBCDataStoreFactory.PASSWD.key);
231     }
232    
233 alfonx 1164 public java.lang.Integer getPort() {
234     java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);
235     if (port == null)
236     return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;
237     return port;
238 alfonx 1161 }
239    
240 alfonx 1164 public String getSchema() {
241     return (String) get(JDBCDataStoreFactory.SCHEMA.key);
242     }
243    
244 alfonx 1161 public String getUsername() {
245     return (String) get(JDBCDataStoreFactory.USER.key);
246     }
247    
248 alfonx 1164 /**
249     * @return <code>true</code>, if all parameters look OK, without actually
250     * opening any connection.
251     */
252     public boolean isWellDefined() {
253     if (getDbType() == null)
254     return false;
255    
256     if (getHost() == null)
257     return false;
258    
259     if (getUsername() == null)
260     return false;
261    
262     if (getPassword() == null)
263     return false;
264    
265     if (getPort() == null)
266     return false;
267    
268     if (getSchema() == null)
269     return false;
270    
271     return true;
272 alfonx 1161 }
273    
274 alfonx 1164 public void setCachedTypeNames(String[] cachedTypeNames) {
275     this.cachedTypeNames = cachedTypeNames;
276 alfonx 1161 }
277    
278 alfonx 1164 public void setDatabase(String db) {
279     put(JDBCDataStoreFactory.DATABASE.key, db);
280 alfonx 1161 }
281    
282     public void setDbType(DbType dbType) {
283     put(PostgisNGDataStoreFactory.DBTYPE.key, dbType.toString());
284    
285     if (dbType == DbType.postgis) {
286     if (getPort() == null) {
287     // For a PostGIS DBMS automatically set the port to 5432
288     setPort(5432);
289     }
290     if (getUsername() == null) {
291     // For a PostGIS DBMS automatically set the user to postgres
292     setUsername("postgres");
293     }
294    
295     }
296    
297     }
298    
299 alfonx 1164 public void setExposePrimaryKey(Boolean exportPk) {
300     put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);
301 alfonx 1161 }
302    
303 alfonx 1164 public void setHost(String host) {
304     put(JDBCDataStoreFactory.HOST.key, host);
305 alfonx 1161 }
306    
307 alfonx 1164 public void setPassword(String password) {
308     put(JDBCDataStoreFactory.PASSWD.key, password);
309     }
310 alfonx 1161
311 alfonx 1164 public void setPort(java.lang.Integer port) {
312     put(JDBCDataStoreFactory.PORT.key, port);
313     }
314 alfonx 1161
315 alfonx 1164 public void setSchema(String schema) {
316     put(JDBCDataStoreFactory.SCHEMA.key, schema);
317     }
318 alfonx 1161
319 alfonx 1164 public void setUsername(String username) {
320     put(JDBCDataStoreFactory.USER.key, username);
321 alfonx 1161 }
322    
323     /**
324     * @return transforms the settings to a String that can be stored in a
325     * .properties line. @see #parsePropertiesString
326     */
327     public String toPropertiesString() {
328    
329     StringBuffer serialized = new StringBuffer(100);
330    
331     serialized.append(getDbType().toString());
332     serialized.append(DELIMITER);
333    
334 alfonx 1164 serialized.append(getTitle());
335     serialized.append(DELIMITER);
336 alfonx 1165
337 alfonx 1162 serialized.append(getHost());
338 alfonx 1161 serialized.append(DELIMITER);
339    
340     serialized.append(getPort().toString());
341     serialized.append(DELIMITER);
342    
343 alfonx 1162 serialized.append(getUsername());
344 alfonx 1161 serialized.append(DELIMITER);
345    
346 alfonx 1162 serialized.append(getPassword());
347 alfonx 1161 serialized.append(DELIMITER);
348    
349 alfonx 1162 serialized.append(getDatabase());
350 alfonx 1161 serialized.append(DELIMITER);
351    
352     serialized.append(getExposePrimaryKey().toString());
353     serialized.append(DELIMITER);
354    
355     serialized.append(getSchema().toString());
356 alfonx 1164 // serialized.append(DELIMITER);
357 alfonx 1161
358     return serialized.toString();
359     }
360    
361     @Override
362     public String toString() {
363    
364     StringBuffer s = new StringBuffer();
365    
366     if (getDbType() != null) {
367     s.append(getDbType().getProtocolString() + "://");
368     }
369    
370     s.append(getHost());
371    
372 alfonx 1163 if (getPort() != null && getPort() != 5432) {
373 alfonx 1161 s.append(":" + getPort());
374     }
375    
376     s.append("/" + getDatabase());
377    
378     return s.toString();
379     }
380 alfonx 1195
381     /**
382     * returns a valid Connection to our postgresql Database. Name of database,
383     * username and password must be provided.
384     *
385     * @param database
386     * name of the database
387     * @param username
388     * a valid username
389     * @param password
390     * valid password
391     * @return database connection
392     * @throws SQLException
393     * @throws ClassNotFoundException
394     */
395     public Connection getDatabaseConnection() {
396    
397     try {
398    
399     if (dbc == null || dbc.isClosed()) {
400     dbc = createNewDatabaseConnection();
401     }
402     return dbc;
403     } catch (Exception e) {
404     return null;
405     }
406     }
407    
408     private Connection createNewDatabaseConnection() throws SQLException {
409     try {
410     Class.forName("org.postgresql.Driver");
411     } catch (ClassNotFoundException e) {
412     throw new RuntimeException(e);
413     }
414     String url = "jdbc:postgresql://" + getHost()
415     // + (":" + PropertyUtils.getDbPort())
416     + ("/" + getDatabase());
417    
418     String password = getPassword();
419     String username = getUsername();
420    
421     // log.debug("DB URL: " + url + " u=" + username + " p="
422     // + password);
423    
424     return DriverManager.getConnection(url, username, password);
425     }
426    
427     public String[] getDescribedTablesWithGeometry() {
428    
429     if (cachedGeometryTableNames == null) {
430    
431     Connection dc;
432     try {
433     dc = createNewDatabaseConnection();
434     try {
435    
436     cachedGeometryTableNames = PGUtil
437     .getColumnsDescribedInGeometryColumnsTable(dc
438     .createStatement());
439    
440     } catch (SQLException e) {
441     log.error(e, e);
442     } finally {
443     dc.close();
444     }
445     } catch (SQLException e1) {
446     log.error(e1, e1);
447     return new String[0];
448     }
449     }
450     return cachedGeometryTableNames;
451     }
452    
453     @Override
454     protected void finalize() throws Throwable {
455     super.finalize();
456     dispose();
457     }
458    
459     public void dispose() {
460     try {
461     if (dbc != null && !dbc.isClosed()) {
462     dbc.close();
463     dbc = null;
464     }
465     } catch (SQLException e) {
466     log.error("Error while disposing the database connection to "
467     + toString(), e);
468     }
469     }
470 alfonx 1327
471     @Override
472     public boolean isAvailable() {
473     // TODO
474     throw new NotImplementedException("");
475     }
476 alfonx 1195 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26