/[schmitzm]/branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1165 - (hide annotations)
Mon Oct 25 08:50:00 2010 UTC (14 years, 4 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/io/DbServerSettings.java
File MIME type: text/plain
File size: 9808 byte(s)


1 alfonx 1161 package skrueger.geotools.io;
2    
3     import java.awt.Component;
4     import java.net.MalformedURLException;
5     import java.util.HashMap;
6     import java.util.regex.Pattern;
7    
8     import org.apache.commons.lang.ArrayUtils;
9 alfonx 1163 import org.apache.commons.lang.StringUtils;
10 alfonx 1161 import org.geotools.data.postgis.PostgisNGDataStoreFactory;
11     import org.geotools.jdbc.JDBCDataStore;
12     import org.geotools.jdbc.JDBCDataStoreFactory;
13 alfonx 1163 import org.jfree.util.Log;
14 alfonx 1161
15     import schmitzm.swing.BooleanInputOption;
16     import schmitzm.swing.ManualInputOption;
17     import schmitzm.swing.ManualInputOption.Integer;
18     import schmitzm.swing.ManualInputOption.PasswordViewable;
19     import schmitzm.swing.ManualInputOption.Text;
20     import schmitzm.swing.MultipleOptionPane;
21     import schmitzm.swing.SelectionInputOption;
22     import schmitzm.swing.SelectionInputOption.Combo;
23    
24     /**
25     * This class describes all settings needed to connect to a WFS server. This
26     * class extends a {@link HashMap} and contains two groups of keys:<br/>
27     * The first group of keys, are all keys provided by Geotools to create a WFS
28     * datastore, like: {@link JDBCDataStore#SCHEMA} .<br/>
29     * The second group are additional keys defined in the enum
30     * {@link DbServerSettings.Key}.<br/>
31     * This class can serialize all important parameters needed to define the
32     * connection into a {@link String} with {@link #toPropertiesString()} and
33     * re-import the String with {@link #parsePropertiesString(String)}.
34     */
35 alfonx 1164 public class DbServerSettings extends ServerSettings<Object, Object> {
36 alfonx 1161
37 alfonx 1164 public enum DbType {
38     postgis("postgresql");
39    
40     private final String protocolString;
41    
42     DbType(String protocolString) {
43     this.protocolString = protocolString;
44     }
45    
46     public String getProtocolString() {
47     return protocolString;
48     };
49     }
50    
51 alfonx 1161 /**
52     * params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");
53     * params.put(JDBCDataStoreFactory.HOST.key, host); // the name or ip
54     * params.put(JDBCDataStoreFactory.PORT.key, port); // the port that
55     *
56     * params.put(JDBCDataStoreFactory.DATABASE.key, database); // the
57     *
58     * // name params.put(JDBCDataStoreFactory.USER.key, username); // the user
59     * to params.put(JDBCDataStoreFactory.PASSWD.key, password); // the
60     *
61     * params.put(JDBCDataStoreFactory.SCHEMA, schema);
62     *
63     * params.put(JDBCDataStoreFactory.EXPOSE_PK.key, true); *
64     */
65    
66     public enum Key {
67     }
68    
69 alfonx 1164 /**
70     * Opens a GUI that asks the use define a DB connection.
71     *
72     * @param dbServer
73     * <code>null</code> to create a new instance, or an instance to
74     * edit.
75     * @return <code>null</code> if the user cancelled the creation of a new
76     * {@link DbServerSettings}, otherwise the edited instance.
77     */
78     public static DbServerSettings createOrEdit(Component owner,
79     DbServerSettings dbServer) {
80     boolean newCreated = false;
81 alfonx 1161
82 alfonx 1164 if (dbServer == null) {
83     newCreated = true;
84     dbServer = new DbServerSettings(DbType.postgis);
85     }
86 alfonx 1161
87 alfonx 1164 Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>(
88     "Database type", true, DbType.values(), ArrayUtils.indexOf(
89     DbType.values(), dbServer.getDbType()), DbType.values());
90    
91     Text hostInput = new ManualInputOption.Text("Hostname", true,
92     dbServer.getHost());
93    
94     Integer portInput = new ManualInputOption.Integer("Port", true,
95     dbServer.getPort());
96    
97     Text databaseInput = new ManualInputOption.Text("Database", true,
98     dbServer.getDatabase());
99    
100     Text schemaInput = new ManualInputOption.Text("Schema", true,
101     dbServer.getSchema());
102    
103     Text userInput = new ManualInputOption.Text("Username", true,
104     dbServer.getUsername());
105    
106     PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
107     "Password", true, dbServer.getPassword());
108    
109     BooleanInputOption exposePkInput = new BooleanInputOption(
110     "Expose primary keys", dbServer.getExposePrimaryKey());
111    
112     Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
113     "DB Connection paramters", dpTypeInput, hostInput, portInput,
114     databaseInput, schemaInput, userInput, passwdInput,
115     exposePkInput);
116    
117     if (input == null) {
118     if (newCreated)
119     return null;
120     else
121     return dbServer;
122     } else {
123     dbServer.setDbType((DbType) input[0]);
124     dbServer.setHost((String) input[1]);
125     dbServer.setPort((java.lang.Integer) input[2]);
126     dbServer.setDatabase((String) input[3]);
127     dbServer.setSchema((String) input[4]);
128     dbServer.setUsername((String) input[5]);
129     dbServer.setPassword(String.valueOf((char[]) input[6]));
130     dbServer.setExposePrimaryKey((Boolean) input[7]);
131 alfonx 1161 }
132    
133 alfonx 1164 return dbServer;
134    
135 alfonx 1161 }
136    
137 alfonx 1164 /**
138     * @return transforms the settings to a String that can be stored in a
139     * .properties line. @see #parsePropertiesString
140     * @throws MalformedURLException
141     */
142     public static DbServerSettings parsePropertiesString(String propString)
143     throws MalformedURLException {
144    
145     if (propString == null || propString.isEmpty())
146     throw new IllegalArgumentException("parameter to parse was empty");
147     try {
148    
149     String[] split = propString.split(Pattern.quote(DELIMITER));
150    
151     int i = 0;
152     DbServerSettings dbServer = new DbServerSettings(
153     DbType.valueOf(split[i++]));
154    
155     dbServer.setTitle(split[i++]);
156     dbServer.setHost(split[i++]);
157     dbServer.setPort(java.lang.Integer.valueOf(split[i++]));
158     dbServer.setUsername(StringUtils.stripToNull(split[i++]));
159     dbServer.setPassword(split[i++]);
160     dbServer.setDatabase(split[i++]);
161     dbServer.setExposePrimaryKey(Boolean.valueOf(split[i++]));
162     dbServer.setSchema(stringOrNull(split[i++]));
163    
164     return dbServer;
165     } catch (Exception e) {
166     Log.warn("couldn't parse " + propString, e);
167     return null;
168     }
169 alfonx 1161 }
170    
171 alfonx 1164 private String[] cachedTypeNames = null;
172    
173     public DbServerSettings() {
174 alfonx 1165 this(DbType.postgis);
175 alfonx 1161 }
176    
177 alfonx 1164 public DbServerSettings(DbType dbType) {
178     setDbType(dbType);
179     setHost("localhost");
180     setSchema("public");
181 alfonx 1165 put(JDBCDataStoreFactory.PK_METADATA_TABLE.key, null);
182 alfonx 1161 }
183    
184 alfonx 1164 public String[] getCachedTypeNames() {
185     return cachedTypeNames;
186 alfonx 1161 }
187    
188 alfonx 1164 public String getDatabase() {
189     return (String) get(JDBCDataStoreFactory.DATABASE.key);
190 alfonx 1161 }
191    
192 alfonx 1164 public DbType getDbType() {
193     String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);
194     if (dbt != null)
195     return DbType.valueOf(dbt);
196     return null;
197     }
198    
199 alfonx 1161 public Boolean getExposePrimaryKey() {
200     Boolean expk = (Boolean) get(JDBCDataStoreFactory.EXPOSE_PK.key);
201     if (expk == null)
202     return (Boolean) JDBCDataStoreFactory.EXPOSE_PK.sample;
203     return expk;
204     }
205    
206     public String getHost() {
207     return (String) get(JDBCDataStoreFactory.HOST.key);
208     }
209    
210     public String getPassword() {
211     return (String) get(JDBCDataStoreFactory.PASSWD.key);
212     }
213    
214 alfonx 1164 public java.lang.Integer getPort() {
215     java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);
216     if (port == null)
217     return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;
218     return port;
219 alfonx 1161 }
220    
221 alfonx 1164 public String getSchema() {
222     return (String) get(JDBCDataStoreFactory.SCHEMA.key);
223     }
224    
225 alfonx 1161 public String getUsername() {
226     return (String) get(JDBCDataStoreFactory.USER.key);
227     }
228    
229 alfonx 1164 /**
230     * @return <code>true</code>, if all parameters look OK, without actually
231     * opening any connection.
232     */
233     public boolean isWellDefined() {
234     if (getDbType() == null)
235     return false;
236    
237     if (getHost() == null)
238     return false;
239    
240     if (getUsername() == null)
241     return false;
242    
243     if (getPassword() == null)
244     return false;
245    
246     if (getPort() == null)
247     return false;
248    
249     if (getSchema() == null)
250     return false;
251    
252     return true;
253 alfonx 1161 }
254    
255 alfonx 1164 public void setCachedTypeNames(String[] cachedTypeNames) {
256     this.cachedTypeNames = cachedTypeNames;
257 alfonx 1161 }
258    
259 alfonx 1164 public void setDatabase(String db) {
260     put(JDBCDataStoreFactory.DATABASE.key, db);
261 alfonx 1161 }
262    
263     public void setDbType(DbType dbType) {
264     put(PostgisNGDataStoreFactory.DBTYPE.key, dbType.toString());
265    
266     if (dbType == DbType.postgis) {
267     if (getPort() == null) {
268     // For a PostGIS DBMS automatically set the port to 5432
269     setPort(5432);
270     }
271     if (getUsername() == null) {
272     // For a PostGIS DBMS automatically set the user to postgres
273     setUsername("postgres");
274     }
275    
276     }
277    
278     }
279    
280 alfonx 1164 public void setExposePrimaryKey(Boolean exportPk) {
281     put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);
282 alfonx 1161 }
283    
284 alfonx 1164 public void setHost(String host) {
285     put(JDBCDataStoreFactory.HOST.key, host);
286 alfonx 1161 }
287    
288 alfonx 1164 public void setPassword(String password) {
289     put(JDBCDataStoreFactory.PASSWD.key, password);
290     }
291 alfonx 1161
292 alfonx 1164 public void setPort(java.lang.Integer port) {
293     put(JDBCDataStoreFactory.PORT.key, port);
294     }
295 alfonx 1161
296 alfonx 1164 public void setSchema(String schema) {
297     put(JDBCDataStoreFactory.SCHEMA.key, schema);
298     }
299 alfonx 1161
300 alfonx 1164 public void setUsername(String username) {
301     put(JDBCDataStoreFactory.USER.key, username);
302 alfonx 1161 }
303    
304     /**
305     * @return transforms the settings to a String that can be stored in a
306     * .properties line. @see #parsePropertiesString
307     */
308     public String toPropertiesString() {
309    
310     StringBuffer serialized = new StringBuffer(100);
311    
312     serialized.append(getDbType().toString());
313     serialized.append(DELIMITER);
314    
315 alfonx 1164 serialized.append(getTitle());
316     serialized.append(DELIMITER);
317 alfonx 1165
318 alfonx 1162 serialized.append(getHost());
319 alfonx 1161 serialized.append(DELIMITER);
320    
321     serialized.append(getPort().toString());
322     serialized.append(DELIMITER);
323    
324 alfonx 1162 serialized.append(getUsername());
325 alfonx 1161 serialized.append(DELIMITER);
326    
327 alfonx 1162 serialized.append(getPassword());
328 alfonx 1161 serialized.append(DELIMITER);
329    
330 alfonx 1162 serialized.append(getDatabase());
331 alfonx 1161 serialized.append(DELIMITER);
332    
333     serialized.append(getExposePrimaryKey().toString());
334     serialized.append(DELIMITER);
335    
336     serialized.append(getSchema().toString());
337 alfonx 1164 // serialized.append(DELIMITER);
338 alfonx 1161
339     return serialized.toString();
340     }
341    
342     @Override
343     public String toString() {
344    
345     StringBuffer s = new StringBuffer();
346    
347     if (getDbType() != null) {
348     s.append(getDbType().getProtocolString() + "://");
349     }
350    
351     s.append(getHost());
352    
353 alfonx 1163 if (getPort() != null && getPort() != 5432) {
354 alfonx 1161 s.append(":" + getPort());
355     }
356    
357     s.append("/" + getDatabase());
358    
359     return s.toString();
360     }
361     }

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