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

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