Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / impl / DefaultDataManager.java @ 29445

History | View | Annotate | Download (15.5 KB)

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

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

    
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.DataManager;
11
import org.gvsig.fmap.dal.DataServerExplorer;
12
import org.gvsig.fmap.dal.DataServerExplorerParameters;
13
import org.gvsig.fmap.dal.DataStore;
14
import org.gvsig.fmap.dal.DataStoreParameters;
15
import org.gvsig.fmap.dal.exception.InitializeException;
16
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
17
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
18
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
19
import org.gvsig.fmap.dal.feature.FeatureStore;
20
import org.gvsig.fmap.dal.feature.FeatureType;
21
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndex;
22
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
23
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
24
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
25
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
26
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
27
import org.gvsig.fmap.dal.raster.impl.DefaultCoverageStore;
28
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
29
import org.gvsig.fmap.dal.resource.ResourceManager;
30
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
31
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
32
import org.gvsig.fmap.dal.spi.DataStoreProvider;
33
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.evaluator.Evaluator;
36
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
37
import org.gvsig.tools.operations.OperationManager;
38

    
39
public class DefaultDataManager implements DataManager, DataManagerProviderServices {
40

    
41
        final static private String DATA_MANAGER_STORE = "Data.manager.stores";
42
        final static private String DATA_MANAGER_STORE_DESCRIPTION = "DAL stores providers";
43

    
44
        final static private String DATA_MANAGER_STORE_PARAMS = "Data.manager.stores.params";
45
        final static private String DATA_MANAGER_STORE_PARAMS_DESCRIPTION = "DAL stores providers parameters";
46

    
47
        final static private String DATA_MANAGER_EXPLORER = "Data.manager.explorers";
48
        final static private String DATA_MANAGER_EXPLORER_DESCRIPTION = "DAL explorers providers";
49

    
50
        final static private String DATA_MANAGER_EXPLORER_PARAMS = "Data.manager.explorers.params";
51
        final static private String DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION = "DAL explorer providers parameters";
52

    
53
        final static private String DATA_MANAGER_INDEX = "Data.manager.indexes";
54
        final static private String DATA_MANAGER_INDEX_DESCRIPTION = "DAL index providers";
55

    
56
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR = "Data.manager.expresion.evaluator";
57
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT = "default";
58
        final static private String DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION = "DAL expresion evaluators.";
59

    
60
        /** This map contains the name of the default provider for each data type */
61
        private Map defaultDataIndexProviders;
62

    
63

    
64
        public DefaultDataManager() {
65
                /*
66
                 * Create te extensions point in te registry.
67
                 */
68
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE,
69
                                DATA_MANAGER_STORE_DESCRIPTION);
70

    
71
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE_PARAMS,
72
                                DATA_MANAGER_STORE_PARAMS_DESCRIPTION);
73

    
74
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_EXPLORER,
75
                                DATA_MANAGER_EXPLORER_DESCRIPTION);
76

    
77
                ToolsLocator.getExtensionPointManager().add(
78
                                DATA_MANAGER_EXPLORER_PARAMS,
79
                                DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION);
80

    
81
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_INDEX,
82
                                DATA_MANAGER_INDEX_DESCRIPTION);
83

    
84
                ToolsLocator.getExtensionPointManager().add(
85
                                DATA_MANAGER_EXPRESION_EVALUATOR,
86
                                DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION);
87

    
88
                initializeIndexes();
89
        }
90

    
91
        /**
92
         *
93
         * @return ResourceManager
94
         */
95

    
96
        public ResourceManager getResourceManager() {
97
                return DALLocator.getResourceManager();
98
        }
99

    
100
        public OperationManager getOperationManager() {
101
                return ToolsLocator.getOperationManager();
102
        }
103

    
104
        public String getTemporaryDirectory() {
105
                // FIXME Define a better tempdir solution
106
                String tmp = System.getenv("TMP");
107
                if (tmp == null) {
108
                        tmp = System.getenv("TEMP");
109
                }
110
                if (tmp == null) {
111
                        File tmp_file = new File(System.getenv("HOME"), "_daltmp_");
112
                        int i=1;
113
                        while (!tmp_file.exists() || !tmp_file.isDirectory()){
114
                                tmp_file = new File(tmp_file.getAbsolutePath()+i);
115
                                i++;
116
                        }
117
                        if (!tmp_file.exists()){
118
                                tmp_file.mkdir();
119
                        }
120
                        tmp = tmp_file.getAbsolutePath();
121
                }
122
                return tmp;
123
        }
124

    
125
        /*
126
         * ====================================================================
127
         *
128
         * Store related services
129
         */
130
        public void registerStoreProvider(String name,
131
                        Class storeProviderClass,
132
                        Class parametersClass) {
133
                if (name == null || storeProviderClass == null || parametersClass == null){
134
                        // FIXME Exception
135
                        throw new IllegalArgumentException(
136
                                        "Any parameters can be null");
137
                }
138

    
139
                if (!DataStoreParameters.class.isAssignableFrom(parametersClass)) {
140
                        // FIXME Exception
141
                        throw new IllegalArgumentException(parametersClass.getName()
142
                                        + " must implement org.gvsig.fmap.dal.DataStoreParameters");
143
                }
144

    
145

    
146

    
147
                if (CoverageStoreProvider.class.isAssignableFrom(storeProviderClass)) {
148

    
149

    
150
                } else if (FeatureStoreProvider.class
151
                                .isAssignableFrom(storeProviderClass)) {
152

    
153

    
154
                } else{
155
                        // FIXME Exception
156
                        throw new IllegalArgumentException(
157
                                        "Not supported implemtation: name=" + name
158
                                                        + " provider class="
159
                                                        + storeProviderClass.getName());
160
                }
161

    
162
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE,
163
                                DATA_MANAGER_STORE_DESCRIPTION).append(name, null,
164
                                storeProviderClass);
165

    
166
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_STORE_PARAMS,
167
                                DATA_MANAGER_STORE_PARAMS_DESCRIPTION).append(name, null,
168
                                parametersClass);
169
        }
170

    
171
        public DataStoreParameters createStoreParameters(String name)
172
                        throws InitializeException, ProviderNotRegisteredException {
173
                try {
174
                        DataStoreParameters params = (DataStoreParameters) ToolsLocator
175
                                        .getExtensionPointManager().get(DATA_MANAGER_STORE_PARAMS)
176
                                        .create(name);
177
                        if (params == null) {
178
                                throw new ProviderNotRegisteredException(name);
179
                        }
180
                        return params;
181
                } catch (InstantiationException e) {
182
                        throw new InitializeException(e);
183
                } catch (IllegalAccessException e) {
184
                        throw new InitializeException(e);
185
                } catch (SecurityException e) {
186
                        throw new InitializeException(e);
187
                } catch (IllegalArgumentException e) {
188
                        throw new InitializeException(e);
189
                }
190
        }
191

    
192
        public DataStore createStore(DataStoreParameters parameters)
193
                        throws InitializeException, ProviderNotRegisteredException,
194
                        ValidateDataParametersException {
195
                String name = parameters.getDataStoreName();
196

    
197
                parameters.validate();
198

    
199
                DataStore store;
200
                Extension storeProviderExtension = ToolsLocator
201
                                .getExtensionPointManager().get(DATA_MANAGER_STORE).get(name);
202

    
203

    
204

    
205
                if (storeProviderExtension == null) {
206
                        throw new ProviderNotRegisteredException(name);
207
                }
208

    
209
                Class providerClass = storeProviderExtension.getExtension();
210
                if (providerClass == null) {
211
                        throw new ProviderNotRegisteredException(name);
212
                }
213

    
214
                if (CoverageStoreProvider.class.isAssignableFrom(providerClass)) {
215
                        store =  new DefaultCoverageStore();
216

    
217

    
218
                } else if (FeatureStoreProvider.class.isAssignableFrom(providerClass)) {
219

    
220
                        store = new DefaultFeatureStore();
221

    
222
                } else{
223
                        // FIXME Exception
224
                        throw new InitializeException(new RuntimeException(
225
                                        "Not supported implemtation: name=" + name
226
                                        + " provider class="
227
                                        + providerClass.getName()));
228
                }
229

    
230

    
231

    
232
                this.intializeDataStore((DataStoreImplementation) store, parameters);
233

    
234
                return store;
235
        }
236

    
237
        public List getStoreProviders() {
238
                return ToolsLocator.getExtensionPointManager().get(DATA_MANAGER_STORE)
239
                                .getNames();
240
        }
241

    
242
        /*
243
         * ====================================================================
244
         *
245
         * Explorer related services
246
         */
247
        public void registerExplorerProvider(String name, Class explorerClass,
248
                        Class parametersClass) {
249

    
250
                if (name == null || explorerClass == null || parametersClass == null) {
251
                        // FIXME Exception
252
                        throw new IllegalArgumentException("Any parameters can be null");
253
                }
254

    
255

    
256
                if (!DataServerExplorerParameters.class
257
                                .isAssignableFrom(parametersClass)) {
258
                        // FIXME Exception
259
                        throw new IllegalArgumentException(
260
                                        parametersClass.getName()
261
                                                        + " must implement org.gvsig.fmap.dal.DataServerExplorerParameters");
262
                }
263

    
264
                if (!DataServerExplorer.class.isAssignableFrom(explorerClass)) {
265
                        // FIXME Exception
266
                        throw new IllegalArgumentException(explorerClass.getName()
267
                                        + " must implement org.gvsig.fmap.dal.DataServerExplorer");
268
                }
269

    
270

    
271
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_EXPLORER,
272
                                DATA_MANAGER_EXPLORER_DESCRIPTION).append(name, null,
273
                                explorerClass);
274

    
275
                ToolsLocator.getExtensionPointManager().add(
276
                                DATA_MANAGER_EXPLORER_PARAMS,
277
                                DATA_MANAGER_EXPLORER_PARAMS_DESCRIPTION).append(name, null,
278
                                parametersClass);
279
        }
280

    
281
        public DataServerExplorerParameters createServerExplorerParameters(String name)
282
                        throws InitializeException, ProviderNotRegisteredException {
283
                try {
284
                        DataServerExplorerParameters params = (DataServerExplorerParameters) ToolsLocator
285
                                        .getExtensionPointManager()
286
                                        .get(
287
                                                        DATA_MANAGER_EXPLORER_PARAMS)
288
                                        .create(name);
289
                        if (params == null) {
290
                                throw new ProviderNotRegisteredException(name);
291
                        }
292
                        return params;
293
                } catch (InstantiationException e) {
294
                        throw new InitializeException(e);
295
                } catch (IllegalAccessException e) {
296
                        throw new InitializeException(e);
297
                } catch (SecurityException e) {
298
                        throw new InitializeException(e);
299
                } catch (IllegalArgumentException e) {
300
                        throw new InitializeException(e);
301
                }
302
        }
303

    
304
        public DataServerExplorer createServerExplorer(DataServerExplorerParameters parameters)
305
                        throws InitializeException, ProviderNotRegisteredException,
306
                        ValidateDataParametersException {
307

    
308
                parameters.validate();
309

    
310
                String name = parameters.getExplorerName();
311

    
312
                try {
313
                        DataServerExplorerProvider server = (DataServerExplorerProvider) ToolsLocator
314
                                        .getExtensionPointManager().get(DATA_MANAGER_EXPLORER)
315
                                        .create(
316
                                                        name,
317
                                                        new Object[] {
318
                                                                        parameters,
319
                                                                        new DefaultDataServerExplorerProviderServices() });
320
                        if (server == null) {
321
                                throw new ProviderNotRegisteredException(name);
322
                        }
323
                        return server;
324
                } catch (InstantiationException e) {
325
                        throw new InitializeException(e);
326
                } catch (IllegalAccessException e) {
327
                        throw new InitializeException(e);
328
                } catch (SecurityException e) {
329
                        throw new InitializeException(e);
330
                } catch (IllegalArgumentException e) {
331
                        throw new InitializeException(e);
332
                } catch (NoSuchMethodException e) {
333
                        throw new InitializeException(e);
334
                } catch (InvocationTargetException e) {
335
                        throw new InitializeException(e);
336
                }
337
        }
338

    
339
        public List getExplorerProviders() {
340
                return ToolsLocator.getExtensionPointManager().get(
341
                                DATA_MANAGER_EXPLORER).getNames();
342
        }
343

    
344

    
345
        /*
346
         * ====================================================================
347
         *
348
         * Expresion evaluation related services
349
         */
350
        public Evaluator createExpresion(String expresion)
351
                        throws InitializeException {
352
                try {
353
                        return (Evaluator) ToolsLocator.getExtensionPointManager().get(
354
                                        DATA_MANAGER_EXPRESION_EVALUATOR).create(
355
                                        DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT,
356
                                        new Object[] { expresion });
357
                } catch (SecurityException e) {
358
                        throw new InitializeException(e);
359
                } catch (IllegalArgumentException e) {
360
                        throw new InitializeException(e);
361
                } catch (NoSuchMethodException e) {
362
                        throw new InitializeException(e);
363
                } catch (InstantiationException e) {
364
                        throw new InitializeException(e);
365
                } catch (IllegalAccessException e) {
366
                        throw new InitializeException(e);
367
                } catch (InvocationTargetException e) {
368
                        throw new InitializeException(e);
369
                }
370
        }
371

    
372
        public void registerDefaultEvaluator(Class evaluatorClass) {
373
                if (!Evaluator.class.isAssignableFrom(evaluatorClass)) {
374
                        throw new ClassCastException();
375
                }
376
                ToolsLocator.getExtensionPointManager().add(
377
                                DATA_MANAGER_EXPRESION_EVALUATOR,
378
                                DATA_MANAGER_EXPRESION_EVALUATOR_DESCRIPTION).append(
379
                                DATA_MANAGER_EXPRESION_EVALUATOR_DEFAULT,
380
                                "Default expresion evaluator for use in DAL", evaluatorClass);
381
        }
382

    
383
        /*
384
         * ====================================================================
385
         *
386
         * Index related services
387
         */
388

    
389

    
390
        public List getFeatureIndexProviders() {
391
                return ToolsLocator.getExtensionPointManager().get(DATA_MANAGER_INDEX)
392
                .getNames();
393
        }
394

    
395
        public void setDefaultFeatureIndexProviderName(int dataType, String name) {
396
                defaultDataIndexProviders.put(new Integer(dataType), name);
397
        }
398

    
399
        public String getDefaultFeatureIndexProviderName(int dataType) {
400
                return (String) defaultDataIndexProviders.get(new Integer(dataType));
401
        }
402

    
403
        public FeatureIndexProviderServices createFeatureIndexProvider(
404
                        String name, FeatureStore store, FeatureType type, String indexName,
405
                        FeatureAttributeDescriptor attr)
406
                        throws InitializeException, ProviderNotRegisteredException {
407

    
408
                        if (name == null) {
409
                                name = getDefaultFeatureIndexProviderName(attr.getDataType());
410
                        }
411

    
412
                        if (name == null) {
413
                                throw new InitializeException("There not any index provider registered.", null);
414
                        }
415

    
416
                        try {
417
                                FeatureIndexProvider provider = (FeatureIndexProvider) ToolsLocator
418
                                                .getExtensionPointManager()
419
                                                .get(
420
                                                                DATA_MANAGER_INDEX)
421
                                                .create(name);
422
                                if (provider == null) {
423
                                        throw new ProviderNotRegisteredException(name);
424
                                }
425
                                FeatureIndexProviderServices services = new DefaultFeatureIndex((FeatureStoreProviderServices) store, type, provider, attr.getName(), indexName);
426
                                services.initialize();
427
                                return services;
428
                        } catch (InstantiationException e) {
429
                                throw new InitializeException(e);
430
                        } catch (IllegalAccessException e) {
431
                                throw new InitializeException(e);
432
                        } catch (SecurityException e) {
433
                                throw new InitializeException(e);
434
                        } catch (IllegalArgumentException e) {
435
                                throw new InitializeException(e);
436
                        }
437
        }
438

    
439
        public void registerFeatureIndexProvider(String name, String description,
440
                        Class clazz, int dataType) {
441
                ToolsLocator.getExtensionPointManager().add(DATA_MANAGER_INDEX,
442
                                DATA_MANAGER_INDEX_DESCRIPTION).append(name, null,
443
                                clazz);
444

    
445
                if (getDefaultFeatureIndexProviderName(dataType) == null) {
446
                        setDefaultFeatureIndexProviderName(dataType, name);
447
                }
448
        }
449

    
450
        private void initializeIndexes() {
451
                this.defaultDataIndexProviders = new HashMap();
452
        }
453

    
454
        public void intializeDataStore(DataStoreImplementation store,
455
                        DataStoreParameters parameters)
456
                        throws InitializeException, ProviderNotRegisteredException {
457

    
458
                store.intializePhase1(this, parameters);
459
                String name = parameters.getDataStoreName();
460
                DataStoreProvider provider;
461
                try {
462
                        provider = (DataStoreProvider) ToolsLocator
463
                                        .getExtensionPointManager().get(DATA_MANAGER_STORE).create(
464
                                                        name,
465
                                                        new Object[] { parameters,
466
                                                                        (DataStoreProviderServices) store });
467

    
468
                } catch (Exception e) {
469
                        throw new InitializeException(e);
470
                }
471
                if (provider == null) {
472
                        throw new ProviderNotRegisteredException(name);
473
                }
474
                store.intializePhase2(provider);
475

    
476
        }
477

    
478
}