Statistics
| Revision:

root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / DiscoveryServiceClient.java @ 27094

History | View | Annotate | Download (4.98 KB)

1
package es.gva.cit.catalog;
2

    
3
import java.io.IOException;
4
import java.net.Socket;
5
import java.net.URI;
6
import java.net.URISyntaxException;
7
import java.net.UnknownHostException;
8

    
9
import com.iver.utiles.swing.jcomboServer.ServerData;
10

    
11
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
12
import es.gva.cit.catalog.drivers.IDiscoveryServiceDriver;
13
import es.gva.cit.catalog.exceptions.NotSupportedProtocolException;
14
import es.gva.cit.catalog.exceptions.NotSupportedVersionException;
15
import es.gva.cit.catalog.exceptions.ServerIsNotReadyException;
16
import es.gva.cit.catalog.querys.DiscoveryServiceQuery;
17
import es.gva.cit.catalog.ui.search.SearchAditionalPropertiesPanel;
18
import es.gva.cit.catalog.utils.URIUtils;
19

    
20
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
21
 *
22
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
23
 *
24
 * This program is free software; you can redistribute it and/or
25
 * modify it under the terms of the GNU General Public License
26
 * as published by the Free Software Foundation; either version 2
27
 * of the License, or (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
37
 *
38
 * For more information, contact:
39
 *
40
 *  Generalitat Valenciana
41
 *   Conselleria d'Infraestructures i Transport
42
 *   Av. Blasco Ib??ez, 50
43
 *   46010 VALENCIA
44
 *   SPAIN
45
 *
46
 *      +34 963862235
47
 *   gvsig@gva.es
48
 *      www.gvsig.gva.es
49
 *
50
 *    or
51
 *
52
 *   IVER T.I. S.A
53
 *   Salamanca 50
54
 *   46005 Valencia
55
 *   Spain
56
 *
57
 *   +34 963163400
58
 *   dac@iver.es
59
 */
60
/* CVS MESSAGES:
61
 *
62
 * $Id$
63
 * $Log$
64
 *
65
 */
66
/**
67
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
68
 */
69
public class DiscoveryServiceClient {
70
        /**
71
         * The server URI
72
         */
73
        private URI uri = null;
74
        /**
75
         * The driver to make the querys
76
         */
77
        private IDiscoveryServiceDriver driver;
78
        /**
79
         * The service capabilities
80
         */
81
        private DiscoveryServiceCapabilities capabilities;
82
        /**
83
         * The server status message
84
         */
85
        private String serverStatus = null;
86

    
87
        public DiscoveryServiceClient(String sUri,IDiscoveryServiceDriver driver) {
88
                setDriver(driver);
89
                if (driver == null){
90
                        serverStatus = "errorServerNotFound";
91
                }else{
92
                        try {
93
                                this.uri = URIUtils.createUri(sUri,
94
                                                driver.getDefaultSchema(),
95
                                                driver.getDefaultPort());
96
                        } catch (URISyntaxException e) {
97
                                serverStatus = "errorServerNotFound";
98
                        }
99
                }
100
        }
101
        
102
        /**
103
         * It make a getCapabilities operation
104
         * @return the service version
105
         * @throws ServerIsNotReadyException 
106
         */
107
        public DiscoveryServiceCapabilities getCapabilities() throws ServerIsNotReadyException {        
108
                if (serverIsReady()){
109
                        try {
110
                                if (getDriver().isProtocolSupported(getUri())) {
111
                                        capabilities = getDriver().getCapabilities(getUri());
112
                                        return capabilities;
113
                                }
114
                        } catch (NotSupportedProtocolException e) {
115
                                capabilities = new DiscoveryServiceCapabilities();
116
                                capabilities.setAvailable(false);
117
                                capabilities.setServerMessage("notSupportedProtocol");
118
                        } catch (NotSupportedVersionException e) {
119
                                capabilities = new DiscoveryServiceCapabilities();
120
                                capabilities.setAvailable(false);
121
                                capabilities.setServerMessage("notSupportedVersion");
122
                        } 
123
                }
124
                return capabilities;    
125
        } 
126

    
127
        /**
128
         * It tries if the server is ready 
129
         * @return boolean
130
         * true --> server is ready
131
         * false --> server is not ready
132
         */
133
        public boolean serverIsReady() throws ServerIsNotReadyException {        
134
                Socket sock = null;
135
                try {
136
                        sock = new Socket(getUri().getHost(),
137
                                        getUri().getPort());
138
                } catch (UnknownHostException e) {
139
                        throw new ServerIsNotReadyException(e);
140
                } catch (IOException e) {
141
                        throw new ServerIsNotReadyException(e);
142
                }
143
                return (sock != null);
144
        } 
145

    
146
        /**
147
         * @return the server URI
148
         */
149
        public URI getUri() {
150
                return uri;
151
        }
152

    
153
        /**
154
         * @return Return the server URI like a String
155
         */
156
        public String getSUri() {
157
                return uri.toString();
158
        } 
159

    
160
        /**
161
         * @return Returns the driver.
162
         */
163
        protected IDiscoveryServiceDriver getDriver() {        
164
                return driver;
165
        }
166

    
167
        /**
168
         * 
169
         * @param driver the driver to set
170
         */
171
        protected void setDriver(IDiscoveryServiceDriver driver) {
172
                this.driver = driver;
173
        } 
174

    
175
        /**
176
         * @return the server protocol
177
         */
178
        public String getProtocol() {        
179
                return driver.getServiceName();
180
        }
181
        
182
        /**
183
         * Gets the aditional panel
184
         * @return
185
         */
186
        public SearchAditionalPropertiesPanel getAditionalSearchPanel(){
187
                return driver.getAditionalSearchPanel();
188
        }
189
        
190
        /**
191
         * Gets a query
192
         * @return
193
         */
194
        public DiscoveryServiceQuery createQuery(){
195
                return driver.createQuery();
196
        }
197
        
198
        /**
199
         * @return if the protocol always works with the 
200
         * same server
201
         */
202
        public ServerData getOneServer(){
203
                return driver.getOneServer();
204
        }        
205
}