001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.catalog;
016:
017: import java.io.IOException;
018: import java.io.Serializable;
019: import java.net.URL;
020: import java.util.Map;
021:
022: import net.refractions.udig.catalog.internal.Messages;
023:
024: import org.geotools.data.DataStoreFactorySpi;
025: import org.geotools.data.DataStoreFactorySpi.Param;
026:
027: /**
028: * A support class for creating Service Extensions based on Geotools Datastores. Provides feeback information for when the
029: * datastore cannot use the offered parameters
030: *
031: * @author Jesse
032: * @since 1.1.0
033: */
034: public abstract class AbstractDataStoreServiceExtension implements
035: ServiceExtension2 {
036:
037: protected class ParamInfo {
038:
039: public String the_schema;
040: public Integer the_port;
041: public String host;
042: public String the_database;
043: public String password;
044: public String username;
045: public String protocol;
046:
047: public ParamInfo(String protocol, String username,
048: String password, String host, String the_database,
049: Integer the_port, String the_schema) {
050: this .protocol = protocol;
051: this .username = username;
052: this .password = password;
053: this .the_database = the_database;
054: this .host = host;
055: this .the_port = the_port;
056: this .the_schema = the_schema;
057:
058: }
059:
060: }
061:
062: public final String reasonForFailure(
063: Map<String, Serializable> params) {
064: String parameterProcessingResult = processParameters(params,
065: getDataStoreFactory().getParametersInfo());
066: if (parameterProcessingResult != null)
067: return parameterProcessingResult;
068:
069: String result = doOtherChecks(params);
070: if (result != null)
071: return result;
072: return null;
073: }
074:
075: /**
076: * Do any other checks besides a basic parsing of the parameters.
077: *
078: * @param params parameters to be used for creating the datastore
079: *
080: * @return null if everything checks out
081: */
082: protected abstract String doOtherChecks(
083: Map<String, Serializable> params);
084:
085: /**
086: * Returns an instance of the datastore factory that can create the datastore.
087: *
088: * @return an instance of the datastore factory that can create the datastore.
089: */
090: protected abstract DataStoreFactorySpi getDataStoreFactory();
091:
092: private String processParameters(Map<String, Serializable> params,
093: Param[] arrayParameters) {
094: if (params == null) {
095: return Messages.DataStoreServiceExtension_nullparams;
096: }
097: for (int i = 0; i < arrayParameters.length; i++) {
098: Param param = arrayParameters[i];
099: Object value;
100: if (!params.containsKey(param.key)) {
101: if (param.required) {
102: return param.key
103: + Messages.DataStoreServiceExtension_missingKey
104: + param.description; // missing required key!
105: } else {
106: continue;
107: }
108: }
109: try {
110: value = param.lookUp(params);
111: } catch (IOException e) {
112: // could not upconvert/parse to expected type!
113: // even if this parameter is not required
114: // we are going to refuse to process
115: // these params
116: return Messages.DataStoreServiceExtension_theParam
117: + param.key
118: + Messages.DataStoreServiceExtension_wrongType
119: + param.type
120: + Messages.DataStoreServiceExtension_butWas
121: + params.get(param.key).getClass();
122: }
123: if (value == null) {
124: if (param.required) {
125: return param.key
126: + Messages.DataStoreServiceExtension_nullParam
127: + param.description;
128: }
129: } else {
130: if (!param.type.isInstance(value)) {
131: return Messages.DataStoreServiceExtension_theParam
132: + param.key
133: + Messages.DataStoreServiceExtension_wrongType
134: + param.type
135: + Messages.DataStoreServiceExtension_butWas
136: + params.get(param.key).getClass(); // value was not of the required type
137: }
138: }
139: }
140: return null;
141: }
142:
143: /**
144: * For special urls like DB urls. parses out the required information from the url.
145: * Consider:
146: * jdbc.postgis://username:password@hosr:port/database/schema.
147: * it will parse out these paarts from the url. In cases like oracle the equivalents have to be
148: * understood.
149: *
150: * @param url
151: * @return
152: */
153: protected ParamInfo parseParamInfo(URL url) {
154: String host = url.getHost();
155: if (host != null && !"".equals(host)) { //$NON-NLS-1$
156: if (host.endsWith("postgis.jdbc")) { //$NON-NLS-1$
157: host = host.substring(0, host.length() - 12);
158: }
159: }
160: Integer the_port = url.getPort() == -1 ? new Integer(5432)
161: : new Integer(url.getPort());
162: String path = url.getPath();
163: String the_database;
164: String the_schema;
165: if (path != null) {
166: int endDB = path.indexOf('/', 1);
167: the_database = path.substring(1, endDB);
168: the_schema = path.substring(endDB + 1);
169: } else {
170: the_database = ""; //$NON-NLS-1$
171: the_schema = null;
172: }
173: if (the_schema == null)
174: the_schema = "public"; //$NON-NLS-1$
175:
176: String userInfo = url.getUserInfo() == null ? "" : url.getUserInfo(); //$NON-NLS-1$
177: String username;
178: String password;
179: if (userInfo.contains(":")) { //$NON-NLS-1$
180: int indexOf = userInfo.indexOf(':', 1);
181: username = userInfo.substring(0, indexOf);
182: password = userInfo.substring(indexOf + 1, userInfo
183: .length());
184: } else {
185: username = userInfo;
186: password = ""; //$NON-NLS-1$
187: }
188: return new ParamInfo(url.getProtocol(), username, password,
189: host, the_database, the_port, the_schema);
190: }
191:
192: }
|