root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClientOld / wms / WMSAdapterFactory.java @ 3321
History | View | Annotate | Download (7.76 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.remoteClientOld.wms; |
42 |
|
43 |
import java.io.IOException; |
44 |
import java.net.URL; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Iterator; |
47 |
|
48 |
import org.gvsig.remoteClientOld.descriptor.Capabilities; |
49 |
import org.gvsig.remoteClientOld.exception.UnsupportedVersionException; |
50 |
|
51 |
/**
|
52 |
* Factor?a abstracta de drivers WMS
|
53 |
*
|
54 |
* @author Fernando Gonz?lez Cort?s
|
55 |
*/
|
56 |
public class WMSAdapterFactory { |
57 |
private static ArrayList versions = new ArrayList(); |
58 |
|
59 |
static {
|
60 |
/*
|
61 |
* Se meten en el array versions las distintas versiones
|
62 |
* del protocolo en orden descendente
|
63 |
*/
|
64 |
versions.add(WMSAdapter1_1_1.class); |
65 |
//versions.add(WMSDriver1_1_0.class);
|
66 |
} |
67 |
|
68 |
/**
|
69 |
* M?todo que dada una respuesta de getCapabilities y un iterador sobre una
|
70 |
* coleccion de WMSClient's ordenada descendentemente devuelve el cliente
|
71 |
* cuya version es igual o inmediatamente inferior
|
72 |
*
|
73 |
* @param caps Capabilities con la respuesta del servidor
|
74 |
* @param clients Iterador de conjunto ordenado descendientemente
|
75 |
*
|
76 |
* @return cliente cuya version es igual o inmediatamente inferior
|
77 |
* @throws IllegalAccessException
|
78 |
* @throws InstantiationException
|
79 |
*
|
80 |
*/
|
81 |
private static WMSAdapter getDriver(Capabilities caps, Iterator clients) throws InstantiationException, IllegalAccessException { |
82 |
while (clients.hasNext()) {
|
83 |
WMSAdapter client = (WMSAdapter) ((Class)clients.next()).newInstance();
|
84 |
int ret = caps.getServiceInformation().getVersion().compareTo(client.getVersion());
|
85 |
|
86 |
if (ret >= 0) { |
87 |
return client;
|
88 |
} |
89 |
} |
90 |
return null; |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
95 |
* el objeto Capabilities obtenido con dicha versi?n
|
96 |
*
|
97 |
* @param host maquina con la que se negocia
|
98 |
*
|
99 |
* @return instancia de un cliente capaz de negociar con el host que se
|
100 |
* pasa como par?metro
|
101 |
*
|
102 |
* @throws UnsupportedVersionException Si no se encuentra un cliente
|
103 |
* apropiado para el host dado
|
104 |
* @throws IOException Si se produce un error de entrada salida en la
|
105 |
* comunicaci?n con el servidor
|
106 |
* @throws IllegalAccessException
|
107 |
* @throws InstantiationException
|
108 |
*/
|
109 |
private static WMSAdapter negotiate(URL host) |
110 |
throws UnsupportedVersionException, IOException, InstantiationException, IllegalAccessException |
111 |
{ |
112 |
if (versions.size() == 0) |
113 |
{ |
114 |
throw new UnsupportedVersionException("No registered clients"); |
115 |
} |
116 |
|
117 |
Iterator iVersion = versions.iterator();
|
118 |
|
119 |
if (iVersion.hasNext()) {
|
120 |
|
121 |
do {
|
122 |
Capabilities aux = null;
|
123 |
WMSAdapter iV; |
124 |
do {
|
125 |
if (!iVersion.hasNext()) {
|
126 |
throw new UnsupportedVersionException( |
127 |
"Cannot understand server version");
|
128 |
} |
129 |
|
130 |
iV = (WMSAdapter)((Class)iVersion.next()).newInstance();
|
131 |
aux = iV.getCapabilities(); //Se pide version al servidor
|
132 |
} while (aux.getServiceInformation().getVersion() == null); //Se itera hasta que se lee una versi?n |
133 |
|
134 |
int res = aux.getServiceInformation().getVersion().compareTo(iV.getVersion());
|
135 |
|
136 |
if (res == 0) { //Si es la misma que nuestra version |
137 |
return createVersionDriver(iV.getVersion());
|
138 |
} else if (res > 0) { //Si es mayor que nuestra version |
139 |
throw new UnsupportedVersionException( |
140 |
"Server Version too high: " + aux.getServiceInformation().getVersion());
|
141 |
} else { //Si es menor que nuestra version |
142 |
//Obtenemos la primera version menor o igual que tengamos
|
143 |
WMSAdapter lower = getDriver(aux, iVersion); |
144 |
|
145 |
if (lower == null) { //Si no hay ninguna |
146 |
throw new UnsupportedVersionException( |
147 |
"Lowest server version is " + aux.getServiceInformation().getVersion());
|
148 |
} else {
|
149 |
if (lower.getVersion().equals(aux.getServiceInformation().getVersion())) {
|
150 |
return createVersionDriver(lower.getVersion());
|
151 |
} else { //Si hay una version menor que la que retorno el servidor |
152 |
iV = lower; |
153 |
} |
154 |
} |
155 |
} |
156 |
} while (iVersion.hasNext());
|
157 |
} |
158 |
|
159 |
throw new UnsupportedVersionException("Negotiation failed"); |
160 |
} |
161 |
|
162 |
/**
|
163 |
* It creates an instance of a WMSDriver class.
|
164 |
*
|
165 |
* @param String, with the version of the driver to be created
|
166 |
* @return WMSDriver.
|
167 |
*/
|
168 |
private static WMSAdapter createVersionDriver(String version) |
169 |
throws UnsupportedVersionException
|
170 |
{ |
171 |
try
|
172 |
{ |
173 |
Class driver;
|
174 |
version.replace('.', '_'); |
175 |
driver = Class.forName("org.gvsig.remoteClient.driver.wms.wms1_1_1.WMSDriver" + version); |
176 |
return (WMSAdapter)driver.newInstance();
|
177 |
} |
178 |
catch (Exception e) |
179 |
{ |
180 |
throw new UnsupportedVersionException("WMSDriverFactory. Unknown driver version " + e); |
181 |
} |
182 |
} |
183 |
|
184 |
|
185 |
/**
|
186 |
* Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
187 |
* el objeto Capabilities obtenido con dicha versi?n
|
188 |
*
|
189 |
* @param host maquina con la que se negocia
|
190 |
*
|
191 |
* @return instancia de un cliente capaz de negociar con el host que se
|
192 |
* pasa como par?metro
|
193 |
*
|
194 |
* @throws UnsupportedVersionException Si no se encuentra un cliente
|
195 |
* apropiado para el host dado
|
196 |
* @throws IOException Si se produce un error de entrada salida en la
|
197 |
* comunicaci?n con el servidor
|
198 |
*/
|
199 |
public static WMSClient getClient(URL host) |
200 |
throws UnsupportedVersionException, IOException |
201 |
{ |
202 |
try {
|
203 |
return new WMSClient(negotiate(host)); |
204 |
} catch (InstantiationException e) { |
205 |
throw new RuntimeException(e); |
206 |
} catch (IllegalAccessException e) { |
207 |
throw new RuntimeException(e); |
208 |
} |
209 |
} |
210 |
/**
|
211 |
* @deprecated
|
212 |
* */
|
213 |
public static WMSAdapter getDriver(URL host) |
214 |
throws UnsupportedVersionException, IOException |
215 |
{ |
216 |
try {
|
217 |
return negotiate(host);
|
218 |
} catch (InstantiationException e) { |
219 |
throw new RuntimeException(e); |
220 |
} catch (IllegalAccessException e) { |
221 |
throw new RuntimeException(e); |
222 |
} |
223 |
} |
224 |
} |