001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog;
018:
019: import java.io.Serializable;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import net.refractions.udig.catalog.internal.postgis.PostGISServiceImpl;
026: import net.refractions.udig.catalog.internal.postgis.PostgisPlugin;
027: import net.refractions.udig.catalog.postgis.internal.Messages;
028: import net.refractions.udig.core.internal.CorePlugin;
029:
030: import org.geotools.data.DataStoreFactorySpi;
031: import org.geotools.data.postgis.PostgisDataStoreFactory;
032:
033: /**
034: * PostGis ServiceExtension
035: * @author David Zwiers, Refractions Research
036: * @since 0.6
037: */
038: public class PostGISServiceExtension extends
039: AbstractDataStoreServiceExtension implements ServiceExtension2 {
040:
041: /**
042: * @param id
043: * @param params
044: * @return x
045: */
046: public IService createService(URL id,
047: Map<String, Serializable> params) {
048: if (params != null
049: && params
050: .containsKey(getFactory().getParametersInfo()[2].key)
051: && params.get(getFactory().getParametersInfo()[2].key) instanceof String) {
052: String val = (String) params.get(getFactory()
053: .getParametersInfo()[2].key);
054: params.remove(val);
055: params.put(getFactory().getParametersInfo()[2].key,
056: new Integer(val));
057: }
058: if (!getFactory().canProcess(params))
059: return null;
060: if (id == null) {
061: try {
062: URL toURL = toURL(params);
063: return new PostGISServiceImpl(toURL, params);
064: } catch (MalformedURLException e) {
065: PostgisPlugin.log(
066: "Unable to construct proper service URL.", e); //$NON-NLS-1$
067: return null;
068: }
069: }
070: return new PostGISServiceImpl(id, params);
071: }
072:
073: public static URL toURL(Map<String, Serializable> params)
074: throws MalformedURLException {
075: String the_host = (String) params.get(PostGISServiceExtension
076: .getFactory().getParametersInfo()[1].key);
077: Integer intPort = (Integer) params.get(PostGISServiceExtension
078: .getFactory().getParametersInfo()[2].key);
079: String the_schema = (String) params.get(PostGISServiceExtension
080: .getFactory().getParametersInfo()[3].key);
081: String the_database = (String) params
082: .get(PostGISServiceExtension.getFactory()
083: .getParametersInfo()[4].key);
084: String the_username = (String) params
085: .get(PostGISServiceExtension.getFactory()
086: .getParametersInfo()[3].key);
087: String the_password = (String) params
088: .get(PostGISServiceExtension.getFactory()
089: .getParametersInfo()[4].key);
090:
091: URL toURL = toURL(the_username, the_password, the_host,
092: intPort, the_database, the_schema);
093: return toURL;
094: }
095:
096: /**
097: * This is a guess ...
098: *
099: * @see net.refractions.udig.catalog.ServiceExtension#createParams(java.net.URL)
100: * @param url
101: * @return x
102: */
103: public Map<String, Serializable> createParams(URL url) {
104: if (!isPostGIS(url)) {
105: return null;
106: }
107:
108: ParamInfo info = parseParamInfo(url);
109:
110: Map<String, Serializable> params = new HashMap<String, Serializable>();
111: params.put(getFactory().getParametersInfo()[0].key, "postgis"); // dbtype //$NON-NLS-1$
112: params.put(getFactory().getParametersInfo()[5].key,
113: info.username); // user
114: params.put(getFactory().getParametersInfo()[6].key,
115: info.password); // pass
116: params.put(getFactory().getParametersInfo()[1].key, info.host); // host
117: params.put(getFactory().getParametersInfo()[4].key,
118: info.the_database); // database
119: params.put(getFactory().getParametersInfo()[2].key,
120: info.the_port); // port
121: params.put(getFactory().getParametersInfo()[3].key,
122: info.the_schema); // database
123: return params;
124: }
125:
126: private static PostgisDataStoreFactory factory = null;
127:
128: /**
129: * @return x
130: */
131: public static PostgisDataStoreFactory getFactory() {
132: if (factory == null) {
133: // factory = new PostgisDataStoreFactory();
134: factory = new PostgisDataStoreFactory();
135: }
136: return factory;
137: }
138:
139: /** A couple quick checks on the url */
140: public static final boolean isPostGIS(URL url) {
141: if (url == null)
142: return false;
143: return url.getProtocol().toLowerCase().equals("postgis") || url.getProtocol().toLowerCase().equals("postgis.jdbc") || //$NON-NLS-1$ //$NON-NLS-2$
144: url.getProtocol().toLowerCase().equals("jdbc.postgis"); //$NON-NLS-1$
145: }
146:
147: public static URL toURL(String the_username, String the_password,
148: String the_host, Integer intPort, String the_database,
149: String the_schema) throws MalformedURLException {
150: String the_spec = "postgis.jdbc://" + the_username //$NON-NLS-1$
151: + ":" + the_password + "@" + the_host //$NON-NLS-1$ //$NON-NLS-2$
152: + ":" + intPort + "/" + the_database //$NON-NLS-1$ //$NON-NLS-2$
153: + "/" + the_schema; //$NON-NLS-1$
154: return toURL(the_spec);
155: }
156:
157: public static URL toURL(String the_spec)
158: throws MalformedURLException {
159: return new URL(null, the_spec, CorePlugin.RELAXED_HANDLER);
160: }
161:
162: @Override
163: protected String doOtherChecks(Map<String, Serializable> params) {
164: return null;
165: }
166:
167: @Override
168: protected DataStoreFactorySpi getDataStoreFactory() {
169: return getFactory();
170: }
171:
172: public String reasonForFailure(URL url) {
173: if (!isPostGIS(url))
174: return Messages.PostGISServiceExtension_badURL;
175: return reasonForFailure(createParams(url));
176: }
177:
178: }
|