Browse Source

polished VFS support (SPR-7197)

pull/1234/head
Juergen Hoeller 15 years ago
parent
commit
cbb90236dc
  1. 35
      org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java
  2. 71
      org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java
  3. 16
      org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java
  4. 13
      org.springframework.core/src/main/java/org/springframework/core/io/support/VfsPatternUtils.java

35
org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java

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

71
org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -32,29 +32,27 @@ import org.springframework.util.ReflectionUtils; @@ -32,29 +32,27 @@ import org.springframework.util.ReflectionUtils;
/**
* 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
* VFS 3.x (package <code>org.jboss.vfs</code>).
*
* <p/>
* Thanks go to Marius Bogoevici for the initial patch.
* JBoss AS 5+ uses VFS 2.x (package <code>org.jboss.virtual</code>) while
* JBoss AS 6+ uses VFS 3.x (package <code>org.jboss.vfs</code>).
*
* <p>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.
*
*
* @author Costin Leau
* @since 3.0.3
*/
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 VFS3_PKG = "org.jboss.vfs.";
private static final String VFS_NAME = "VFS";
private static enum VFS_VER {
V2, V3
}
private static enum VFS_VER { 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_URI = null;
@ -71,7 +69,6 @@ public abstract class VfsUtils { @@ -71,7 +69,6 @@ public abstract class VfsUtils {
protected static Class<?> VIRTUAL_FILE_VISITOR_INTERFACE;
protected static Method VIRTUAL_FILE_METHOD_VISIT;
private static Method VFS_UTILS_METHOD_IS_NESTED_FILE = null;
private static Method VFS_UTILS_METHOD_GET_COMPATIBLE_URI = null;
private static Field VISITOR_ATTRIBUTES_FIELD_RECURSE = null;
@ -79,33 +76,33 @@ public abstract class VfsUtils { @@ -79,33 +76,33 @@ public abstract class VfsUtils {
static {
ClassLoader loader = VfsUtils.class.getClassLoader();
String pkg;
Class<?> vfsClass;
String pkg = "";
Class<?> vfsClass = null;
// check JBoss 6
// check for JBoss 6
try {
vfsClass = loader.loadClass(VFS3_PKG + VFS_NAME);
version = VFS_VER.V3;
pkg = VFS3_PKG;
if (log.isDebugEnabled())
log.debug("JBoss VFS packages for JBoss AS 6 found");
} catch (ClassNotFoundException ex) {
if (logger.isDebugEnabled()) {
logger.debug("JBoss VFS packages for JBoss AS 6 found");
}
}
catch (ClassNotFoundException ex) {
// fallback to JBoss 5
if (log.isDebugEnabled())
log.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages");
if (logger.isDebugEnabled())
logger.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages");
try {
vfsClass = loader.loadClass(VFS2_PKG + VFS_NAME);
version = VFS_VER.V2;
pkg = VFS2_PKG;
if (log.isDebugEnabled())
log.debug("JBoss VFS packages for JBoss AS 5 found");
if (logger.isDebugEnabled())
logger.debug("JBoss VFS packages for JBoss AS 5 found");
} 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);
}
}
@ -144,8 +141,8 @@ public abstract class VfsUtils { @@ -144,8 +141,8 @@ public abstract class VfsUtils {
Class<?> visitorAttributesClass = loader.loadClass(pkg + "VisitorAttributes");
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);
}
}
@ -153,13 +150,15 @@ public abstract class VfsUtils { @@ -153,13 +150,15 @@ public abstract class VfsUtils {
protected static Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException {
try {
return method.invoke(target, args);
} catch (InvocationTargetException ex) {
}
catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException();
if (targetEx instanceof IOException) {
throw (IOException) targetEx;
}
ReflectionUtils.handleInvocationTargetException(ex);
} catch (Exception ex) {
}
catch (Exception ex) {
ReflectionUtils.handleReflectionException(ex);
}
@ -169,7 +168,8 @@ public abstract class VfsUtils { @@ -169,7 +168,8 @@ public abstract class VfsUtils {
static boolean exists(Object vfsResource) {
try {
return (Boolean) invokeVfsMethod(VIRTUAL_FILE_METHOD_EXISTS, vfsResource);
} catch (IOException ex) {
}
catch (IOException ex) {
return false;
}
}
@ -177,7 +177,8 @@ public abstract class VfsUtils { @@ -177,7 +177,8 @@ public abstract class VfsUtils {
static boolean isReadable(Object vfsResource) {
try {
return ((Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource) > 0);
} catch (IOException ex) {
}
catch (IOException ex) {
return false;
}
}
@ -201,7 +202,8 @@ public abstract class VfsUtils { @@ -201,7 +202,8 @@ public abstract class VfsUtils {
static String getName(Object vfsResource) {
try {
return (String) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_NAME, vfsResource);
} catch (IOException ex) {
}
catch (IOException ex) {
throw new IllegalStateException("Cannot get resource name", ex);
}
}
@ -221,7 +223,8 @@ public abstract class VfsUtils { @@ -221,7 +223,8 @@ public abstract class VfsUtils {
}
try {
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);
}
}

16
org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

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

13
org.springframework.core/src/main/java/org/springframework/core/io/support/VfsPatternUtils.java

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

Loading…
Cancel
Save