001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Refractions Research Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.catalog;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URI;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.geotools.util.ProgressListener;
026:
027: /**
028: * Abstract service implementation for file based services.
029: *
030: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
031: *
032: */
033: public abstract class AbstractFileService extends AbstractService {
034:
035: /**
036: * the file
037: */
038: File file;
039:
040: public AbstractFileService(Catalog parent, File file) {
041: super (parent);
042: this .file = file;
043: }
044:
045: public AbstractFileService(Catalog parent, Map params, File file) {
046: super (parent, params);
047: this .file = file;
048: }
049:
050: /**
051: * @return the underlying file handle.
052: */
053: public File getFile() {
054: return file;
055: }
056:
057: /**
058: * Returns a single element list which contains a single instance of
059: * {@link AbstractFileGeoResource}.
060: */
061: public List members(ProgressListener monitor) throws IOException {
062: if (getMembers() != null)
063: return getMembers();
064:
065: synchronized (this ) {
066: if (getMembers() != null) {
067: return getMembers();
068: }
069:
070: List members = null;
071: try {
072: members = createMembers(monitor);
073: return members;
074: } catch (IOException e) {
075: //TODO: log this
076: setMessage(e);
077: throw e;
078: }
079:
080: }
081: }
082:
083: abstract protected List/*<AbstractFileGeoResource>*/
084: createMembers(ProgressListener monitor) throws IOException;
085:
086: /**
087: * Supports default Service resolves with an additional resolve to a
088: * {@link java.io.File}.
089: * <p>
090: * Subclasses may wish to extend, or override this method.
091: * </p>
092: */
093: public boolean canResolve(Class adaptee) {
094:
095: return List.class.isAssignableFrom(adaptee)
096: || ServiceInfo.class.isAssignableFrom(adaptee)
097: || File.class.isAssignableFrom(adaptee);
098: }
099:
100: /**
101: * Supports default Service resolves with an additional resolve to a
102: * {@link java.io.File}.
103: * <p>
104: * Subclasses may wish to extend, or override this method.
105: * </p>
106: */
107: public Object resolve(Class adaptee, ProgressListener monitor)
108: throws IOException {
109:
110: if (adaptee.isAssignableFrom(List.class)) {
111: return members(monitor);
112: }
113:
114: if (adaptee.isAssignableFrom(ServiceInfo.class)) {
115: return getInfo(monitor);
116: }
117:
118: if (adaptee.isAssignableFrom(File.class)) {
119: return file;
120: }
121:
122: return null;
123: }
124:
125: /**
126: * Returns the file uri.
127: *
128: * @see #getFile()
129: */
130: public URI getIdentifier() {
131: return file.toURI();
132: }
133:
134: }
|