Statistics
| Revision:

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 @ 40435

History | View | Annotate | Download (10.4 KB)

1
package org.gvsig.fmap.dal.resource.impl;
2

    
3
import java.lang.reflect.InvocationTargetException;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Map.Entry;
9
import java.util.Timer;
10
import java.util.TimerTask;
11

    
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

    
15
import org.gvsig.fmap.dal.DALLocator;
16
import org.gvsig.fmap.dal.DataParameters;
17
import org.gvsig.fmap.dal.exception.CopyParametersException;
18
import org.gvsig.fmap.dal.exception.DataException;
19
import org.gvsig.fmap.dal.exception.InitializeException;
20
import org.gvsig.fmap.dal.resource.Resource;
21
import org.gvsig.fmap.dal.resource.ResourceParameters;
22
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
23
import org.gvsig.fmap.dal.resource.exception.DisposeResorceManagerException;
24
import org.gvsig.fmap.dal.resource.exception.ResourceException;
25
import org.gvsig.fmap.dal.resource.exception.ResourceNotClosedOnDisposeManagerException;
26
import org.gvsig.fmap.dal.resource.exception.ResourceNotRegisteredException;
27
import org.gvsig.fmap.dal.resource.spi.AbstractResource;
28
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
29
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.observer.Observer;
32
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
33

    
34

    
35
public class DefaultResourceManager implements ResourceManagerProviderServices {
36

    
37
        final static private String DATA_MANAGER_RESOURCE = "Data.manager.resources";
38
        final static private String DATA_MANAGER_RESOURCE_PARAMS = "Data.manager.resources.params";
39

    
40
        // FIXME: rellenar las cadenas
41
        private static final String DATA_MANAGER_RESOURCE_DESCRIPTION = "DAL Resources types";
42
        private static final String DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION = "DAL Resources types Parameters";
43

    
44
        private Map resources = new HashMap();
45

    
46
        private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(this);
47

    
48
        private Timer timer = null;
49
        private Logger logger;
50

    
51
        private long mlsecondsToBeIdle = 0;
52

    
53
        public DefaultResourceManager() {
54
                /*
55
                 * Create te extensions point in te registry.
56
                 */
57
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE,
58
                                DATA_MANAGER_RESOURCE_DESCRIPTION);
59

    
60
                ToolsLocator.getExtensionPointManager().add(
61
                                DATA_MANAGER_RESOURCE_PARAMS,
62
                                DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION);
63
        }
64

    
65
        public Logger getLogger() {
66
                if (this.logger == null) {
67
                        this.logger = LoggerFactory.getLogger(this.getClass());
68
                }
69
                return this.logger;
70
        }
71

    
72
        public synchronized void remove(Resource resource)
73
                        throws DataException {
74
                remove(resource.getName());
75
        }
76

    
77
        public synchronized void remove(String name)
78
                        throws DataException {
79
                ResourceProvider res = (ResourceProvider) this.resources.get(name);
80
                if (res == null){
81
                        throw new IllegalArgumentException("Resource not register:" + name);
82
                }
83
                if (res.getConsumersCount() < 1) {
84
                        this.resources.remove(name);
85
                        res.notifyDispose();
86
                }
87
                res = null;
88
        }
89

    
90
    public synchronized Resource getResource(String key) {
91
                return (Resource)this.resources.get(key);
92
        }
93

    
94
    public synchronized Iterator iterator() {
95
                return this.resources.values().iterator();
96
        }
97

    
98
        public void addObserver(Observer o) {
99
                // TODO a?adir el observador a los recursos que ya existiesen
100
                this.delegateObservable.addObserver(o);
101
        }
102

    
103
        public void deleteObserver(Observer o) {
104
                this.delegateObservable.deleteObserver(o);
105
        }
106

    
107
        public void deleteObservers() {
108
                this.delegateObservable.deleteObservers();
109
        }
110

    
111
        public synchronized void collectResources() throws
112
                        DataException {
113
                ResourceProvider res;
114
                Iterator iter = this.resources.entrySet().iterator();
115
                while (iter.hasNext()) {
116
                        Entry entry = (Entry) iter.next();
117
                        res = (ResourceProvider) entry.getValue();
118
                        if (res.getConsumersCount() < 1) {
119
                                res.closeRequest();
120
                                res.notifyDispose();
121
                                iter.remove();
122
                                continue;
123
                        }
124
                        if (mlsecondsToBeIdle > 0
125
                                        && System.currentTimeMillis()
126
                                                        - res.getLastTimeUsed() > mlsecondsToBeIdle) {
127

    
128
                        }
129

    
130
                }
131
        }
132

    
133
    private synchronized AbstractResource findResource(ResourceParameters params) {
134
                AbstractResource res;
135
                Iterator iter = this.resources.values().iterator();
136
                while (iter.hasNext()) {
137
                        res = (AbstractResource) iter.next();
138
                        try {
139
                                if (res.isThis(params)) {
140
                                        return res;
141
                                }
142
                        } catch (ResourceException e) {
143
                                getLogger()
144
                                                .warn(
145
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
146
                                                                e);
147
                        } catch (CopyParametersException e) {
148
                                getLogger()
149
                                                .warn(
150
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
151
                                                                e);
152
                        }
153
                }
154
                return null;
155
        }
156

    
157
    private synchronized AbstractResource addResource(AbstractResource resource)
158
                        throws AccessResourceException {
159
                resources.put(resource.getName(), resource);
160
                resource.addObservers(this.delegateObservable);
161
                return resource;
162
        }
163

    
164
        public void startResourceCollector(long milis, Observer observer) {
165
                if (this.timer == null){
166
                        this.timer = new Timer();
167
                } else{
168
                        this.timer.cancel();
169
                }
170
                // TODO observer
171
                this.timer.scheduleAtFixedRate(new TimerTask() {
172

    
173

    
174
                        public void run() {
175
                                try {
176
                                        DALLocator.getResourceManager().collectResources();
177
                                } catch (DataException e) {
178
                                        // TODO Notificar con el observer
179
                                }
180
                        }
181

    
182
                }, milis, milis);
183
        }
184

    
185
        public void stopResourceCollector() {
186
                if (this.timer != null) {
187
                        this.timer.cancel();
188
                }
189

    
190
        }
191

    
192
        public DataParameters createParameters(String type, Object[] args)
193
                        throws InitializeException {
194
                try {
195
                        DataParameters parameters;
196
                        if (args == null) {
197
                                parameters = (DataParameters) ToolsLocator.getExtensionPointManager()
198
                                                .get(
199
                                                DATA_MANAGER_RESOURCE_PARAMS).create(type);
200
                        } else {
201

    
202
                                parameters = (DataParameters) ToolsLocator.getExtensionPointManager()
203
                                                .get(DATA_MANAGER_RESOURCE_PARAMS).create(type, args);
204
                        }
205
                        if (parameters == null){
206
                                throw new ResourceNotRegisteredException(type);
207
                        }
208
                        return parameters;
209
                } catch (InstantiationException e) {
210
                        throw new InitializeException(type, e);
211
                } catch (IllegalAccessException e) {
212
                        throw new InitializeException(type, e);
213
                } catch (SecurityException e) {
214
                        throw new InitializeException(type, e);
215
                } catch (IllegalArgumentException e) {
216
                        throw new InitializeException(type, e);
217
                } catch (NoSuchMethodException e) {
218
                        throw new InitializeException(type, e);
219
                } catch (InvocationTargetException e) {
220
                        throw new InitializeException(type, e);
221
                }
222
        }
223

    
224
        public DataParameters createParameters(String type)
225
                        throws InitializeException {
226
                return createParameters(type, null);
227
        }
228

    
229
        public ResourceProvider createAddResource(String type, Object[] params)
230
                        throws InitializeException {
231
                return createAddResource((ResourceParameters) createParameters(type,
232
                                params));
233
        }
234

    
235
        public ResourceProvider createResource(String type, Object[] params)
236
                        throws InitializeException {
237
                return createResource((ResourceParameters) createParameters(type,
238
                                params));
239
        }
240

    
241
        public ResourceProvider createResource(ResourceParameters params)
242
                        throws InitializeException {
243
                AbstractResource resource = this.findResource(params);
244
                if (resource != null) {
245
                        return resource;
246
                }
247

    
248
                try {
249

    
250
                        resource = (AbstractResource) ToolsLocator
251
                                        .getExtensionPointManager().get(DATA_MANAGER_RESOURCE)
252
                                        .create(
253
                                                        params.getTypeName(), new Object[] { params }
254
                                );
255
                        if (resource == null) {
256
                                throw new ResourceNotRegisteredException(params.getTypeName());
257
                        }
258
                } catch (InstantiationException e) {
259
                        throw new InitializeException(e);
260
                } catch (IllegalAccessException e) {
261
                        throw new InitializeException(e);
262
                } catch (SecurityException e) {
263
                        throw new InitializeException(e);
264
                } catch (IllegalArgumentException e) {
265
                        throw new InitializeException(e);
266
                } catch (NoSuchMethodException e) {
267
                        throw new InitializeException(e);
268
                } catch (InvocationTargetException e) {
269
                        throw new InitializeException(e);
270
                }
271

    
272
                return resource;
273
        }
274

    
275
        public ResourceProvider createAddResource(ResourceParameters params)
276
                        throws InitializeException {
277

    
278
                try {
279
                        return addResource((AbstractResource) createResource(params));
280
                } catch (AccessResourceException e) {
281
                        throw new InitializeException(params.getTypeName(), e);
282
                }
283
        }
284

    
285
        public boolean register(String type, String description,
286
                        Class resourceHandler, Class resourceParams) {
287

    
288

    
289
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE,
290
                                DATA_MANAGER_RESOURCE_DESCRIPTION).append(type, description,
291
                                                resourceHandler);
292

    
293
                ToolsLocator.getExtensionPointManager().add(
294
                                DATA_MANAGER_RESOURCE_PARAMS,
295
                                DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION).append(type,
296
                                description, resourceParams);
297

    
298
                return true;
299
        }
300

    
301
        public List getResourceProviders() {
302
                return ToolsLocator.getExtensionPointManager().get(
303
                                DATA_MANAGER_RESOURCE).getNames();
304
        }
305

    
306
    public synchronized void closeResources() throws DataException {
307
                this.collectResources();
308
                Resource res;
309
                Iterator iter = this.resources.values().iterator();
310
                while (iter.hasNext()) {
311
                        res = (Resource) iter.next();
312
                        res.closeRequest();
313
                }
314
        }
315

    
316
    public synchronized void dispose() throws DisposeResorceManagerException {
317
                DisposeResorceManagerException exception = new DisposeResorceManagerException();
318

    
319
                try {
320
                        this.collectResources();
321
                } catch (DataException e) {
322
                        exception.add(e);
323
                }
324
                Resource res;
325
                Iterator iter = this.resources.values().iterator();
326
                while (iter.hasNext()) {
327
                        res = (Resource) iter.next();
328
                        try {
329
                                res.closeRequest();
330
                                if (res.openCount() > 0) {
331
                                        exception
332
                                                        .add(new ResourceNotClosedOnDisposeManagerException(
333
                                                                        res));
334
                                }
335
                                iter.remove();
336
                        } catch (ResourceException e) {
337
                                exception.add(e);
338
                        }
339
                }
340

    
341
                this.resources = null;
342
                this.delegateObservable.deleteObservers();
343
                this.delegateObservable = null;
344

    
345
                if (!exception.isEmpty()) {
346
                        throw exception;
347
                }
348
        }
349

    
350
        public int getTimeToBeIdle() {
351
                if (mlsecondsToBeIdle == 0) {
352
                        return 0;
353
                }
354
                return (int) (mlsecondsToBeIdle / 1000);
355
        }
356

    
357
        public void setTimeToBeIdle(int seconds) {
358
                if (seconds < 0) {
359
                        throw new IllegalArgumentException("seconds must be >= 0");
360
                }
361
                mlsecondsToBeIdle = seconds * 1000;
362
        }
363

    
364
}