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.config;
006:
007: import org.geotools.data.DataStore;
008: import org.geotools.data.DataStoreFactorySpi;
009: import org.vfny.geoserver.global.dto.DataStoreInfoDTO;
010: import org.vfny.geoserver.util.DataStoreUtils;
011: import java.io.IOException;
012: import java.util.HashMap;
013: import java.util.Map;
014: import javax.servlet.ServletContext;
015:
016: /**
017: * DataStoreInfo purpose.
018: *
019: * <p>
020: * Used to describe a datastore, typically one specified in the catalog.xml
021: * config file.
022: * </p>
023: *
024: * <p></p>
025: *
026: * @author dzwiers, Refractions Research, Inc.
027: * @version $Id: DataStoreConfig.java 6326 2007-03-15 18:36:40Z jdeolive $
028: */
029: public class DataStoreConfig {
030: /** unique datasore identifier */
031: private String id;
032:
033: /** unique namespace to refer to this datastore */
034: private String nameSpaceId;
035:
036: /** wether this data store is enabled */
037: private boolean enabled = true;
038:
039: /** a short description about this data store */
040: private String title;
041:
042: /** a short description about this data store */
043: private String _abstract;
044:
045: /** connection parameters to create the DataStoreInfo */
046: private Map connectionParams;
047:
048: /** Config ONLY! DataStoreFactory used to test params */
049: private DataStoreFactorySpi factory;
050:
051: /**
052: * Create a new DataStoreConfig from a dataStoreId and factoryDescription
053: *
054: * <p>
055: * Creates a DataStoreInfo to represent an instance with default data.
056: * </p>
057: *
058: * @param dataStoreId Description of DataStore (see DataStoreUtils)
059: * @param factoryDescription DOCUMENT ME!
060: *
061: * @see defaultSettings()
062: */
063: public DataStoreConfig(String dataStoreId, String factoryDescription) {
064: this (dataStoreId, DataStoreUtils
065: .aquireFactory(factoryDescription));
066: }
067:
068: /** Creates a new DataStoreConfig for the provided factory. */
069: public DataStoreConfig(String dataStoreId,
070: DataStoreFactorySpi factory) {
071: this .factory = factory;
072: id = dataStoreId;
073: nameSpaceId = "";
074: enabled = true;
075: title = "";
076: _abstract = "";
077: connectionParams = DataStoreUtils.defaultParams(factory);
078: }
079:
080: /**
081: * DataStoreInfo constructor.
082: *
083: * <p>
084: * Creates a copy of the DataStoreInfoDTO provided. All the datastructures
085: * are cloned.
086: * </p>
087: *
088: * @param dto The datastore to copy.
089: */
090: public DataStoreConfig(DataStoreInfoDTO dto) {
091: reset(dto);
092: }
093:
094: /**
095: * Called to update Config class based on DTO information
096: *
097: * @param dto DOCUMENT ME!
098: *
099: * @throws NullPointerException DOCUMENT ME!
100: */
101: public void reset(DataStoreInfoDTO dto) {
102: if (dto == null) {
103: throw new NullPointerException(
104: "Non null DataStoreInfoDTO required");
105: }
106:
107: factory = DataStoreUtils.aquireFactory(dto
108: .getConnectionParams());
109:
110: id = dto.getId();
111: nameSpaceId = dto.getNameSpaceId();
112: enabled = dto.isEnabled();
113: _abstract = dto.getAbstract();
114: connectionParams = new HashMap(dto.getConnectionParams());
115: }
116:
117: /**
118: * Implement loadDTO.
119: *
120: * <p>
121: * Populates the data fields with the DataStoreInfoDTO provided.
122: * </p>
123: *
124: * @param ds the DataStoreInfoDTO to use.
125: *
126: * @throws NullPointerException DOCUMENT ME!
127: *
128: * @see org.vfny.geoserver.config.DataStructure#loadDTO(java.lang.Object)
129: */
130: public void update(DataStoreInfoDTO ds) {
131: if (ds == null) {
132: throw new NullPointerException(
133: "DataStoreInfo Data Transfer Object required");
134: }
135:
136: id = ds.getId();
137: nameSpaceId = ds.getNameSpaceId();
138: enabled = ds.isEnabled();
139: _abstract = ds.getAbstract();
140:
141: try {
142: connectionParams = new HashMap(ds.getConnectionParams()); //clone?
143: } catch (Exception e) {
144: connectionParams = new HashMap();
145: }
146: }
147:
148: /**
149: * Implement toDTO.
150: *
151: * <p>
152: * Create a DataStoreInfoDTO from the current config object.
153: * </p>
154: *
155: * @return The data represented as a DataStoreInfoDTO
156: *
157: * @see org.vfny.geoserver.config.DataStructure#toDTO()
158: */
159: public DataStoreInfoDTO toDTO() {
160: DataStoreInfoDTO ds = new DataStoreInfoDTO();
161: ds.setId(id);
162: ds.setNameSpaceId(nameSpaceId);
163: ds.setEnabled(enabled);
164: ds.setAbstract(_abstract);
165:
166: try {
167: ds.setConnectionParams(new HashMap(connectionParams));
168: } catch (Exception e) {
169: // default already created
170: }
171:
172: return ds;
173: }
174:
175: /**
176: * getAbstract purpose.
177: *
178: * <p>
179: * Description ...
180: * </p>
181: *
182: * @return
183: */
184: public String getAbstract() {
185: return _abstract;
186: }
187:
188: /**
189: * getConnectionParams purpose.
190: *
191: * <p>
192: * Description ...
193: * </p>
194: *
195: * @return
196: */
197: public Map getConnectionParams() {
198: return connectionParams;
199: }
200:
201: /**
202: * isEnabled purpose.
203: *
204: * <p>
205: * Description ...
206: * </p>
207: *
208: * @return
209: */
210: public boolean isEnabled() {
211: return enabled;
212: }
213:
214: /**
215: * This is the DataStore ID
216: *
217: * <p>
218: *
219: * </p>
220: *
221: * @return
222: */
223: public String getId() {
224: return id;
225: }
226:
227: /**
228: * getNameSpace purpose.
229: *
230: * <p>
231: * Description ...
232: * </p>
233: *
234: * @return
235: */
236: public String getNameSpaceId() {
237: return nameSpaceId;
238: }
239:
240: /**
241: * getTitle purpose.
242: *
243: * <p>
244: * Description ...
245: * </p>
246: *
247: * @return
248: */
249: public String getTitle() {
250: return title;
251: }
252:
253: /**
254: * setAbstract purpose.
255: *
256: * <p>
257: * Description ...
258: * </p>
259: *
260: * @param string
261: */
262: public void setAbstract(String string) {
263: if (string != null) {
264: _abstract = string;
265: }
266: }
267:
268: /**
269: * setConnectionParams purpose.
270: *
271: * <p>
272: * Description ...
273: * </p>
274: *
275: * @param map
276: */
277: public void setConnectionParams(Map map) {
278: connectionParams = map;
279: }
280:
281: /**
282: * setEnabled purpose.
283: *
284: * <p>
285: * Description ...
286: * </p>
287: *
288: * @param b
289: */
290: public void setEnabled(boolean b) {
291: enabled = b;
292: }
293:
294: /**
295: * setNameSpace purpose.
296: *
297: * <p>
298: * Description ...
299: * </p>
300: *
301: * @param support
302: */
303: public void setNameSpaceId(String support) {
304: if (support != null) {
305: nameSpaceId = support;
306: }
307: }
308:
309: /**
310: * setTitle purpose.
311: *
312: * <p>
313: * Description ...
314: * </p>
315: *
316: * @param string
317: */
318: public void setTitle(String string) {
319: if (string != null) {
320: title = string;
321: }
322: }
323:
324: // Access to Dyanmic Content
325:
326: /**
327: * It would be nice if we did not throw this away - but life is too short
328: *
329: * @return Real DataStore generated by this DataStoreConfig
330: *
331: * @throws IOException If DataStore could not be aquired
332: */
333: public DataStore findDataStore(ServletContext sc)
334: throws IOException {
335: return DataStoreUtils.acquireDataStore(connectionParams, sc);
336: }
337:
338: /**
339: * Get DataStoreFactorySpi used for this DataStoreConfig.
340: *
341: * @return DataStoreFactorySpi that this DataStoreConfig matches
342: */
343: public DataStoreFactorySpi getFactory() {
344: return factory;
345: }
346: }
|