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 @ 44276
History | View | Annotate | Download (20.3 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.serverexplorer.filesystem.FilesystemFileFilter; |
47 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory; |
52 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
53 |
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer; |
54 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
55 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
56 |
import org.gvsig.fmap.dal.spi.FileMultiResource; |
57 |
import org.gvsig.tools.exception.BaseException; |
58 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
59 |
import org.gvsig.tools.util.ResourcesStorage; |
60 |
import org.gvsig.tools.util.SimpleFileResource; |
61 |
|
62 |
@SuppressWarnings("UseSpecificCatch") |
63 |
public class DefaultFilesystemServerExplorer extends AbstractDataServerExplorer |
64 |
implements FilesystemServerExplorerProviderServices,
|
65 |
FilesystemFileFilter { |
66 |
|
67 |
private File root; |
68 |
private File current; // Current path |
69 |
private List<FilesystemServerExplorerProvider> serverProviders; |
70 |
|
71 |
public DefaultFilesystemServerExplorer(
|
72 |
FilesystemServerExplorerParameters parameters, |
73 |
DataServerExplorerProviderServices services) |
74 |
throws InitializeException {
|
75 |
super(parameters, services);
|
76 |
if (this.getFSParameters().getRoot() != null) { |
77 |
this.root = new File(this.getFSParameters().getRoot()); |
78 |
} |
79 |
if (this.getFSParameters().getInitialpath() != null) { |
80 |
this.current = new File(this.getFSParameters().getInitialpath()); |
81 |
} |
82 |
if (this.root == null && this.current == null) { |
83 |
// throw new InitializeException(this.getName(),
|
84 |
// new IllegalArgumentException());
|
85 |
} else if (this.current == null) { |
86 |
this.current = new File(this.getFSParameters().getRoot()); |
87 |
} |
88 |
} |
89 |
|
90 |
private FilesystemServerExplorerParameters getFSParameters() {
|
91 |
return (FilesystemServerExplorerParameters) this.getParameters(); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
protected void doDispose() throws BaseException { |
96 |
this.root = null; |
97 |
super.doDispose();
|
98 |
} |
99 |
|
100 |
@Override
|
101 |
public List list(int mode) throws DataException { |
102 |
if (this.current == null) { |
103 |
throw new IllegalStateException(); |
104 |
} |
105 |
if (!this.current.exists()) { |
106 |
throw new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current); |
107 |
} |
108 |
|
109 |
if (!this.current.isDirectory()) { |
110 |
throw new IllegalArgumentException(this.getProviderName() |
111 |
+ ": Path not a directory '" + this.current + "'"); |
112 |
} |
113 |
|
114 |
List files = new ArrayList(); // return files |
115 |
List providers = this.getProviders(mode); |
116 |
String allfiles[] = this.current.list(); |
117 |
|
118 |
for (String f : allfiles) { |
119 |
File file = new File(this.root, f); |
120 |
Iterator providersit = providers.iterator();
|
121 |
while (providersit.hasNext()) {
|
122 |
FilesystemServerExplorerProvider provider = (FilesystemServerExplorerProvider) providersit |
123 |
.next(); |
124 |
if (provider.accept(file)) {
|
125 |
DataStoreParameters dsp = this.createStoreParameters(file);
|
126 |
if (dsp != null) { |
127 |
files.add(dsp); |
128 |
} |
129 |
break;
|
130 |
} |
131 |
} |
132 |
} |
133 |
return files;
|
134 |
} |
135 |
|
136 |
public List list() throws DataException { |
137 |
if (this.current == null) { |
138 |
throw new IllegalStateException(); // FIXME |
139 |
} |
140 |
if (!this.current.exists()) { |
141 |
throw new FileNotFoundException(this.current); |
142 |
} |
143 |
|
144 |
if (!this.current.isDirectory()) { |
145 |
throw new IllegalArgumentException(this.getProviderName() |
146 |
+ ": Path not a directory '" + this.current + "'"); |
147 |
} |
148 |
|
149 |
String files[] = this.current.list(); |
150 |
int i;
|
151 |
File theFile;
|
152 |
ArrayList list = new ArrayList(); |
153 |
DataStoreParameters dsp; |
154 |
|
155 |
for (i = 0; i < files.length; i++) { |
156 |
theFile = new File(this.root, files[i]); |
157 |
dsp = this.createStoreParameters(theFile);
|
158 |
if (dsp != null) { |
159 |
list.add(dsp); |
160 |
} |
161 |
} |
162 |
return list;
|
163 |
} |
164 |
|
165 |
public void setCurrentPath(File path) throws FileNotFoundException { |
166 |
// FIXME Comprobar si es un directorio existente
|
167 |
if (!path.exists()) {
|
168 |
throw new FileNotFoundException(path); |
169 |
} |
170 |
if (!path.isDirectory()) {
|
171 |
throw new IllegalArgumentException(path.getPath() |
172 |
+ " is not a directory");
|
173 |
} |
174 |
if (!isFromRoot(path)) {
|
175 |
throw new IllegalArgumentException(path.getPath() |
176 |
+ " is not from root");
|
177 |
|
178 |
} |
179 |
|
180 |
this.current = path;
|
181 |
} |
182 |
|
183 |
public File getCurrentPath() { |
184 |
return this.current; |
185 |
} |
186 |
|
187 |
public File getRoot() { |
188 |
return this.root; |
189 |
} |
190 |
|
191 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
192 |
String providerName = dsp.getDataStoreName();
|
193 |
try {
|
194 |
this.checkIsMine(dsp);
|
195 |
FilesystemServerExplorerProvider provider = this
|
196 |
.getProvider(providerName); |
197 |
|
198 |
provider.remove(dsp); |
199 |
} catch (DataException e) {
|
200 |
throw new RemoveException(this.getProviderName(), e); |
201 |
} |
202 |
} |
203 |
|
204 |
public boolean add(String providerName, NewDataStoreParameters ndsp, |
205 |
boolean overwrite) throws DataException { |
206 |
|
207 |
try {
|
208 |
this.checkIsMine(ndsp);
|
209 |
FilesystemServerExplorerProvider provider = this
|
210 |
.getProvider(providerName); |
211 |
|
212 |
ndsp.validate(); |
213 |
provider.create(ndsp, overwrite); |
214 |
return true; |
215 |
} catch (DataException | ValidateDataParametersException e) {
|
216 |
throw new ServerExplorerAddException(this.getProviderName(), e); |
217 |
} |
218 |
} |
219 |
|
220 |
public boolean canAdd() { |
221 |
return this.root.canWrite(); |
222 |
} |
223 |
|
224 |
public String getProviderName() { |
225 |
return FilesystemServerExplorer.NAME;
|
226 |
} |
227 |
|
228 |
public NewDataStoreParameters getAddParameters(String storeName) |
229 |
throws DataException {
|
230 |
FilesystemServerExplorerProvider provider = this.getProvider(storeName);
|
231 |
if (provider == null) { |
232 |
throw new IllegalArgumentException( |
233 |
"Not registered in this explorer"); // FIXME |
234 |
} |
235 |
|
236 |
NewDataStoreParameters nParams = provider.getCreateParameters(); |
237 |
// nParams.setAttribute("path", this.getCurrentPath().getPath());
|
238 |
return nParams;
|
239 |
} |
240 |
|
241 |
public boolean canAdd(String storeName) throws DataException { |
242 |
if (storeName == null) { |
243 |
return false;// CanAdd with genericFilter |
244 |
} |
245 |
FilesystemServerExplorerProvider provider = this.getProvider(storeName);
|
246 |
if (provider == null) { |
247 |
throw new IllegalArgumentException( |
248 |
"Not registered in this explorer"); // FIXME |
249 |
} |
250 |
|
251 |
return provider.canCreate();
|
252 |
|
253 |
} |
254 |
|
255 |
// ==========================================
|
256 |
private FilesystemServerExplorerProvider getProvider(String storeName) |
257 |
throws InitializeException, ProviderNotRegisteredException {
|
258 |
Iterator providers = getProviders(FilesystemServerExplorer.MODE_ALL).iterator();
|
259 |
FilesystemServerExplorerProvider provider; |
260 |
while (providers.hasNext()) {
|
261 |
provider = (FilesystemServerExplorerProvider) providers.next(); |
262 |
if (provider.getDataStoreProviderName().equals(storeName)) {
|
263 |
return provider;
|
264 |
} |
265 |
} |
266 |
return null; |
267 |
} |
268 |
|
269 |
private DataManagerProviderServices getManager() {
|
270 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
271 |
} |
272 |
|
273 |
public DataStoreParameters createStoreParameters(File file, String providerName) throws DataException { |
274 |
|
275 |
return this.getParametersFor(file, providerName, true); |
276 |
} |
277 |
|
278 |
public DataStoreParameters createStoreParameters(File file) throws DataException { |
279 |
|
280 |
return this.getParametersFor(file, null, true); |
281 |
} |
282 |
|
283 |
public DataStoreParameters getParametersFor(File file, String providerName, boolean checksExist) throws DataException { |
284 |
|
285 |
if (checksExist) {
|
286 |
if (!file.exists()) {
|
287 |
return null; |
288 |
} |
289 |
if (!file.isFile()) {
|
290 |
return null; |
291 |
} |
292 |
if (!file.canRead()) {
|
293 |
return null; |
294 |
} |
295 |
if (file.isHidden()) { // XXX ??? |
296 |
return null; |
297 |
} |
298 |
} |
299 |
if (providerName == null) { |
300 |
providerName = this.getProviderName(file);
|
301 |
} |
302 |
if (providerName != null) { |
303 |
DataStoreParameters params = this.getManager()
|
304 |
.createStoreParameters(providerName); |
305 |
((FilesystemStoreParameters) params).setFile(file); |
306 |
return params;
|
307 |
|
308 |
} |
309 |
return null; |
310 |
} |
311 |
|
312 |
public List<String> getProviderNameList(File file) { |
313 |
Iterator filters = getFilters();
|
314 |
List list = new ArrayList(); |
315 |
while (filters.hasNext()) {
|
316 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
317 |
if (filter.accept(file)) {
|
318 |
list.add(filter.getDataStoreProviderName()); |
319 |
} |
320 |
} |
321 |
return list;
|
322 |
} |
323 |
|
324 |
@Override
|
325 |
public String getProviderName(File file) { |
326 |
Iterator filters = getFilters();
|
327 |
while (filters.hasNext()) {
|
328 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
329 |
if (filter.accept(file)) {
|
330 |
return filter.getDataStoreProviderName();
|
331 |
} |
332 |
} |
333 |
return null; |
334 |
} |
335 |
|
336 |
public List getDataStoreProviderNames() { |
337 |
Set names = new HashSet(); |
338 |
|
339 |
Iterator filters = getFilters();
|
340 |
while (filters.hasNext()) {
|
341 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
342 |
names.add(filter.getDataStoreProviderName()); |
343 |
} |
344 |
return new ArrayList(names); |
345 |
} |
346 |
|
347 |
private void checkIsMine(DataStoreParameters dsp) |
348 |
throws IllegalArgumentException, DataException { |
349 |
if (!(dsp instanceof FilesystemStoreParameters)) { |
350 |
throw new IllegalArgumentException("not instance of FilesystemStoreParameters"); |
351 |
} |
352 |
Iterator filters = getFilters();
|
353 |
File file = ((FilesystemStoreParameters) dsp).getFile();
|
354 |
if (!this.isFromRoot(file)) { |
355 |
throw new IllegalArgumentException("worng explorer"); |
356 |
} |
357 |
FilesystemFileFilter filter; |
358 |
while (filters.hasNext()) {
|
359 |
filter = (FilesystemFileFilter) filters.next(); |
360 |
if (dsp.getDataStoreName()
|
361 |
.equals(filter.getDataStoreProviderName())) { |
362 |
return;
|
363 |
} |
364 |
} |
365 |
throw new IllegalArgumentException("worng explorer"); |
366 |
} |
367 |
|
368 |
private boolean isFromRoot(File file) { |
369 |
if (this.root == null) { |
370 |
return true; |
371 |
} |
372 |
return file.getAbsolutePath().startsWith(this.root.getAbsolutePath()); |
373 |
} |
374 |
|
375 |
public List<FilesystemServerExplorerProvider> getProviders() { |
376 |
if (this.serverProviders == null) { |
377 |
Iterator iter = DALFileLocator.getFilesystemServerExplorerManager()
|
378 |
.getRegisteredProviders(); |
379 |
this.serverProviders = new ArrayList(); |
380 |
Extension ext; |
381 |
FilesystemServerExplorerProvider provider; |
382 |
FilesystemServerExplorerProviderFactory factory; |
383 |
while (iter.hasNext()) {
|
384 |
ext = (Extension) iter.next(); |
385 |
try {
|
386 |
factory = (FilesystemServerExplorerProviderFactory) ext.create(); |
387 |
} catch (Exception e) { |
388 |
throw new RuntimeException(e);// FIXME !!! |
389 |
} |
390 |
try {
|
391 |
provider=factory.createProvider(); |
392 |
} catch (InitializeException e) {
|
393 |
throw new RuntimeException(e);// FIXME !!! |
394 |
} |
395 |
provider.initialize(this);
|
396 |
this.serverProviders.add(provider);
|
397 |
} |
398 |
} |
399 |
return this.serverProviders; |
400 |
} |
401 |
|
402 |
public List getProviders(int mode) { |
403 |
List<FilesystemServerExplorerProvider> providers = new ArrayList(); |
404 |
List<FilesystemServerExplorerProvider> allProviders = getProviders();
|
405 |
for (FilesystemServerExplorerProvider provider:allProviders){
|
406 |
if (provider.isMode(mode)) {
|
407 |
provider.initialize(this);
|
408 |
providers.add(provider); |
409 |
} |
410 |
} |
411 |
return providers;
|
412 |
} |
413 |
|
414 |
public Iterator getFilters(int mode) { |
415 |
return this.getProviders(mode).iterator(); |
416 |
} |
417 |
|
418 |
public Iterator<FilesystemServerExplorerProvider> getFilters() { |
419 |
return this.getProviders().iterator(); |
420 |
} |
421 |
|
422 |
public FilesystemFileFilter getFilter(int mode, final String description) { |
423 |
|
424 |
final List filters = new ArrayList(); |
425 |
Iterator it = this.getFilters(mode); |
426 |
while (it.hasNext()) {
|
427 |
filters.add(it.next()); |
428 |
} |
429 |
FilesystemFileFilter filter = new FilesystemFileFilter() {
|
430 |
|
431 |
public boolean accept(File f) { |
432 |
if (f.isDirectory()) {
|
433 |
return true; |
434 |
} |
435 |
for (int i = 0; i < filters.size(); i++) { |
436 |
if (((FilesystemFileFilter) filters.get(i)).accept(f)) {
|
437 |
return true; |
438 |
} |
439 |
} |
440 |
return false; |
441 |
} |
442 |
|
443 |
public String getDescription() { |
444 |
return description;
|
445 |
} |
446 |
|
447 |
public String getDataStoreProviderName() { |
448 |
return null; |
449 |
} |
450 |
}; |
451 |
return filter;
|
452 |
} |
453 |
|
454 |
public FilesystemFileFilter getGenericFilter() {
|
455 |
// FIXME: Este metodo, getGenericFilter, no tengo claro que deba existir (jjdc)
|
456 |
return this; |
457 |
} |
458 |
|
459 |
public String getDataStoreProviderName() { |
460 |
return null; |
461 |
} |
462 |
|
463 |
public String getDescription() { |
464 |
return "All supporteds"; |
465 |
} |
466 |
|
467 |
public boolean accept(File pathname) { |
468 |
try {
|
469 |
return this.createStoreParameters(pathname) != null; |
470 |
} catch (DataException e) {
|
471 |
throw new RuntimeException(e); // FIXME excpetion?? |
472 |
} |
473 |
} |
474 |
|
475 |
public DataStore open(File file) throws DataException, |
476 |
ValidateDataParametersException { |
477 |
DataManager manager = DALLocator.getDataManager(); |
478 |
String providerName = this.getProviderName(file); |
479 |
|
480 |
DataStoreParameters params = this.getAddParameters(file);
|
481 |
return manager.openStore(providerName, params);
|
482 |
} |
483 |
|
484 |
public NewDataStoreParameters getAddParameters(File file) |
485 |
throws DataException {
|
486 |
DataStoreParameters params = this.getParametersFor(file, null, false); |
487 |
NewDataStoreParameters newParams = this.getAddParameters(params
|
488 |
.getDataStoreName()); |
489 |
((FilesystemStoreParameters) newParams) |
490 |
.setFile(((FilesystemStoreParameters) params).getFile()); |
491 |
return newParams;
|
492 |
} |
493 |
|
494 |
@Deprecated
|
495 |
@Override
|
496 |
public File getResourcePath(DataStore dataStore, String resourceName) throws DataException { |
497 |
SimpleFileResource resource = (SimpleFileResource) this.getResource(dataStore, resourceName);
|
498 |
if( resource==null ) { |
499 |
return null; |
500 |
} |
501 |
return resource.getFile();
|
502 |
} |
503 |
|
504 |
@Override
|
505 |
public org.gvsig.tools.util.ResourcesStorage.Resource getResource(DataStore dataStore, String resourceName) throws DataException { |
506 |
FilesystemServerExplorerProvider provider = this.getProvider(dataStore.getProviderName());
|
507 |
if (provider == null) { |
508 |
return null; |
509 |
} |
510 |
String rootPathName = provider.getResourceRootPathName(dataStore);
|
511 |
if (rootPathName == null) { |
512 |
return null; |
513 |
} |
514 |
if( !dataStore.getChildren().isEmpty() ) {
|
515 |
FileMultiResource res = new FileMultiResource(
|
516 |
new File(rootPathName), |
517 |
dataStore.getName(), |
518 |
resourceName |
519 |
); |
520 |
return res;
|
521 |
} |
522 |
File f = new File(FilenameUtils.getFullPathNoEndSeparator(rootPathName),resourceName); |
523 |
if( f.exists() ) {
|
524 |
return new SimpleFileResource(f); |
525 |
} |
526 |
return new SimpleFileResource(new File(rootPathName + "." + resourceName)); |
527 |
} |
528 |
|
529 |
@Override
|
530 |
public List<ResourcesStorage.Resource> getResources(DataStore dataStore, String resourceName) throws DataException { |
531 |
FilesystemServerExplorerProvider provider = this.getProvider(dataStore.getProviderName());
|
532 |
if (provider == null) { |
533 |
return null; |
534 |
} |
535 |
String rootPathName = provider.getResourceRootPathName(dataStore);
|
536 |
if (rootPathName == null) { |
537 |
return null; |
538 |
} |
539 |
List<ResourcesStorage.Resource>ress = new ArrayList<>(); |
540 |
int n = 0; |
541 |
while(true) { |
542 |
String multiresourceName;
|
543 |
if( n==0 ) { |
544 |
multiresourceName = resourceName; |
545 |
} else {
|
546 |
multiresourceName = String.valueOf(n)+"."+resourceName; |
547 |
} |
548 |
File f = new File(rootPathName + "." + multiresourceName); |
549 |
if( !f.exists() ) {
|
550 |
break;
|
551 |
} |
552 |
ress.add(new SimpleFileResource(f));
|
553 |
n++; |
554 |
} |
555 |
if( ress.isEmpty() ) {
|
556 |
return null; |
557 |
} |
558 |
return ress;
|
559 |
} |
560 |
|
561 |
|
562 |
@Override
|
563 |
public DataStoreParameters get(String name) throws DataException { |
564 |
File theFile = new File(this.current,name); |
565 |
DataStoreParameters dsp = this.createStoreParameters(theFile);
|
566 |
return dsp;
|
567 |
} |
568 |
} |