]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduces Commons Virtual File System (VFS) integration
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Mar 2010 21:04:46 +0000 (21:04 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Mar 2010 21:04:46 +0000 (21:04 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@3417 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

demo/site/org.argeo.slc.demo.log4j/log4j.properties
runtime/org.argeo.slc.support.simple/pom.xml
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResource.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceFactory.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceSet.java [new file with mode: 0644]

index 293b4b9a6490fd4bd2d1425c4dbcab4b5603b932..1b67114231b030dab2a4f1038b38fecc4de3c9b2 100644 (file)
@@ -15,7 +15,8 @@ log4j.logger.org.hibernate=WARN
 log4j.logger.org.springframework=WARN
 #log4j.logger.org.springframework.web=DEBUG
 #log4j.logger.org.springframework.jms=WARN
-#log4j.logger.org.springframework.security=WARN
+#log4j.logger.org.springframework.security=TRACE
+#log4j.logger.org.springframework.ldap=TRACE
 #log4j.logger.org.springframework.osgi.web=TRACE
 
 log4j.logger.org.apache.activemq=WARN
index 014a2a21965835b2c6dfec697cb4408053ba0e4f..a53ef42ceb9b6ff3c417e8456b33ad7a1f40116a 100644 (file)
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.slc</groupId>
@@ -28,6 +29,7 @@
                                                        org.dbunit.dataset.xml;resolution:="optional",
                                                        org.dbunit.operation;resolution:="optional",
                                                        junit.framework;resolution:="optional",
+                                                       org.apache.commons.vfs.*
                                                </Import-Package>
                                        </instructions>
                                </configuration>
                        <artifactId>com.springsource.org.tmatesoft.svn</artifactId>
                </dependency>
 
+               <!--  Commons VFS -->
+               <dependency>
+                       <groupId>org.apache.commons</groupId>
+                       <artifactId>com.springsource.org.apache.commons.vfs</artifactId>
+               </dependency>
+
                <!-- TODO: check if necessary here -->
                <dependency>
                        <groupId>org.springframework</groupId>
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResource.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResource.java
new file mode 100644 (file)
index 0000000..c4a8344
--- /dev/null
@@ -0,0 +1,93 @@
+package org.argeo.slc.vfs;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.commons.io.IOExceptionWithCause;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.NameScope;
+import org.argeo.slc.SlcException;
+import org.springframework.core.io.Resource;
+
+public class VfsResource implements Resource {
+       private FileObject fileObject;
+
+       public VfsResource(FileObject fileObject) {
+               this.fileObject = fileObject;
+       }
+
+       public Resource createRelative(String relativePath) throws IOException {
+               return new VfsResource(fileObject.resolveFile(relativePath,
+                               NameScope.DESCENDENT_OR_SELF));
+       }
+
+       public boolean exists() {
+               try {
+                       return fileObject.exists();
+               } catch (FileSystemException e) {
+                       throw new SlcException("Cannot find out whether " + fileObject
+                                       + " exists", e);
+               }
+       }
+
+       public String getDescription() {
+               return "VFS resource " + fileObject;
+       }
+
+       public File getFile() throws IOException {
+               throw new IOException("Cannot access " + getDescription()
+                               + " as a local file");
+               // TODO: access local files
+               // if(fileObject instanceof LocalFile){
+               // ((LocalFile)fileObject).
+               // }
+               // return null;
+       }
+
+       public String getFilename() {
+               return fileObject.getName().getBaseName();
+       }
+
+       public URI getURI() throws IOException {
+               try {
+                       return new URI(fileObject.getName().getURI());
+               } catch (URISyntaxException e) {
+                       throw new IOExceptionWithCause(e);
+               }
+       }
+
+       public URL getURL() throws IOException {
+               return fileObject.getURL();
+       }
+
+       public boolean isOpen() {
+               return fileObject.isContentOpen();
+       }
+
+       public boolean isReadable() {
+               try {
+                       return fileObject.isReadable();
+               } catch (FileSystemException e) {
+                       throw new SlcException("Cannot find out whether " + fileObject
+                                       + " is readable", e);
+               }
+       }
+
+       public long lastModified() throws IOException {
+               return fileObject.getContent().getLastModifiedTime();
+       }
+
+       public InputStream getInputStream() throws IOException {
+               return fileObject.getContent().getInputStream();
+       }
+
+       public FileObject getFileObject() {
+               return fileObject;
+       }
+
+}
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceFactory.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceFactory.java
new file mode 100644 (file)
index 0000000..157cc90
--- /dev/null
@@ -0,0 +1,36 @@
+package org.argeo.slc.vfs;
+
+import org.apache.commons.vfs.CacheStrategy;
+import org.apache.commons.vfs.FileSystemManager;
+import org.apache.commons.vfs.impl.StandardFileSystemManager;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.core.io.Resource;
+
+public class VfsResourceFactory implements FactoryBean, InitializingBean {
+       private String url;
+       private FileSystemManager fileSystemManager;
+
+       public Object getObject() throws Exception {
+               return new VfsResource(fileSystemManager.resolveFile(url));
+       }
+
+       public Class<?> getObjectType() {
+               return Resource.class;
+       }
+
+       public boolean isSingleton() {
+               return false;
+       }
+
+       public void afterPropertiesSet() throws Exception {
+               if (fileSystemManager == null) {
+                       fileSystemManager = new StandardFileSystemManager();
+                       ((StandardFileSystemManager) fileSystemManager)
+                                       .setCacheStrategy(CacheStrategy.ON_RESOLVE);
+                       ((StandardFileSystemManager) fileSystemManager).init();
+               }
+
+       }
+
+}
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceSet.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceSet.java
new file mode 100644 (file)
index 0000000..be00ef8
--- /dev/null
@@ -0,0 +1,54 @@
+package org.argeo.slc.vfs;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemManager;
+import org.apache.commons.vfs.VFS;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.core.deploy.ResourceSet;
+import org.springframework.core.io.Resource;
+
+public class VfsResourceSet implements ResourceSet {
+       private String base;
+
+       public Map<String, Resource> listResources() {
+               try {
+                       FileSystemManager fileSystemManager = VFS.getManager();
+                       FileObject fileObject = fileSystemManager.resolveFile(base);
+                       Map<String, Resource> map = new HashMap<String, Resource>();
+                       addToMap(map, "", fileObject);
+
+                       // TODO: add filters
+                       return map;
+               } catch (FileSystemException e) {
+                       throw new SlcException("Cannot list VFS resources from " + base, e);
+               }
+       }
+
+       /** recursive */
+       protected void addToMap(Map<String, Resource> map, String parentPath,
+                       FileObject fileObject) {
+               try {
+                       String newParentPath = parentPath
+                                       + fileObject.getName().getBaseName() + '/';
+                       if (fileObject.getType().hasChildren()) {
+                               for (FileObject child : fileObject.getChildren()) {
+                                       addToMap(map, newParentPath, child);
+                               }
+                       } else {
+                               map.put(parentPath + fileObject.getName().getBaseName(),
+                                               new VfsResource(fileObject));
+                       }
+               } catch (FileSystemException e) {
+                       throw new SlcException("Cannot add children from " + parentPath, e);
+               }
+       }
+
+       public void setBase(String base) {
+               this.base = base;
+       }
+
+}