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