001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.property;
017:
018: import org.geotools.data.DataStore;
019: import org.geotools.data.DataStoreFactorySpi;
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: /**
029: * DataStore factory that creates {@linkplain org.geotools.data.property.PropertyDataStore}s
030: *
031: * @author jgarnett
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/property/src/main/java/org/geotools/data/property/PropertyDataStoreFactory.java $
033: * @version $Id: PropertyDataStoreFactory.java 27862 2007-11-12 19:51:19Z desruisseaux $
034: */
035: public class PropertyDataStoreFactory implements DataStoreFactorySpi {
036: private static final Logger LOGGER = org.geotools.util.logging.Logging
037: .getLogger(PropertyDataStoreFactory.class.getPackage()
038: .getName());
039:
040: // public DataSourceMetadataEnity createMetadata( Map params )
041: // throws IOException {
042: // if( !canProcess( params )){
043: // throw new IOException( "Provided params cannot be used to connect");
044: // }
045: // File dir = (File) DIRECTORY.lookUp( params );
046: // return new DataSourceMetadataEnity( dir, "Property file access for " + dir );
047: // }
048:
049: /** DOCUMENT ME! */
050: public static final Param DIRECTORY = new Param("directory",
051: File.class, "Directory containting property files", true);
052:
053: public static final Param NAMESPACE = new Param("namespace",
054: String.class, "namespace of datastore", false);
055:
056: /**
057: * DOCUMENT ME!
058: *
059: * @param params DOCUMENT ME!
060: *
061: * @return DOCUMENT ME!
062: *
063: * @throws IOException DOCUMENT ME!
064: */
065: public DataStore createDataStore(Map params) throws IOException {
066: File dir = directoryLookup(params);
067: String namespaceURI = (String) NAMESPACE.lookUp(params);
068: if (dir.exists() && dir.isDirectory()) {
069: return new PropertyDataStore(dir, namespaceURI);
070: } else {
071: throw new IOException("Directory is required");
072: }
073: }
074:
075: /**
076: * DOCUMENT ME!
077: *
078: * @param params DOCUMENT ME!
079: *
080: * @return DOCUMENT ME!
081: *
082: * @throws IOException DOCUMENT ME!
083: */
084: public DataStore createNewDataStore(Map params) throws IOException {
085: File dir = directoryLookup(params);
086:
087: if (dir.exists()) {
088: throw new IOException(dir + " already exists");
089: }
090:
091: boolean created;
092:
093: created = dir.mkdir();
094:
095: if (!created) {
096: throw new IOException("Could not create the directory"
097: + dir);
098: }
099:
100: String namespaceURI = (String) NAMESPACE.lookUp(params);
101: return new PropertyDataStore(dir, namespaceURI);
102: }
103:
104: /**
105: * DOCUMENT ME!
106: *
107: * @return DOCUMENT ME!
108: */
109: public String getDisplayName() {
110: return "Properties";
111: }
112:
113: /**
114: * DOCUMENT ME!
115: *
116: * @return DOCUMENT ME!
117: */
118: public String getDescription() {
119: return "Allows access to Java Property files containing Feature information";
120: }
121:
122: /**
123: * DOCUMENT ME!
124: *
125: * @return DOCUMENT ME!
126: */
127: public Param[] getParametersInfo() {
128: return new Param[] { DIRECTORY, };
129: }
130:
131: /**
132: * Test to see if this datastore is available, if it has all the
133: * appropriate libraries to construct a datastore. This datastore just
134: * returns true for now. This method is used for gui apps, so as to not
135: * advertise data store capabilities they don't actually have.
136: *
137: * @return <tt>true</tt> if and only if this factory is available to create
138: * DataStores.
139: *
140: * @task REVISIT: I'm just adding this method to compile, maintainer should
141: * revisit to check for any libraries that may be necessary for
142: * datastore creations. ch.
143: */
144: public boolean isAvailable() {
145: return true;
146: }
147:
148: /**
149: * DOCUMENT ME!
150: *
151: * @param params DOCUMENT ME!
152: *
153: * @return DOCUMENT ME!
154: */
155: public boolean canProcess(Map params) {
156: try {
157: directoryLookup(params);
158: return true;
159: } catch (Exception erp) {
160: if (LOGGER.isLoggable(Level.FINE)) {
161: LOGGER.log(Level.FINE, "can't process parameters", erp);
162: }
163: return false;
164: }
165: }
166:
167: /**
168: */
169: public Map getImplementationHints() {
170: return java.util.Collections.EMPTY_MAP;
171: }
172:
173: /**
174: * Lookups the directory containing property files in the params argument, and
175: * returns the corresponding <code>java.io.File</code>.
176: * <p>
177: * The file is first checked for existence as an absolute path in the filesystem. If
178: * such a directory is not found, then it is treated as a relative path, taking Java
179: * system property <code>"user.dir"</code> as the base.
180: * </p>
181: * @param params
182: * @throws IllegalArgumentException if directory is not a directory.
183: * @throws FileNotFoundException if directory does not exists
184: * @throws IOException if {@linkplain #DIRECTORY} doesn't find parameter in <code>params</code>
185: * file does not exists.
186: */
187: private File directoryLookup(Map params) throws IOException,
188: FileNotFoundException, IllegalArgumentException {
189: File directory = (File) DIRECTORY.lookUp(params);
190: if (!directory.exists()) {
191: File currentDir = new File(System.getProperty("user.dir"));
192: directory = new File(currentDir, (String) params
193: .get(DIRECTORY.key));
194: if (!directory.exists()) {
195: throw new FileNotFoundException(directory
196: .getAbsolutePath());
197: }
198: if (!directory.isDirectory()) {
199: throw new IllegalArgumentException(directory
200: .getAbsolutePath()
201: + " is not a directory");
202: }
203: }
204: return directory;
205: }
206: }
|