001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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: */
017: package net.refractions.udig.catalog.rasterings;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import net.refractions.udig.catalog.IService;
027: import net.refractions.udig.catalog.IServiceInfo;
028:
029: import org.eclipse.core.runtime.IProgressMonitor;
030: import org.eclipse.core.runtime.NullProgressMonitor;
031: import org.geotools.data.coverage.grid.AbstractGridFormat;
032: import org.geotools.data.coverage.grid.GridFormatFactorySpi;
033: import org.opengis.coverage.grid.GridCoverageReader;
034:
035: /**
036: * Provides a handle to a raster service allowing the service to be lazily loaded.
037: * <p>
038: * This class provides functionality common to services attached to GridCoverage based resources.
039: * </p>
040: *
041: * @author mleslie
042: * @since 0.6.0
043: */
044: public abstract class AbstractRasterService extends IService {
045: private URL id;
046: /** <code>status</code> field describes the status of the service */
047: protected Status status = Status.NOTCONNECTED;
048: /** <code>message</code> field reports any errors encountered. May be null. */
049: protected Exception message = null;
050: /** <code>reader</code> field */
051: protected GridCoverageReader reader;
052: private AbstractGridFormat format;
053: private GridFormatFactorySpi factory;
054:
055: /**
056: * Construct <code>AbstractRasterService</code>.
057: *
058: * @param id
059: * @param factory
060: */
061: public AbstractRasterService(URL id, GridFormatFactorySpi factory) {
062: this .id = id;
063: this .factory = factory;
064: }
065:
066: /**
067: * Adapts to to provide GridCoverageReader
068: */
069: public <T> boolean canResolve(Class<T> adaptee) {
070: return adaptee != null
071: && (adaptee.isAssignableFrom(GridCoverageReader.class) || super
072: .canResolve(adaptee));
073: }
074:
075: public Status getStatus() {
076: return this .status;
077: }
078:
079: public Throwable getMessage() {
080: return this .message;
081: }
082:
083: public URL getIdentifier() {
084: return this .id;
085: }
086:
087: /**
088: * Finds or creates the GridFormat object describing the service.
089: *
090: * @return GridFormat describing this coverage.
091: */
092: public Object getFormat() {
093: if (this .format == null) {
094: this .format = (AbstractGridFormat) this .factory
095: .createFormat();
096: }
097: return this .format;
098: }
099:
100: /**
101: * Finds or creates the Reader used to access this service. Apon any exception, the message
102: * field is populated and null is returned.
103: *
104: * @return Reader linked to this service.
105: */
106: public GridCoverageReader getReader(IProgressMonitor monitor) {
107: if (this .reader == null) {
108: try {
109: AbstractGridFormat frmt = (AbstractGridFormat) getFormat();
110: URL id = getIdentifier();
111: if (id.toExternalForm().startsWith("C:/")) {
112: id = new URL("file:///" + id.toExternalForm());
113: }
114: this .reader = frmt.getReader(getIdentifier());
115: } catch (Exception ex) {
116: this .message = ex;
117: }
118: }
119: return this .reader;
120: }
121:
122: /**
123: * Retrieves the string identifying this service. This is the location of the resource to be
124: * loaded.
125: *
126: * @return String describing this service.
127: */
128: public String getDescription() {
129: return getIdentifier().toString();
130: }
131:
132: /**
133: * Retrieves a relatively human readable title for this service.
134: *
135: * @return Title of this service
136: */
137: public String getTitle() {
138: return getIdentifier().getFile();
139: }
140:
141: @Override
142: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
143: throws IOException {
144:
145: if (monitor == null)
146: monitor = new NullProgressMonitor();
147:
148: if (adaptee == null) {
149: throw new NullPointerException("No adaptor specified"); //$NON-NLS-1$
150: }
151: if (adaptee.isAssignableFrom(GridCoverageReader.class)) {
152: return adaptee.cast(getReader(monitor));
153: }
154: return super .resolve(adaptee, monitor);
155: }
156:
157: @Override
158: public abstract List<AbstractRasterGeoResource> members(
159: IProgressMonitor monitor) throws IOException;
160:
161: @Override
162: public Map<String, Serializable> getConnectionParams() {
163: return new HashMap<String, Serializable>();
164: }
165:
166: public abstract IServiceInfo getInfo(IProgressMonitor monitor)
167: throws IOException;
168: }
|