/[schmitzm]/trunk/src/skrueger/geotools/io/DbServerSettings.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/io/DbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1164 - (hide annotations)
Mon Oct 25 07:25:26 2010 UTC (14 years, 4 months ago) by alfonx
File MIME type: text/plain
File size: 9729 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 1161 }
175    
176 alfonx 1164 public DbServerSettings(DbType dbType) {
177     setDbType(dbType);
178     setHost("localhost");
179     setSchema("public");
180 alfonx 1161 }
181    
182 alfonx 1164 public String[] getCachedTypeNames() {
183     return cachedTypeNames;
184 alfonx 1161 }
185    
186 alfonx 1164 public String getDatabase() {
187     return (String) get(JDBCDataStoreFactory.DATABASE.key);
188 alfonx 1161 }
189    
190 alfonx 1164 public DbType getDbType() {
191     String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key);
192     if (dbt != null)
193     return DbType.valueOf(dbt);
194     return null;
195     }
196    
197 alfonx 1161 public Boolean getExposePrimaryKey() {
198     Boolean expk = (Boolean) get(JDBCDataStoreFactory.EXPOSE_PK.key);
199     if (expk == null)
200     return (Boolean) JDBCDataStoreFactory.EXPOSE_PK.sample;
201     return expk;
202     }
203    
204     public String getHost() {
205     return (String) get(JDBCDataStoreFactory.HOST.key);
206     }
207    
208     public String getPassword() {
209     return (String) get(JDBCDataStoreFactory.PASSWD.key);
210     }
211    
212 alfonx 1164 public java.lang.Integer getPort() {
213     java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key);
214     if (port == null)
215     return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample;
216     return port;
217 alfonx 1161 }
218    
219 alfonx 1164 public String getSchema() {
220     return (String) get(JDBCDataStoreFactory.SCHEMA.key);
221     }
222    
223 alfonx 1161 public String getUsername() {
224     return (String) get(JDBCDataStoreFactory.USER.key);
225     }
226    
227 alfonx 1164 /**
228     * @return <code>true</code>, if all parameters look OK, without actually
229     * opening any connection.
230     */
231     public boolean isWellDefined() {
232     if (getDbType() == null)
233     return false;
234    
235     if (getHost() == null)
236     return false;
237    
238     if (getUsername() == null)
239     return false;
240    
241     if (getPassword() == null)
242     return false;
243    
244     if (getPort() == null)
245     return false;
246    
247     if (getSchema() == null)
248     return false;
249    
250     return true;
251 alfonx 1161 }
252    
253 alfonx 1164 public void setCachedTypeNames(String[] cachedTypeNames) {
254     this.cachedTypeNames = cachedTypeNames;
255 alfonx 1161 }
256    
257 alfonx 1164 public void setDatabase(String db) {
258     put(JDBCDataStoreFactory.DATABASE.key, db);
259 alfonx 1161 }
260    
261     public void setDbType(DbType dbType) {
262     put(PostgisNGDataStoreFactory.DBTYPE.key, dbType.toString());
263    
264     if (dbType == DbType.postgis) {
265     if (getPort() == null) {
266     // For a PostGIS DBMS automatically set the port to 5432
267     setPort(5432);
268     }
269     if (getUsername() == null) {
270     // For a PostGIS DBMS automatically set the user to postgres
271     setUsername("postgres");
272     }
273    
274     }
275    
276     }
277    
278 alfonx 1164 public void setExposePrimaryKey(Boolean exportPk) {
279     put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk);
280 alfonx 1161 }
281    
282 alfonx 1164 public void setHost(String host) {
283     put(JDBCDataStoreFactory.HOST.key, host);
284 alfonx 1161 }
285    
286 alfonx 1164 public void setPassword(String password) {
287     put(JDBCDataStoreFactory.PASSWD.key, password);
288     }
289 alfonx 1161
290 alfonx 1164 public void setPort(java.lang.Integer port) {
291     put(JDBCDataStoreFactory.PORT.key, port);
292     }
293 alfonx 1161
294 alfonx 1164 public void setSchema(String schema) {
295     put(JDBCDataStoreFactory.SCHEMA.key, schema);
296     }
297 alfonx 1161
298 alfonx 1164 public void setUsername(String username) {
299     put(JDBCDataStoreFactory.USER.key, username);
300 alfonx 1161 }
301    
302     /**
303     * @return transforms the settings to a String that can be stored in a
304     * .properties line. @see #parsePropertiesString
305     */
306     public String toPropertiesString() {
307    
308     StringBuffer serialized = new StringBuffer(100);
309    
310     serialized.append(getDbType().toString());
311     serialized.append(DELIMITER);
312    
313 alfonx 1164 serialized.append(getTitle());
314     serialized.append(DELIMITER);
315    
316 alfonx 1162 serialized.append(getHost());
317 alfonx 1161 serialized.append(DELIMITER);
318    
319     serialized.append(getPort().toString());
320     serialized.append(DELIMITER);
321    
322 alfonx 1162 serialized.append(getUsername());
323 alfonx 1161 serialized.append(DELIMITER);
324    
325 alfonx 1162 serialized.append(getPassword());
326 alfonx 1161 serialized.append(DELIMITER);
327    
328 alfonx 1162 serialized.append(getDatabase());
329 alfonx 1161 serialized.append(DELIMITER);
330    
331     serialized.append(getExposePrimaryKey().toString());
332     serialized.append(DELIMITER);
333    
334     serialized.append(getSchema().toString());
335 alfonx 1164 // serialized.append(DELIMITER);
336 alfonx 1161
337     return serialized.toString();
338     }
339    
340     @Override
341     public String toString() {
342    
343     StringBuffer s = new StringBuffer();
344    
345     if (getDbType() != null) {
346     s.append(getDbType().getProtocolString() + "://");
347     }
348    
349     s.append(getHost());
350    
351 alfonx 1163 if (getPort() != null && getPort() != 5432) {
352 alfonx 1161 s.append(":" + getPort());
353     }
354    
355     s.append("/" + getDatabase());
356    
357     return s.toString();
358     }
359     }

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