001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
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: package org.geotools.data.hsql;
017:
018: import java.io.IOException;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.geotools.data.DataStore;
026: import org.geotools.data.DataStoreFactorySpi;
027:
028: /**
029: * Creates a HsqlDataStoreFactory based on the correct params.
030: *
031: * <p>
032: * This factory should be registered in the META-INF/ folder, under services/
033: * in the DataStoreFactorySpi file.
034: * </p>
035: *
036: * @author Amr Alam, Refractions Research
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/hsql/src/main/java/org/geotools/data/hsql/HsqlDataStoreFactory.java $
038: */
039: public class HsqlDataStoreFactory implements DataStoreFactorySpi {
040: private static final Logger LOGGER = org.geotools.util.logging.Logging
041: .getLogger(HsqlDataStoreFactory.class.getName());
042:
043: /** Creates Hsql JDBC driver class. */
044: private static final String DRIVER_CLASS = "org.hsqldb.jdbcDriver";
045:
046: /** Param, package visibiity for JUnit tests */
047: public static final Param DBTYPE = new Param("dbtype",
048: String.class, "must be 'hsql'", true, "hsql");
049:
050: /** Param, package visibiity for JUnit tests */
051: public static final Param HOST = new Param("host", String.class,
052: "hsql host machine", true, "localhost");
053:
054: /** Param, package visibiity for JUnit tests */
055: public static final Param PORT = new Param("port", String.class,
056: "hsql connection port", true, "9001");
057:
058: /** Param, package visibiity for JUnit tests */
059: public static final Param DATABASE = new Param("database",
060: String.class, "hsql database");
061:
062: /** Param, package visibiity for JUnit tests */
063: public static final Param DBFILENAME = new Param("hsqlfilename",
064: String.class, "hsql database filename");
065:
066: /** Param, package visibiity for JUnit tests */
067: public static final Param USER = new Param("user", String.class,
068: "user name to login as", false);
069:
070: /** Param, package visibiity for JUnit tests */
071: public static final Param PASSWD = new Param("passwd",
072: String.class, "password used to login", false);
073:
074: /** Param, package visibiity for JUnit tests */
075: public static final Param NAMESPACE = new Param("namespace",
076: String.class, "namespace prefix used", false);
077:
078: /** Array with all of the params */
079: public static final Param[] arrayParameters = { DBTYPE, DBFILENAME,
080: USER, PASSWD, NAMESPACE };
081:
082: private static Map datastores = Collections
083: .synchronizedMap(new HashMap());
084:
085: /**
086: * Creates a new instance of HsqlDataStoreFactory
087: */
088: public HsqlDataStoreFactory() {
089: }
090:
091: /**
092: * Checks to see if all the hsql params are there.
093: *
094: * <p>
095: * Should have:
096: * </p>
097: *
098: * <ul>
099: * <li>
100: * dbtype: equal to hsql
101: * </li>
102: * <li>
103: * user
104: * </li>
105: * <li>
106: * passwd
107: * </li>
108: * <li>
109: * dbfilename
110: * </li>
111: * </ul>
112: *
113: *
114: * @param params Set of parameters needed for a hsql data store.
115: *
116: * @return <code>true</code> if dbtype equals hsql, and contains keys for
117: * host, user, passwd, and database.
118: */
119: public boolean canProcess(Map params) {
120: Object value;
121:
122: if (params != null) {
123: for (int i = 0; i < arrayParameters.length; i++) {
124: if (!(((value = params.get(arrayParameters[i].key)) != null) && (arrayParameters[i].type
125: .isInstance(value)))) {
126: if (arrayParameters[i].required) {
127: if (LOGGER.isLoggable(Level.FINE)) {
128: LOGGER.warning("Failed on : "
129: + arrayParameters[i].key);
130: LOGGER.fine(params.toString());
131: }
132:
133: return (false);
134: }
135: }
136: }
137: } else {
138: return (false);
139: }
140:
141: if ((((String) params.get("dbtype")).equalsIgnoreCase("hsql"))) {
142: return (true);
143: } else {
144: return (false);
145: }
146: }
147:
148: /**
149: * Construct a hsql data store using the params.
150: *
151: * @param params The full set of information needed to construct a live
152: * data source. Should have dbtype equal to 'hsql', as well as
153: * dbfilename, user, passwd, and namespace (optional).
154: *
155: * @return The created DataSource, this may be null if the required
156: * resource was not found or if insufficent parameters were given.
157: * Note that canProcess() should have returned false if the
158: * problem is to do with insuficent parameters.
159: *
160: * @throws IOException See DataSourceException
161: */
162: public DataStore createDataStore(Map params) throws IOException {
163: if (datastores.containsKey(params))
164: return (DataStore) datastores.get(params);
165:
166: return createNewDataStore(params);
167: }
168:
169: /**
170: * DOCUMENT ME!
171: *
172: * @param params
173: *
174: *
175: * @throws IOException See UnsupportedOperationException
176: */
177: public DataStore createNewDataStore(Map params) throws IOException {
178: // lookup will throw nice exceptions back to the client code
179: // String host = (String) HOST.lookUp(params);
180: String filename = (String) DBFILENAME.lookUp(params);
181: String user = (String) USER.lookUp(params);
182: String passwd = (String) PASSWD.lookUp(params);
183:
184: // String port = (String) PORT.lookUp(params);
185: // String database = (String) DATABASE.lookUp(params);
186: String namespace = (String) NAMESPACE.lookUp(params);
187:
188: if (!canProcess(params)) {
189: // Do this as a last sanity check.
190: LOGGER.warning("Can not process : " + params);
191: throw new IOException("The parameteres map isn't correct!!");
192: }
193:
194: HsqlConnectionFactory connFact = new HsqlConnectionFactory(
195: filename, user, passwd);
196:
197: HsqlDataStore ds;
198: if (namespace != null) {
199: ds = new HsqlDataStore(connFact, namespace);
200: } else {
201: ds = new HsqlDataStore(connFact);
202: }
203: datastores.put(params, ds);
204: return ds;
205: }
206:
207: /**
208: * DOCUMENT ME!
209: *
210: * @return "HSQL"
211: */
212: public String getDisplayName() {
213: return "HSQL";
214: }
215:
216: /**
217: * Describe the nature of the datasource constructed by this factory.
218: *
219: * @return A human readable description that is suitable for inclusion in a
220: * list of available datasources. Currently uses the string "HSQL
221: * Database"
222: */
223: public String getDescription() {
224: return "HSQL Database";
225: }
226:
227: // /**
228: // *
229: // */
230: // public DataSourceMetadataEnity createMetadata( Map params ) throws IOException {
231: // String host = (String) HOST.lookUp(params);
232: // String user = (String) USER.lookUp(params);
233: // String port = (String) PORT.lookUp(params);
234: // String database = (String) DATABASE.lookUp(params);
235: // return new DataSourceMetadataEnity( host+"port", database, "HSQL connection to "+host+" as "+user );
236: // }
237:
238: /**
239: * Test to see if this datastore is available, if it has all the
240: * appropriate libraries to construct a datastore. This datastore just
241: * returns true for now. This method is used for gui apps, so as to not
242: * advertise data store capabilities they don't actually have.
243: *
244: * @return <tt>true</tt> if and only if this factory is available to create
245: * DataStores.
246: */
247: public boolean isAvailable() {
248: try {
249: Class.forName(DRIVER_CLASS);
250: } catch (ClassNotFoundException cnfe) {
251: LOGGER.warning("HSQL data sources are not available: "
252: + cnfe.getMessage());
253:
254: return false;
255: }
256:
257: return true;
258: }
259:
260: /**
261: * Describe parameters.
262: *
263: *
264: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
265: */
266: public Param[] getParametersInfo() {
267: return new Param[] { DBTYPE, DBFILENAME, USER, PASSWD,
268: NAMESPACE };
269: }
270:
271: /**
272: * @return Returns the implementation hints. The default implementation returns en
273: * empty map.
274: */
275: public Map getImplementationHints() {
276: return Collections.EMPTY_MAP;
277: }
278:
279: }
|