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