View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  			// TODO: add filters
40  			return map;
41  		} catch (FileSystemException e) {
42  			throw new SlcException("Cannot list VFS resources from " + base, e);
43  		}
44  	}
45  
46  	/** recursive */
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  }