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.global;
006:
007: import org.geotools.coverage.grid.io.AbstractGridFormat;
008: import org.geotools.factory.Hints;
009: import org.opengis.coverage.grid.Format;
010: import org.opengis.coverage.grid.GridCoverageReader;
011: import org.opengis.parameter.InvalidParameterValueException;
012: import org.opengis.parameter.ParameterNotFoundException;
013: import org.vfny.geoserver.global.dto.CoverageStoreInfoDTO;
014: import org.vfny.geoserver.util.CoverageStoreUtils;
015: import java.io.File;
016: import java.lang.ref.SoftReference;
017: import java.util.HashMap;
018: import java.util.Map;
019: import java.util.NoSuchElementException;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: /**
024: * This is the configuration iformation for one coverage Format.
025: *
026: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
027: * modification)
028: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
029: * modification)
030: */
031: public final class CoverageStoreInfo extends GlobalLayerSupertype {
032: /** for logging */
033: private static final Logger LOGGER = org.geotools.util.logging.Logging
034: .getLogger(CoverageStoreInfo.class.toString());
035:
036: /**
037: * CoverageStoreInfo we are representing
038: */
039: private Format format = null;
040:
041: /**
042: * ref to the parent class's collection
043: */
044: private Data data;
045:
046: /**
047: *
048: */
049: private String id;
050: private SoftReference reader = null;
051: private SoftReference hintReader = null;
052:
053: /**
054: *
055: */
056: private String nameSpaceId;
057:
058: /**
059: *
060: */
061: private String type;
062:
063: /**
064: *
065: */
066: private String url;
067: private boolean enabled;
068:
069: /**
070: *
071: */
072: private String title;
073: private String _abstract;
074:
075: /**
076: * Storage for metadata
077: */
078: private Map meta;
079:
080: /**
081: * CoverageStoreInfo constructor.
082: *
083: * <p>
084: * Stores the specified data for later use.
085: * </p>
086: *
087: * @param config
088: * CoverageStoreInfoDTO the current configuration to use.
089: * @param data
090: * Data a ref to use later to look up related informtion
091: */
092: public CoverageStoreInfo(CoverageStoreInfoDTO config, Data data) {
093: this .data = data;
094: meta = new HashMap(10);
095: enabled = config.isEnabled();
096: id = config.getId();
097: nameSpaceId = config.getNameSpaceId();
098: type = config.getType();
099: url = config.getUrl();
100: title = config.getTitle();
101: _abstract = config.getAbstract();
102: format = lookupFormat();
103: }
104:
105: private Format lookupFormat() {
106: final int length = CoverageStoreUtils.formats.length;
107:
108: for (int i = 0; i < length; i++) {
109: if (CoverageStoreUtils.formats[i].getName().equals(type)) {
110: return CoverageStoreUtils.formats[i];
111: }
112: }
113:
114: return null;
115: }
116:
117: /**
118: * toDTO purpose.
119: *
120: * <p>
121: * This method is package visible only, and returns a reference to the
122: * GeoServerDTO. This method is unsafe, and should only be used with extreme
123: * caution.
124: * </p>
125: *
126: * @return CoverageStoreInfoDTO the generated object
127: */
128: Object toDTO() {
129: CoverageStoreInfoDTO dto = new CoverageStoreInfoDTO();
130: dto.setAbstract(_abstract);
131: dto.setEnabled(enabled);
132: dto.setId(id);
133: dto.setNameSpaceId(nameSpaceId);
134: dto.setType(type);
135: dto.setUrl(url);
136: dto.setTitle(title);
137:
138: return dto;
139: }
140:
141: /**
142: * getId purpose.
143: *
144: * <p>
145: * Returns the format's id.
146: * </p>
147: *
148: * @return String the id.
149: */
150: public String getId() {
151: return id;
152: }
153:
154: // /**
155: // * Get Connect params.
156: // *
157: // * @return DOCUMENT ME!
158: // */
159: //
160: // public static Map getParams(Map m, String baseDir) {
161: // Map params = Collections.synchronizedMap(new HashMap(m));
162: //
163: // for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
164: // Map.Entry entry = (Map.Entry) i.next();
165: // String key = (String) entry.getKey();
166: // Object value = entry.getValue();
167: //
168: // try {
169: // if ("url".equals(key) && value instanceof String) {
170: // String path = (String) value;
171: // if (LOGGER.isLoggable(Level.INFO)) {
172: // LOGGER.info("in string url");
173: // }
174: // if (path.startsWith("file:data/")) {
175: // path = path.substring(5); // remove 'file:' prefix
176: //
177: // File file = new File(baseDir, path);
178: // entry.setValue(file.toURL().toExternalForm());
179: // }
180: // // Not sure about this
181: // } else if (value instanceof URL
182: // && ((URL) value).getProtocol().equals("file")) {
183: // if (LOGGER.isLoggable(Level.INFO)) {
184: // LOGGER.info("in URL url");
185: // }
186: // URL url = (URL) value;
187: // String path = url.getPath();
188: // if (LOGGER.isLoggable(Level.INFO)) {
189: // LOGGER.info(new StringBuffer("path is ").append(path)
190: // .toString());
191: // }
192: // if (path.startsWith("data/")) {
193: // File file = new File(baseDir, path);
194: // entry.setValue(file.toURL());
195: // }
196: // }
197: // } catch (MalformedURLException ignore) {
198: // // ignore attempt to fix relative paths
199: // }
200: // }
201: //
202: // return params;
203: // }
204:
205: /**
206: * DOCUMENT ME !
207: *
208: * @return Format
209: *
210: * @throws IllegalStateException
211: * if this CoverageStoreInfo is disabled by configuration
212: * @throws NoSuchElementException
213: * if no CoverageStoreInfo is found
214: */
215: public Format getFormat() throws IllegalStateException,
216: NoSuchElementException {
217: if (!isEnabled()) {
218: throw new IllegalStateException(
219: "this format is not enabled, check your configuration");
220: }
221:
222: if (format == null) {
223: LOGGER.warning("failed to establish connection with "
224: + toString());
225: throw new NoSuchElementException(
226: "No format found capable of managing " + toString());
227: }
228:
229: return format;
230: }
231:
232: /**
233: * getTitle purpose.
234: *
235: * <p>
236: * Returns the dataStore's title.
237: * </p>
238: *
239: * @return String the title.
240: */
241: public String getTitle() {
242: return title;
243: }
244:
245: /**
246: * getAbstract purpose.
247: *
248: * <p>
249: * Returns the dataStore's abstract.
250: * </p>
251: *
252: * @return String the abstract.
253: */
254: public String getAbstract() {
255: return _abstract;
256: }
257:
258: /**
259: * isEnabled purpose.
260: *
261: * <p>
262: * Returns true when the data store is enabled.
263: * </p>
264: *
265: * @return true when the data store is enabled.
266: */
267: public boolean isEnabled() {
268: return enabled;
269: }
270:
271: /**
272: * Implement toString.
273: *
274: * @return String
275: *
276: * @see java.lang.Object#toString()
277: */
278: public String toString() {
279: return new StringBuffer("FormatConfig[type=").append(getType())
280: .append(", enabled=").append(isEnabled()).append(
281: ", abstract=").append(getAbstract())
282: .append("]").toString();
283: }
284:
285: /**
286: * Implement containsMetaData.
287: *
288: * @param key
289: *
290: * @return
291: *
292: * @see org.geotools.data.MetaData#containsMetaData(java.lang.String)
293: */
294: public boolean containsMetaData(String key) {
295: return meta.containsKey(key);
296: }
297:
298: /**
299: * Implement putMetaData.
300: *
301: * @param key
302: * @param value
303: *
304: * @see org.geotools.data.MetaData#putMetaData(java.lang.String,
305: * java.lang.Object)
306: */
307: public void putMetaData(String key, Object value) {
308: meta.put(key, value);
309: }
310:
311: /**
312: * Implement getMetaData.
313: *
314: * @param key
315: *
316: * @return
317: *
318: * @see org.geotools.data.MetaData#getMetaData(java.lang.String)
319: */
320: public Object getMetaData(String key) {
321: return meta.get(key);
322: }
323:
324: /**
325: * @return Returns the type.
326: */
327: public String getType() {
328: return type;
329: }
330:
331: /**
332: * @return Returns the url.
333: */
334: public String getUrl() {
335: return url;
336: }
337:
338: /**
339: * getNameSpace purpose.
340: *
341: * @return NameSpaceInfo the namespace for this format.
342: */
343: public NameSpaceInfo getNameSpace() {
344: return (NameSpaceInfo) data.getNameSpace(getNamesSpacePrefix());
345: }
346:
347: /**
348: * Access namespace id
349: *
350: * @return DOCUMENT ME!
351: */
352: public String getNamesSpacePrefix() {
353: return nameSpaceId;
354: }
355:
356: public synchronized GridCoverageReader getReader() {
357: if ((reader != null) && (reader.get() != null)) {
358: return (GridCoverageReader) reader.get();
359: }
360:
361: try {
362: // /////////////////////////////////////////////////////////
363: //
364: // Getting coverage config
365: //
366: // /////////////////////////////////////////////////////////
367: final CoverageStoreInfo gcInfo = data.getFormatInfo(id);
368:
369: if (gcInfo == null) {
370: return null;
371: }
372:
373: // /////////////////////////////////////////////////////////
374: //
375: // Getting coverage reader using the format and the real path.
376: //
377: // /////////////////////////////////////////////////////////
378: final File obj = GeoserverDataDirectory.findDataFile(gcInfo
379: .getUrl());
380:
381: // XXX CACHING READERS HERE
382: reader = new SoftReference(((AbstractGridFormat) gcInfo
383: .getFormat()).getReader(obj));
384:
385: return (GridCoverageReader) reader.get();
386: } catch (InvalidParameterValueException e) {
387: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
388: } catch (ParameterNotFoundException e) {
389: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
390: } catch (IllegalArgumentException e) {
391: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
392: } catch (SecurityException e) {
393: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
394: }
395:
396: return null;
397: }
398:
399: public synchronized GridCoverageReader createReader(Hints hints) {
400: if ((hintReader != null) && (hintReader.get() != null)) {
401: return (GridCoverageReader) hintReader.get();
402: } else if ((hints == null)
403: && ((reader != null) && (reader.get() != null))) {
404: return (GridCoverageReader) reader.get();
405: }
406:
407: try {
408: // /////////////////////////////////////////////////////////
409: //
410: // Getting coverage config
411: //
412: // /////////////////////////////////////////////////////////
413: final CoverageStoreInfo gcInfo = data.getFormatInfo(id);
414:
415: if (gcInfo == null) {
416: return null;
417: }
418:
419: // /////////////////////////////////////////////////////////
420: //
421: // Getting coverage reader using the format and the real path.
422: //
423: // /////////////////////////////////////////////////////////
424: final File obj = GeoserverDataDirectory.findDataFile(gcInfo
425: .getUrl());
426:
427: // XXX CACHING READERS HERE
428: hintReader = new SoftReference(((AbstractGridFormat) gcInfo
429: .getFormat()).getReader(obj, hints));
430:
431: return (GridCoverageReader) hintReader.get();
432: } catch (InvalidParameterValueException e) {
433: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
434: } catch (ParameterNotFoundException e) {
435: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
436: } catch (IllegalArgumentException e) {
437: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
438: } catch (SecurityException e) {
439: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
440: }
441:
442: return null;
443: }
444: }
|