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