From: Mathieu Baudier Date: Sun, 7 Mar 2010 21:04:46 +0000 (+0000) Subject: Introduces Commons Virtual File System (VFS) integration X-Git-Tag: argeo-slc-2.1.7~1398 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=f994151ce12b65c50189d4296cfda839149f4a73;p=gpl%2Fargeo-slc.git Introduces Commons Virtual File System (VFS) integration git-svn-id: https://svn.argeo.org/slc/trunk@3417 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/demo/site/org.argeo.slc.demo.log4j/log4j.properties b/demo/site/org.argeo.slc.demo.log4j/log4j.properties index 293b4b9a6..1b6711423 100644 --- a/demo/site/org.argeo.slc.demo.log4j/log4j.properties +++ b/demo/site/org.argeo.slc.demo.log4j/log4j.properties @@ -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 diff --git a/runtime/org.argeo.slc.support.simple/pom.xml b/runtime/org.argeo.slc.support.simple/pom.xml index 014a2a219..a53ef42ce 100644 --- a/runtime/org.argeo.slc.support.simple/pom.xml +++ b/runtime/org.argeo.slc.support.simple/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.slc @@ -28,6 +29,7 @@ org.dbunit.dataset.xml;resolution:="optional", org.dbunit.operation;resolution:="optional", junit.framework;resolution:="optional", + org.apache.commons.vfs.* @@ -70,6 +72,12 @@ com.springsource.org.tmatesoft.svn + + + org.apache.commons + com.springsource.org.apache.commons.vfs + + org.springframework 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 index 000000000..c4a834469 --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResource.java @@ -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 index 000000000..157cc902f --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceFactory.java @@ -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 index 000000000..be00ef848 --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResourceSet.java @@ -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 listResources() { + try { + FileSystemManager fileSystemManager = VFS.getManager(); + FileObject fileObject = fileSystemManager.resolveFile(base); + Map map = new HashMap(); + 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 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; + } + +}