001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.util;
006:
007: import com.vividsolutions.jts.geom.Envelope;
008: import org.geoserver.feature.FeatureSourceUtils;
009: import org.geoserver.feature.retype.RetypingDataStore;
010: import org.geotools.data.DataStore;
011: import org.geotools.data.DataStoreFactorySpi;
012: import org.geotools.data.DataStoreFactorySpi.Param;
013: import org.geotools.data.DataStoreFinder;
014: import org.geotools.data.FeatureSource;
015: import org.vfny.geoserver.global.DataStoreInfo;
016: import org.vfny.geoserver.global.GeoserverDataDirectory;
017: import java.io.File;
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import javax.servlet.ServletContext;
025:
026: /**
027: * A collecitno of utilties for dealing with GeotTools DataStore.
028: *
029: * @author Richard Gould, Refractions Research, Inc.
030: * @author $Author: cholmesny $ (last modification)
031: * @version $Id: DataStoreUtils.java 7428 2007-08-14 15:01:59Z aaime $
032: */
033: public abstract class DataStoreUtils {
034:
035: /**
036: * Uses the standard datastore factory mechanism, but first manipulates the
037: * specified parameters so that data dir relative paths gets turned into
038: * absolute ones
039: * @param params
040: * @param sc
041: * @return
042: * @throws IOException
043: */
044: public static DataStore acquireDataStore(Map params,
045: ServletContext sc) throws IOException {
046: //DJB: changed this for geoserver_data_dir
047: //String baseDir = sc.getRealPath("/");
048: File baseDir = GeoserverDataDirectory
049: .getGeoserverDataDirectory();
050:
051: return getDataStore(getParams(params, baseDir.getAbsolutePath()));
052: }
053:
054: /**
055: * Looks up the datastore using the given params, verbatim, and then
056: * eventually wraps it into a renaming wrapper so that feature type
057: * names are good ones from the wfs point of view (that is, no ":" in the type names)
058: * @param params
059: * @return
060: */
061: public static DataStore getDataStore(Map params) throws IOException {
062: DataStore store = DataStoreFinder.getDataStore(params);
063: if (store == null)
064: return null;
065:
066: String[] names = store.getTypeNames();
067: for (int i = 0; i < names.length; i++) {
068: if (names[i].indexOf(":") >= 0)
069: return new RetypingDataStore(store);
070: }
071: return store;
072: }
073:
074: public static Map getParams(Map m, ServletContext sc) {
075: File data_dir = GeoserverDataDirectory
076: .getGeoserverDataDirectory();
077: String baseDir = data_dir.getPath();
078:
079: return getParams(m, baseDir);
080: }
081:
082: /**
083: * Get Connect params.
084: * <p>
085: * This is used to smooth any relative path kind of issues for any
086: * file URLS. This code should be expanded to deal with any other context
087: * sensitve isses dataStores tend to have.
088: * </p>
089: */
090: protected static Map getParams(Map m, String baseDir) {
091: return DataStoreInfo.getParams(m, baseDir);
092: }
093:
094: /**
095: * When loading from DTO use the params to locate factory.
096: *
097: * <p>
098: * bleck
099: * </p>
100: *
101: * @param params
102: *
103: * @return
104: */
105: public static DataStoreFactorySpi aquireFactory(Map params) {
106: for (Iterator i = DataStoreFinder.getAvailableDataStores(); i
107: .hasNext();) {
108: DataStoreFactorySpi factory = (DataStoreFactorySpi) i
109: .next();
110:
111: if (factory.canProcess(params)) {
112: return factory;
113: }
114: }
115:
116: return null;
117: }
118:
119: /**
120: * After user has selected Description can aquire Factory based on
121: * display name.
122: *
123: * <p>
124: * Use factory for:
125: * </p>
126: *
127: * <ul>
128: * <li>
129: * List of Params (attrb name, help text)
130: * </li>
131: * <li>
132: * Checking user's input with factory.canProcess( params )
133: * </li>
134: * </ul>
135: *
136: *
137: * @param diplayName
138: *
139: * @return
140: */
141: public static DataStoreFactorySpi aquireFactory(String displayName) {
142: for (Iterator i = DataStoreFinder.getAvailableDataStores(); i
143: .hasNext();) {
144: DataStoreFactorySpi factory = (DataStoreFactorySpi) i
145: .next();
146:
147: if (factory.getDisplayName().equals(displayName)) {
148: return factory;
149: }
150:
151: if (factory.getClass().toString().equals(displayName)) {
152: return factory;
153: }
154: }
155:
156: return null;
157: }
158:
159: /**
160: * Utility method for finding Params
161: *
162: * @param factory DOCUMENT ME!
163: * @param key DOCUMENT ME!
164: *
165: * @return DOCUMENT ME!
166: */
167: public static Param find(DataStoreFactorySpi factory, String key) {
168: return find(factory.getParametersInfo(), key);
169: }
170:
171: /**
172: * Utility methods for find param by key
173: *
174: * @param params DOCUMENT ME!
175: * @param key DOCUMENT ME!
176: *
177: * @return DOCUMENT ME!
178: */
179: public static Param find(Param[] params, String key) {
180: for (int i = 0; i < params.length; i++) {
181: if (key.equalsIgnoreCase(params[i].key)) {
182: return params[i];
183: }
184: }
185:
186: return null;
187: }
188:
189: /**
190: * Returns the descriptions for the available DataStores.
191: *
192: * <p>
193: * Arrrg! Put these in the select box.
194: * </p>
195: *
196: * @return Descriptions for user to choose from
197: */
198: public static List listDataStoresDescriptions() {
199: List list = new ArrayList();
200:
201: for (Iterator i = DataStoreFinder.getAvailableDataStores(); i
202: .hasNext();) {
203: DataStoreFactorySpi factory = (DataStoreFactorySpi) i
204: .next();
205: list.add(factory.getDisplayName());
206: }
207:
208: return list;
209: }
210:
211: public static Map defaultParams(String description) {
212: return defaultParams(aquireFactory(description));
213: }
214:
215: public static Map defaultParams(DataStoreFactorySpi factory) {
216: Map defaults = new HashMap();
217: Param[] params = factory.getParametersInfo();
218:
219: for (int i = 0; i < params.length; i++) {
220: Param param = params[i];
221: String key = param.key;
222: String value = null;
223:
224: //if (param.required ) {
225: if (param.sample != null) {
226: // Required params may have nice sample values
227: //
228: value = param.text(param.sample);
229: }
230:
231: if (value == null) {
232: // or not
233: value = "";
234: }
235:
236: //}
237: if (value != null) {
238: defaults.put(key, value);
239: }
240: }
241:
242: return defaults;
243: }
244:
245: /**
246: * Convert map to real values based on factory Params.
247: *
248: * <p>
249: * The resulting map should still be checked with factory.acceptsMap( map )
250: * </p>
251: *
252: * @param factory
253: * @param params
254: *
255: * @return Map with real values that may be acceptable to Factory
256: *
257: * @throws IOException DOCUMENT ME!
258: */
259: public static Map toConnectionParams(DataStoreFactorySpi factory,
260: Map params) throws IOException {
261: Map map = new HashMap(params.size());
262:
263: Param[] info = factory.getParametersInfo();
264:
265: // Convert Params into the kind of Map we actually need
266: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
267: String key = (String) i.next();
268:
269: Object value = find(info, key).lookUp(params);
270:
271: if (value != null) {
272: map.put(key, value);
273: }
274: }
275:
276: return map;
277: }
278:
279: /**
280: * @deprecated use {@link org.geoserver.feature.FeatureSourceUtils#
281: */
282: public static Envelope getBoundingBoxEnvelope(FeatureSource fs)
283: throws IOException {
284: return FeatureSourceUtils.getBoundingBoxEnvelope(fs);
285: }
286: }
|