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.util.List;
020: import java.util.Map;
021: import java.util.logging.Logger;
022:
023: import org.geotools.util.ProgressListener;
024:
025: /**
026: * Abstract implementation of Service.
027: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/catalog/AbstractService.java $
028: */
029: public abstract class AbstractService implements Service {
030: /**
031: * Logger
032: */
033: protected static Logger logger = org.geotools.util.logging.Logging
034: .getLogger("org.geotools.catalog");
035:
036: /**
037: * parent catalog containing the service
038: */
039: private Catalog parent;
040: /**
041: * connection paramters
042: */
043: private Map params;
044: /**
045: * error message
046: */
047: private Throwable msg;
048: /**
049: * cached geo resource members
050: */
051: private List members;
052:
053: /**
054: * Creates a new service handle contained within a catalog.
055: *
056: * @param parent The catalog containg the service.
057: */
058: public AbstractService(Catalog parent) {
059: this .parent = parent;
060: }
061:
062: /**
063: * Creates a new service handle contained within a catalog, with a
064: * set of connection paramters.
065: *
066: * @param parent The catalog containing the service.
067: * @param params The connection params used to connect to the service.
068: */
069: public AbstractService(Catalog parent, Map params) {
070: this (parent);
071: this .params = params;
072: }
073:
074: /**
075: * @param monitor Progress monitor for blocking call.
076: *
077: * @return he parent Catalog.
078: */
079: public Resolve parent(ProgressListener monitor) {
080: return parent;
081: }
082:
083: /**
084: * @return Connection parameters, possibly null.
085: */
086: public Map getConnectionParams() {
087: return params;
088: }
089:
090: /**
091: * Sets the connection params for the service handle.
092: *
093: * @param params Map of connection paramters.
094: */
095: protected void setConnectionParams(Map params) {
096: this .params = params;
097: }
098:
099: /**
100: * Sets the cached value of the members of the service.
101: *
102: * @param members List of {@link GeoResource}.
103: */
104: protected void setMembers(List members) {
105: this .members = members;
106: }
107:
108: /**
109: * @return The cached members.
110: */
111: protected List getMembers() {
112: return members;
113: }
114:
115: /**
116: * @return The cached error message.
117: */
118: public Throwable getMessage() {
119: return msg;
120: }
121:
122: /**
123: * Sets the cached error message.
124: *
125: * @param msg An exception which occured when connecting to the service.
126: */
127: protected void setMessage(Throwable msg) {
128: this .msg = msg;
129: }
130:
131: /**
132: * Default implementation of getStatus.
133: * <p>
134: * The following rules are used to determine the status:
135: * <ol>
136: * <li>If {@link #msg} is non-null, then the service handle is
137: * {@link Status#BROKEN}.
138: * <li>If above is false, then if {@link #members} is non-null then the
139: * service handle is {@link Status#CONNECTED}
140: * <li>If non of the above hold, the service handle is {@link Status#NOTCONNECTED}.
141: * </ol>
142: * </p>
143: *
144: * <p>
145: * Subclasses can control this method by setting the members {@link #msg}
146: * and {@link #members} with {@link #setMessage(Throwable)} and
147: * {@link #setMembers(List)} respectivley. Or subclasses may wish to
148: * override this method entirley.
149: * </p>
150: *
151: */
152: public Status getStatus() {
153: if (msg != null) {
154: return Status.BROKEN;
155: }
156:
157: if (members != null) {
158: return Status.CONNECTED;
159: }
160:
161: return Status.NOTCONNECTED;
162: }
163:
164: /**
165: * This should represent the identifier
166: *
167: * @param other
168: *
169: *
170: * @see Object#equals(java.lang.Object)
171: */
172: public final boolean equals(Object other) {
173: if ((other != null) && other instanceof Service) {
174: Service service = (Service) other;
175:
176: if ((getIdentifier() != null)
177: && (service.getIdentifier() != null)) {
178: return getIdentifier().equals(service.getIdentifier());
179: }
180: }
181:
182: return false;
183: }
184:
185: /**
186: * This method does nothing. Sublcasses should override if events are
187: * supported.
188: *
189: * @param listener DOCUMENT ME!
190: */
191: public void addListener(ResolveChangeListener listener) {
192: // do nothing
193: }
194:
195: /**
196: * This method does nothing. Sublcasses should override if events are
197: * supported.
198: *
199: * @param listener DOCUMENT ME!
200: */
201: public void removeListener(ResolveChangeListener listener) {
202: // do nothing
203: }
204:
205: /**
206: * This method does nothing. Sublcasses should override if events are
207: * supported.
208: *
209: * @param event DOCUMENT ME!
210: */
211: public void fire(ResolveChangeEvent event) {
212: // do nothing
213: }
214:
215: /**
216: * This should represent the identified
217: *
218: *
219: * @see Object#hashCode()
220: */
221: public final int hashCode() {
222: if (getIdentifier() != null) {
223: return getIdentifier().hashCode();
224: }
225:
226: return super .hashCode();
227: }
228:
229: /**
230: * Indicate class and id.
231: *
232: * @return string representing this IResolve
233: */
234: public String toString() {
235: StringBuffer buf = new StringBuffer();
236: String classname = getClass().getName();
237: String name = classname
238: .substring(classname.lastIndexOf('.') + 1);
239: buf.append(name);
240: buf.append("("); //$NON-NLS-1$
241: buf.append(getIdentifier());
242: buf.append(")"); //$NON-NLS-1$
243:
244: return buf.toString();
245: }
246: }
|