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