svn-gvsig-desktop / branches / v10 / extensions / extPublish / src / org / gvsig / publish / serversmodel / Server.java @ 19900
History | View | Annotate | Download (7.2 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004-2006 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 Iba?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 |
/**
|
42 |
* @author Jos? Vicente Hig?n (josevicente.higon@iver.es)
|
43 |
*/
|
44 |
package org.gvsig.publish.serversmodel; |
45 |
|
46 |
import java.net.MalformedURLException; |
47 |
import java.net.URL; |
48 |
import java.util.Collection; |
49 |
import java.util.Iterator; |
50 |
import java.util.LinkedHashMap; |
51 |
import java.util.Observable; |
52 |
import java.util.Set; |
53 |
|
54 |
import org.gvsig.publish.IPublishPersistence; |
55 |
import org.gvsig.publish.PublishLogger; |
56 |
import org.gvsig.publish.PublishRegister; |
57 |
import org.gvsig.publish.infoproject.ILayerInfo; |
58 |
import org.gvsig.publish.infoproject.IProjectInfo; |
59 |
import org.gvsig.publish.infoproject.IViewInfo; |
60 |
|
61 |
import com.iver.utiles.XMLEntity; |
62 |
|
63 |
|
64 |
public abstract class Server extends Observable implements IPublishPersistence { |
65 |
|
66 |
private URL serverURL = null; |
67 |
/*
|
68 |
* HashMap with its Services
|
69 |
*/
|
70 |
private LinkedHashMap services = null; |
71 |
/*
|
72 |
* Server associations
|
73 |
*/
|
74 |
private IProjectInfo projectInfo = null; |
75 |
|
76 |
/**
|
77 |
*
|
78 |
*
|
79 |
* @return unique server identifier.
|
80 |
*/
|
81 |
public abstract String getId(); |
82 |
/**
|
83 |
*
|
84 |
* @return string to register
|
85 |
*/
|
86 |
public abstract String getRegisterTag(); |
87 |
|
88 |
/**
|
89 |
* Constructor
|
90 |
*
|
91 |
* Creates all the services of the server
|
92 |
*
|
93 |
*/
|
94 |
public Server(){
|
95 |
//creates a hashmap with its services
|
96 |
services = new LinkedHashMap(); |
97 |
} |
98 |
/**
|
99 |
* Adds information about the project
|
100 |
* <p>
|
101 |
* You can override this method (in a extended class) in order to initialize some properties
|
102 |
* @param project
|
103 |
*/
|
104 |
public void setProjectInfo(IProjectInfo project){ |
105 |
this.projectInfo = project;
|
106 |
//this.serverTitle = project.getName();
|
107 |
} |
108 |
/**
|
109 |
*
|
110 |
* @return information about the project
|
111 |
*/
|
112 |
public IProjectInfo getProjectInfo(){
|
113 |
return projectInfo;
|
114 |
} |
115 |
/**
|
116 |
* Adds information about a view in all services
|
117 |
* @param view
|
118 |
*/
|
119 |
public void addInfo(IViewInfo view){ |
120 |
if (getProjectInfo() == null){ |
121 |
setProjectInfo(view.getProjectInfo()); |
122 |
} |
123 |
for(int i = 0; i< getServicesCount();i++){ |
124 |
getService(i).addInfo(view); |
125 |
} |
126 |
//notify observers (publication)
|
127 |
setChanged(); |
128 |
notifyObservers(); |
129 |
} |
130 |
/**
|
131 |
* Adds information about a layer in all services
|
132 |
* @param layer
|
133 |
*/
|
134 |
public void addInfo(ILayerInfo layer){ |
135 |
if (getProjectInfo() == null){ |
136 |
setProjectInfo(layer.getViewInfo().getProjectInfo()); |
137 |
} |
138 |
for(int i = 0; i< getServicesCount();i++){ |
139 |
getService(i).addInfo(layer); |
140 |
} |
141 |
//notify observers (publication)
|
142 |
setChanged(); |
143 |
notifyObservers(); |
144 |
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* Creates all available services.
|
149 |
* <p>
|
150 |
* To do that, it looks up the services register
|
151 |
*/
|
152 |
// public void createsAvailableServices(){
|
153 |
// Set set = PublishRegister.register().getServicesNames(this.getType());
|
154 |
// Iterator i = set.iterator();
|
155 |
// while(i.hasNext()){
|
156 |
// String serviceName = (String)i.next();
|
157 |
// //Service service = (Service) Service.register.getInstance(this.getType(),serviceName);
|
158 |
// Service service = (Service) PublishRegister.register().getService(this,serviceName);
|
159 |
// addService(service);
|
160 |
// }
|
161 |
// }
|
162 |
/**
|
163 |
* Gets a service identified by a name
|
164 |
* @param serviceName
|
165 |
* @return
|
166 |
*/
|
167 |
public Service getService(String serviceName){ |
168 |
Service s = null;
|
169 |
s = (Service) services.get(serviceName); |
170 |
return s;
|
171 |
} |
172 |
/**
|
173 |
* Useful for generate a tree model
|
174 |
* @return
|
175 |
*/
|
176 |
public Service getService(int position){ |
177 |
Collection collection = services.values();
|
178 |
Object[] objects = collection.toArray(); |
179 |
return (Service) objects[position];
|
180 |
} |
181 |
/**
|
182 |
*
|
183 |
* @return number of services in the server
|
184 |
* TODO: Why not a services.size()???
|
185 |
*/
|
186 |
public int getServicesCount(){ |
187 |
Set set = services.entrySet();
|
188 |
Object[] objects = set.toArray(); |
189 |
return objects.length;
|
190 |
} |
191 |
/**
|
192 |
* Adds a service into the server
|
193 |
* @param serviceName
|
194 |
* @param service
|
195 |
*/
|
196 |
public void addService(Service service){ |
197 |
services.put(service.getId(), service); |
198 |
} |
199 |
|
200 |
/**
|
201 |
* String to identify the type of server
|
202 |
*
|
203 |
* @return
|
204 |
*/
|
205 |
//public abstract String getType();
|
206 |
|
207 |
/**
|
208 |
*
|
209 |
* Call the method publish of its services
|
210 |
* @throws PublishException
|
211 |
*/
|
212 |
public void publish() throws PublishException{ |
213 |
Collection allServices = services.values();
|
214 |
Iterator i = allServices.iterator();
|
215 |
while(i.hasNext()){
|
216 |
Service s = (Service) i.next(); |
217 |
s.publish(); |
218 |
} |
219 |
} |
220 |
/**
|
221 |
* Each server must verify if its remote resources are compatible
|
222 |
*/
|
223 |
public abstract void postInitialize(); |
224 |
|
225 |
/**
|
226 |
*
|
227 |
* @return server's url
|
228 |
*/
|
229 |
public URL getServerURL() { |
230 |
return serverURL;
|
231 |
} |
232 |
/**
|
233 |
* Sets the server's url
|
234 |
* @param serverURL
|
235 |
*/
|
236 |
public void setServerURL(URL serverURL) { |
237 |
this.serverURL = serverURL;
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* Puts all the properties into a XML in order to persist it
|
242 |
* @return
|
243 |
*/
|
244 |
public XMLEntity getXMLEntity() {
|
245 |
XMLEntity xml=new XMLEntity();
|
246 |
//put server properties
|
247 |
xml.putProperty("registertag", getRegisterTag());
|
248 |
xml.putProperty("id", getId());
|
249 |
xml.putProperty("url", getServerURL().toString());
|
250 |
//put associations (its services)
|
251 |
XMLEntity children=new XMLEntity();
|
252 |
children.setName("services");
|
253 |
for (int i=0;i<getServicesCount();i++){ |
254 |
children.addChild(getService(i).getXMLEntity()); |
255 |
} |
256 |
xml.addChild(children); |
257 |
return xml;
|
258 |
} |
259 |
/**
|
260 |
* Initializes from the persistence
|
261 |
* @param xml
|
262 |
*/
|
263 |
public void setXMLEntity(XMLEntity xml) { |
264 |
//set properties
|
265 |
String urlString = xml.getStringProperty("url"); |
266 |
URL url=null; |
267 |
URL aux=null; |
268 |
try {
|
269 |
url = new URL(urlString); |
270 |
//auxiliary well formed url
|
271 |
aux = new URL("http://"); |
272 |
} catch (MalformedURLException e1) { |
273 |
PublishLogger.getLog().error("ERROR Server: Malformed url setting the server url from persistence");
|
274 |
setServerURL(aux); |
275 |
} |
276 |
setServerURL(url); |
277 |
//set all its services
|
278 |
XMLEntity children = xml.firstChild("name", "services"); |
279 |
for (int i=0;i<children.getChildrenCount();i++){ |
280 |
XMLEntity xmlChild=children.getChild(i); |
281 |
String service_type = xmlChild.getStringProperty("registertag"); |
282 |
Service service=PublishRegister.register().getService(this,service_type);
|
283 |
service.setXMLEntity(xmlChild); |
284 |
addService(service); |
285 |
} |
286 |
} |
287 |
/**
|
288 |
* Removes a service from the server
|
289 |
* @param service to remove
|
290 |
*/
|
291 |
public void removeService(Service service) { |
292 |
services.remove(service.getId()); |
293 |
|
294 |
} |
295 |
|
296 |
} |