1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.vfs;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.commons.vfs2.FileObject;
22 import org.apache.commons.vfs2.FileSystemException;
23 import org.apache.commons.vfs2.FileSystemManager;
24 import org.apache.commons.vfs2.VFS;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.core.deploy.ResourceSet;
27 import org.springframework.core.io.Resource;
28
29 public class VfsResourceSet implements ResourceSet {
30 private String base;
31
32 public Map<String, Resource> listResources() {
33 try {
34 FileSystemManager fileSystemManager = VFS.getManager();
35 FileObject fileObject = fileSystemManager.resolveFile(base);
36 Map<String, Resource> map = new HashMap<String, Resource>();
37 addToMap(map, "", fileObject);
38
39
40 return map;
41 } catch (FileSystemException e) {
42 throw new SlcException("Cannot list VFS resources from " + base, e);
43 }
44 }
45
46
47 protected void addToMap(Map<String, Resource> map, String parentPath,
48 FileObject fileObject) {
49 try {
50 String newParentPath = parentPath
51 + fileObject.getName().getBaseName() + '/';
52 if (fileObject.getType().hasChildren()) {
53 for (FileObject child : fileObject.getChildren()) {
54 addToMap(map, newParentPath, child);
55 }
56 } else {
57 map.put(parentPath + fileObject.getName().getBaseName(),
58 new VfsResource(fileObject));
59 }
60 } catch (FileSystemException e) {
61 throw new SlcException("Cannot add children from " + parentPath, e);
62 }
63 }
64
65 public void setBase(String base) {
66 this.base = base;
67 }
68
69 }