svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / resource / impl / DefaultResourceManager.java @ 42609
History | View | Annotate | Download (12 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.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.lang.reflect.InvocationTargetException; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
import java.util.Map.Entry; |
33 |
import java.util.Timer; |
34 |
import java.util.TimerTask; |
35 |
|
36 |
import org.gvsig.fmap.dal.DALLocator; |
37 |
import org.gvsig.fmap.dal.DataParameters; |
38 |
import org.gvsig.fmap.dal.exception.CopyParametersException; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
41 |
import org.gvsig.fmap.dal.resource.Resource; |
42 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
43 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
44 |
import org.gvsig.fmap.dal.resource.exception.DisposeResorceManagerException; |
45 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
46 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotClosedOnDisposeManagerException; |
47 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotRegisteredException; |
48 |
import org.gvsig.fmap.dal.resource.spi.AbstractResource; |
49 |
import org.gvsig.fmap.dal.resource.spi.MultiResource; |
50 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
51 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
54 |
import org.gvsig.tools.observer.Observer; |
55 |
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
|
60 |
public class DefaultResourceManager implements ResourceManagerProviderServices { |
61 |
|
62 |
final static private String DATA_MANAGER_RESOURCE = "Data.manager.resources"; |
63 |
final static private String DATA_MANAGER_RESOURCE_PARAMS = "Data.manager.resources.params"; |
64 |
|
65 |
// FIXME: rellenar las cadenas
|
66 |
private static final String DATA_MANAGER_RESOURCE_DESCRIPTION = "DAL Resources types"; |
67 |
private static final String DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION = "DAL Resources types Parameters"; |
68 |
|
69 |
private Map<String,Resource> resources = new HashMap(); |
70 |
|
71 |
private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(this); |
72 |
|
73 |
private Timer timer = null; |
74 |
private Logger logger; |
75 |
|
76 |
private long mlsecondsToBeIdle = 0; |
77 |
|
78 |
public DefaultResourceManager() {
|
79 |
/*
|
80 |
* Create te extensions point in te registry.
|
81 |
*/
|
82 |
ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE, |
83 |
DATA_MANAGER_RESOURCE_DESCRIPTION); |
84 |
|
85 |
ToolsLocator.getExtensionPointManager().add( |
86 |
DATA_MANAGER_RESOURCE_PARAMS, |
87 |
DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION); |
88 |
} |
89 |
|
90 |
public Logger getLogger() { |
91 |
if (this.logger == null) { |
92 |
this.logger = LoggerFactory.getLogger(this.getClass()); |
93 |
} |
94 |
return this.logger; |
95 |
} |
96 |
|
97 |
public synchronized void remove(Resource resource) |
98 |
throws DataException {
|
99 |
remove(resource.getName()); |
100 |
} |
101 |
|
102 |
public synchronized void remove(String name) |
103 |
throws DataException {
|
104 |
ResourceProvider res = (ResourceProvider) this.resources.get(name);
|
105 |
if (res == null){ |
106 |
throw new IllegalArgumentException("Resource not register:" + name); |
107 |
} |
108 |
if (res.getConsumersCount() < 1) { |
109 |
this.resources.remove(name);
|
110 |
res.notifyDispose(); |
111 |
} |
112 |
res = null;
|
113 |
} |
114 |
|
115 |
public synchronized Resource getResource(String key) { |
116 |
return (Resource)this.resources.get(key); |
117 |
} |
118 |
|
119 |
public synchronized Iterator<Resource> iterator() { |
120 |
return this.resources.values().iterator(); |
121 |
} |
122 |
|
123 |
public void addObserver(Observer o) { |
124 |
// TODO a?adir el observador a los recursos que ya existiesen
|
125 |
this.delegateObservable.addObserver(o);
|
126 |
} |
127 |
|
128 |
public void deleteObserver(Observer o) { |
129 |
this.delegateObservable.deleteObserver(o);
|
130 |
} |
131 |
|
132 |
public void deleteObservers() { |
133 |
this.delegateObservable.deleteObservers();
|
134 |
} |
135 |
|
136 |
public synchronized void collectResources() throws |
137 |
DataException { |
138 |
ResourceProvider res; |
139 |
Iterator iter = this.resources.entrySet().iterator(); |
140 |
while (iter.hasNext()) {
|
141 |
Entry entry = (Entry) iter.next(); |
142 |
res = (ResourceProvider) entry.getValue(); |
143 |
if (res.getConsumersCount() < 1) { |
144 |
res.closeRequest(); |
145 |
res.notifyDispose(); |
146 |
iter.remove(); |
147 |
continue;
|
148 |
} |
149 |
if (mlsecondsToBeIdle > 0 |
150 |
&& System.currentTimeMillis()
|
151 |
- res.getLastTimeUsed() > mlsecondsToBeIdle) { |
152 |
|
153 |
} |
154 |
|
155 |
} |
156 |
} |
157 |
|
158 |
private synchronized AbstractResource findResource(ResourceParameters params) { |
159 |
AbstractResource res; |
160 |
Iterator iter = this.resources.values().iterator(); |
161 |
while (iter.hasNext()) {
|
162 |
res = (AbstractResource) iter.next(); |
163 |
try {
|
164 |
if (res.isThis(params)) {
|
165 |
return res;
|
166 |
} |
167 |
} catch (ResourceException e) {
|
168 |
getLogger() |
169 |
.warn( |
170 |
"Se ha producido un error comprobando si se puede reutilizar el recurso.",
|
171 |
e); |
172 |
} catch (CopyParametersException e) {
|
173 |
getLogger() |
174 |
.warn( |
175 |
"Se ha producido un error comprobando si se puede reutilizar el recurso.",
|
176 |
e); |
177 |
} |
178 |
} |
179 |
return null; |
180 |
} |
181 |
|
182 |
private synchronized AbstractResource addResource(AbstractResource resource) |
183 |
throws AccessResourceException {
|
184 |
resources.put(resource.getName(), resource); |
185 |
resource.addObservers(this.delegateObservable);
|
186 |
return resource;
|
187 |
} |
188 |
|
189 |
public void startResourceCollector(long milis, Observer observer) { |
190 |
if (this.timer == null){ |
191 |
this.timer = new Timer(); |
192 |
} else{
|
193 |
this.timer.cancel();
|
194 |
} |
195 |
// TODO observer
|
196 |
this.timer.scheduleAtFixedRate(new TimerTask() { |
197 |
|
198 |
|
199 |
public void run() { |
200 |
try {
|
201 |
DALLocator.getResourceManager().collectResources(); |
202 |
} catch (DataException e) {
|
203 |
// TODO Notificar con el observer
|
204 |
} |
205 |
} |
206 |
|
207 |
}, milis, milis); |
208 |
} |
209 |
|
210 |
public void stopResourceCollector() { |
211 |
if (this.timer != null) { |
212 |
this.timer.cancel();
|
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
public DataParameters createParameters(String type, Object[] args) |
218 |
throws InitializeException {
|
219 |
try {
|
220 |
DataParameters parameters; |
221 |
if (args == null) { |
222 |
parameters = (DataParameters) ToolsLocator.getExtensionPointManager() |
223 |
.get( |
224 |
DATA_MANAGER_RESOURCE_PARAMS).create(type); |
225 |
} else {
|
226 |
|
227 |
parameters = (DataParameters) ToolsLocator.getExtensionPointManager() |
228 |
.get(DATA_MANAGER_RESOURCE_PARAMS).create(type, args); |
229 |
} |
230 |
if (parameters == null){ |
231 |
throw new ResourceNotRegisteredException(type); |
232 |
} |
233 |
return parameters;
|
234 |
} catch (InstantiationException e) { |
235 |
throw new InitializeException(type, e); |
236 |
} catch (IllegalAccessException e) { |
237 |
throw new InitializeException(type, e); |
238 |
} catch (SecurityException e) { |
239 |
throw new InitializeException(type, e); |
240 |
} catch (IllegalArgumentException e) { |
241 |
throw new InitializeException(type, e); |
242 |
} catch (NoSuchMethodException e) { |
243 |
throw new InitializeException(type, e); |
244 |
} catch (InvocationTargetException e) { |
245 |
throw new InitializeException(type, e); |
246 |
} |
247 |
} |
248 |
|
249 |
public DataParameters createParameters(String type) |
250 |
throws InitializeException {
|
251 |
return createParameters(type, null); |
252 |
} |
253 |
|
254 |
public ResourceProvider createAddResource(String type, Object[] params) |
255 |
throws InitializeException {
|
256 |
return createAddResource((ResourceParameters) createParameters(type,
|
257 |
params)); |
258 |
} |
259 |
|
260 |
public ResourceProvider createResource(String type, Object[] params) |
261 |
throws InitializeException {
|
262 |
return createResource((ResourceParameters) createParameters(type,
|
263 |
params)); |
264 |
} |
265 |
|
266 |
public ResourceProvider createResource(ResourceParameters params)
|
267 |
throws InitializeException {
|
268 |
AbstractResource resource = this.findResource(params);
|
269 |
if (resource != null) { |
270 |
return resource;
|
271 |
} |
272 |
|
273 |
try {
|
274 |
|
275 |
resource = (AbstractResource) ToolsLocator |
276 |
.getExtensionPointManager().get(DATA_MANAGER_RESOURCE) |
277 |
.create( |
278 |
params.getTypeName(), new Object[] { params } |
279 |
); |
280 |
if (resource == null) { |
281 |
throw new ResourceNotRegisteredException(params.getTypeName()); |
282 |
} |
283 |
} catch (InstantiationException e) { |
284 |
throw new InitializeException(e); |
285 |
} catch (IllegalAccessException e) { |
286 |
throw new InitializeException(e); |
287 |
} catch (SecurityException e) { |
288 |
throw new InitializeException(e); |
289 |
} catch (IllegalArgumentException e) { |
290 |
throw new InitializeException(e); |
291 |
} catch (NoSuchMethodException e) { |
292 |
throw new InitializeException(e); |
293 |
} catch (InvocationTargetException e) { |
294 |
throw new InitializeException(e); |
295 |
} |
296 |
|
297 |
return resource;
|
298 |
} |
299 |
|
300 |
public ResourceProvider createAddResource(ResourceParameters params)
|
301 |
throws InitializeException {
|
302 |
|
303 |
try {
|
304 |
String name = params.getResurceID();
|
305 |
Resource res = (Resource)(this.resources.get(name));
|
306 |
if( res!=null ) { |
307 |
return (AbstractResource)res;
|
308 |
} |
309 |
return addResource((AbstractResource) createResource(params));
|
310 |
} catch (AccessResourceException e) {
|
311 |
throw new InitializeException(params.getTypeName(), e); |
312 |
} |
313 |
} |
314 |
|
315 |
public boolean register(String type, String description, |
316 |
Class resourceHandler, Class resourceParams) { |
317 |
|
318 |
|
319 |
ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE, |
320 |
DATA_MANAGER_RESOURCE_DESCRIPTION).append(type, description, |
321 |
resourceHandler); |
322 |
|
323 |
ToolsLocator.getExtensionPointManager().add( |
324 |
DATA_MANAGER_RESOURCE_PARAMS, |
325 |
DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION).append(type, |
326 |
description, resourceParams); |
327 |
|
328 |
return true; |
329 |
} |
330 |
|
331 |
public List getResourceProviders() { |
332 |
return ToolsLocator.getExtensionPointManager().get(
|
333 |
DATA_MANAGER_RESOURCE).getNames(); |
334 |
} |
335 |
|
336 |
public synchronized void closeResources() throws DataException { |
337 |
this.collectResources();
|
338 |
Resource res; |
339 |
Iterator iter = this.resources.values().iterator(); |
340 |
while (iter.hasNext()) {
|
341 |
res = (Resource) iter.next(); |
342 |
res.closeRequest(); |
343 |
} |
344 |
} |
345 |
|
346 |
public synchronized void dispose() throws DisposeResorceManagerException { |
347 |
DisposeResorceManagerException exception = new DisposeResorceManagerException();
|
348 |
|
349 |
try {
|
350 |
this.collectResources();
|
351 |
} catch (DataException e) {
|
352 |
exception.add(e); |
353 |
} |
354 |
Resource res; |
355 |
Iterator iter = this.resources.values().iterator(); |
356 |
while (iter.hasNext()) {
|
357 |
res = (Resource) iter.next(); |
358 |
try {
|
359 |
if (res.openCount() > 0) { |
360 |
exception.add( |
361 |
new ResourceNotClosedOnDisposeManagerException(
|
362 |
res)); |
363 |
} |
364 |
res.closeRequest(); |
365 |
iter.remove(); |
366 |
} catch (ResourceException e) {
|
367 |
exception.add(e); |
368 |
} |
369 |
} |
370 |
|
371 |
this.resources = null; |
372 |
this.delegateObservable.deleteObservers();
|
373 |
this.delegateObservable = null; |
374 |
|
375 |
if (!exception.isEmpty()) {
|
376 |
throw exception;
|
377 |
} |
378 |
} |
379 |
|
380 |
public int getTimeToBeIdle() { |
381 |
if (mlsecondsToBeIdle == 0) { |
382 |
return 0; |
383 |
} |
384 |
return (int) (mlsecondsToBeIdle / 1000); |
385 |
} |
386 |
|
387 |
public void setTimeToBeIdle(int seconds) { |
388 |
if (seconds < 0) { |
389 |
throw new IllegalArgumentException("seconds must be >= 0"); |
390 |
} |
391 |
mlsecondsToBeIdle = seconds * 1000;
|
392 |
} |
393 |
} |