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 24496 jmvivo
package org.gvsig.fmap.dal.resource.impl;
2 19399 vcaballero
3 23894 jjdelcerro
import java.lang.reflect.InvocationTargetException;
4 24198 jmvivo
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 19399 vcaballero
11 24496 jmvivo
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 24198 jmvivo
import org.gvsig.tools.ToolsLocator;
24 23066 jmvivo
import org.gvsig.tools.observer.Observer;
25 24268 jjdelcerro
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
26 23898 jjdelcerro
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28 19399 vcaballero
29 20580 jmvivo
30 23894 jjdelcerro
public class DefaultResourceManager implements ResourceManagerProviderServices {
31 20580 jmvivo
32 23894 jjdelcerro
        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 24198 jmvivo
        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 23894 jjdelcerro
39 20580 jmvivo
        private Map resources = new HashMap();
40 19608 jmvivo
41 24268 jjdelcerro
        private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(this);
42 20580 jmvivo
43 23507 jmvivo
        private Timer timer = null;
44 23898 jjdelcerro
        private Logger logger;
45 23507 jmvivo
46 24198 jmvivo
        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 23898 jjdelcerro
        public Logger getLogger() {
59
                if (this.logger == null) {
60
                        this.logger = LoggerFactory.getLogger(this.getClass());
61
                }
62
                return this.logger;
63
        }
64
65 23894 jjdelcerro
        public synchronized void remove(Resource resource)
66 23507 jmvivo
                        throws DataException {
67 23894 jjdelcerro
                remove(resource.getName());
68 19399 vcaballero
        }
69 19608 jmvivo
70 23894 jjdelcerro
        public synchronized void remove(String name)
71 23820 jjdelcerro
                        throws DataException {
72 23894 jjdelcerro
                ResourceProvider res = (ResourceProvider) this.resources.get(name);
73 20624 jmvivo
                if (res == null){
74 23894 jjdelcerro
                        throw new IllegalArgumentException("Resource not register:" + name);
75 20624 jmvivo
                }
76 23894 jjdelcerro
                if (res.getConsumersCount() < 1) {
77
                        this.resources.remove(name);
78
                        res.notifyDispose();
79 20624 jmvivo
                }
80
                res = null;
81 19399 vcaballero
        }
82
83 20580 jmvivo
        public Resource getResource(String key){
84
                return (Resource)this.resources.get(key);
85
        }
86 19608 jmvivo
87 20624 jmvivo
        public Iterator iterator(){
88
                return this.resources.values().iterator();
89
        }
90
91 21045 jmvivo
        public void addObserver(Observer o) {
92 23894 jjdelcerro
                // TODO a?adir el observador a los recursos que ya existiesen
93 24178 cordinyana
                this.delegateObservable.addObserver(o);
94 19608 jmvivo
        }
95
96 21045 jmvivo
        public void deleteObserver(Observer o) {
97 24178 cordinyana
                this.delegateObservable.deleteObserver(o);
98 20580 jmvivo
        }
99
100
        public void deleteObservers() {
101 24178 cordinyana
                this.delegateObservable.deleteObservers();
102 20580 jmvivo
        }
103
104 23894 jjdelcerro
        public synchronized void collectResources() throws
105 23507 jmvivo
                        DataException {
106 23894 jjdelcerro
                ResourceProvider res;
107 23507 jmvivo
                Iterator iter = this.resources.keySet().iterator();
108
                String key;
109
                while (iter.hasNext()) {
110
                        key = (String) iter.next();
111 23894 jjdelcerro
                        res = (ResourceProvider) this.resources.get(key);
112
                        if (res.getConsumersCount() < 1) {
113
                                res.notifyDispose();
114
                                iter.remove();
115 23507 jmvivo
                        }
116
117
                }
118 23894 jjdelcerro
        }
119 23507 jmvivo
120 23894 jjdelcerro
        private AbstractResource findResource(ResourceParameters params) {
121
                AbstractResource res;
122
                Iterator iter = this.resources.values().iterator();
123 23507 jmvivo
                while (iter.hasNext()) {
124 23894 jjdelcerro
                        res = (AbstractResource) iter.next();
125 23898 jjdelcerro
                        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 23894 jjdelcerro
                        }
140 23507 jmvivo
                }
141 23894 jjdelcerro
                return null;
142
        }
143 23507 jmvivo
144 23898 jjdelcerro
        private AbstractResource addResource(AbstractResource resource)
145
                        throws AccessResourceException {
146 23894 jjdelcerro
                resources.put(resource.getName(), resource);
147 24178 cordinyana
                resource.addObservers(this.delegateObservable);
148 23894 jjdelcerro
                return resource;
149 23507 jmvivo
        }
150
151 23894 jjdelcerro
        public void startResourceCollector(long milis, Observer observer) {
152 23507 jmvivo
                if (this.timer == null){
153
                        this.timer = new Timer();
154
                } else{
155
                        this.timer.cancel();
156
                }
157 23820 jjdelcerro
                // TODO observer
158 23507 jmvivo
                this.timer.scheduleAtFixedRate(new TimerTask() {
159
160
161
                        public void run() {
162
                                try {
163 23894 jjdelcerro
                                        DALLocator.getResourceManager().collectResources();
164 23507 jmvivo
                                } catch (DataException e) {
165 23820 jjdelcerro
                                        // TODO Notificar con el observer
166 23507 jmvivo
                                }
167
                        }
168
169 23894 jjdelcerro
                }, milis, milis);
170 23507 jmvivo
        }
171
172 23894 jjdelcerro
        public void stopResourceCollector() {
173 23507 jmvivo
                if (this.timer != null) {
174
                        this.timer.cancel();
175
                }
176
177
        }
178 23894 jjdelcerro
179
        public DataParameters createParameters(String type, Object[] args)
180
                        throws InitializeException {
181
                try {
182
                        if (args == null) {
183 24198 jmvivo
                                return (DataParameters) ToolsLocator.getExtensionPointManager()
184
                                                .get(
185 24204 jmvivo
                                                DATA_MANAGER_RESOURCE_PARAMS).create(type);
186 23894 jjdelcerro
                        } else {
187 24198 jmvivo
                                return (DataParameters) ToolsLocator.getExtensionPointManager()
188 24204 jmvivo
                                                .get(DATA_MANAGER_RESOURCE_PARAMS).create(type, args);
189 23894 jjdelcerro
                        }
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 24198 jmvivo
226
                        resource = (AbstractResource) ToolsLocator
227
                                        .getExtensionPointManager().get(DATA_MANAGER_RESOURCE)
228
                                        .create(
229
                                                        params.getTypeName(), new Object[] { params }
230 23894 jjdelcerro
                                );
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 23898 jjdelcerro
                try {
246
                        return addResource(resource);
247
                } catch (AccessResourceException e) {
248
                        throw new InitializeException(e);
249
                }
250 23894 jjdelcerro
        }
251
252
        public boolean register(String type, String description,
253
                        Class resourceHandler, Class resourceParams) {
254
255
256 24198 jmvivo
                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 23894 jjdelcerro
                return true;
266
        }
267
268 24185 jjdelcerro
        public List getResourceProviders() {
269 24198 jmvivo
                return ToolsLocator.getExtensionPointManager().get(
270
                                DATA_MANAGER_RESOURCE).getNames();
271 24130 jiyarza
        }
272
273 19399 vcaballero
}