001: /*
002: * GeoLBS - OpenSource Location Based Servces toolkit
003: * (C) 2004, Julian J. Ray, All Rights Reserved
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: */
016:
017: package org.geotools.data.geomedia;
018:
019: import java.io.IOException;
020: import java.sql.SQLException;
021: import java.util.Map;
022: import java.util.Collections;
023: import java.util.logging.Logger;
024:
025: import org.geotools.data.DataSourceException;
026: import org.geotools.data.DataStore;
027: import org.geotools.data.DataStoreFactorySpi;
028: import org.geotools.data.jdbc.ConnectionPool;
029:
030: /**
031: * Creates a GeoMediaDataStoreFactoru based on the correct params.
032: *
033: * <p>
034: * This factory should be registered in the META-INF/ folder, under services/ in the DataStoreFactorySpi file.
035: * </p>
036: *
037: * @author Jody Garnett, Refractions Research
038: * @author Sean Geoghegan, Defence Science and Technology Organisation
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geomedia/src/main/java/org/geotools/data/geomedia/GeoMediaDataStoreFactory.java $
040: */
041: public class GeoMediaDataStoreFactory implements DataStoreFactorySpi {
042: /** A logger for logging */
043: private static final Logger LOGGER = org.geotools.util.logging.Logging
044: .getLogger("org.geotools.data.geomedia");
045:
046: /** DOCUMENT ME! */
047: private GeoMediaConnectionParam[] mConnectionParams = null;
048:
049: /**
050: * Creates a new instance of GeoMediaDataStoreFactory
051: */
052: public GeoMediaDataStoreFactory() {
053: }
054:
055: /**
056: * DOCUMENT ME!
057: *
058: * @param params DOCUMENT ME!
059: */
060: public void setConnectionParams(GeoMediaConnectionParam[] params) {
061: mConnectionParams = params;
062: }
063:
064: /**
065: * DOCUMENT ME!
066: *
067: * @return DOCUMENT ME!
068: */
069: public GeoMediaConnectionParam[] getConnectionParams() {
070: return mConnectionParams;
071: }
072:
073: /**
074: * Determines whether DataStore created by this factory can process the parameters.
075: *
076: * <p>
077: * Required Parameters are:
078: * </p>
079: *
080: * <ul>
081: * <li>
082: * <code>dbtype</code> - must equal "oracle"
083: * </li>
084: * <li>
085: * <code>host</code>
086: * </li>
087: * <li>
088: * <code>port</code>
089: * </li>
090: * <li>
091: * <code>user</code>
092: * </li>
093: * <li>
094: * <code>passwd</code>
095: * </li>
096: * <li>
097: * <code>instance</code>
098: * </li>
099: * </ul>
100: *
101: * <p>
102: * There are no defaults since each parameter must be explicitly defined by the user, or another
103: * DataSourceFactorySpi should be used. This behaviour is defined in the DataStoreFactorySpi contract.
104: * </p>
105: *
106: * @param params The parameter to check.
107: *
108: * @return True if all the required parameters are supplied.
109: */
110: public boolean canProcess(Map params) {
111: if (mConnectionParams == null) {
112: return false;
113: }
114: return params.containsKey("dbtype")
115: && params.get("dbtype").equals("geomedia")
116: && params.containsKey("dbkey")
117: && params.containsKey("dbdriver")
118: && params.containsKey("user")
119: && params.containsKey("passwd");
120:
121: //&& params.containsKey("parameters");
122: }
123:
124: /**
125: * Construct a postgis data store using the params.
126: *
127: * @param params The full set of information needed to construct a live data source. Should have dbtype equal to
128: * geomedia, as well as host, user, passwd, database, dbkey.
129: *
130: * @return The created DataSource, this may be null if the required resource was not found or if insufficent
131: * parameters were given. Note that canProcess() should have returned false if the problem is to do with
132: * insufficent parameters.
133: *
134: * @throws IOException DOCUMENT ME!
135: * @throws DataSourceException Thrown if there were any problems creating or connecting the datasource.
136: */
137: public DataStore createDataStore(Map params) throws IOException {
138: /* There are no defaults here. Calling canProcess verifies that
139: * all these variables exist.
140: */
141: String databaseDriver = (String) DBDRIVER.lookUp(params);
142: String databasePoolKey = (String) DBKEY.lookUp(params);
143:
144: String user = (String) USER.lookUp(params);
145: String passwd = (String) PASSWD.lookUp(params);
146:
147: try {
148: GeoMediaConnectionFactory factory = new GeoMediaConnectionFactory(
149: databaseDriver, databasePoolKey, mConnectionParams);
150:
151: factory.setLogin(user, passwd);
152:
153: ConnectionPool pool = factory.getConnectionPool();
154: GeoMediaDataStore dataStore = new GeoMediaDataStore(pool);
155:
156: return dataStore;
157: } catch (SQLException ex) {
158: throw new DataSourceException(ex.getMessage());
159: }
160: }
161:
162: /**
163: * Oracle cannot create a new database.
164: *
165: * @param params
166: *
167: *
168: * @throws IOException DOCUMENT ME!
169: * @throws UnsupportedOperationException Cannot create new database
170: */
171: public DataStore createNewDataStore(Map params) throws IOException {
172: throw new UnsupportedOperationException(
173: "GeoTools cannot create a new GeoMedia Database");
174: }
175:
176: public String getDisplayName() {
177: return "GeoMedia";
178: }
179:
180: /**
181: * Describe the nature of the datastore constructed by this factory.
182: *
183: * @return A human readable description that is suitable for inclusion in a list of available datasources.
184: */
185: public String getDescription() {
186: return "GeoMedia Spatial Database";
187: }
188:
189: // // TODO: have a clue
190: // public DataSourceMetadataEnity createMetadata( Map params ) throws IOException {
191: // String databaseDriver = (String) DBDRIVER.lookUp( params );
192: // String databasePoolKey = (String) DBKEY.lookUp( params );
193: //
194: // String user = (String) USER.lookUp( params );
195: //
196: // return new DataSourceMetadataEnity( databasePoolKey, "GeoMedia "+user, "GeoMedia connection to "+databasePoolKey+" as "+user );
197: // }
198: /**
199: * Test to see if this datastore is available, if it has all the
200: * appropriate libraries to construct a datastore. This datastore just
201: * returns true for now. This method is used for gui apps, so as to
202: * not advertise data store capabilities they don't actually have.
203: *
204: * @return <tt>true</tt> if and only if this factory is available to create
205: * DataStores.
206: *
207: * @task REVISIT: I'm just adding this method to compile, maintainer should
208: * revisit to check for any libraries that may be necessary for
209: * datastore creations. ch.
210: */
211: public boolean isAvailable() {
212: return true;
213: }
214:
215: static final Param DBTYPE = new Param("dbtype", String.class,
216: "This must be 'geomedia'.", true);
217: static final Param DBDRIVER = new Param("dbdriver", String.class,
218: "Class name of a Type4 Javax DataSource database driver.",
219: true);
220: static final Param USER = new Param("user", String.class,
221: "The user name to log in with.", true);
222: static final Param PASSWD = new Param("passwd", String.class,
223: "The password.", true);
224: static final Param DBKEY = new Param("databasePoolKey",
225: String.class, "the port number on the host, usually 1433",
226: true, "1433");
227: static final Param[] PARAM_INFO = new Param[] { DBTYPE, DBDRIVER,
228: USER, PASSWD };
229:
230: /**
231: * Describe parameters.
232: *
233: *
234: * @see org.geolbs.data.DataStoreFactorySpi#getParametersInfo()
235: */
236: public Param[] getParametersInfo() {
237: return PARAM_INFO;
238: }
239:
240: /**
241: * Returns the implementation hints. The default implementation returns en empty map.
242: */
243: public Map getImplementationHints() {
244: return Collections.EMPTY_MAP;
245: }
246: }
|