Statistics
| Revision:

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

History | View | Annotate | Download (12.2 KB)

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

    
3
import java.lang.reflect.InvocationTargetException;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7

    
8
import org.gvsig.fmap.dal.DALLocator;
9
import org.gvsig.fmap.dal.DataExplorer;
10
import org.gvsig.fmap.dal.DataExplorerParameters;
11
import org.gvsig.fmap.dal.DataManager;
12
import org.gvsig.fmap.dal.DataStore;
13
import org.gvsig.fmap.dal.DataStoreParameters;
14
import org.gvsig.fmap.dal.exceptions.InitializeException;
15
import org.gvsig.fmap.dal.exceptions.ProviderNotRegisteredException;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndex;
20
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
21
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
22
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
23
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
24
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
25
import org.gvsig.fmap.dal.resource.ResourceManager;
26
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
27
import org.gvsig.fmap.dal.spi.DataStoreProvider;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.evaluator.Evaluator;
30
import org.gvsig.tools.operations.OperationManager;
31

    
32
public class DefaultDataManager implements DataManager, DataManagerProviderServices {
33

    
34
        final static private String DATA_MANAGER_STORE = "Data.manager.stores";
35
        final static private String DATA_MANAGER_STORE_DESCRIPTION = "DAL stores providers";
36

    
37
        final static private String DATA_MANAGER_STORE_PARAMS = "Data.manager.stores.params";
38
        final static private String DATA_MANAGER_STORE_PARAMS_DESCRIPTION = "DAL stores providers parameters";
39

    
40
        final static private String DATA_MANAGER_EXPLORER = "Data.manager.explorers";
41
        final static private String DATA_MANAGER_EXPLORER_DESCRIPTION = "DAL explorers providers";
42

    
43
        final static private String DATA_MANAGER_EXPLORER_PARAMS = "Data.manager.explorers.params";
44
        final static private String DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION = "DAL explorer providers parameters";
45

    
46
        final static private String DATA_MANAGER_INDEX = "Data.manager.indexes";
47
        final static private String DATA_MANAGER_INDEX_DESCRIPTION = "DAL index providers";
48

    
49
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR = "Data.manager.expresion.evaluator";
50
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT = "default";
51
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION = "DAL expresion evaluators.";
52

    
53
        /** This map contains the name of the default provider for each data type */
54
        private Map defaultDataIndexProviders;
55

    
56

    
57
        public DefaultDataManager() {
58
                /*
59
                 * Create te extensions point in te registry.
60
                 */
61
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE,
62
                                DATA_MANAGER_STORE_DESCRIPTION);
63

    
64
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE_PARAMS,
65
                                DATA_MANAGER_STORE_PARAMS_DESCRIPTION);
66

    
67
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_EXPLORER,
68
                                DATA_MANAGER_EXPLORER_DESCRIPTION);
69

    
70
                ToolsLocator.getExtensionPointManager().add(
71
                                DATA_MANAGER_EXPLORER_PARAMS,
72
                                DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION);
73

    
74
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_INDEX,
75
                                DATA_MANAGER_INDEX_DESCRIPTION);
76

    
77
                ToolsLocator.getExtensionPointManager().add(
78
                                DATA_MANAGER_EXPRESION_EVALUATOR,
79
                                DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION);
80

    
81
                initializeIndexes();
82
        }
83

    
84
        /**
85
         *
86
         * @return ResourceManager
87
         */
88

    
89
        public ResourceManager getResourceManager() {
90
                return DALLocator.getResourceManager();
91
        }
92

    
93
        public OperationManager getOperationManager() {
94
                return ToolsLocator.getOperationManager();
95
        }
96

    
97
        public String getTemporaryDirectory() {
98
                // FIXME Define a better tempdir solution
99
                String tmp = System.getenv("TMP");
100
                if (tmp == null) {
101
                        tmp = System.getenv("TEMP");
102
                }
103
                if (tmp == null) {
104
                        tmp = System.getenv("HOME");
105
                }
106
                return tmp;
107
        }
108

    
109
        /*
110
         * ====================================================================
111
         *
112
         * Store related services
113
         */
114
        public void registerStoreProvider(String name,
115
                        Class storeProviderClass,
116
                        Class parametersClass) {
117

    
118
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE,
119
                                DATA_MANAGER_STORE_DESCRIPTION).append(name, null,
120
                                storeProviderClass);
121

    
122
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE_PARAMS,
123
                                DATA_MANAGER_STORE_PARAMS_DESCRIPTION).append(name, null,
124
                                parametersClass);
125
        }
126

    
127
        public DataStoreParameters createStoreParameters(String name)
128
                        throws InitializeException, ProviderNotRegisteredException {
129
                try {
130
                        DataStoreParameters params = (DataStoreParameters) ToolsLocator
131
                                        .getExtensionPointManager().get(DATA_MANAGER_STORE_PARAMS)
132
                                        .create(name);
133
                        if (params == null) {
134
                                throw new ProviderNotRegisteredException(name);
135
                        }
136
                        return params;
137
                } catch (InstantiationException e) {
138
                        throw new InitializeException(e);
139
                } catch (IllegalAccessException e) {
140
                        throw new InitializeException(e);
141
                } catch (SecurityException e) {
142
                        throw new InitializeException(e);
143
                } catch (IllegalArgumentException e) {
144
                        throw new InitializeException(e);
145
                }
146
        }
147

    
148
        public DataStore createStore(DataStoreParameters parameters)
149
                        throws InitializeException, ProviderNotRegisteredException {
150
                String name = parameters.getDataStoreName();
151

    
152
                try {
153
                        DataStoreProvider storeProvider = (DataStoreProvider) ToolsLocator
154
                                        .getExtensionPointManager().get(DATA_MANAGER_STORE).create(
155
                                                        name, new Object[] { parameters });
156
                        if (storeProvider == null) {
157
                                throw new ProviderNotRegisteredException(name);
158
                        }
159

    
160
                        return new DefaultFeatureStore(this, parameters,
161
                                        (FeatureStoreProvider) storeProvider);
162
                } catch (InstantiationException e) {
163
                        throw new InitializeException(e);
164
                } catch (IllegalAccessException e) {
165
                        throw new InitializeException(e);
166
                } catch (SecurityException e) {
167
                        throw new InitializeException(e);
168
                } catch (IllegalArgumentException e) {
169
                        throw new InitializeException(e);
170
                } catch (NoSuchMethodException e) {
171
                        throw new InitializeException(e);
172
                } catch (InvocationTargetException e) {
173
                        throw new InitializeException(e);
174
                }
175
        }
176

    
177
        public List getStoreProviders() {
178
                return ToolsLocator.getExtensionPointManager().get(DATA_MANAGER_STORE)
179
                                .getNames();
180
        }
181

    
182
        /*
183
         * ====================================================================
184
         *
185
         * Explorer related services
186
         */
187
        public void registerExplorerProvider(String name, Class explorerClass,
188
                        Class parametersClass) {
189

    
190
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_EXPLORER,
191
                                DATA_MANAGER_EXPLORER_DESCRIPTION).append(name, null,
192
                                explorerClass);
193

    
194
                ToolsLocator.getExtensionPointManager().add(
195
                                DATA_MANAGER_EXPLORER_PARAMS,
196
                                DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION).append(name, null,
197
                                parametersClass);
198
        }
199

    
200
        public DataExplorerParameters createExplorerParameters(String name)
201
                        throws InitializeException, ProviderNotRegisteredException {
202
                try {
203
                        DataExplorerParameters params = (DataExplorerParameters) ToolsLocator
204
                                        .getExtensionPointManager()
205
                                        .get(
206
                                                        DATA_MANAGER_EXPLORER_PARAMS)
207
                                        .create(name);
208
                        if (params == null) {
209
                                throw new ProviderNotRegisteredException(name);
210
                        }
211
                        return params;
212
                } catch (InstantiationException e) {
213
                        throw new InitializeException(e);
214
                } catch (IllegalAccessException e) {
215
                        throw new InitializeException(e);
216
                } catch (SecurityException e) {
217
                        throw new InitializeException(e);
218
                } catch (IllegalArgumentException e) {
219
                        throw new InitializeException(e);
220
                }
221
        }
222

    
223
        public DataExplorer createExplorer(DataExplorerParameters parameters)
224
                        throws InitializeException, ProviderNotRegisteredException {
225

    
226
                String name = parameters.getType().getName();
227

    
228
                try {
229
                        DataExplorer explorer = (DataExplorer) ToolsLocator
230
                                        .getExtensionPointManager().get(DATA_MANAGER_STORE).create(
231
                                                        name, new Object[] { parameters });
232
                        if (explorer == null) {
233
                                throw new ProviderNotRegisteredException(name);
234
                        }
235
                        return explorer;
236
                } catch (InstantiationException e) {
237
                        throw new InitializeException(e);
238
                } catch (IllegalAccessException e) {
239
                        throw new InitializeException(e);
240
                } catch (SecurityException e) {
241
                        throw new InitializeException(e);
242
                } catch (IllegalArgumentException e) {
243
                        throw new InitializeException(e);
244
                } catch (NoSuchMethodException e) {
245
                        throw new InitializeException(e);
246
                } catch (InvocationTargetException e) {
247
                        throw new InitializeException(e);
248
                }
249
        }
250

    
251
        public List getExplorerProviders() {
252
                return ToolsLocator.getExtensionPointManager().get(
253
                                DATA_MANAGER_EXPLORER).getNames();
254
        }
255

    
256

    
257
        /*
258
         * ====================================================================
259
         *
260
         * Expresion evaluation related services
261
         */
262
        public Evaluator createExpresion(String expresion)
263
                        throws InitializeException {
264
                try {
265
                        return (Evaluator) ToolsLocator.getExtensionPointManager().get(
266
                                        DATA_MANAGER_EXPRESION_EVALUATOR).create(
267
                                        DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT,
268
                                        new Object[] { expresion });
269
                } catch (SecurityException e) {
270
                        throw new InitializeException(e);
271
                } catch (IllegalArgumentException e) {
272
                        throw new InitializeException(e);
273
                } catch (NoSuchMethodException e) {
274
                        throw new InitializeException(e);
275
                } catch (InstantiationException e) {
276
                        throw new InitializeException(e);
277
                } catch (IllegalAccessException e) {
278
                        throw new InitializeException(e);
279
                } catch (InvocationTargetException e) {
280
                        throw new InitializeException(e);
281
                }
282
        }
283

    
284
        public void registerDefaultEvaluator(Class evaluatorClass) {
285
                if (!evaluatorClass.isInstance(Evaluator.class)) {
286
                        throw new ClassCastException();
287
                }
288
                ToolsLocator.getExtensionPointManager().add(
289
                                DATA_MANAGER_EXPRESION_EVALUATOR,
290
                                DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION).append(
291
                                DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT,
292
                                "Default expresion evaluator for use in DAL", evaluatorClass);
293
        }
294

    
295
        /*
296
         * ====================================================================
297
         *
298
         * Index related services
299
         */
300

    
301

    
302
        public List getFeatureIndexProviders() {
303
                return ToolsLocator.getExtensionPointManager().get(DATA_MANAGER_INDEX)
304
                .getNames();
305
        }
306

    
307
        public void setDefaultFeatureIndexProviderName(int dataType, String name) {
308
                defaultDataIndexProviders.put(new Integer(dataType), name);
309
        }
310

    
311
        public String getDefaultFeatureIndexProviderName(int dataType) {
312
                return (String) defaultDataIndexProviders.get(new Integer(dataType));
313
        }
314

    
315
        public FeatureIndexProviderServices createFeatureIndexProvider(
316
                        String name, FeatureStore store, FeatureType type, String indexName,
317
                        FeatureAttributeDescriptor attr)
318
                        throws InitializeException, ProviderNotRegisteredException {
319

    
320
                        if (name == null) {
321
                                name = getDefaultFeatureIndexProviderName(attr.getDataType());
322
                        }
323

    
324
                        try {
325
                                FeatureIndexProvider provider = (FeatureIndexProvider) ToolsLocator
326
                                                .getExtensionPointManager()
327
                                                .get(
328
                                                                DATA_MANAGER_INDEX)
329
                                                .create(name);
330
                                if (provider == null) {
331
                                        throw new ProviderNotRegisteredException(name);
332
                                }
333
                                FeatureIndexProviderServices services = new DefaultFeatureIndex((FeatureStoreProviderServices) store, type, provider, attr.getName(), indexName);
334
                                services.initialize();
335
                                return services;
336
                        } catch (InstantiationException e) {
337
                                throw new InitializeException(e);
338
                        } catch (IllegalAccessException e) {
339
                                throw new InitializeException(e);
340
                        } catch (SecurityException e) {
341
                                throw new InitializeException(e);
342
                        } catch (IllegalArgumentException e) {
343
                                throw new InitializeException(e);
344
                        }
345
        }
346

    
347
        public void registerFeatureIndexProvider(String name, String description,
348
                        Class clazz, int dataType) {
349
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_INDEX,
350
                                DATA_MANAGER_INDEX_DESCRIPTION).append(name, null,
351
                                clazz);
352

    
353
                if (getDefaultFeatureIndexProviderName(dataType) == null) {
354
                        setDefaultFeatureIndexProviderName(dataType, name);
355
                }
356
        }
357

    
358
        private void initializeIndexes() {
359
                this.defaultDataIndexProviders = new HashMap();
360
        }
361

    
362
}