01: package org.geotools.catalog.postgis;
02:
03: import java.net.URI;
04: import java.util.HashMap;
05: import java.util.Map;
06: import java.util.StringTokenizer;
07:
08: import org.geotools.catalog.Catalog;
09: import org.geotools.catalog.Service;
10: import org.geotools.catalog.ServiceFactory;
11: import org.geotools.data.postgis.PostgisDataStoreFactory;
12:
13: public class PostGISServiceFactory implements ServiceFactory {
14:
15: public Service createService(Catalog parent, URI id, Map params) {
16: PostgisDataStoreFactory dataStoreFactory = new PostgisDataStoreFactory();
17: if (dataStoreFactory.canProcess(params)) {
18: return new PostGISService(parent, params, dataStoreFactory);
19: }
20:
21: return null;
22: }
23:
24: public boolean canProcess(URI uri) {
25: //jdbc style?
26: if (uri.getScheme() != null
27: && uri.getScheme().startsWith("jdbc:")) {
28: //make sure postgres driver
29: if (!uri.getScheme().startsWith("jdbc:postgresql"))
30: return false;
31: }
32:
33: return uri.getHost() != null && uri.getPath() != null;
34: }
35:
36: public Map createParams(URI uri) {
37: if (canProcess(uri)) {
38: HashMap params = new HashMap();
39: params.put(PostgisDataStoreFactory.HOST, uri.getHost());
40: params.put(PostgisDataStoreFactory.DATABASE, uri.getPath());
41:
42: if (uri.getPort() != -1) {
43: params.put(PostgisDataStoreFactory.PORT, new Integer(
44: uri.getPort()));
45: }
46:
47: if (uri.getQuery() != null) {
48: String user = null;
49: String pass = null;
50:
51: StringTokenizer st = new StringTokenizer(
52: uri.getQuery(), "&");
53: while (st.hasMoreTokens()) {
54: String parameter = st.nextToken();
55: if (parameter.startsWith("user=")) {
56: user = parameter.substring("user=".length());
57: }
58: if (parameter.startsWith("password=")) {
59: pass = parameter
60: .substring("password=".length());
61: }
62: }
63:
64: if (user != null) {
65: params.put(PostgisDataStoreFactory.USER, user);
66: }
67:
68: if (pass != null) {
69: params.put(PostgisDataStoreFactory.PASSWD, pass);
70: }
71: }
72:
73: return params;
74: }
75:
76: return null;
77: }
78:
79: }
|