001: package net.refractions.udig.catalog.internal.postgis.ui;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.Map;
010:
011: import net.refractions.udig.catalog.CatalogPlugin;
012: import net.refractions.udig.catalog.IResolve;
013: import net.refractions.udig.catalog.IService;
014: import net.refractions.udig.catalog.PostGISServiceExtension;
015: import net.refractions.udig.catalog.internal.postgis.PostGISGeoResource;
016: import net.refractions.udig.catalog.internal.postgis.PostGISServiceImpl;
017: import net.refractions.udig.catalog.internal.postgis.PostgisPlugin;
018: import net.refractions.udig.catalog.ui.UDIGConnectionFactory;
019:
020: import org.geotools.data.postgis.PostgisDataStoreFactory;
021:
022: public class PostGisConnectionFactory extends UDIGConnectionFactory {
023:
024: @Override
025: public boolean canProcess(Object context) {
026: return toCapabilitiesURL(context) != null;
027: }
028:
029: @Override
030: public Map<String, Serializable> createConnectionParameters(
031: Object context) {
032: if (context instanceof PostGISServiceImpl) {
033: PostGISServiceImpl postgis = (PostGISServiceImpl) context;
034: return postgis.getConnectionParams();
035: }
036: URL url = toCapabilitiesURL(context);
037: if (url == null) {
038: // so we are not sure it is a postgis url
039: // lets guess
040: url = CatalogPlugin.locateURL(context);
041: }
042: if (url != null && PostGISServiceExtension.isPostGIS(url)) {
043: // well we have a url - lets try it!
044: List<IResolve> list = CatalogPlugin.getDefault()
045: .getLocalCatalog().find(url, null);
046: for (IResolve resolve : list) {
047: if (resolve instanceof PostGISServiceImpl) {
048: // got a hit!
049: PostGISServiceImpl postgisService = (PostGISServiceImpl) context;
050: return postgisService.getConnectionParams();
051: } else if (resolve instanceof PostGISGeoResource) {
052: PostGISGeoResource layer = (PostGISGeoResource) resolve;
053: PostGISServiceImpl postgis;
054: try {
055: postgis = (PostGISServiceImpl) layer
056: .parent(null);
057: return postgis.getConnectionParams();
058: } catch (IOException e) {
059: toCapabilitiesURL(layer.getIdentifier());
060: }
061: }
062: }
063: return createParams(url);
064: }
065: return null;
066: }
067:
068: @SuppressWarnings("unchecked")//$NON-NLS-1$
069: @Override
070: public URL createConnectionURL(Object context) {
071: if (context instanceof URL) {
072: return (URL) context;
073: }
074: if (context instanceof Map) {
075: Map params = (Map) context;
076:
077: try {
078: return PostGISServiceExtension.toURL(params);
079: } catch (MalformedURLException e) {
080: return null;
081: }
082:
083: }
084: if (context instanceof String) {
085: return toCapabilitiesURL((String) context);
086: }
087: return null;
088: }
089:
090: /**
091: * Convert "data" to a PostGIS url
092: * <p>
093: * Candidates for conversion are:
094: * <ul>
095: * <li>URL - from browser DnD
096: * <li>PostGISServiceImpl - from catalog DnD
097: * <li>IService - from search DnD
098: * </ul>
099: * </p>
100: * <p>
101: * No external processing should be required here, it is enough to guess and let
102: * the ServiceFactory try a real connect.
103: * </p>
104: * @param data IService, URL, or something else
105: * @return URL considered a possibility for a PostGIS connection, or null
106: */
107: protected URL toCapabilitiesURL(Object data) {
108: if (data instanceof IResolve) {
109: return toCapabilitiesURL((IResolve) data);
110: } else if (data instanceof URL) {
111: return toCapabilitiesURL((URL) data);
112: } else if (data instanceof String) {
113: return toCapabilitiesURL((String) data);
114: } else if (CatalogPlugin.locateURL(data) != null) {
115: return toCapabilitiesURL(CatalogPlugin.locateURL(data));
116: } else {
117: return null; // no idea what this should be
118: }
119: }
120:
121: protected URL toCapabilitiesURL(IResolve resolve) {
122: if (resolve instanceof IService) {
123: return toCapabilitiesURL((IService) resolve);
124: }
125: return toCapabilitiesURL(resolve.getIdentifier());
126: }
127:
128: protected URL toCapabilitiesURL(IService resolve) {
129: if (resolve instanceof PostGISServiceImpl) {
130: return toCapabilitiesURL((PostGISServiceImpl) resolve);
131: }
132: return toCapabilitiesURL(resolve.getIdentifier());
133: }
134:
135: /** No further QA checks needed - we know this one works */
136: protected URL toCapabilitiesURL(PostGISServiceImpl postgis) {
137: return postgis.getIdentifier();
138: }
139:
140: /** Quick sanity check to see if url is a PostGIS url */
141: protected URL toCapabilitiesURL(URL url) {
142: if (url == null)
143: return null;
144:
145: String protocol = url.getProtocol() != null ? url.getProtocol()
146: .toLowerCase() : null;
147:
148: if (!"http".equals(protocol) //$NON-NLS-1$
149: && !"https".equals(protocol)) { //$NON-NLS-1$
150: return null;
151: }
152: if (url.toExternalForm().indexOf("postgis.jdbc") != -1) { //$NON-NLS-1$
153: return url;
154: }
155: return null;
156: }
157:
158: /** Quick sanity check to see if url is a PostGIS url String */
159: protected URL toCapabilitiesURL(String string) {
160: if (string == null)
161: return null;
162:
163: if (!string.contains("postgis.jdbc") && !string.contains("jdbc.postgis") && !string.contains("postgis")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
164: return null;
165: }
166: //jdbc.postgresql://username:password@host:port/database
167: int startindex = string.indexOf("//") + 2; //$NON-NLS-1$
168: int usernameEnd = string.indexOf(":", startindex); //$NON-NLS-1$
169: int passwordEnd = string.indexOf("@", usernameEnd); //$NON-NLS-1$
170: int hostEnd = string.indexOf(":", passwordEnd); //$NON-NLS-1$
171: int portEnd = string.indexOf("/", hostEnd); //$NON-NLS-1$
172: int databaseEnd = string.indexOf("/", portEnd + 1); //$NON-NLS-1$
173:
174: //int databaseEnd = string.indexOf(" ", databaseStart);
175: String the_host = string.substring(passwordEnd + 1, hostEnd);
176: String the_username = string.substring(startindex, usernameEnd);
177: String the_password = string.substring(usernameEnd + 1,
178: passwordEnd);
179: String the_port;
180: String the_database;
181: String the_schema;
182: if (databaseEnd < 1) {
183: databaseEnd = string.length();
184: the_schema = "public"; //$NON-NLS-1$
185: } else {
186: the_schema = string.substring(databaseEnd + 1);
187: }
188: if (portEnd < 1) {
189: the_port = string.substring(hostEnd + 1);
190: the_database = ""; //$NON-NLS-1$
191: } else {
192: the_port = string.substring(hostEnd + 1, portEnd);
193: the_database = string.substring(portEnd + 1, databaseEnd);
194: }
195: Integer intPort;
196: if (!the_port.equalsIgnoreCase("")) { //$NON-NLS-1$
197: intPort = new Integer(the_port);
198: } else {
199: intPort = new Integer(5432);
200: }
201:
202: //URL(String protocol, String host, int port, String file)
203: URL url = null;
204: try {
205: url = PostGISServiceExtension.toURL(the_username,
206: the_password, the_host, intPort, the_database,
207: the_schema);
208:
209: } catch (MalformedURLException e) {
210: // TODO Catch e
211: PostgisPlugin.log("bad url", e); //$NON-NLS-1$
212: }
213: return url;
214: }
215:
216: /** 'Create' params given the provided url, no magic occurs */
217: @SuppressWarnings("unchecked")//$NON-NLS-1$
218: protected Map<String, Serializable> createParams(URL url) {
219: PostGISServiceExtension serviceFactory = new PostGISServiceExtension();
220: Map params = serviceFactory.createParams(url);
221: if (params != null)
222: return params;
223:
224: Map<String, Serializable> params2 = new HashMap<String, Serializable>();
225:
226: params2.put(PostgisDataStoreFactory.DBTYPE.key, "postgis"); //$NON-NLS-1$
227: params2.put(PostgisDataStoreFactory.HOST.key, url.getHost());
228: String dbport = ((Integer) url.getPort()).toString();
229: try {
230: params2.put(PostgisDataStoreFactory.PORT.key, new Integer(
231: dbport));
232: } catch (NumberFormatException e) {
233: params2.put(PostgisDataStoreFactory.PORT.key, new Integer(
234: 5432));
235: }
236:
237: String the_database = url.getPath() == null ? "" : url.getPath(); //$NON-NLS-1$
238: params2.put(PostgisDataStoreFactory.DATABASE.key, the_database); // database
239: String userInfo = url.getUserInfo() == null ? "" : url.getUserInfo(); //$NON-NLS-1$
240: params2.put(PostgisDataStoreFactory.USER.key, userInfo); // user
241: params2.put(PostgisDataStoreFactory.PASSWD.key, ""); // pass //$NON-NLS-1$
242:
243: return params2;
244: }
245:
246: }
|