svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / resource / spi / MultiResource.java @ 44376
History | View | Annotate | Download (10.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.resource.spi; |
25 |
|
26 |
import java.lang.ref.WeakReference; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.exception.InitializeException; |
35 |
import org.gvsig.fmap.dal.resource.Resource; |
36 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
37 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
38 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
39 |
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException; |
40 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
41 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
42 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
43 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
44 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException; |
45 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
46 |
import org.slf4j.Logger; |
47 |
import org.slf4j.LoggerFactory; |
48 |
|
49 |
/**
|
50 |
* Resource implementation which is able to show an unique interface to a group
|
51 |
* of Resources.
|
52 |
*
|
53 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
54 |
*/
|
55 |
public class MultiResource extends AbstractResource { |
56 |
|
57 |
public static final String TYPE_NAME = "multi"; |
58 |
|
59 |
public static final String DESCRIPTION = "Group of multiple resources"; |
60 |
|
61 |
private static final Logger LOG = |
62 |
LoggerFactory.getLogger(MultiResource.class); |
63 |
|
64 |
private Map resources = new HashMap<>(); |
65 |
|
66 |
// Initially, the first one
|
67 |
private int defaultResourcePos = 0; |
68 |
|
69 |
//Blocks adding methods
|
70 |
private boolean isFrozen=false; |
71 |
|
72 |
/**
|
73 |
* Creates a new {@link MultiResource} from the given parameters.
|
74 |
*
|
75 |
* @param parameters
|
76 |
* to create the resource from
|
77 |
* @throws InitializeException
|
78 |
* if there is an error creating the resource
|
79 |
*/
|
80 |
public MultiResource(MultiResourceParameters parameters)
|
81 |
throws InitializeException {
|
82 |
super(parameters);
|
83 |
} |
84 |
|
85 |
private List getResources() { |
86 |
return new ArrayList<>(resources.values()); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Adds a new {@link ResourceProvider} to the list of Resources managed by
|
91 |
* this group.
|
92 |
*
|
93 |
* @param parameters
|
94 |
* to create the resource from
|
95 |
* @param defaultResource
|
96 |
* if the added resource is to be used as the multi resource
|
97 |
* default one
|
98 |
* @throws InitializeException
|
99 |
* if there is an error adding the resource
|
100 |
*/
|
101 |
public void addResource(ResourceParameters parameters, |
102 |
boolean defaultResource) throws InitializeException { |
103 |
String name=(String)parameters.getDynValue("name"); |
104 |
commonAddResource(name, parameters,defaultResource); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* Adds a new {@link ResourceProvider} to the list of Resources managed by
|
109 |
* this group.
|
110 |
*
|
111 |
* @param name
|
112 |
* of the resource to add
|
113 |
* @param params
|
114 |
* parameters to create the resource with
|
115 |
* @param defaultResource
|
116 |
* if the added resource is to be used as the multi resource
|
117 |
* default one
|
118 |
* @throws InitializeException
|
119 |
* if there is an error adding the resource
|
120 |
*/
|
121 |
public void addResource(String name, Object[] params, |
122 |
boolean defaultResource) throws InitializeException { |
123 |
commonAddResource(name, params,defaultResource); |
124 |
} |
125 |
|
126 |
private void commonAddResource(String name, Object params, |
127 |
boolean defaultResource) throws InitializeException { |
128 |
String multiresourceName=null; |
129 |
try {
|
130 |
multiresourceName=this.getName();
|
131 |
} catch (AccessResourceException e1) {
|
132 |
LOG.warn("Imposible to get MultiResource name");
|
133 |
} |
134 |
Resource res = (Resource)(this.resources.get(name));
|
135 |
if( res!=null ) { |
136 |
return;
|
137 |
} |
138 |
if (isFrozen){
|
139 |
LOG.warn("Added resource "+name+" to a frozen multiresource "+multiresourceName); |
140 |
} |
141 |
ResourceProvider resourceProvider =null;
|
142 |
if (params instanceof ResourceParameters){ |
143 |
resourceProvider = ((ResourceManagerProviderServices) DALLocator. |
144 |
getResourceManager()).createAddResource((ResourceParameters)params); |
145 |
}else if (params instanceof Object[]){ |
146 |
resourceProvider = ((ResourceManagerProviderServices) DALLocator. |
147 |
getResourceManager()).createAddResource(name,(Object[])params); |
148 |
}else{
|
149 |
LOG.warn("Resource could not be created "+name);
|
150 |
} |
151 |
try {
|
152 |
resources.put(resourceProvider.getName(),resourceProvider); |
153 |
} catch (AccessResourceException e) {
|
154 |
LOG.warn("Resource not added "+name+ " to "+multiresourceName); |
155 |
} |
156 |
if (defaultResource) {
|
157 |
defaultResourcePos = resources.size() - 1;
|
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
public boolean isThis(ResourceParameters parameters) |
165 |
throws ResourceException {
|
166 |
if (parameters instanceof MultiResourceParameters) { |
167 |
MultiResourceParameters multiParams = |
168 |
(MultiResourceParameters) parameters; |
169 |
return multiParams.getName() != null |
170 |
&& multiParams.getName().equals( |
171 |
getMultiResourceParameters().getName()); |
172 |
} |
173 |
|
174 |
return false; |
175 |
} |
176 |
|
177 |
public void notifyChanges() throws ResourceNotifyChangesException { |
178 |
List resources = getResources();
|
179 |
for (int i = 0; i < resources.size(); i++) { |
180 |
((ResourceProvider) getResources().get(i)).notifyChanges(); |
181 |
} |
182 |
super.notifyChanges();
|
183 |
} |
184 |
|
185 |
public void notifyClose() throws ResourceNotifyCloseException { |
186 |
List resources = getResources();
|
187 |
for (int i = 0; i < resources.size(); i++) { |
188 |
((ResourceProvider) getResources().get(i)).notifyClose(); |
189 |
} |
190 |
super.notifyClose();
|
191 |
} |
192 |
|
193 |
public void notifyDispose() throws ResourceNotifyDisposeException { |
194 |
List resources = getResources();
|
195 |
for (int i = 0; i < resources.size(); i++) { |
196 |
((ResourceProvider) getResources().get(i)).notifyDispose(); |
197 |
} |
198 |
super.notifyDispose();
|
199 |
} |
200 |
|
201 |
public void notifyOpen() throws ResourceNotifyOpenException { |
202 |
List resources = getResources();
|
203 |
for (int i = 0; i < resources.size(); i++) { |
204 |
((ResourceProvider) getResources().get(i)).notifyOpen(); |
205 |
} |
206 |
super.notifyOpen();
|
207 |
} |
208 |
|
209 |
public void prepare() throws PrepareResourceException { |
210 |
List resources = getResources();
|
211 |
for (int i = 0; i < resources.size(); i++) { |
212 |
((ResourceProvider) getResources().get(i)).prepare(); |
213 |
} |
214 |
super.prepare();
|
215 |
} |
216 |
|
217 |
public void closeRequest() throws ResourceException { |
218 |
List resources = getResources();
|
219 |
for (int i = 0; i < resources.size(); i++) { |
220 |
((ResourceProvider) getResources().get(i)).closeRequest(); |
221 |
} |
222 |
super.closeRequest();
|
223 |
} |
224 |
|
225 |
public Object get() throws AccessResourceException { |
226 |
ResourceProvider defaultResource = getDefaultResource(); |
227 |
if (defaultResource == null) { |
228 |
return null; |
229 |
} else {
|
230 |
return defaultResource.get();
|
231 |
} |
232 |
} |
233 |
|
234 |
public Object getData() { |
235 |
ResourceProvider defaultResource = getDefaultResource(); |
236 |
if (defaultResource == null) { |
237 |
return null; |
238 |
} else {
|
239 |
return defaultResource.getData();
|
240 |
} |
241 |
} |
242 |
|
243 |
private MultiResourceParameters getMultiResourceParameters() {
|
244 |
return (MultiResourceParameters) getParameters();
|
245 |
} |
246 |
|
247 |
public String getName() throws AccessResourceException { |
248 |
String name ="MultiResource "+ getMultiResourceParameters().getName(); |
249 |
return name;
|
250 |
/*
|
251 |
StringBuffer buffer =
|
252 |
new StringBuffer().append("MultiResource ").append(
|
253 |
getMultiResourceParameters().getName()).append(":: ");
|
254 |
List resources = getResources();
|
255 |
for (int i = 0; i < resources.size(); i++) {
|
256 |
buffer.append(((ResourceProvider) resources.get(i)).getName());
|
257 |
if (defaultResourcePos == i) {
|
258 |
buffer.append(" (default)");
|
259 |
}
|
260 |
if (i < resources.size() - 1) {
|
261 |
buffer.append(", ");
|
262 |
}
|
263 |
}
|
264 |
return buffer.toString();
|
265 |
*/
|
266 |
} |
267 |
|
268 |
public String toString() { |
269 |
try {
|
270 |
return getName();
|
271 |
} catch (AccessResourceException e) {
|
272 |
LOG.error("Error getting the resource name", e);
|
273 |
} |
274 |
return super.toString(); |
275 |
} |
276 |
|
277 |
private ResourceProvider getDefaultResource() {
|
278 |
if (getResources().size() > 0) { |
279 |
return (ResourceProvider) getResources().get(defaultResourcePos);
|
280 |
} else {
|
281 |
return null; |
282 |
} |
283 |
} |
284 |
|
285 |
public void setData(Object data) { |
286 |
ResourceProvider defaultResource = getDefaultResource(); |
287 |
if (defaultResource != null) { |
288 |
defaultResource.setData(data); |
289 |
} |
290 |
} |
291 |
|
292 |
/**
|
293 |
* Blocks the adding methods. It is called when all their resources components are added.
|
294 |
*/
|
295 |
public void frozen(){ |
296 |
this.isFrozen=true; |
297 |
} |
298 |
|
299 |
/**
|
300 |
* Locks the multiresource components to avoid another concurrent modification
|
301 |
*/
|
302 |
@Override
|
303 |
public Object execute(ResourceAction action) |
304 |
throws ResourceExecuteException {
|
305 |
Object value = null; |
306 |
synchronized (lock) {
|
307 |
Iterator it=resources.values().iterator();
|
308 |
while (it.hasNext()){
|
309 |
AbstractResource resource=(AbstractResource)it.next(); |
310 |
resource.multiResourcelock=this.lock;
|
311 |
} |
312 |
executeBegins(); |
313 |
try {
|
314 |
value = performExecution(action); |
315 |
} catch (Exception e) { |
316 |
throw new ResourceExecuteException(this, e); |
317 |
} finally {
|
318 |
executeEnds(); |
319 |
} |
320 |
} |
321 |
return value;
|
322 |
} |
323 |
|
324 |
public void addMultiResourceConsumer(ResourceConsumer consumer) { |
325 |
super.addConsumer(consumer);
|
326 |
Iterator it=resources.values().iterator();
|
327 |
while (it.hasNext()){
|
328 |
AbstractResource resource=(AbstractResource)it.next(); |
329 |
resource.addConsumer(consumer); |
330 |
} |
331 |
} |
332 |
|
333 |
} |