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