Statistics
| Revision:

root / trunk / extensions / extPublish / src / org / gvsig / publish / serversmodel / Service.java @ 29308

History | View | Annotate | Download (7.56 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
package org.gvsig.publish.serversmodel;
42

    
43
import java.util.Collection;
44
import java.util.Iterator;
45
import java.util.LinkedHashMap;
46
import java.util.Observable;
47
import java.util.Set;
48

    
49
import org.gvsig.publish.IPublishPersistence;
50
import org.gvsig.publish.PublishLogger;
51
import org.gvsig.publish.PublishRegister;
52
import org.gvsig.publish.exceptions.InvalidRemoteResourceException;
53
import org.gvsig.publish.exceptions.PublishException;
54
import org.gvsig.publish.infoproject.ILayerInfo;
55
import org.gvsig.publish.infoproject.IViewInfo;
56
import org.gvsig.publish.infoproject.filter.FilterProjectInfo;
57

    
58
import com.iver.utiles.XMLEntity;
59

    
60
/**
61
 * A server implements severals services.
62
 * 
63
 * @author jvhigon
64
 *
65
 */
66
public abstract class Service extends Observable implements IPublishPersistence {
67

    
68
        /*
69
         * HasMap with its remote resources
70
         */
71
        private LinkedHashMap remoteResources = null;
72
        //Associations
73
        private Server server = null;
74

    
75
        /**
76
         * Constructor
77
         * Only creates a empty hashmap with its remote resources 
78
         */
79
        public Service(Server server){
80
                remoteResources = new LinkedHashMap();
81
                this.server = server;
82
        }
83
        /**
84
         * 
85
         * @return string which identifies the service in the publish register
86
         */
87
        public abstract String getRegisterTag();
88
        /**
89
         * 
90
         * @return unique identifier which identifies a service instance 
91
         */
92
        public abstract String getId();
93
        /**
94
         * 
95
         * @return Filter to apply to project information 
96
         */
97
        public abstract FilterProjectInfo getFilter();
98
        
99

    
100
        /**
101
         * 
102
         * @return the server which implements that service
103
         */
104
        public Server getServer(){
105
                return server;
106
        }        
107
        /**
108
         * 
109
         * @throws PublishException
110
         */
111
        public void publish() throws PublishException{
112
                Collection allRemoteResources = remoteResources.values();
113
                Iterator i = allRemoteResources.iterator();
114
                while(i.hasNext()){
115
                        RemoteResource r = (RemoteResource) i.next();
116
                        r.publish();
117
                }                
118
        }
119
        /**
120
         * 
121
         * @return number of remote resources in the service
122
         */
123
        public int getRemoteResourcesCount(){
124
                return remoteResources.size();
125
        }
126
        /**
127
         * 
128
         * @param position
129
         * @return the remote resource added in the position specified by the parameter
130
         */
131
        public RemoteResource getRemoteResource(int position){
132
                Collection collection = remoteResources.values();
133
                Object[] objects = collection.toArray();
134
                return (RemoteResource) objects[position];                
135
        }
136
        /**
137
         * 
138
         * I need an attribute name in the remote resource
139
         * @throws InvalidRemoteResourceException if the remote resource already exists (same id)
140
         */
141
        public void addRemoteResource(RemoteResource r) throws InvalidRemoteResourceException{
142
                if (remoteResources.containsKey(r.getId())){
143
                        throw new InvalidRemoteResourceException("two_remoteresources_with_same_id");
144
                }else{
145
                        remoteResources.put(r.getId(), r);
146
                }
147
        }
148
        /**
149
         * Adds information about the view. For each layer info, 
150
         * creates a remote resource (the first registered)
151
         * This method is only useful when one service only has one remote resource type.
152
         * If you have more one kind of remote resource, you must override this method.
153
         * 
154
         * @param view
155
         * @throws InvalidRemoteResourceException if there are duplicated remote resources (same id)
156
         */
157
        public void addInfo(IViewInfo view) throws InvalidRemoteResourceException {
158
                Set set = PublishRegister.register().getRemoteResourcesNames(getServer().getRegisterTag(),getRegisterTag());
159
                //if no remote resources registered return
160
                if (set ==null){
161
                        PublishLogger.getLog().error("ERROR " + getClassName() + ":I can't add Info");
162
                        return;
163
                }
164
                //At the moment only one remote resource by service
165
                String remoteResourceName = (String)set.iterator().next();
166

    
167
                //get all the layers and creates a new remote resource
168
                ILayerInfo[] layers = view.getLayers();
169
                for (int i=0; i< layers.length; i++){
170
                        RemoteResource r = PublishRegister.register().getRemoteResource(this,remoteResourceName);
171
                        r.setLayerInfo(layers[i]);
172
                        //add the remote resource 
173
                        addRemoteResource(r);
174
                }
175

    
176
        }
177
        /**
178
         * Adds a layer information . Creates a remote resource if the service is in the register.
179
         * 
180
         * @param layer
181
         * @throws InvalidRemoteResourceException  
182
         */
183
        public void addInfo(ILayerInfo layer) throws InvalidRemoteResourceException  {
184
                Set set = PublishRegister.register().getRemoteResourcesNames(getServer().getRegisterTag(),getRegisterTag());
185
                if (set != null){
186
                //At the moment only one remote resource by service
187
                String remoteResourceName = (String)set.iterator().next();
188
                RemoteResource r = PublishRegister.register().getRemoteResource(this,remoteResourceName);
189
                //sets the layer info
190
                r.setLayerInfo(layer);
191
                //add the remote resource
192
                addRemoteResource(r);
193
                }
194
        }
195

    
196
        /**
197
         * Puts all the properties into a XML in order to persist it
198
         * @return
199
         */
200
        public XMLEntity getXMLEntity() {
201
                XMLEntity xml=new XMLEntity();
202
                //put version
203
                //xml.setName(getVersion());
204
                xml.putProperty("registertag", getRegisterTag());
205
                xml.putProperty("id", getId());
206
                //put associations
207
                XMLEntity children=new XMLEntity();
208
                children.setName("remoteresources");                
209
                for (int i=0;i<getRemoteResourcesCount();i++){
210
                        children.addChild(getRemoteResource(i).getXMLEntity());
211
                }
212
                xml.addChild(children);
213
                return xml;
214
        }
215
        /**
216
         * Initializes the remote resource from the persistence
217
         * @param xml
218
         */
219
        public void setXMLEntity(XMLEntity xml) {
220
                //set properties
221
                //set children        
222
                XMLEntity children = xml.firstChild("name", "remoteresources");
223
                for (int i=0;i<children.getChildrenCount();i++){
224
                        XMLEntity xmlChild=children.getChild(i);
225
                        String remote_type = xmlChild.getStringProperty("registertag");
226
                        RemoteResource remote=PublishRegister.register().getRemoteResource(this,remote_type);
227
                        remote.setXMLEntity(xmlChild);
228
                        try {
229
                                addRemoteResource(remote);
230
                        } catch (InvalidRemoteResourceException e) {
231
                                PublishLogger.getLog().warn("WARN: skipping remote resource with id " + remote.getId() + " because it already exists", e);
232
                        }
233
                }
234
        }
235
        /**
236
         * Removes a remote resource. It call the method removeChild() of all its remote resources
237
         * 
238
         */
239
        public void removeRemoteResource(RemoteResource r) {
240
                //If the remote resource is in the root
241
                if (remoteResources.containsKey(r.getId())){
242
                        remoteResources.remove(r.getId());
243
                }else{
244
                        for (int i = 0; i < getRemoteResourcesCount(); i++){
245
                                getRemoteResource(i).removeChild(r);
246
                        }
247
                }
248
                
249
                
250
                //notify observer
251
                setChanged();
252
                notifyObservers();
253
        }
254
        /**
255
         * Removes itself from its service
256
         */
257
        public void remove() {
258
                getServer().removeService(this);                
259
        }
260
}