polished VFS support (SPR-7197)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3415 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-06-11 19:54:58 +00:00
parent 7a9f9febf8
commit 858294f185
4 changed files with 74 additions and 61 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.util.Assert;
/** /**
* VFS based {@link Resource} implementation. * VFS based {@link Resource} implementation.
* Supports the corresponding VFS API versions on JBoss AS 5.x as well as 6.x.
* *
* @author Ales Justin * @author Ales Justin
* @author Juergen Hoeller * @author Juergen Hoeller
@ -39,53 +40,58 @@ public class VfsResource extends AbstractResource {
private final Object resource; private final Object resource;
public VfsResource(Object resources) { public VfsResource(Object resources) {
Assert.notNull(resources, "VirtualFile must not be null"); Assert.notNull(resources, "VirtualFile must not be null");
this.resource = resources; this.resource = resources;
} }
public boolean exists() { public boolean exists() {
return VfsUtils.exists(resource); return VfsUtils.exists(this.resource);
} }
public boolean isReadable() { public boolean isReadable() {
return VfsUtils.isReadable(resource); return VfsUtils.isReadable(this.resource);
} }
public long lastModified() throws IOException { public long lastModified() throws IOException {
return VfsUtils.getLastModified(resource); return VfsUtils.getLastModified(this.resource);
} }
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
return VfsUtils.getInputStream(resource); return VfsUtils.getInputStream(this.resource);
} }
public URL getURL() throws IOException { public URL getURL() throws IOException {
try { try {
return VfsUtils.getURL(resource); return VfsUtils.getURL(this.resource);
} catch (Exception ex) { }
catch (Exception ex) {
throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex); throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex);
} }
} }
public URI getURI() throws IOException { public URI getURI() throws IOException {
try { try {
return VfsUtils.getURI(resource); return VfsUtils.getURI(this.resource);
} catch (Exception ex) { }
catch (Exception ex) {
throw new NestedIOException("Failed to obtain URI for " + this.resource, ex); throw new NestedIOException("Failed to obtain URI for " + this.resource, ex);
} }
} }
public File getFile() throws IOException { public File getFile() throws IOException {
return VfsUtils.getFile(resource); return VfsUtils.getFile(this.resource);
} }
public Resource createRelative(String relativePath) throws IOException { public Resource createRelative(String relativePath) throws IOException {
if (!relativePath.startsWith(".") && relativePath.contains("/")) { if (!relativePath.startsWith(".") && relativePath.contains("/")) {
try { try {
return new VfsResource(VfsUtils.getChild(resource, relativePath)); return new VfsResource(VfsUtils.getChild(this.resource, relativePath));
} catch (IOException ex) { }
// fall back to #getRelative catch (IOException ex) {
// fall back to getRelative
} }
} }
@ -93,7 +99,7 @@ public class VfsResource extends AbstractResource {
} }
public String getFilename() { public String getFilename() {
return VfsUtils.getName(resource); return VfsUtils.getName(this.resource);
} }
public String getDescription() { public String getDescription() {
@ -109,4 +115,5 @@ public class VfsResource extends AbstractResource {
public int hashCode() { public int hashCode() {
return this.resource.hashCode(); return this.resource.hashCode();
} }
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2010 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,29 +32,27 @@ import org.springframework.util.ReflectionUtils;
/** /**
* Utility for detecting the JBoss VFS version available in the classpath. * Utility for detecting the JBoss VFS version available in the classpath.
* JBoss AS 5+ uses VFS 2.x (package <code>org.jboss.virtual</code>) while JBoss AS 6+ uses * JBoss AS 5+ uses VFS 2.x (package <code>org.jboss.virtual</code>) while
* VFS 3.x (package <code>org.jboss.vfs</code>). * JBoss AS 6+ uses VFS 3.x (package <code>org.jboss.vfs</code>).
* *
* <p/> * <p>Thanks go to Marius Bogoevici for the initial patch.
* Thanks go to Marius Bogoevici for the initial patch.
* *
* <b>Note:</b> This is an internal class and should not be used outside the framework. * <b>Note:</b> This is an internal class and should not be used outside the framework.
* *
* @author Costin Leau * @author Costin Leau
* @since 3.0.3
*/ */
public abstract class VfsUtils { public abstract class VfsUtils {
private static final Log log = LogFactory.getLog(VfsUtils.class); private static final Log logger = LogFactory.getLog(VfsUtils.class);
private static final String VFS2_PKG = "org.jboss.virtual."; private static final String VFS2_PKG = "org.jboss.virtual.";
private static final String VFS3_PKG = "org.jboss.vfs."; private static final String VFS3_PKG = "org.jboss.vfs.";
private static final String VFS_NAME = "VFS"; private static final String VFS_NAME = "VFS";
private static enum VFS_VER { private static enum VFS_VER { V2, V3 }
V2, V3
}
private static VFS_VER version = null; private static VFS_VER version;
private static Method VFS_METHOD_GET_ROOT_URL = null; private static Method VFS_METHOD_GET_ROOT_URL = null;
private static Method VFS_METHOD_GET_ROOT_URI = null; private static Method VFS_METHOD_GET_ROOT_URI = null;
@ -71,7 +69,6 @@ public abstract class VfsUtils {
protected static Class<?> VIRTUAL_FILE_VISITOR_INTERFACE; protected static Class<?> VIRTUAL_FILE_VISITOR_INTERFACE;
protected static Method VIRTUAL_FILE_METHOD_VISIT; protected static Method VIRTUAL_FILE_METHOD_VISIT;
private static Method VFS_UTILS_METHOD_IS_NESTED_FILE = null; private static Method VFS_UTILS_METHOD_IS_NESTED_FILE = null;
private static Method VFS_UTILS_METHOD_GET_COMPATIBLE_URI = null; private static Method VFS_UTILS_METHOD_GET_COMPATIBLE_URI = null;
private static Field VISITOR_ATTRIBUTES_FIELD_RECURSE = null; private static Field VISITOR_ATTRIBUTES_FIELD_RECURSE = null;
@ -79,33 +76,33 @@ public abstract class VfsUtils {
static { static {
ClassLoader loader = VfsUtils.class.getClassLoader(); ClassLoader loader = VfsUtils.class.getClassLoader();
String pkg;
Class<?> vfsClass;
String pkg = ""; // check for JBoss 6
Class<?> vfsClass = null;
// check JBoss 6
try { try {
vfsClass = loader.loadClass(VFS3_PKG + VFS_NAME); vfsClass = loader.loadClass(VFS3_PKG + VFS_NAME);
version = VFS_VER.V3; version = VFS_VER.V3;
pkg = VFS3_PKG; pkg = VFS3_PKG;
if (log.isDebugEnabled()) if (logger.isDebugEnabled()) {
log.debug("JBoss VFS packages for JBoss AS 6 found"); logger.debug("JBoss VFS packages for JBoss AS 6 found");
} catch (ClassNotFoundException ex) { }
}
catch (ClassNotFoundException ex) {
// fallback to JBoss 5 // fallback to JBoss 5
if (log.isDebugEnabled()) if (logger.isDebugEnabled())
log.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages"); logger.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages");
try { try {
vfsClass = loader.loadClass(VFS2_PKG + VFS_NAME); vfsClass = loader.loadClass(VFS2_PKG + VFS_NAME);
version = VFS_VER.V2; version = VFS_VER.V2;
pkg = VFS2_PKG; pkg = VFS2_PKG;
if (log.isDebugEnabled()) if (logger.isDebugEnabled())
log.debug("JBoss VFS packages for JBoss AS 5 found"); logger.debug("JBoss VFS packages for JBoss AS 5 found");
} catch (ClassNotFoundException ex1) { } catch (ClassNotFoundException ex1) {
log.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled"); logger.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled");
throw new IllegalStateException("Cannot detect JBoss VFS packages", ex1); throw new IllegalStateException("Cannot detect JBoss VFS packages", ex1);
} }
} }
@ -144,8 +141,8 @@ public abstract class VfsUtils {
Class<?> visitorAttributesClass = loader.loadClass(pkg + "VisitorAttributes"); Class<?> visitorAttributesClass = loader.loadClass(pkg + "VisitorAttributes");
VISITOR_ATTRIBUTES_FIELD_RECURSE = ReflectionUtils.findField(visitorAttributesClass, "RECURSE"); VISITOR_ATTRIBUTES_FIELD_RECURSE = ReflectionUtils.findField(visitorAttributesClass, "RECURSE");
}
} catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
throw new IllegalStateException("Could not detect the JBoss VFS infrastructure", ex); throw new IllegalStateException("Could not detect the JBoss VFS infrastructure", ex);
} }
} }
@ -153,13 +150,15 @@ public abstract class VfsUtils {
protected static Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException { protected static Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException {
try { try {
return method.invoke(target, args); return method.invoke(target, args);
} catch (InvocationTargetException ex) { }
catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException(); Throwable targetEx = ex.getTargetException();
if (targetEx instanceof IOException) { if (targetEx instanceof IOException) {
throw (IOException) targetEx; throw (IOException) targetEx;
} }
ReflectionUtils.handleInvocationTargetException(ex); ReflectionUtils.handleInvocationTargetException(ex);
} catch (Exception ex) { }
catch (Exception ex) {
ReflectionUtils.handleReflectionException(ex); ReflectionUtils.handleReflectionException(ex);
} }
@ -169,7 +168,8 @@ public abstract class VfsUtils {
static boolean exists(Object vfsResource) { static boolean exists(Object vfsResource) {
try { try {
return (Boolean) invokeVfsMethod(VIRTUAL_FILE_METHOD_EXISTS, vfsResource); return (Boolean) invokeVfsMethod(VIRTUAL_FILE_METHOD_EXISTS, vfsResource);
} catch (IOException ex) { }
catch (IOException ex) {
return false; return false;
} }
} }
@ -177,7 +177,8 @@ public abstract class VfsUtils {
static boolean isReadable(Object vfsResource) { static boolean isReadable(Object vfsResource) {
try { try {
return ((Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource) > 0); return ((Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource) > 0);
} catch (IOException ex) { }
catch (IOException ex) {
return false; return false;
} }
} }
@ -201,7 +202,8 @@ public abstract class VfsUtils {
static String getName(Object vfsResource) { static String getName(Object vfsResource) {
try { try {
return (String) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_NAME, vfsResource); return (String) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_NAME, vfsResource);
} catch (IOException ex) { }
catch (IOException ex) {
throw new IllegalStateException("Cannot get resource name", ex); throw new IllegalStateException("Cannot get resource name", ex);
} }
} }
@ -221,7 +223,8 @@ public abstract class VfsUtils {
} }
try { try {
return new File((URI) invokeVfsMethod(VFS_UTILS_METHOD_GET_COMPATIBLE_URI, null, vfsResource)); return new File((URI) invokeVfsMethod(VFS_UTILS_METHOD_GET_COMPATIBLE_URI, null, vfsResource));
} catch (Exception ex) { }
catch (Exception ex) {
throw new NestedIOException("Failed to obtain File reference for " + vfsResource, ex); throw new NestedIOException("Failed to obtain File reference for " + vfsResource, ex);
} }
} }

View File

@ -33,6 +33,7 @@ import java.util.jar.JarFile;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -645,10 +646,11 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
*/ */
private static class VfsResourceMatchingDelegate { private static class VfsResourceMatchingDelegate {
public static Set<Resource> findMatchingResources(Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException { public static Set<Resource> findMatchingResources(
Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException {
Object root = VfsPatternUtils.findRoot(rootResource.getURL()); Object root = VfsPatternUtils.findRoot(rootResource.getURL());
PatternVirtualFileVisitor visitor = new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), PatternVirtualFileVisitor visitor =
locationPattern, pathMatcher); new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
VfsPatternUtils.visit(root, visitor); VfsPatternUtils.visit(root, visitor);
return visitor.getResources(); return visitor.getResources();
} }
@ -674,7 +676,6 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/"); this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
} }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName(); String methodName = method.getName();
if (Object.class.equals(method.getDeclaringClass())) { if (Object.class.equals(method.getDeclaringClass())) {
@ -701,8 +702,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
public void visit(Object vfsResource) { public void visit(Object vfsResource) {
if (this.pathMatcher.match(this.subPattern, VfsPatternUtils.getPath(vfsResource).substring( if (this.pathMatcher.match(this.subPattern,
this.rootPath.length()))) { VfsPatternUtils.getPath(vfsResource).substring(this.rootPath.length()))) {
this.resources.add(new VfsResource(vfsResource)); this.resources.add(new VfsResource(vfsResource));
} }
} }
@ -727,4 +728,5 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
return sb.toString(); return sb.toString();
} }
} }
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,13 +24,14 @@ import java.net.URL;
import org.springframework.core.io.VfsUtils; import org.springframework.core.io.VfsUtils;
/** /**
* Artificial class used for accessing the {@link VfsUtils} methods without exposing them * Artificial class used for accessing the {@link VfsUtils} methods
* to the entire world. * without exposing them to the entire world.
*
* @author Costin Leau
* *
* @author Costin Leau
* @since 3.0.3
*/ */
abstract class VfsPatternUtils extends VfsUtils { abstract class VfsPatternUtils extends VfsUtils {
static Object getVisitorAttribute() { static Object getVisitorAttribute() {
return doGetVisitorAttribute(); return doGetVisitorAttribute();
} }
@ -46,7 +47,7 @@ abstract class VfsPatternUtils extends VfsUtils {
static void visit(Object resource, InvocationHandler visitor) throws IOException { static void visit(Object resource, InvocationHandler visitor) throws IOException {
Object visitorProxy = Proxy.newProxyInstance(VIRTUAL_FILE_VISITOR_INTERFACE.getClassLoader(), Object visitorProxy = Proxy.newProxyInstance(VIRTUAL_FILE_VISITOR_INTERFACE.getClassLoader(),
new Class<?>[] { VIRTUAL_FILE_VISITOR_INTERFACE }, visitor); new Class<?>[] { VIRTUAL_FILE_VISITOR_INTERFACE }, visitor);
invokeVfsMethod(VIRTUAL_FILE_METHOD_VISIT, resource, visitorProxy); invokeVfsMethod(VIRTUAL_FILE_METHOD_VISIT, resource, visitorProxy);
} }
} }