001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2005, 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.internal.db2;
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.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.Map.Entry;
027:
028: import net.refractions.udig.catalog.AbstractDataStoreServiceExtension;
029: import net.refractions.udig.catalog.IService;
030: import net.refractions.udig.catalog.ServiceExtension;
031: import net.refractions.udig.catalog.db2.internal.Messages;
032:
033: import org.geotools.data.DataStoreFactorySpi;
034: import org.geotools.data.db2.DB2DataStoreFactory;
035:
036: /**
037: * DB2 service extension implementation
038: *
039: * @author Justin Deoliveira,Refractions Research Inc.,jdeolive@refractions.net
040: * @since 1.0.1
041: */
042: public class DB2ServiceExtension extends
043: AbstractDataStoreServiceExtension implements ServiceExtension {
044: private static DB2DataStoreFactory factory = null;
045:
046: public static DB2DataStoreFactory getFactory() {
047: if (factory == null) {
048: factory = new DB2DataStoreFactory();
049: }
050: return factory;
051: }
052:
053: public IService createService(URL id2,
054: Map<String, Serializable> params) {
055: URL id = id2;
056: if (!getFactory().isAvailable())
057: return null;
058: // We expect the port value (key '3') to be a String but some of the extensions (ArcServiceExtension)
059: // change this from a String to an Integer which causes us to fail.
060: // In order to cope with this, we make a local copy of the parameters and force the port
061: // value to be a String.
062: Map<String, Serializable> paramsLocal = new HashMap<String, Serializable>();
063: Set<Entry<String, Serializable>> entries = params.entrySet();
064: Iterator<Entry<String, Serializable>> it = entries.iterator();
065: while (it.hasNext()) {
066: Entry<String, Serializable> entry = it.next();
067: paramsLocal
068: .put(entry.getKey(), entry.getValue().toString());
069: }
070: if (!getFactory().canProcess(paramsLocal))
071: return null;
072: if (id == null) {
073: id = paramsToUrl(paramsLocal);
074: }
075: if (id == null) {
076: return null; // should we actually throw an exception?
077: }
078: return new DB2Service(id, params);
079: }
080:
081: /**
082: * Returns the database parameter values as a pseudo-URL.
083: * @param params
084: * @return a pseudo-URL value
085: */
086: protected URL paramsToUrl(Map<String, Serializable> params) {
087: URL dbUrl = null;
088: String host = (String) params.get(getFactory()
089: .getParametersInfo()[1].key);
090: String port = (String) params.get(getFactory()
091: .getParametersInfo()[2].key);
092: String db = (String) params.get(getFactory()
093: .getParametersInfo()[3].key);
094: try {
095: dbUrl = new URL(
096: "http://" + host + ".db2.jdbc:" + port + "/" + db); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
097: } catch (MalformedURLException e) {
098: // log this?
099: e.printStackTrace();
100: }
101: return dbUrl;
102: }
103:
104: public Map<String, Serializable> createParams(URL url) {
105: if (!isDB2URL(url)) {
106: return null;
107: }
108:
109: ParamInfo info = parseParamInfo(url);
110:
111: Map<String, Serializable> params = new HashMap<String, Serializable>();
112: params.put(getFactory().getParametersInfo()[0].key, "db2"); // dbtype //$NON-NLS-1$
113: params.put(getFactory().getParametersInfo()[1].key, info.host); // host
114: params.put(getFactory().getParametersInfo()[2].key,
115: info.the_port); // port
116: params.put(getFactory().getParametersInfo()[3].key,
117: info.the_database); // database
118: params.put(getFactory().getParametersInfo()[4].key,
119: info.username); // user
120: params.put(getFactory().getParametersInfo()[5].key,
121: info.password); // pass
122: return params;
123: }
124:
125: /** A couple quick checks on the url
126: * @param url
127: * @return true if this is a DB2 URL
128: * */
129: private static final boolean isDB2URL(URL url) {
130: if (url == null)
131: return false;
132: return url.getProtocol().toLowerCase().equals("db2") || url.getProtocol().toLowerCase().equals("db2.jdbc") || //$NON-NLS-1$ //$NON-NLS-2$
133: url.getProtocol().toLowerCase().equals("jdbc.db2"); //$NON-NLS-1$
134: }
135:
136: @Override
137: protected String doOtherChecks(Map<String, Serializable> params) {
138: return null;
139: }
140:
141: @Override
142: protected DataStoreFactorySpi getDataStoreFactory() {
143: return getFactory();
144: }
145:
146: public String reasonForFailure(URL url) {
147: if (url == null)
148: return Messages.DB2ServiceExtension_nullURL;
149: if (!isDB2URL(url))
150: return Messages.DB2ServiceExtension_notDB2URL;
151: return reasonForFailure(createParams(url));
152: }
153: }
|