001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.catalog.ui;
016:
017: import java.io.Serializable;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.util.Map;
021:
022: import net.refractions.udig.catalog.CatalogPlugin;
023: import net.refractions.udig.catalog.ServiceExtension2;
024:
025: /**
026: * Adds some generic checks to attempt to process URLs and Map context objects. Essentially queries
027: * the ServiceExtension for the Service in order to determine whether it can handle the context objects.
028: *
029: * <p> The context objects this will process are:
030: * <ul>
031: * <li>URLs</li>
032: * <li>Map</li>
033: * <li>String - it will try to convert the string to a URL if other processing needs to be done to a string then the subclass will have to do it</li>
034: *
035: * @author Jesse
036: * @since 1.1.0
037: */
038: public abstract class AbstractUDIGConnectionFactory extends
039: UDIGConnectionFactory {
040:
041: /**
042: * Will use the service extension to try and determine if the context is useable.
043: *
044: * <p> Will try to process URLs, Maps and Strings. String processing is limited to trying
045: * to create a URL from the string and then processing it with the ServiceExtension</p>
046: * <p>{@link CatalogPlugin#locateURL(Object)} is used to attempt to create a URL from the context object
047: * <p>{@link #doOtherChecks(Object)} will be called if the context is not a URL or a Map or if the String cannot be processed as a URL.
048: * @param context The object to be "processed" or "adapted" into connection
049: * information.
050: * @return True if the info can be returned based on the conext, otherwise
051: * false.
052: */
053: @SuppressWarnings("unchecked")
054: @Override
055: public final boolean canProcess(Object context) {
056: ServiceExtension2 serviceExtension = getServiceExtension();
057: if (context instanceof URL) {
058: URL url = (URL) context;
059: return serviceExtension.reasonForFailure(url) == null;
060: }
061: if (context instanceof String) {
062:
063: // if the string cannot be processed we want to fall through so
064: // that doOtherChecks() can be called.
065: String string = (String) context;
066: try {
067: URL url = new URL(string);
068: if (serviceExtension.reasonForFailure(url) == null)
069: return true;
070: } catch (MalformedURLException e) {
071: // continue.
072: }
073: }
074: if (context instanceof Map) {
075: Map map = (Map) context;
076: return serviceExtension.reasonForFailure(map) == null;
077: }
078: if (CatalogPlugin.locateURL(context) != null) {
079: return serviceExtension.reasonForFailure(CatalogPlugin
080: .locateURL(context)) == null;
081: }
082: return doOtherChecks(context);
083: }
084:
085: /**
086: * If contexts other than URLs, Maps, and Strings (or if Strings need to be otherwise processed) then subclass must
087: * perform those checks here.
088: *
089: * @param context The object to be "processed" or "adapted" into connection
090: * information.
091: * @return True if the info can be returned based on the conext, otherwise
092: * false.
093: */
094: protected abstract boolean doOtherChecks(Object context);
095:
096: /**
097: * Returns the Service extension for the extension in question.
098: *
099: * @return the Service extension for the extension in question.
100: */
101: protected abstract ServiceExtension2 getServiceExtension();
102:
103: /**
104: * Returns the parameters using the Service extension if the context is a URL or String (provided that Service extension
105: * claims to be able to process them). If the context is a map it will be returned (again only if Service Extension claims to be
106: * able to consume it). Otherwise {@link #doCreateConnectionParameters(Object)} will be called.
107: */
108: @SuppressWarnings("unchecked")
109: @Override
110: public final Map<String, Serializable> createConnectionParameters(
111: Object context) {
112: ServiceExtension2 serviceExtension = getServiceExtension();
113: if (context instanceof URL) {
114: URL url = (URL) context;
115: if (serviceExtension.reasonForFailure(url) == null)
116: return serviceExtension.createParams(url);
117: }
118: if (context instanceof String) {
119:
120: // if the string cannot be processed we want to fall through so
121: // that doOtherChecks() can be called.
122: String string = (String) context;
123: try {
124: URL url = new URL(string);
125: if (serviceExtension.reasonForFailure(url) == null)
126: return serviceExtension.createParams(url);
127: ;
128: } catch (MalformedURLException e) {
129: // continue.
130: }
131: }
132:
133: if (context instanceof Map) {
134: Map params = (Map) context;
135: if (serviceExtension.reasonForFailure(params) == null)
136: return params;
137:
138: }
139: URL locateURL = CatalogPlugin.locateURL(context);
140: if (locateURL != null) {
141: if (serviceExtension.reasonForFailure(locateURL) == null)
142: return serviceExtension.createParams(locateURL);
143: }
144: return doCreateConnectionParameters(context);
145: }
146:
147: /**
148: * Called if {@link #createConnectionParameters(Object)} fails to return a value
149: */
150: protected abstract Map<String, Serializable> doCreateConnectionParameters(
151: Object context);
152:
153: /**
154: * Returns the URL if the context is a URL or can be made a URL from a String(provided that Service extension
155: * claims to be able to process them). If the context anything else then
156: * {@link #doCreateConnectionURL(Object)} will be called.
157: */
158: @Override
159: public final URL createConnectionURL(Object context) {
160: ServiceExtension2 serviceExtension = getServiceExtension();
161: if (context instanceof URL) {
162: URL url = (URL) context;
163: if (serviceExtension.reasonForFailure(url) == null)
164: return url;
165: }
166: if (context instanceof String) {
167:
168: // if the string cannot be processed we want to fall through so
169: // that doOtherChecks() can be called.
170: String string = (String) context;
171: try {
172: URL url = new URL(string);
173: if (serviceExtension.reasonForFailure(url) == null)
174: return url;
175: } catch (MalformedURLException e) {
176: // continue.
177: }
178: }
179: URL locateURL = CatalogPlugin.locateURL(context);
180: if (locateURL != null) {
181: if (serviceExtension.reasonForFailure(locateURL) == null)
182: return locateURL;
183: }
184: return doCreateConnectionURL(context);
185: }
186:
187: /**
188: * Called if {@link #createConnectionURL(Object)} fails to return a value
189: */
190: protected abstract URL doCreateConnectionURL(Object context);
191:
192: }
|