Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / resource / impl / DefaultResourceManager.java @ 24496

History | View | Annotate | Download (7.86 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.Timer;
9
import java.util.TimerTask;
10

    
11
import org.gvsig.fmap.dal.DALLocator;
12
import org.gvsig.fmap.dal.DataParameters;
13
import org.gvsig.fmap.dal.exceptions.CopyParametersException;
14
import org.gvsig.fmap.dal.exceptions.DataException;
15
import org.gvsig.fmap.dal.exceptions.InitializeException;
16
import org.gvsig.fmap.dal.resource.Resource;
17
import org.gvsig.fmap.dal.resource.ResourceParameters;
18
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
19
import org.gvsig.fmap.dal.resource.exception.ResourceException;
20
import org.gvsig.fmap.dal.resource.spi.AbstractResource;
21
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
22
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
23
import org.gvsig.tools.ToolsLocator;
24
import org.gvsig.tools.observer.Observer;
25
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

    
29

    
30
public class DefaultResourceManager implements ResourceManagerProviderServices {
31

    
32
        final static private String DATA_MANAGER_RESOURCE = "Data.manager.resources";
33
        final static private String DATA_MANAGER_RESOURCE_PARAMS = "Data.manager.resources.params";
34

    
35
        // FIXME: rellenar las cadenas
36
        private static final String DATA_MANAGER_RESOURCE_DESCRIPTION = "DAL Resources types";
37
        private static final String DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION = "DAL Resources types Parameters";
38

    
39
        private Map resources = new HashMap();
40

    
41
        private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(this);
42

    
43
        private Timer timer = null;
44
        private Logger logger;
45

    
46
        public DefaultResourceManager() {
47
                /*
48
                 * Create te extensions point in te registry.
49
                 */
50
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE,
51
                                DATA_MANAGER_RESOURCE_DESCRIPTION);
52

    
53
                ToolsLocator.getExtensionPointManager().add(
54
                                DATA_MANAGER_RESOURCE_PARAMS,
55
                                DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION);
56
        }
57

    
58
        public Logger getLogger() {
59
                if (this.logger == null) {
60
                        this.logger = LoggerFactory.getLogger(this.getClass());
61
                }
62
                return this.logger;
63
        }
64

    
65
        public synchronized void remove(Resource resource)
66
                        throws DataException {
67
                remove(resource.getName());
68
        }
69

    
70
        public synchronized void remove(String name)
71
                        throws DataException {
72
                ResourceProvider res = (ResourceProvider) this.resources.get(name);
73
                if (res == null){
74
                        throw new IllegalArgumentException("Resource not register:" + name);
75
                }
76
                if (res.getConsumersCount() < 1) {
77
                        this.resources.remove(name);
78
                        res.notifyDispose();
79
                }
80
                res = null;
81
        }
82

    
83
        public Resource getResource(String key){
84
                return (Resource)this.resources.get(key);
85
        }
86

    
87
        public Iterator iterator(){
88
                return this.resources.values().iterator();
89
        }
90

    
91
        public void addObserver(Observer o) {
92
                // TODO a?adir el observador a los recursos que ya existiesen
93
                this.delegateObservable.addObserver(o);
94
        }
95

    
96
        public void deleteObserver(Observer o) {
97
                this.delegateObservable.deleteObserver(o);
98
        }
99

    
100
        public void deleteObservers() {
101
                this.delegateObservable.deleteObservers();
102
        }
103

    
104
        public synchronized void collectResources() throws
105
                        DataException {
106
                ResourceProvider res;
107
                Iterator iter = this.resources.keySet().iterator();
108
                String key;
109
                while (iter.hasNext()) {
110
                        key = (String) iter.next();
111
                        res = (ResourceProvider) this.resources.get(key);
112
                        if (res.getConsumersCount() < 1) {
113
                                res.notifyDispose();
114
                                iter.remove();
115
                        }
116

    
117
                }
118
        }
119

    
120
        private AbstractResource findResource(ResourceParameters params) {
121
                AbstractResource res;
122
                Iterator iter = this.resources.values().iterator();
123
                while (iter.hasNext()) {
124
                        res = (AbstractResource) iter.next();
125
                        try {
126
                                if (res.isThis(params)) {
127
                                        return res;
128
                                }
129
                        } catch (ResourceException e) {
130
                                getLogger()
131
                                                .warn(
132
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
133
                                                                e);
134
                        } catch (CopyParametersException e) {
135
                                getLogger()
136
                                                .warn(
137
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
138
                                                                e);
139
                        }
140
                }
141
                return null;
142
        }
143

    
144
        private AbstractResource addResource(AbstractResource resource)
145
                        throws AccessResourceException {
146
                resources.put(resource.getName(), resource);
147
                resource.addObservers(this.delegateObservable);
148
                return resource;
149
        }
150

    
151
        public void startResourceCollector(long milis, Observer observer) {
152
                if (this.timer == null){
153
                        this.timer = new Timer();
154
                } else{
155
                        this.timer.cancel();
156
                }
157
                // TODO observer
158
                this.timer.scheduleAtFixedRate(new TimerTask() {
159

    
160

    
161
                        public void run() {
162
                                try {
163
                                        DALLocator.getResourceManager().collectResources();
164
                                } catch (DataException e) {
165
                                        // TODO Notificar con el observer
166
                                }
167
                        }
168

    
169
                }, milis, milis);
170
        }
171

    
172
        public void stopResourceCollector() {
173
                if (this.timer != null) {
174
                        this.timer.cancel();
175
                }
176

    
177
        }
178

    
179
        public DataParameters createParameters(String type, Object[] args)
180
                        throws InitializeException {
181
                try {
182
                        if (args == null) {
183
                                return (DataParameters) ToolsLocator.getExtensionPointManager()
184
                                                .get(
185
                                                DATA_MANAGER_RESOURCE_PARAMS).create(type);
186
                        } else {
187
                                return (DataParameters) ToolsLocator.getExtensionPointManager()
188
                                                .get(DATA_MANAGER_RESOURCE_PARAMS).create(type, args);
189
                        }
190
                } catch (InstantiationException e) {
191
                        throw new InitializeException(e);
192
                } catch (IllegalAccessException e) {
193
                        throw new InitializeException(e);
194
                } catch (SecurityException e) {
195
                        throw new InitializeException(e);
196
                } catch (IllegalArgumentException e) {
197
                        throw new InitializeException(e);
198
                } catch (NoSuchMethodException e) {
199
                        throw new InitializeException(e);
200
                } catch (InvocationTargetException e) {
201
                        throw new InitializeException(e);
202
                }
203
        }
204

    
205
        public DataParameters createParameters(String type)
206
                        throws InitializeException {
207
                return createParameters(type, null);
208
        }
209

    
210
        public ResourceProvider createResource(String type, Object[] params)
211
                        throws InitializeException {
212
                return createResource((ResourceParameters) createParameters(type,
213
                                params));
214
        }
215

    
216
        public ResourceProvider createResource(ResourceParameters params)
217
                        throws InitializeException {
218

    
219
                AbstractResource resource = this.findResource(params);
220
                if (resource != null) {
221
                        return resource;
222
                }
223

    
224
                try {
225

    
226
                        resource = (AbstractResource) ToolsLocator
227
                                        .getExtensionPointManager().get(DATA_MANAGER_RESOURCE)
228
                                        .create(
229
                                                        params.getTypeName(), new Object[] { params }
230
                                );
231
                } catch (InstantiationException e) {
232
                        throw new InitializeException(e);
233
                } catch (IllegalAccessException e) {
234
                        throw new InitializeException(e);
235
                } catch (SecurityException e) {
236
                        throw new InitializeException(e);
237
                } catch (IllegalArgumentException e) {
238
                        throw new InitializeException(e);
239
                } catch (NoSuchMethodException e) {
240
                        throw new InitializeException(e);
241
                } catch (InvocationTargetException e) {
242
                        throw new InitializeException(e);
243
                }
244

    
245
                try {
246
                        return addResource(resource);
247
                } catch (AccessResourceException e) {
248
                        throw new InitializeException(e);
249
                }
250
        }
251

    
252
        public boolean register(String type, String description,
253
                        Class resourceHandler, Class resourceParams) {
254

    
255

    
256
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_RESOURCE,
257
                                DATA_MANAGER_RESOURCE_DESCRIPTION).append(type, description,
258
                                                resourceHandler);
259

    
260
                ToolsLocator.getExtensionPointManager().add(
261
                                DATA_MANAGER_RESOURCE_PARAMS,
262
                                DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION).append(type,
263
                                description, resourceParams);
264

    
265
                return true;
266
        }
267

    
268
        public List getResourceProviders() {
269
                return ToolsLocator.getExtensionPointManager().get(
270
                                DATA_MANAGER_RESOURCE).getNames();
271
        }
272

    
273
}