Skip to content

Commit cbb9023

Browse files
committed
polished VFS support (SPR-7197)
1 parent 53ed9b2 commit cbb9023

File tree

4 files changed

+74
-61
lines changed

4 files changed

+74
-61
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* VFS based {@link Resource} implementation.
30+
* Supports the corresponding VFS API versions on JBoss AS 5.x as well as 6.x.
3031
*
3132
* @author Ales Justin
3233
* @author Juergen Hoeller
@@ -39,61 +40,66 @@ public class VfsResource extends AbstractResource {
3940

4041
private final Object resource;
4142

43+
4244
public VfsResource(Object resources) {
4345
Assert.notNull(resources, "VirtualFile must not be null");
4446
this.resource = resources;
4547
}
4648

49+
4750
public boolean exists() {
48-
return VfsUtils.exists(resource);
51+
return VfsUtils.exists(this.resource);
4952
}
5053

5154
public boolean isReadable() {
52-
return VfsUtils.isReadable(resource);
55+
return VfsUtils.isReadable(this.resource);
5356
}
5457

5558
public long lastModified() throws IOException {
56-
return VfsUtils.getLastModified(resource);
59+
return VfsUtils.getLastModified(this.resource);
5760
}
5861

5962
public InputStream getInputStream() throws IOException {
60-
return VfsUtils.getInputStream(resource);
63+
return VfsUtils.getInputStream(this.resource);
6164
}
6265

6366
public URL getURL() throws IOException {
6467
try {
65-
return VfsUtils.getURL(resource);
66-
} catch (Exception ex) {
68+
return VfsUtils.getURL(this.resource);
69+
}
70+
catch (Exception ex) {
6771
throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex);
6872
}
6973
}
7074

7175
public URI getURI() throws IOException {
7276
try {
73-
return VfsUtils.getURI(resource);
74-
} catch (Exception ex) {
77+
return VfsUtils.getURI(this.resource);
78+
}
79+
catch (Exception ex) {
7580
throw new NestedIOException("Failed to obtain URI for " + this.resource, ex);
7681
}
7782
}
7883

7984
public File getFile() throws IOException {
80-
return VfsUtils.getFile(resource);
85+
return VfsUtils.getFile(this.resource);
8186
}
8287

8388
public Resource createRelative(String relativePath) throws IOException {
8489
if (!relativePath.startsWith(".") && relativePath.contains("/")) {
8590
try {
86-
return new VfsResource(VfsUtils.getChild(resource, relativePath));
87-
} catch (IOException ex) {
88-
// fall back to #getRelative
91+
return new VfsResource(VfsUtils.getChild(this.resource, relativePath));
92+
}
93+
catch (IOException ex) {
94+
// fall back to getRelative
8995
}
9096
}
9197

9298
return new VfsResource(VfsUtils.getRelative(new URL(getURL(), relativePath)));
9399
}
94100

95101
public String getFilename() {
96-
return VfsUtils.getName(resource);
102+
return VfsUtils.getName(this.resource);
97103
}
98104

99105
public String getDescription() {
@@ -109,4 +115,5 @@ public boolean equals(Object obj) {
109115
public int hashCode() {
110116
return this.resource.hashCode();
111117
}
112-
}
118+
119+
}

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,29 +32,27 @@
3232

3333
/**
3434
* Utility for detecting the JBoss VFS version available in the classpath.
35-
* JBoss AS 5+ uses VFS 2.x (package <code>org.jboss.virtual</code>) while JBoss AS 6+ uses
36-
* VFS 3.x (package <code>org.jboss.vfs</code>).
37-
*
38-
* <p/>
39-
* Thanks go to Marius Bogoevici for the initial patch.
35+
* JBoss AS 5+ uses VFS 2.x (package <code>org.jboss.virtual</code>) while
36+
* JBoss AS 6+ uses VFS 3.x (package <code>org.jboss.vfs</code>).
37+
*
38+
* <p>Thanks go to Marius Bogoevici for the initial patch.
4039
*
4140
* <b>Note:</b> This is an internal class and should not be used outside the framework.
42-
*
41+
*
4342
* @author Costin Leau
43+
* @since 3.0.3
4444
*/
4545
public abstract class VfsUtils {
4646

47-
private static final Log log = LogFactory.getLog(VfsUtils.class);
47+
private static final Log logger = LogFactory.getLog(VfsUtils.class);
4848

4949
private static final String VFS2_PKG = "org.jboss.virtual.";
5050
private static final String VFS3_PKG = "org.jboss.vfs.";
5151
private static final String VFS_NAME = "VFS";
5252

53-
private static enum VFS_VER {
54-
V2, V3
55-
}
53+
private static enum VFS_VER { V2, V3 }
5654

57-
private static VFS_VER version = null;
55+
private static VFS_VER version;
5856

5957
private static Method VFS_METHOD_GET_ROOT_URL = null;
6058
private static Method VFS_METHOD_GET_ROOT_URI = null;
@@ -71,41 +69,40 @@ private static enum VFS_VER {
7169
protected static Class<?> VIRTUAL_FILE_VISITOR_INTERFACE;
7270
protected static Method VIRTUAL_FILE_METHOD_VISIT;
7371

74-
7572
private static Method VFS_UTILS_METHOD_IS_NESTED_FILE = null;
7673
private static Method VFS_UTILS_METHOD_GET_COMPATIBLE_URI = null;
7774
private static Field VISITOR_ATTRIBUTES_FIELD_RECURSE = null;
7875
private static Method GET_PHYSICAL_FILE = null;
7976

8077
static {
8178
ClassLoader loader = VfsUtils.class.getClassLoader();
79+
String pkg;
80+
Class<?> vfsClass;
8281

83-
String pkg = "";
84-
85-
Class<?> vfsClass = null;
86-
87-
// check JBoss 6
82+
// check for JBoss 6
8883
try {
8984
vfsClass = loader.loadClass(VFS3_PKG + VFS_NAME);
9085
version = VFS_VER.V3;
9186
pkg = VFS3_PKG;
9287

93-
if (log.isDebugEnabled())
94-
log.debug("JBoss VFS packages for JBoss AS 6 found");
95-
} catch (ClassNotFoundException ex) {
88+
if (logger.isDebugEnabled()) {
89+
logger.debug("JBoss VFS packages for JBoss AS 6 found");
90+
}
91+
}
92+
catch (ClassNotFoundException ex) {
9693
// fallback to JBoss 5
97-
if (log.isDebugEnabled())
98-
log.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages");
94+
if (logger.isDebugEnabled())
95+
logger.debug("JBoss VFS packages for JBoss AS 6 not found; falling back to JBoss AS 5 packages");
9996
try {
10097
vfsClass = loader.loadClass(VFS2_PKG + VFS_NAME);
10198

10299
version = VFS_VER.V2;
103100
pkg = VFS2_PKG;
104101

105-
if (log.isDebugEnabled())
106-
log.debug("JBoss VFS packages for JBoss AS 5 found");
102+
if (logger.isDebugEnabled())
103+
logger.debug("JBoss VFS packages for JBoss AS 5 found");
107104
} catch (ClassNotFoundException ex1) {
108-
log.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled");
105+
logger.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled");
109106
throw new IllegalStateException("Cannot detect JBoss VFS packages", ex1);
110107
}
111108
}
@@ -144,22 +141,24 @@ private static enum VFS_VER {
144141

145142
Class<?> visitorAttributesClass = loader.loadClass(pkg + "VisitorAttributes");
146143
VISITOR_ATTRIBUTES_FIELD_RECURSE = ReflectionUtils.findField(visitorAttributesClass, "RECURSE");
147-
148-
} catch (ClassNotFoundException ex) {
144+
}
145+
catch (ClassNotFoundException ex) {
149146
throw new IllegalStateException("Could not detect the JBoss VFS infrastructure", ex);
150147
}
151148
}
152149

153150
protected static Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException {
154151
try {
155152
return method.invoke(target, args);
156-
} catch (InvocationTargetException ex) {
153+
}
154+
catch (InvocationTargetException ex) {
157155
Throwable targetEx = ex.getTargetException();
158156
if (targetEx instanceof IOException) {
159157
throw (IOException) targetEx;
160158
}
161159
ReflectionUtils.handleInvocationTargetException(ex);
162-
} catch (Exception ex) {
160+
}
161+
catch (Exception ex) {
163162
ReflectionUtils.handleReflectionException(ex);
164163
}
165164

@@ -169,15 +168,17 @@ protected static Object invokeVfsMethod(Method method, Object target, Object...
169168
static boolean exists(Object vfsResource) {
170169
try {
171170
return (Boolean) invokeVfsMethod(VIRTUAL_FILE_METHOD_EXISTS, vfsResource);
172-
} catch (IOException ex) {
171+
}
172+
catch (IOException ex) {
173173
return false;
174174
}
175175
}
176176

177177
static boolean isReadable(Object vfsResource) {
178178
try {
179179
return ((Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource) > 0);
180-
} catch (IOException ex) {
180+
}
181+
catch (IOException ex) {
181182
return false;
182183
}
183184
}
@@ -201,7 +202,8 @@ static URI getURI(Object vfsResource) throws IOException {
201202
static String getName(Object vfsResource) {
202203
try {
203204
return (String) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_NAME, vfsResource);
204-
} catch (IOException ex) {
205+
}
206+
catch (IOException ex) {
205207
throw new IllegalStateException("Cannot get resource name", ex);
206208
}
207209
}
@@ -221,7 +223,8 @@ static File getFile(Object vfsResource) throws IOException {
221223
}
222224
try {
223225
return new File((URI) invokeVfsMethod(VFS_UTILS_METHOD_GET_COMPATIBLE_URI, null, vfsResource));
224-
} catch (Exception ex) {
226+
}
227+
catch (Exception ex) {
225228
throw new NestedIOException("Failed to obtain File reference for " + vfsResource, ex);
226229
}
227230
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.apache.commons.logging.Log;
3535
import org.apache.commons.logging.LogFactory;
36+
3637
import org.springframework.core.io.DefaultResourceLoader;
3738
import org.springframework.core.io.FileSystemResource;
3839
import org.springframework.core.io.Resource;
@@ -645,10 +646,11 @@ protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> r
645646
*/
646647
private static class VfsResourceMatchingDelegate {
647648

648-
public static Set<Resource> findMatchingResources(Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException {
649+
public static Set<Resource> findMatchingResources(
650+
Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException {
649651
Object root = VfsPatternUtils.findRoot(rootResource.getURL());
650-
PatternVirtualFileVisitor visitor = new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root),
651-
locationPattern, pathMatcher);
652+
PatternVirtualFileVisitor visitor =
653+
new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
652654
VfsPatternUtils.visit(root, visitor);
653655
return visitor.getResources();
654656
}
@@ -674,7 +676,6 @@ public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher
674676
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
675677
}
676678

677-
678679
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
679680
String methodName = method.getName();
680681
if (Object.class.equals(method.getDeclaringClass())) {
@@ -701,8 +702,8 @@ else if ("toString".equals(methodName)) {
701702
}
702703

703704
public void visit(Object vfsResource) {
704-
if (this.pathMatcher.match(this.subPattern, VfsPatternUtils.getPath(vfsResource).substring(
705-
this.rootPath.length()))) {
705+
if (this.pathMatcher.match(this.subPattern,
706+
VfsPatternUtils.getPath(vfsResource).substring(this.rootPath.length()))) {
706707
this.resources.add(new VfsResource(vfsResource));
707708
}
708709
}
@@ -727,4 +728,5 @@ public String toString() {
727728
return sb.toString();
728729
}
729730
}
730-
}
731+
732+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,13 +24,14 @@
2424
import org.springframework.core.io.VfsUtils;
2525

2626
/**
27-
* Artificial class used for accessing the {@link VfsUtils} methods without exposing them
28-
* to the entire world.
29-
*
30-
* @author Costin Leau
27+
* Artificial class used for accessing the {@link VfsUtils} methods
28+
* without exposing them to the entire world.
3129
*
30+
* @author Costin Leau
31+
* @since 3.0.3
3232
*/
3333
abstract class VfsPatternUtils extends VfsUtils {
34+
3435
static Object getVisitorAttribute() {
3536
return doGetVisitorAttribute();
3637
}
@@ -46,7 +47,7 @@ static Object findRoot(URL url) throws IOException {
4647
static void visit(Object resource, InvocationHandler visitor) throws IOException {
4748
Object visitorProxy = Proxy.newProxyInstance(VIRTUAL_FILE_VISITOR_INTERFACE.getClassLoader(),
4849
new Class<?>[] { VIRTUAL_FILE_VISITOR_INTERFACE }, visitor);
49-
5050
invokeVfsMethod(VIRTUAL_FILE_METHOD_VISIT, resource, visitorProxy);
5151
}
52+
5253
}

0 commit comments

Comments
 (0)