001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.catalog.shapefile;
017:
018: import org.geotools.catalog.AbstractService;
019: import org.geotools.catalog.Catalog;
020: import org.geotools.catalog.ServiceInfo;
021: import org.geotools.catalog.defaults.DefaultServiceInfo;
022: import org.geotools.data.shapefile.ShapefileDataStore;
023: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
024: import org.geotools.util.ProgressListener;
025: import java.io.IOException;
026: import java.net.URI;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * Implementation of a service handle for shapefiles.
033: *
034: * @see org.geotools.gtcatalog.Service
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/catalog/shapefile/ShapefileService.java $
036: */
037: public class ShapefileService extends AbstractService {
038: private ServiceInfo info;
039: private ShapefileDataStore dataStore;
040: private Throwable msg;
041: private URI uri;
042: private Map params;
043: private List members;
044: private static ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
045:
046: public ShapefileService(Catalog parent, URI uri, Map params) {
047: super (parent);
048: this .params = params;
049: this .uri = uri;
050: }
051:
052: public boolean canResolve(Class adaptee) {
053: if (adaptee == null) {
054: return false;
055: }
056:
057: return adaptee.isAssignableFrom(ServiceInfo.class)
058: || adaptee.isAssignableFrom(List.class)
059: || adaptee.isAssignableFrom(ShapefileDataStore.class);
060: }
061:
062: public Object resolve(Class adaptee, ProgressListener monitor)
063: throws IOException {
064: if (adaptee == null) {
065: return null;
066: }
067:
068: if (adaptee.isAssignableFrom(ServiceInfo.class)) {
069: return getInfo(monitor);
070: }
071:
072: if (adaptee.isAssignableFrom(List.class)) {
073: return members(monitor);
074: }
075:
076: if (adaptee.isAssignableFrom(ShapefileDataStore.class)) {
077: return getDataStore(monitor);
078: }
079:
080: return null;
081: }
082:
083: public ServiceInfo getInfo(ProgressListener monitor)
084: throws IOException {
085: if (info == null) {
086: synchronized (getDataStore(monitor)) {
087: if (info == null) {
088: String title = getIdentifier().getPath();
089: String description = getIdentifier().toString();
090: String[] keywords = new String[] { ".shp",
091: "Shapefile",
092: getDataStore(monitor).getTypeNames()[0] };
093:
094: info = new DefaultServiceInfo(title, description,
095: null, null, null, null, keywords, null);
096: }
097: }
098: }
099:
100: return info;
101: }
102:
103: protected ShapefileDataStore getDataStore(ProgressListener monitor)
104: throws IOException {
105: if (dataStore == null) {
106: synchronized (ShapefileDataStore.class) {
107: if (dataStore == null) {
108: if (factory.canProcess(params)) {
109: try {
110: msg = null;
111: dataStore = (ShapefileDataStore) factory
112: .createDataStore(params);
113: } catch (IOException io) {
114: msg = io; // save message for later
115: throw io;
116: } catch (Throwable t) {
117: msg = t; //save error to report back later
118: throw (IOException) new IOException()
119: .initCause(t);
120: }
121: }
122: }
123: }
124: }
125: return dataStore;
126: }
127:
128: public List members(ProgressListener monitor) throws IOException {
129: if (members == null) {
130: synchronized (getDataStore(monitor)) {
131: if (members == null) {
132: members = new LinkedList();
133:
134: String[] typenames = getDataStore(monitor)
135: .getTypeNames();
136:
137: if (typenames != null) {
138: for (int i = 0; i < typenames.length; i++) {
139: members.add(new ShapefileGeoResource(this ,
140: typenames[i]));
141: }
142: }
143: }
144: }
145: }
146:
147: return members;
148: }
149:
150: public Map getConnectionParams() {
151: return params;
152: }
153:
154: public Status getStatus() {
155: if (msg == null) {
156: if (dataStore != null) {
157: return Status.CONNECTED;
158: }
159:
160: return Status.NOTCONNECTED;
161: }
162:
163: return Status.BROKEN;
164: }
165:
166: public Throwable getMessage() {
167: return msg;
168: }
169:
170: public URI getIdentifier() {
171: return uri;
172: }
173: }
|