Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / resource / impl / DefaultResourceManager.java @ 24185

History | View | Annotate | Download (7.86 KB)

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

    
3
import java.lang.reflect.InvocationTargetException;
4
import java.util.*;
5

    
6
import org.gvsig.fmap.data.DALLocator;
7
import org.gvsig.fmap.data.DataParameters;
8
import org.gvsig.fmap.data.exceptions.CopyParametersException;
9
import org.gvsig.fmap.data.exceptions.DataException;
10
import org.gvsig.fmap.data.exceptions.InitializeException;
11
import org.gvsig.fmap.data.resource.Resource;
12
import org.gvsig.fmap.data.resource.ResourceParameters;
13
import org.gvsig.fmap.data.resource.exception.AccessResourceException;
14
import org.gvsig.fmap.data.resource.exception.ResourceException;
15
import org.gvsig.fmap.data.resource.spi.AbstractResource;
16
import org.gvsig.fmap.data.resource.spi.ResourceManagerProviderServices;
17
import org.gvsig.fmap.data.resource.spi.ResourceProvider;
18
import org.gvsig.tools.extensionPoint.ExtensionPoint;
19
import org.gvsig.tools.extensionPoint.ExtensionPoints;
20
import org.gvsig.tools.extensionPoint.ExtensionPointsSingleton;
21
import org.gvsig.tools.observer.Observer;
22
import org.gvsig.tools.observer.impl.DelegateObservable;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

    
26

    
27
public class DefaultResourceManager implements ResourceManagerProviderServices {
28

    
29
        final static private String DATA_MANAGER_RESOURCE = "Data.manager.resources";
30
        final static private String DATA_MANAGER_RESOURCE_PARAMS = "Data.manager.resources.params";
31

    
32
        // FIXME: rellenar las cadenas
33
        private static final String DATA_MANAGER_RESOURCE_DESCRIPTION = "XXXXXXXXXX";
34
        private static final String DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION = "XXXXXXXXXX";
35

    
36
        private Map resources = new HashMap();
37

    
38
        private DelegateObservable delegateObservable = new DelegateObservable(this);
39

    
40
        private Timer timer = null;
41
        private Logger logger;
42

    
43
        public Logger getLogger() {
44
                if (this.logger == null) {
45
                        this.logger = LoggerFactory.getLogger(this.getClass());
46
                }
47
                return this.logger;
48
        }
49

    
50
        public synchronized void remove(Resource resource)
51
                        throws DataException {
52
                remove(resource.getName());
53
        }
54

    
55
        public synchronized void remove(String name)
56
                        throws DataException {
57
                ResourceProvider res = (ResourceProvider) this.resources.get(name);
58
                if (res == null){
59
                        throw new IllegalArgumentException("Resource not register:" + name);
60
                }
61
                if (res.getConsumersCount() < 1) {
62
                        this.resources.remove(name);
63
                        res.notifyDispose();
64
                }
65
                res = null;
66
        }
67

    
68
        public Resource getResource(String key){
69
                return (Resource)this.resources.get(key);
70
        }
71

    
72
        public Iterator iterator(){
73
                return this.resources.values().iterator();
74
        }
75

    
76
        public void addObserver(Observer o) {
77
                // TODO a?adir el observador a los recursos que ya existiesen
78
                this.delegateObservable.addObserver(o);
79
        }
80

    
81
        public void deleteObserver(Observer o) {
82
                this.delegateObservable.deleteObserver(o);
83
        }
84

    
85
        public void deleteObservers() {
86
                this.delegateObservable.deleteObservers();
87
        }
88

    
89
        public synchronized void collectResources() throws
90
                        DataException {
91
                ResourceProvider res;
92
                Iterator iter = this.resources.keySet().iterator();
93
                String key;
94
                while (iter.hasNext()) {
95
                        key = (String) iter.next();
96
                        res = (ResourceProvider) this.resources.get(key);
97
                        if (res.getConsumersCount() < 1) {
98
                                res.notifyDispose();
99
                                iter.remove();
100
                        }
101

    
102
                }
103
        }
104

    
105
        private AbstractResource findResource(ResourceParameters params) {
106
                AbstractResource res;
107
                Iterator iter = this.resources.values().iterator();
108
                while (iter.hasNext()) {
109
                        res = (AbstractResource) iter.next();
110
                        try {
111
                                if (res.isThis(params)) {
112
                                        return res;
113
                                }
114
                        } catch (ResourceException e) {
115
                                getLogger()
116
                                                .warn(
117
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
118
                                                                e);
119
                        } catch (CopyParametersException e) {
120
                                getLogger()
121
                                                .warn(
122
                                                                "Se ha producido un error comprobando si se puede reutilizar el recurso.",
123
                                                                e);
124
                        }
125
                }
126
                return null;
127
        }
128

    
129
        private AbstractResource addResource(AbstractResource resource)
130
                        throws AccessResourceException {
131
                resources.put(resource.getName(), resource);
132
                resource.addObservers(this.delegateObservable);
133
                return resource;
134
        }
135

    
136
        public void startResourceCollector(long milis, Observer observer) {
137
                if (this.timer == null){
138
                        this.timer = new Timer();
139
                } else{
140
                        this.timer.cancel();
141
                }
142
                // TODO observer
143
                this.timer.scheduleAtFixedRate(new TimerTask() {
144

    
145

    
146
                        public void run() {
147
                                try {
148
                                        DALLocator.getResourceManager().collectResources();
149
                                } catch (DataException e) {
150
                                        // TODO Notificar con el observer
151
                                }
152
                        }
153

    
154
                }, milis, milis);
155
        }
156

    
157
        public void stopResourceCollector() {
158
                if (this.timer != null) {
159
                        this.timer.cancel();
160
                }
161

    
162
        }
163

    
164
        public DataParameters createParameters(String type, Object[] args)
165
                        throws InitializeException {
166
                ExtensionPoints eps = ExtensionPointsSingleton.getInstance();
167
                ExtensionPoint epParams = (ExtensionPoint) eps
168
                                .get(DATA_MANAGER_RESOURCE_PARAMS);
169
                try {
170
                        if (args == null) {
171
                                return (DataParameters) epParams.create(type);
172
                        } else {
173
                                return (DataParameters) epParams.create(type, args);
174
                        }
175
                } catch (InstantiationException e) {
176
                        throw new InitializeException(e);
177
                } catch (IllegalAccessException e) {
178
                        throw new InitializeException(e);
179
                } catch (SecurityException e) {
180
                        throw new InitializeException(e);
181
                } catch (IllegalArgumentException e) {
182
                        throw new InitializeException(e);
183
                } catch (NoSuchMethodException e) {
184
                        throw new InitializeException(e);
185
                } catch (InvocationTargetException e) {
186
                        throw new InitializeException(e);
187
                }
188
        }
189

    
190
        public DataParameters createParameters(String type)
191
                        throws InitializeException {
192
                return createParameters(type, null);
193
        }
194

    
195
        public ResourceProvider createResource(String type, Object[] params)
196
                        throws InitializeException {
197
                return createResource((ResourceParameters) createParameters(type,
198
                                params));
199
        }
200

    
201
        public ResourceProvider createResource(ResourceParameters params)
202
                        throws InitializeException {
203

    
204
                AbstractResource resource = this.findResource(params);
205
                if (resource != null) {
206
                        return resource;
207
                }
208

    
209
                ExtensionPoints eps = ExtensionPointsSingleton.getInstance();
210
                ExtensionPoint epResources = (ExtensionPoint) eps
211
                                .get(DATA_MANAGER_RESOURCE);
212
                try {
213
                        resource = (AbstractResource) epResources.create(
214
                                        params
215
                                        .getTypeName(),
216
                                        new Object[] { params }
217
                                );
218
                } catch (InstantiationException e) {
219
                        throw new InitializeException(e);
220
                } catch (IllegalAccessException e) {
221
                        throw new InitializeException(e);
222
                } catch (SecurityException e) {
223
                        throw new InitializeException(e);
224
                } catch (IllegalArgumentException e) {
225
                        throw new InitializeException(e);
226
                } catch (NoSuchMethodException e) {
227
                        throw new InitializeException(e);
228
                } catch (InvocationTargetException e) {
229
                        throw new InitializeException(e);
230
                }
231

    
232
                try {
233
                        return addResource(resource);
234
                } catch (AccessResourceException e) {
235
                        throw new InitializeException(e);
236
                }
237
        }
238

    
239
        public boolean register(String type, String description,
240
                        Class resourceHandler, Class resourceParams) {
241
                ExtensionPoints eps = ExtensionPointsSingleton.getInstance();
242
                ExtensionPoint epResources = (ExtensionPoint) eps
243
                                .get(DATA_MANAGER_RESOURCE);
244
                if (epResources == null) {
245
                        epResources = new ExtensionPoint(DATA_MANAGER_RESOURCE,
246
                                        DATA_MANAGER_RESOURCE_DESCRIPTION);
247
                        eps.put(epResources);
248
                }
249
                eps.add(DATA_MANAGER_RESOURCE, type, resourceHandler);
250

    
251
                ExtensionPoint epParams = (ExtensionPoint) eps
252
                                .get(DATA_MANAGER_RESOURCE_PARAMS);
253
                if (epParams == null) {
254
                        epParams = new ExtensionPoint(DATA_MANAGER_RESOURCE_PARAMS,
255
                                        DATA_MANAGER_RESOURCE_PARAMS_DESCRIPTION);
256
                        eps.put(epParams);
257
                }
258
                eps.add(DATA_MANAGER_RESOURCE_PARAMS, type, resourceParams);
259

    
260
                return true;
261
        }
262

    
263
        public List getResourceProviders() {
264
                // TODO Auto-generated method stub
265
                return null;
266
        }
267

    
268
}