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