Statistics
| Revision:

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

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, new Object[] { parameters });
317
                        if (server == null) {
318
                                throw new ProviderNotRegisteredException(name);
319
                        }
320
                        server.initialize(new DefaultDataServerExplorerProviderServices());
321
                        return server;
322
                } catch (InstantiationException e) {
323
                        throw new InitializeException(e);
324
                } catch (IllegalAccessException e) {
325
                        throw new InitializeException(e);
326
                } catch (SecurityException e) {
327
                        throw new InitializeException(e);
328
                } catch (IllegalArgumentException e) {
329
                        throw new InitializeException(e);
330
                } catch (NoSuchMethodException e) {
331
                        throw new InitializeException(e);
332
                } catch (InvocationTargetException e) {
333
                        throw new InitializeException(e);
334
                }
335
        }
336

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

    
342

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

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

    
381
        /*
382
         * ====================================================================
383
         *
384
         * Index related services
385
         */
386

    
387

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

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

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

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

    
406
                        if (name == null) {
407
                                name = getDefaultFeatureIndexProviderName(attr.getDataType());
408
                        }
409

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

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

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

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

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

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

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

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

    
474
        }
475

    
476
}