Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / serverexplorer / filesystem / impl / DefaultFilesystemServerExplorer.java @ 33844

History | View | Annotate | Download (13.2 KB)

1 24893 jmvivo
package org.gvsig.fmap.dal.serverexplorer.filesystem.impl;
2 23894 jjdelcerro
3
import java.io.File;
4
import java.util.ArrayList;
5 32880 jjdelcerro
import java.util.HashSet;
6 23894 jjdelcerro
import java.util.Iterator;
7
import java.util.List;
8 32880 jjdelcerro
import java.util.Set;
9 23894 jjdelcerro
10 24893 jmvivo
import org.gvsig.fmap.dal.DALFileLocator;
11 24569 jmvivo
import org.gvsig.fmap.dal.DALLocator;
12 32880 jjdelcerro
import org.gvsig.fmap.dal.DataManager;
13 24876 jmvivo
import org.gvsig.fmap.dal.DataServerExplorerParameters;
14 25015 jmvivo
import org.gvsig.fmap.dal.DataStore;
15 24498 jmvivo
import org.gvsig.fmap.dal.DataStoreParameters;
16 24569 jmvivo
import org.gvsig.fmap.dal.NewDataStoreParameters;
17 24502 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
18 24710 jmvivo
import org.gvsig.fmap.dal.exception.FileNotFoundException;
19 24502 jmvivo
import org.gvsig.fmap.dal.exception.InitializeException;
20 24710 jmvivo
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
21 24569 jmvivo
import org.gvsig.fmap.dal.exception.RemoveException;
22 27723 jmvivo
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
23 24893 jmvivo
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter;
24
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
25
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
26
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
27 24959 jmvivo
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
28 24912 jmvivo
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
29 24887 jmvivo
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
30
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
31 31284 cordinyana
import org.gvsig.tools.dispose.impl.AbstractDisposable;
32
import org.gvsig.tools.exception.BaseException;
33 24898 jmvivo
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
34 23894 jjdelcerro
35 31284 cordinyana
public class DefaultFilesystemServerExplorer extends AbstractDisposable
36
                implements FilesystemServerExplorerProviderServices,
37
                FilesystemFileFilter {
38 23894 jjdelcerro
        // FIXME: , IPersistence{
39
40 24893 jmvivo
        FilesystemServerExplorerParameters parameters;
41 29445 jmvivo
42 24569 jmvivo
        private File root;
43 23894 jjdelcerro
44 24569 jmvivo
        private File current;
45 23894 jjdelcerro
46 24887 jmvivo
        private DataServerExplorerProviderServices providerServices;
47 24569 jmvivo
48 26233 vcaballero
        private List serverProviders;
49 24887 jmvivo
50 29445 jmvivo
        public DefaultFilesystemServerExplorer(
51
                        FilesystemServerExplorerParameters parameters,
52
                        DataServerExplorerProviderServices services)
53 23894 jjdelcerro
                        throws InitializeException {
54 24642 jmvivo
                this.parameters = parameters;
55 29445 jmvivo
                this.providerServices = services;
56 24898 jmvivo
                if (this.parameters.getRoot() != null) {
57
                        this.root = new File(this.parameters.getRoot());
58
                }
59
                if (this.parameters.getInitialpath() != null) {
60 24642 jmvivo
                        this.current = new File(this.parameters.getInitialpath());
61
                }
62 32880 jjdelcerro
                if (this.root == null && this.current == null) {
63
                        // throw new InitializeException(this.getName(),
64
                        // new IllegalArgumentException());
65 25164 jmvivo
                } else if (this.current == null) {
66 24959 jmvivo
                        this.current = new File(this.parameters.getRoot());
67
                }
68 23894 jjdelcerro
        }
69
70 32880 jjdelcerro
        /*
71
         * (non-Javadoc)
72
         *
73
         * @see
74
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
75
         * #getParameters()
76 24893 jmvivo
         */
77 24876 jmvivo
        public DataServerExplorerParameters getParameters() {
78 23894 jjdelcerro
                return parameters;
79
        }
80
81 31284 cordinyana
        @Override
82
        protected void doDispose() throws BaseException {
83 24569 jmvivo
                this.parameters = null;
84
                this.root = null;
85 23894 jjdelcerro
        }
86
87 25735 jmvivo
        public List list(int mode) throws DataException {
88 32880 jjdelcerro
                // TODO
89 25735 jmvivo
                return list();
90
        }
91
92 32880 jjdelcerro
        /*
93
         * (non-Javadoc)
94
         *
95
         * @see
96
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
97
         * #list()
98 24893 jmvivo
         */
99 23894 jjdelcerro
        public List list() throws DataException {
100 32880 jjdelcerro
                if (this.current == null) {
101
                        throw new IllegalStateException(); // FIXME
102 25164 jmvivo
                }
103 24569 jmvivo
                if (!this.current.exists()) {
104 32880 jjdelcerro
                        // TODO crear excepcion de Data??
105 24569 jmvivo
                        new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current);
106 23894 jjdelcerro
                }
107 24569 jmvivo
108
                if (!this.current.isDirectory()) {
109 33719 jjdelcerro
                        new IllegalArgumentException(this.getProviderName()
110 32880 jjdelcerro
                                        + ": Path not a directory '" + this.current + "'");
111 24569 jmvivo
                }
112 23894 jjdelcerro
                // DataManager dsm=DataManager.getManager();
113
114 24569 jmvivo
                String files[] = this.current.list();
115 23894 jjdelcerro
                int i;
116
                File theFile;
117 24569 jmvivo
                ArrayList list = new ArrayList();
118 23894 jjdelcerro
                DataStoreParameters dsp = null;
119
120
                for (i = 0; i < files.length; i++) {
121 24569 jmvivo
                        theFile = new File(this.root, files[i]);
122 32880 jjdelcerro
                        dsp = this.createStoreParameters(theFile);
123 24569 jmvivo
                        if (dsp != null) {
124
                                list.add(dsp);
125 23894 jjdelcerro
                        }
126
                }
127 24569 jmvivo
                return list;
128 23894 jjdelcerro
        }
129
130 32880 jjdelcerro
        /*
131
         * (non-Javadoc)
132
         *
133
         * @see
134
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
135
         * #setCurrentPath(java.io.File)
136 24893 jmvivo
         */
137 24710 jmvivo
        public void setCurrentPath(File path) throws FileNotFoundException {
138 24569 jmvivo
                // FIXME Comprobar si es un directorio existente
139 24710 jmvivo
                if (!path.exists()) {
140
                        throw new FileNotFoundException(path);
141
                }
142
                if (!path.isDirectory()) {
143
                        throw new IllegalArgumentException(path.getPath()
144
                                        + " is not a directory");
145
                }
146
                if (!isFromRoot(path)) {
147
                        throw new IllegalArgumentException(path.getPath()
148
                                        + " is not from root");
149
150
                }
151
152 24569 jmvivo
                this.current = path;
153
        }
154
155 32880 jjdelcerro
        /*
156
         * (non-Javadoc)
157
         *
158
         * @see
159
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
160
         * #getCurrentPath()
161 24893 jmvivo
         */
162 24569 jmvivo
        public File getCurrentPath() {
163
                return this.current;
164
        }
165
166 32880 jjdelcerro
        /*
167
         * (non-Javadoc)
168
         *
169
         * @see
170
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
171
         * #getRoot()
172 24893 jmvivo
         */
173 24569 jmvivo
        public File getRoot() {
174
                return this.root;
175
        }
176
177 32880 jjdelcerro
        /*
178
         * (non-Javadoc)
179
         *
180
         * @see
181
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
182
         * #remove(org.gvsig.fmap.dal.DataStoreParameters)
183 24893 jmvivo
         */
184 24569 jmvivo
        public void remove(DataStoreParameters dsp) throws RemoveException {
185
                String providerName = dsp.getDataStoreName();
186
                try {
187 24571 jmvivo
                        this.checkIsMine(dsp);
188 32880 jjdelcerro
                        FilesystemServerExplorerProvider provider = this
189
                                        .getProvider(providerName);
190 24569 jmvivo
191
                        provider.remove(dsp);
192
                } catch (DataException e) {
193 33719 jjdelcerro
                        throw new RemoveException(this.getProviderName(), e);
194 24569 jmvivo
                }
195
        }
196
197 32880 jjdelcerro
        /*
198
         * (non-Javadoc)
199
         *
200
         * @see
201
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
202
         * #add(org.gvsig.fmap.dal.NewDataStoreParameters, boolean)
203 24893 jmvivo
         */
204 32880 jjdelcerro
        public boolean add(String providerName, NewDataStoreParameters ndsp,
205
                        boolean overwrite) throws DataException {
206
                // String providerName = ndsp.getDataStoreName();
207 24898 jmvivo
208 24569 jmvivo
                try {
209 24898 jmvivo
                        this.checkIsMine(ndsp);
210 32880 jjdelcerro
                        FilesystemServerExplorerProvider provider = this
211
                                        .getProvider(providerName);
212 24569 jmvivo
213
                        provider.create(ndsp, overwrite);
214 32880 jjdelcerro
                        return true; // TODO deber?a devolver un booleano el provider o esto
215
                        // ser un void
216 24569 jmvivo
                } catch (DataException e) {
217 33719 jjdelcerro
                        throw new RemoveException(this.getProviderName(), e);
218 24569 jmvivo
                }
219
        }
220
221 32880 jjdelcerro
        /*
222
         * (non-Javadoc)
223
         *
224
         * @see
225
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
226
         * #canAdd()
227 24893 jmvivo
         */
228 24569 jmvivo
        public boolean canAdd() {
229
                return this.root.canWrite();
230
        }
231
232 32880 jjdelcerro
        /*
233
         * (non-Javadoc)
234
         *
235
         * @see
236
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
237
         * #getName()
238 24893 jmvivo
         */
239 33719 jjdelcerro
        public String getProviderName() {
240 24898 jmvivo
                return FilesystemServerExplorer.NAME;
241 24569 jmvivo
        }
242
243 32880 jjdelcerro
        /*
244
         * (non-Javadoc)
245
         *
246
         * @see
247
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
248
         * #getAddParameters(java.lang.String)
249 24893 jmvivo
         */
250 24710 jmvivo
        public NewDataStoreParameters getAddParameters(String storeName)
251
                        throws DataException {
252 24912 jmvivo
                FilesystemServerExplorerProvider provider = this.getProvider(storeName);
253 24710 jmvivo
                if (provider == null) {
254
                        throw new IllegalArgumentException(
255
                                        "Not registered in this explorer"); // FIXME
256
                }
257
258
                NewDataStoreParameters nParams = provider.getCreateParameters();
259 32880 jjdelcerro
                // nParams.setAttribute("path", this.getCurrentPath().getPath());
260 24710 jmvivo
                return nParams;
261 24569 jmvivo
        }
262
263 32880 jjdelcerro
        /*
264
         * (non-Javadoc)
265
         *
266
         * @see
267
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
268
         * #canAdd(java.lang.String)
269 24893 jmvivo
         */
270 32880 jjdelcerro
        public boolean canAdd(String storeName) throws DataException {
271 24898 jmvivo
                if (storeName == null) {
272 32880 jjdelcerro
                        return false;// CanAdd with genericFilter
273 24898 jmvivo
                }
274 24912 jmvivo
                FilesystemServerExplorerProvider provider = this.getProvider(storeName);
275 24710 jmvivo
                if (provider == null) {
276
                        throw new IllegalArgumentException(
277
                                        "Not registered in this explorer"); // FIXME
278
                }
279 24569 jmvivo
280 24710 jmvivo
                return provider.canCreate();
281
282 24569 jmvivo
        }
283
284
        // ==========================================
285
286 24912 jmvivo
        private FilesystemServerExplorerProvider getProvider(String storeName)
287 24710 jmvivo
                        throws InitializeException, ProviderNotRegisteredException {
288 24898 jmvivo
                Iterator providers = getProviders().iterator();
289 24912 jmvivo
                FilesystemServerExplorerProvider provider;
290 24898 jmvivo
                while (providers.hasNext()) {
291 24912 jmvivo
                        provider = (FilesystemServerExplorerProvider) providers.next();
292 24898 jmvivo
                        if (provider.getDataStoreProviderName().equals(storeName)) {
293
                                return provider;
294 24710 jmvivo
                        }
295
                }
296
                return null;
297
        }
298
299 24569 jmvivo
        private DataManagerProviderServices getManager() {
300
                return (DataManagerProviderServices) DALLocator.getDataManager();
301
        }
302
303 32880 jjdelcerro
        /*
304
         * (non-Javadoc)
305
         *
306
         * @see
307
         * org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer
308
         * #getParametersFor(java.io.File)
309 24893 jmvivo
         */
310 32880 jjdelcerro
        public DataStoreParameters createStoreParameters(File file) throws DataException {
311 25735 jmvivo
312
                return this.getParametersFor(file, true);
313
        }
314
315
        public DataStoreParameters getParametersFor(File file, boolean checksExist)
316
                        throws DataException {
317
318
                if (checksExist) {
319
                        if (!file.exists()) {
320
                                return null;
321
                        }
322
                        if (!file.isFile()) {
323
                                return null;
324
                        }
325
                        if (!file.canRead()) {
326
                                return null;
327
                        }
328
                        if (file.isHidden()) { // XXX ???
329
                                return null;
330
                        }
331 24569 jmvivo
                }
332 32880 jjdelcerro
                String providerName = this.getProviderName(file);
333
                if (providerName != null) {
334
                        DataStoreParameters params = this.getManager()
335
                                        .createStoreParameters(providerName);
336
                        ((FilesystemStoreParameters) params).setFile(file);
337
                        return params;
338
339
                }
340
                return null;
341
        }
342
343
        public String getProviderName(File file) {
344 24569 jmvivo
                Iterator filters = getFilters();
345
                while (filters.hasNext()) {
346
                        FilesystemFileFilter filter = (FilesystemFileFilter) filters.next();
347
                        if (filter.accept(file)) {
348 32880 jjdelcerro
                                return filter.getDataStoreProviderName();
349 24569 jmvivo
                        }
350
                }
351
                return null;
352 23894 jjdelcerro
        }
353
354 32880 jjdelcerro
        public List getDataStoreProviderNames() {
355
                Set names = new HashSet();
356
357
                Iterator filters = getFilters();
358
                while (filters.hasNext()) {
359
                        FilesystemFileFilter filter = (FilesystemFileFilter) filters.next();
360
                        names.add(filter.getDataStoreProviderName());
361
                }
362
                return new ArrayList(names);
363
        }
364
365 24569 jmvivo
        private void checkIsMine(DataStoreParameters dsp)
366 24571 jmvivo
                        throws IllegalArgumentException, DataException {
367 28259 jmvivo
                // FIXME Exception ???
368 24898 jmvivo
                if (!(dsp instanceof FilesystemStoreParameters)) {
369
                        new IllegalArgumentException(
370
                                        "not instance of FilesystemStoreParameters");
371 24569 jmvivo
                }
372 24571 jmvivo
                Iterator filters = getFilters();
373
                File file = ((FilesystemStoreParameters) dsp).getFile();
374 24710 jmvivo
                if (!this.isFromRoot(file)) {
375 24569 jmvivo
                        throw new IllegalArgumentException("worng explorer");
376
                }
377 24571 jmvivo
                FilesystemFileFilter filter;
378
                while (filters.hasNext()) {
379
                        filter = (FilesystemFileFilter) filters.next();
380 32880 jjdelcerro
                        if (dsp.getDataStoreName()
381
                                        .equals(filter.getDataStoreProviderName())) {
382 24571 jmvivo
                                return;
383
                        }
384 24569 jmvivo
                }
385 24571 jmvivo
                throw new IllegalArgumentException("worng explorer");
386 23913 jmvivo
        }
387
388 24710 jmvivo
        private boolean isFromRoot(File file) {
389 24898 jmvivo
                if (this.root == null) {
390
                        return true;
391
                }
392 24710 jmvivo
                return file.getAbsolutePath().startsWith(this.root.getAbsolutePath());
393
        }
394 24079 jjdelcerro
395 24898 jmvivo
        public List getProviders() {
396
                if (this.serverProviders == null) {
397 32880 jjdelcerro
                        Iterator iter = DALFileLocator.getFilesystemServerExplorerManager()
398
                                        .getRegisteredProviders();
399 26233 vcaballero
                        this.serverProviders = new ArrayList();
400 24898 jmvivo
                        Extension ext;
401 24912 jmvivo
                        FilesystemServerExplorerProvider provider;
402 24898 jmvivo
                        while (iter.hasNext()) {
403
                                ext = (Extension) iter.next();
404 24959 jmvivo
                                try {
405
                                        provider = (FilesystemServerExplorerProvider) ext.create();
406
                                } catch (Exception e) {
407
                                        throw new RuntimeException(e);// FIXME !!!
408
                                }
409 24898 jmvivo
                                provider.initialize(this);
410 24959 jmvivo
                                this.serverProviders.add(provider);
411 24898 jmvivo
                        }
412
                }
413
                return this.serverProviders;
414
        }
415
416 24893 jmvivo
        public Iterator getFilters() {
417 24959 jmvivo
                return this.getProviders().iterator();
418 24893 jmvivo
        }
419 24887 jmvivo
420 24893 jmvivo
        public FilesystemFileFilter getGenericFilter() {
421
                return this;
422
        }
423
424
        public String getDataStoreProviderName() {
425
                return null;
426
        }
427
428
        public String getDescription() {
429
                return "All supporteds";
430
        }
431
432
        public boolean accept(File pathname) {
433
                try {
434 32880 jjdelcerro
                        return this.createStoreParameters(pathname) != null;
435 24893 jmvivo
                } catch (DataException e) {
436
                        throw new RuntimeException(e); // FIXME excpetion??
437
                }
438
        }
439
440 24898 jmvivo
        public DataServerExplorerProviderServices getServerExplorerProviderServices() {
441
                return this.providerServices;
442
        }
443 24893 jmvivo
444 33719 jjdelcerro
        public DataStore open(File file) throws DataException,
445
                        ValidateDataParametersException {
446
                DataManager manager =  DALLocator.getDataManager();
447
                String providerName = this.getProviderName(file);
448
449
        DataStoreParameters params = this.getAddParameters(file);
450
                return manager.openStore(providerName, params);
451
        }
452 24898 jmvivo
453 25710 vcaballero
        public NewDataStoreParameters getAddParameters(File file)
454
                        throws DataException {
455 25735 jmvivo
                DataStoreParameters params = this.getParametersFor(file, false);
456 32880 jjdelcerro
                NewDataStoreParameters newParams = this.getAddParameters(params
457
                                .getDataStoreName());
458
                ((FilesystemStoreParameters) newParams)
459
                                .setFile(((FilesystemStoreParameters) params).getFile());
460 25710 vcaballero
                return newParams;
461
        }
462 25015 jmvivo
463 33263 jpiera
        public File getResourcePath(DataStore dataStore, String resourceName) throws DataException {
464
                FilesystemServerExplorerProvider provider =
465
                        this.getProvider(dataStore.getName());
466
                if (provider == null){
467
                        return null;
468
                }
469
                String rootPath = provider.getResourceRootPathName(dataStore);
470
                if (rootPath == null){
471
                        return null;
472
                }
473
                return new File(rootPath + "." + resourceName);
474
        }
475 23894 jjdelcerro
}