svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / serverexplorer / filesystem / impl / DefaultFilesystemServerExplorer.java @ 44328
History | View | Annotate | Download (20.7 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.serverexplorer.filesystem.impl; |
24 |
|
25 |
import java.io.File; |
26 |
import java.util.ArrayList; |
27 |
import java.util.HashSet; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import java.util.Set; |
31 |
import org.apache.commons.io.FilenameUtils; |
32 |
|
33 |
import org.gvsig.fmap.dal.DALFileLocator; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataStore; |
37 |
import org.gvsig.fmap.dal.DataStoreParameters; |
38 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
41 |
import org.gvsig.fmap.dal.exception.InitializeException; |
42 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
43 |
import org.gvsig.fmap.dal.exception.RemoveException; |
44 |
import org.gvsig.fmap.dal.exception.ServerExplorerAddException; |
45 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
46 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
47 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
52 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory; |
53 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
54 |
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer; |
55 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
56 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
57 |
import org.gvsig.fmap.dal.spi.ZipResourcesStorage; |
58 |
import org.gvsig.tools.exception.BaseException; |
59 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
60 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
61 |
import org.gvsig.tools.resourcesstorage.FilesResourcesStorage; |
62 |
|
63 |
@SuppressWarnings("UseSpecificCatch") |
64 |
public class DefaultFilesystemServerExplorer extends AbstractDataServerExplorer |
65 |
implements FilesystemServerExplorerProviderServices,
|
66 |
FilesystemFileFilter { |
67 |
|
68 |
private File root; |
69 |
private File current; // Current path |
70 |
private List<FilesystemServerExplorerProvider> serverProviders; |
71 |
|
72 |
public DefaultFilesystemServerExplorer(
|
73 |
FilesystemServerExplorerParameters parameters, |
74 |
DataServerExplorerProviderServices services) |
75 |
throws InitializeException {
|
76 |
super(parameters, services);
|
77 |
if (this.getFSParameters().getRoot() != null) { |
78 |
this.root = new File(this.getFSParameters().getRoot()); |
79 |
} |
80 |
if (this.getFSParameters().getInitialpath() != null) { |
81 |
this.current = new File(this.getFSParameters().getInitialpath()); |
82 |
} |
83 |
if (this.root == null && this.current == null) { |
84 |
// throw new InitializeException(this.getName(),
|
85 |
// new IllegalArgumentException());
|
86 |
} else if (this.current == null) { |
87 |
this.current = new File(this.getFSParameters().getRoot()); |
88 |
} |
89 |
} |
90 |
|
91 |
private FilesystemServerExplorerParameters getFSParameters() {
|
92 |
return (FilesystemServerExplorerParameters) this.getParameters(); |
93 |
} |
94 |
|
95 |
@Override
|
96 |
protected void doDispose() throws BaseException { |
97 |
this.root = null; |
98 |
super.doDispose();
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public List list(int mode) throws DataException { |
103 |
if (this.current == null) { |
104 |
throw new IllegalStateException(); |
105 |
} |
106 |
if (!this.current.exists()) { |
107 |
throw new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current); |
108 |
} |
109 |
|
110 |
if (!this.current.isDirectory()) { |
111 |
throw new IllegalArgumentException(this.getProviderName() |
112 |
+ ": Path not a directory '" + this.current + "'"); |
113 |
} |
114 |
|
115 |
List files = new ArrayList(); // return files |
116 |
List providers = this.getProviders(mode); |
117 |
String allfiles[] = this.current.list(); |
118 |
|
119 |
for (String f : allfiles) { |
120 |
File file = new File(this.root, f); |
121 |
Iterator providersit = providers.iterator();
|
122 |
while (providersit.hasNext()) {
|
123 |
FilesystemServerExplorerProvider provider = (FilesystemServerExplorerProvider) providersit |
124 |
.next(); |
125 |
if (provider.accept(file)) {
|
126 |
DataStoreParameters dsp = this.createStoreParameters(file);
|
127 |
if (dsp != null) { |
128 |
files.add(dsp); |
129 |
} |
130 |
break;
|
131 |
} |
132 |
} |
133 |
} |
134 |
return files;
|
135 |
} |
136 |
|
137 |
public List list() throws DataException { |
138 |
if (this.current == null) { |
139 |
throw new IllegalStateException(); // FIXME |
140 |
} |
141 |
if (!this.current.exists()) { |
142 |
throw new FileNotFoundException(this.current); |
143 |
} |
144 |
|
145 |
if (!this.current.isDirectory()) { |
146 |
throw new IllegalArgumentException(this.getProviderName() |
147 |
+ ": Path not a directory '" + this.current + "'"); |
148 |
} |
149 |
|
150 |
String files[] = this.current.list(); |
151 |
int i;
|
152 |
File theFile;
|
153 |
ArrayList list = new ArrayList(); |
154 |
DataStoreParameters dsp; |
155 |
|
156 |
for (i = 0; i < files.length; i++) { |
157 |
theFile = new File(this.root, files[i]); |
158 |
dsp = this.createStoreParameters(theFile);
|
159 |
if (dsp != null) { |
160 |
list.add(dsp); |
161 |
} |
162 |
} |
163 |
return list;
|
164 |
} |
165 |
|
166 |
public void setCurrentPath(File path) throws FileNotFoundException { |
167 |
// FIXME Comprobar si es un directorio existente
|
168 |
if (!path.exists()) {
|
169 |
throw new FileNotFoundException(path); |
170 |
} |
171 |
if (!path.isDirectory()) {
|
172 |
throw new IllegalArgumentException(path.getPath() |
173 |
+ " is not a directory");
|
174 |
} |
175 |
if (!isFromRoot(path)) {
|
176 |
throw new IllegalArgumentException(path.getPath() |
177 |
+ " is not from root");
|
178 |
|
179 |
} |
180 |
|
181 |
this.current = path;
|
182 |
} |
183 |
|
184 |
public File getCurrentPath() { |
185 |
return this.current; |
186 |
} |
187 |
|
188 |
public File getRoot() { |
189 |
return this.root; |
190 |
} |
191 |
|
192 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
193 |
String providerName = dsp.getDataStoreName();
|
194 |
try {
|
195 |
this.checkIsMine(dsp);
|
196 |
FilesystemServerExplorerProvider provider = this
|
197 |
.getProvider(providerName); |
198 |
|
199 |
provider.remove(dsp); |
200 |
} catch (DataException e) {
|
201 |
throw new RemoveException(this.getProviderName(), e); |
202 |
} |
203 |
} |
204 |
|
205 |
public boolean add(String providerName, NewDataStoreParameters ndsp, |
206 |
boolean overwrite) throws DataException { |
207 |
|
208 |
try {
|
209 |
this.checkIsMine(ndsp);
|
210 |
FilesystemServerExplorerProvider provider = this
|
211 |
.getProvider(providerName); |
212 |
|
213 |
ndsp.validate(); |
214 |
provider.create(ndsp, overwrite); |
215 |
|
216 |
if( ndsp instanceof NewFeatureStoreParameters ) { |
217 |
DataManager dataManager = DALLocator.getDataManager(); |
218 |
ResourcesStorage resources = this.getResourcesStorage(ndsp);
|
219 |
dataManager.writeDALResource(resources, ((NewFeatureStoreParameters) ndsp).getDefaultFeatureType()); |
220 |
} |
221 |
return true; |
222 |
} catch (DataException | ValidateDataParametersException e) {
|
223 |
throw new ServerExplorerAddException(this.getProviderName(), e); |
224 |
} |
225 |
} |
226 |
|
227 |
public boolean canAdd() { |
228 |
return this.root.canWrite(); |
229 |
} |
230 |
|
231 |
public String getProviderName() { |
232 |
return FilesystemServerExplorer.NAME;
|
233 |
} |
234 |
|
235 |
public NewDataStoreParameters getAddParameters(String storeName) |
236 |
throws DataException {
|
237 |
FilesystemServerExplorerProvider provider = this.getProvider(storeName);
|
238 |
if (provider == null) { |
239 |
throw new IllegalArgumentException( |
240 |
"Not registered in this explorer"); // FIXME |
241 |
} |
242 |
|
243 |
NewDataStoreParameters nParams = provider.getCreateParameters(); |
244 |
// nParams.setAttribute("path", this.getCurrentPath().getPath());
|
245 |
return nParams;
|
246 |
} |
247 |
|
248 |
public boolean canAdd(String storeName) throws DataException { |
249 |
if (storeName == null) { |
250 |
return false;// CanAdd with genericFilter |
251 |
} |
252 |
FilesystemServerExplorerProvider provider = this.getProvider(storeName);
|
253 |
if (provider == null) { |
254 |
throw new IllegalArgumentException( |
255 |
"Not registered in this explorer"); // FIXME |
256 |
} |
257 |
|
258 |
return provider.canCreate();
|
259 |
|
260 |
} |
261 |
|
262 |
// ==========================================
|
263 |
private FilesystemServerExplorerProvider getProvider(String providerName) |
264 |
throws InitializeException, ProviderNotRegisteredException {
|
265 |
Iterator providers = getProviders(FilesystemServerExplorer.MODE_ALL).iterator();
|
266 |
FilesystemServerExplorerProvider provider; |
267 |
while (providers.hasNext()) {
|
268 |
provider = (FilesystemServerExplorerProvider) providers.next(); |
269 |
if (provider.getDataStoreProviderName().equals(providerName)) {
|
270 |
return provider;
|
271 |
} |
272 |
} |
273 |
return null; |
274 |
} |
275 |
|
276 |
private DataManagerProviderServices getManager() {
|
277 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
278 |
} |
279 |
|
280 |
public DataStoreParameters createStoreParameters(File file, String providerName) throws DataException { |
281 |
|
282 |
return this.getParametersFor(file, providerName, true); |
283 |
} |
284 |
|
285 |
public DataStoreParameters createStoreParameters(File file) throws DataException { |
286 |
|
287 |
return this.getParametersFor(file, null, true); |
288 |
} |
289 |
|
290 |
public DataStoreParameters getParametersFor(File file, String providerName, boolean checksExist) throws DataException { |
291 |
|
292 |
if (checksExist) {
|
293 |
if (!file.exists()) {
|
294 |
return null; |
295 |
} |
296 |
if (!file.isFile()) {
|
297 |
return null; |
298 |
} |
299 |
if (!file.canRead()) {
|
300 |
return null; |
301 |
} |
302 |
if (file.isHidden()) { // XXX ??? |
303 |
return null; |
304 |
} |
305 |
} |
306 |
if (providerName == null) { |
307 |
providerName = this.getProviderName(file);
|
308 |
} |
309 |
if (providerName != null) { |
310 |
DataStoreParameters params = this.getManager()
|
311 |
.createStoreParameters(providerName); |
312 |
((FilesystemStoreParameters) params).setFile(file); |
313 |
return params;
|
314 |
|
315 |
} |
316 |
return null; |
317 |
} |
318 |
|
319 |
public List<String> getProviderNameList(File file) { |
320 |
Iterator filters = getFilters();
|
321 |
List list = new ArrayList(); |
322 |
while (filters.hasNext()) {
|
323 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
324 |
if (filter.accept(file)) {
|
325 |
list.add(filter.getDataStoreProviderName()); |
326 |
} |
327 |
} |
328 |
return list;
|
329 |
} |
330 |
|
331 |
@Override
|
332 |
public String getProviderName(File file) { |
333 |
Iterator filters = getFilters();
|
334 |
while (filters.hasNext()) {
|
335 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
336 |
if (filter.accept(file)) {
|
337 |
return filter.getDataStoreProviderName();
|
338 |
} |
339 |
} |
340 |
return null; |
341 |
} |
342 |
|
343 |
public List getDataStoreProviderNames() { |
344 |
Set names = new HashSet(); |
345 |
|
346 |
Iterator filters = getFilters();
|
347 |
while (filters.hasNext()) {
|
348 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
349 |
names.add(filter.getDataStoreProviderName()); |
350 |
} |
351 |
return new ArrayList(names); |
352 |
} |
353 |
|
354 |
private void checkIsMine(DataStoreParameters dsp) |
355 |
throws IllegalArgumentException, DataException { |
356 |
if (!(dsp instanceof FilesystemStoreParameters)) { |
357 |
throw new IllegalArgumentException("not instance of FilesystemStoreParameters"); |
358 |
} |
359 |
Iterator filters = getFilters();
|
360 |
File file = ((FilesystemStoreParameters) dsp).getFile();
|
361 |
if (!this.isFromRoot(file)) { |
362 |
throw new IllegalArgumentException("worng explorer"); |
363 |
} |
364 |
FilesystemFileFilter filter; |
365 |
while (filters.hasNext()) {
|
366 |
filter = (FilesystemFileFilter) filters.next(); |
367 |
if (dsp.getDataStoreName()
|
368 |
.equals(filter.getDataStoreProviderName())) { |
369 |
return;
|
370 |
} |
371 |
} |
372 |
throw new IllegalArgumentException("worng explorer"); |
373 |
} |
374 |
|
375 |
private boolean isFromRoot(File file) { |
376 |
if (this.root == null) { |
377 |
return true; |
378 |
} |
379 |
return file.getAbsolutePath().startsWith(this.root.getAbsolutePath()); |
380 |
} |
381 |
|
382 |
public List<FilesystemServerExplorerProvider> getProviders() { |
383 |
if (this.serverProviders == null) { |
384 |
Iterator iter = DALFileLocator.getFilesystemServerExplorerManager()
|
385 |
.getRegisteredProviders(); |
386 |
this.serverProviders = new ArrayList(); |
387 |
Extension ext; |
388 |
FilesystemServerExplorerProvider provider; |
389 |
FilesystemServerExplorerProviderFactory factory; |
390 |
while (iter.hasNext()) {
|
391 |
ext = (Extension) iter.next(); |
392 |
try {
|
393 |
factory = (FilesystemServerExplorerProviderFactory) ext.create(); |
394 |
} catch (Exception e) { |
395 |
throw new RuntimeException(e);// FIXME !!! |
396 |
} |
397 |
try {
|
398 |
provider=factory.createProvider(); |
399 |
} catch (InitializeException e) {
|
400 |
throw new RuntimeException(e);// FIXME !!! |
401 |
} |
402 |
provider.initialize(this);
|
403 |
this.serverProviders.add(provider);
|
404 |
} |
405 |
} |
406 |
return this.serverProviders; |
407 |
} |
408 |
|
409 |
public List getProviders(int mode) { |
410 |
List<FilesystemServerExplorerProvider> providers = new ArrayList(); |
411 |
List<FilesystemServerExplorerProvider> allProviders = getProviders();
|
412 |
for (FilesystemServerExplorerProvider provider:allProviders){
|
413 |
if (provider.isMode(mode)) {
|
414 |
provider.initialize(this);
|
415 |
providers.add(provider); |
416 |
} |
417 |
} |
418 |
return providers;
|
419 |
} |
420 |
|
421 |
public Iterator getFilters(int mode) { |
422 |
return this.getProviders(mode).iterator(); |
423 |
} |
424 |
|
425 |
public Iterator<FilesystemServerExplorerProvider> getFilters() { |
426 |
return this.getProviders().iterator(); |
427 |
} |
428 |
|
429 |
public FilesystemFileFilter getFilter(int mode, final String description) { |
430 |
|
431 |
final List filters = new ArrayList(); |
432 |
Iterator it = this.getFilters(mode); |
433 |
while (it.hasNext()) {
|
434 |
filters.add(it.next()); |
435 |
} |
436 |
FilesystemFileFilter filter = new FilesystemFileFilter() {
|
437 |
|
438 |
public boolean accept(File f) { |
439 |
if (f.isDirectory()) {
|
440 |
return true; |
441 |
} |
442 |
for (int i = 0; i < filters.size(); i++) { |
443 |
if (((FilesystemFileFilter) filters.get(i)).accept(f)) {
|
444 |
return true; |
445 |
} |
446 |
} |
447 |
return false; |
448 |
} |
449 |
|
450 |
public String getDescription() { |
451 |
return description;
|
452 |
} |
453 |
|
454 |
public String getDataStoreProviderName() { |
455 |
return null; |
456 |
} |
457 |
}; |
458 |
return filter;
|
459 |
} |
460 |
|
461 |
public FilesystemFileFilter getGenericFilter() {
|
462 |
// FIXME: Este metodo, getGenericFilter, no tengo claro que deba existir (jjdc)
|
463 |
return this; |
464 |
} |
465 |
|
466 |
public String getDataStoreProviderName() { |
467 |
return null; |
468 |
} |
469 |
|
470 |
public String getDescription() { |
471 |
return "All supporteds"; |
472 |
} |
473 |
|
474 |
public boolean accept(File pathname) { |
475 |
try {
|
476 |
return this.createStoreParameters(pathname) != null; |
477 |
} catch (DataException e) {
|
478 |
throw new RuntimeException(e); // FIXME excpetion?? |
479 |
} |
480 |
} |
481 |
|
482 |
public DataStore open(File file) throws DataException, |
483 |
ValidateDataParametersException { |
484 |
DataManager manager = DALLocator.getDataManager(); |
485 |
String providerName = this.getProviderName(file); |
486 |
|
487 |
DataStoreParameters params = this.getAddParameters(file);
|
488 |
return manager.openStore(providerName, params);
|
489 |
} |
490 |
|
491 |
public NewDataStoreParameters getAddParameters(File file) |
492 |
throws DataException {
|
493 |
DataStoreParameters params = this.getParametersFor(file, null, false); |
494 |
NewDataStoreParameters newParams = this.getAddParameters(params
|
495 |
.getDataStoreName()); |
496 |
((FilesystemStoreParameters) newParams) |
497 |
.setFile(((FilesystemStoreParameters) params).getFile()); |
498 |
return newParams;
|
499 |
} |
500 |
|
501 |
@Deprecated
|
502 |
@Override
|
503 |
public File getResourcePath(DataStore dataStore, String resourceName) throws DataException { |
504 |
try {
|
505 |
ResourcesStorage theResourceStorage = this.getResourcesStorage(dataStore);
|
506 |
if( theResourceStorage==null ) { |
507 |
return null; |
508 |
} |
509 |
FilesResourcesStorage.FileResource res = (FilesResourcesStorage.FileResource) theResourceStorage.getResource(resourceName); |
510 |
if( res==null ) { |
511 |
return null; |
512 |
} |
513 |
return res.getFile();
|
514 |
} catch(Exception ex) { |
515 |
return null; |
516 |
} |
517 |
} |
518 |
|
519 |
private String getResourcePathName(DataStoreParameters parameters) { |
520 |
FilesystemServerExplorerProvider provider; |
521 |
try {
|
522 |
provider = this.getProvider(parameters.getProviderName());
|
523 |
} catch (Exception ex) { |
524 |
return null; |
525 |
} |
526 |
if (provider == null) { |
527 |
return null; |
528 |
} |
529 |
String pathName = provider.getResourceRootPathName(parameters);
|
530 |
if (pathName == null) { |
531 |
return null; |
532 |
} |
533 |
pathName = FilenameUtils.removeExtension(pathName); |
534 |
return pathName;
|
535 |
} |
536 |
|
537 |
@Override
|
538 |
public ResourcesStorage getResourcesStorage(DataStoreParameters parameters) {
|
539 |
String pathName = this.getResourcePathName(parameters); |
540 |
ResourcesStorage theResourcesStorage; |
541 |
if( ZipResourcesStorage.existsStorage(pathName) ) {
|
542 |
String resourceName = FilenameUtils.getBaseName(pathName);
|
543 |
theResourcesStorage = new ZipResourcesStorage(
|
544 |
pathName, |
545 |
resourceName |
546 |
); |
547 |
} else {
|
548 |
theResourcesStorage = new FilesResourcesStorage(
|
549 |
pathName |
550 |
); |
551 |
} |
552 |
return theResourcesStorage;
|
553 |
} |
554 |
|
555 |
@Override
|
556 |
public ResourcesStorage getResourcesStorage(DataStore dataStore) {
|
557 |
ResourcesStorage theResourcesStorage; |
558 |
if( dataStore.getChildren().isEmpty() ) {
|
559 |
theResourcesStorage = this.getResourcesStorage(dataStore.getParameters());
|
560 |
} else {
|
561 |
String pathName = this.getResourcePathName(dataStore.getParameters()); |
562 |
theResourcesStorage = new ZipResourcesStorage(
|
563 |
pathName, |
564 |
dataStore.getName() |
565 |
); |
566 |
} |
567 |
return theResourcesStorage;
|
568 |
} |
569 |
|
570 |
@Override
|
571 |
public DataStoreParameters get(String name) throws DataException { |
572 |
File theFile = new File(this.current,name); |
573 |
DataStoreParameters dsp = this.createStoreParameters(theFile);
|
574 |
return dsp;
|
575 |
} |
576 |
} |