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