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