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.hsql.internal;
018:
019: import java.io.File;
020: import java.io.Serializable;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import net.refractions.udig.catalog.IService;
027: import net.refractions.udig.catalog.ServiceExtension;
028:
029: import org.geotools.data.hsql.HsqlDataStoreFactory;
030:
031: /**
032: *
033: *
034: * @author Amr Alam, Refractions Research
035: */
036: public class HsqlServiceExtension implements ServiceExtension {
037:
038: private static HsqlDataStoreFactory hsqlDSFactory;
039:
040: public static HsqlDataStoreFactory getHsqlDSFactory() {
041: if (hsqlDSFactory == null)
042: hsqlDSFactory = new HsqlDataStoreFactory();
043: return hsqlDSFactory;
044: }
045:
046: public IService createService(URL id,
047: Map<String, Serializable> params) {
048:
049: if (!getFactory().canProcess(params))
050: return null;
051:
052: if (id == null) {
053: String dbFilename = (String) params.get(getFactory()
054: .getParametersInfo()[1].key);
055: try {
056: return new HsqlServiceImpl(new URL(
057: "file://" + dbFilename), params); //$NON-NLS-1$
058: } catch (MalformedURLException e) {
059: // log this?
060: e.printStackTrace();
061: return null;
062: }
063:
064: }
065:
066: return new HsqlServiceImpl(id, params);
067: }
068:
069: public Map<String, Serializable> createParams(URL url) {
070: if (!url.getFile().endsWith(".hsql")) //$NON-NLS-1$
071: return null;
072: HashMap<String, Serializable> params = new HashMap<String, Serializable>();
073: params.put(HsqlDataStoreFactory.DBTYPE.key, "hsql"); //$NON-NLS-1$
074: params.put(HsqlDataStoreFactory.DBFILENAME.key, url
075: .toExternalForm());
076: params.put(HsqlDataStoreFactory.NAMESPACE.key,
077: "http://udig.refractions.net"); //$NON-NLS-1$
078: params.put(HsqlDataStoreFactory.USER.key, "sa"); //$NON-NLS-1$
079: params.put(HsqlDataStoreFactory.PASSWD.key, ""); //$NON-NLS-1$
080:
081: File f = new File(url.getFile());
082: if (f.exists()) {
083: if (!f.isFile())
084: return null;
085: }
086: if (getHsqlDSFactory().canProcess(params)) {
087: return params;
088: }
089: return null;
090: }
091:
092: private static HsqlDataStoreFactory factory = null;
093:
094: /**
095: *
096: * @return x
097: */
098: protected static HsqlDataStoreFactory getFactory() {
099: if (factory == null) {
100: factory = new HsqlDataStoreFactory();
101: }
102: return factory;
103: }
104:
105: }
|