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.internal.oracle;
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.AbstractDataStoreServiceExtension;
026: import net.refractions.udig.catalog.IService;
027: import net.refractions.udig.catalog.ServiceExtension2;
028: import net.refractions.udig.catalog.oracle.internal.Messages;
029:
030: import org.geotools.data.DataStoreFactorySpi;
031: import org.geotools.data.oracle.OracleDataStoreFactory;
032:
033: /**
034: * Oracle Service Extension implementation.
035: *
036: * @author David Zwiers, Refractions Research
037: * @since 0.6
038: */
039: public class OracleServiceExtension extends
040: AbstractDataStoreServiceExtension implements ServiceExtension2 {
041: /**
042: *
043: * @see net.refractions.udig.catalog.ServiceExtension#createService(java.net.URI, java.util.Map)
044: * @param id
045: * @param params
046: * @return
047: */
048: public IService createService(URL id,
049: Map<String, Serializable> params) {
050: try {
051: if (!getFactory().canProcess(params))
052: return null;
053: } catch (Exception e) {
054: return null;
055: }
056: if (id == null) {
057: String host = (String) params.get(getFactory()
058: .getParametersInfo()[1].key);
059: String port = params.get(
060: getFactory().getParametersInfo()[2].key).toString();
061: String db = (String) params.get(getFactory()
062: .getParametersInfo()[5].key);
063: params.put(getFactory().getParametersInfo()[2].key, Integer
064: .valueOf(port));
065: try {
066: return new OracleServiceImpl(
067: new URL(
068: "http://" + host + ".oracle.jdbc:" + port + "/" + db), params); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
069: } catch (MalformedURLException e) {
070: // log this?
071: OraclePlugin.log(null, e);
072: return null;
073: }
074: }
075: return new OracleServiceImpl(id, params);
076: }
077:
078: /**
079: * This is a guess ...
080: *
081: * @see net.refractions.udig.catalog.ServiceExtension#createParams(java.net.URL)
082: * @param url
083: * @return
084: */
085: public Map<String, Serializable> createParams(URL url) {
086: if (!isOracle(url)) {
087: return null;
088: }
089: ParamInfo info = parseParamInfo(url);
090:
091: Map<String, Serializable> params = new HashMap<String, Serializable>();
092: params.put(getFactory().getParametersInfo()[0].key, "oracle"); // dbtype //$NON-NLS-1$
093: params.put(getFactory().getParametersInfo()[1].key, info.host); // host
094: params.put(getFactory().getParametersInfo()[2].key,
095: info.the_port); // port
096: params.put(getFactory().getParametersInfo()[3].key,
097: info.username); // user
098: params.put(getFactory().getParametersInfo()[4].key,
099: info.password); // pass
100: params.put(getFactory().getParametersInfo()[5].key,
101: info.the_database); // database
102:
103: return params;
104: }
105:
106: private static OracleDataStoreFactory factory = null;
107:
108: protected static OracleDataStoreFactory getFactory() {
109: if (factory == null) {
110: factory = new OracleDataStoreFactory();
111: }
112: return factory;
113: }
114:
115: /** A couple quick checks on the url
116: * This should perhaps do more, but I can't think of a good test that
117: * will tell me without a doubt that the url is an Oracle url.
118: */
119: private static final boolean isOracle(URL url) {
120: if (url == null)
121: return false;
122: return url.getProtocol().toLowerCase().equals("oracle") || url.getProtocol().toLowerCase().equals("oracle.jdbc") || //$NON-NLS-1$ //$NON-NLS-2$
123: url.getProtocol().toLowerCase().equals("jdbc.oracle"); //$NON-NLS-1$
124: }
125:
126: @Override
127: protected String doOtherChecks(Map<String, Serializable> params) {
128: return null;
129: }
130:
131: @Override
132: protected DataStoreFactorySpi getDataStoreFactory() {
133: return getFactory();
134: }
135:
136: public String reasonForFailure(URL url) {
137: if (!isOracle(url)) {
138: return Messages.OracleServiceExtension_badUrl;
139: }
140: return reasonForFailure(createParams(url));
141: }
142: }
|