9 changed files with 219 additions and 390 deletions
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2009 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.io; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.net.URI; |
||||
import java.net.URL; |
||||
|
||||
import org.jboss.virtual.VFS; |
||||
|
||||
import org.springframework.util.ResourceUtils; |
||||
|
||||
/** |
||||
* Abstract base class for resources which resolve URLs into File references, |
||||
* such as {@link UrlResource} or {@link ClassPathResource}. |
||||
* |
||||
* <p>Detects the "file" protocol as well as the JBoss "vfs" protocol in URLs, |
||||
* resolving file system references accordingly. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
public abstract class AbstractFileResolvingResource extends AbstractResource { |
||||
|
||||
/** |
||||
* This implementation returns a File reference for the underlying class path |
||||
* resource, provided that it refers to a file in the file system. |
||||
* @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String) |
||||
*/ |
||||
@Override |
||||
public File getFile() throws IOException { |
||||
URL url = getURL(); |
||||
if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { |
||||
return VfsResourceDelegate.getResource(url).getFile(); |
||||
} |
||||
return ResourceUtils.getFile(url, getDescription()); |
||||
} |
||||
|
||||
/** |
||||
* This implementation determines the underlying File |
||||
* (or jar file, in case of a resource in a jar/zip). |
||||
*/ |
||||
@Override |
||||
protected File getFileForLastModifiedCheck() throws IOException { |
||||
URL url = getURL(); |
||||
if (ResourceUtils.isJarURL(url)) { |
||||
URL actualUrl = ResourceUtils.extractJarFileURL(url); |
||||
if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { |
||||
return VfsResourceDelegate.getResource(actualUrl).getFile(); |
||||
} |
||||
return ResourceUtils.getFile(actualUrl, "Jar URL"); |
||||
} |
||||
else { |
||||
return getFile(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* This implementation returns a File reference for the underlying class path |
||||
* resource, provided that it refers to a file in the file system. |
||||
* @see org.springframework.util.ResourceUtils#getFile(java.net.URI, String) |
||||
*/ |
||||
protected File getFile(URI uri) throws IOException { |
||||
if (uri.getScheme().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { |
||||
return VfsResourceDelegate.getResource(uri).getFile(); |
||||
} |
||||
return ResourceUtils.getFile(uri, getDescription()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime. |
||||
*/ |
||||
private static class VfsResourceDelegate { |
||||
|
||||
public static Resource getResource(URL url) throws IOException { |
||||
return new VfsResource(VFS.getRoot(url)); |
||||
} |
||||
|
||||
public static Resource getResource(URI uri) throws IOException { |
||||
return new VfsResource(VFS.getRoot(uri)); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,48 +0,0 @@
@@ -1,48 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.io.support; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.URL; |
||||
import java.net.URI; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.util.PathMatcher; |
||||
|
||||
/** |
||||
* Abstraction for a path matching strategy, for customizing the way in which a resource can be |
||||
* scanned for finding underlying resources that match a given pattern. |
||||
* Used for implementing application-server specific resource scanning strategies (e.g. for JBoss AS) |
||||
* |
||||
* @author Marius Bogoevici |
||||
* |
||||
*/ |
||||
public interface ResourceHandlingDelegate { |
||||
|
||||
boolean canHandleResource(URL url); |
||||
|
||||
boolean canHandleResource(URI uri); |
||||
|
||||
Set<Resource> findMatchingResources(Resource rootResource, String subPattern, PathMatcher pathMatcher) throws |
||||
IOException; |
||||
|
||||
Resource loadResource(URL url) throws IOException; |
||||
|
||||
Resource loadResource(URI uri) throws IOException; |
||||
|
||||
} |
@ -1,107 +0,0 @@
@@ -1,107 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2007 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.io.support; |
||||
|
||||
import java.net.URL; |
||||
import java.net.URI; |
||||
import java.util.Set; |
||||
import java.io.IOException; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.File; |
||||
|
||||
import org.springframework.util.ReflectionUtils; |
||||
import org.springframework.util.PathMatcher; |
||||
import org.springframework.util.ResourceUtils; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.core.io.support.jboss.VfsResourceHandlingDelegate; |
||||
import org.springframework.core.io.Resource; |
||||
|
||||
/** |
||||
* Utility class for determining whether a given URL is a resource |
||||
* location that should receive special treatmen, such as looking up a |
||||
* resource in JBoss VFS. |
||||
* |
||||
* @author Thomas Risberg |
||||
* @author Marius Bogoevici |
||||
* @since 3.0 |
||||
*/ |
||||
public abstract class ResourceHandlingUtils { |
||||
|
||||
private static ResourceHandlingDelegate resourceHandlingDelegate; |
||||
|
||||
static { |
||||
try { |
||||
Class jBossVersionClass = PathMatchingResourcePatternResolver.class.getClassLoader().loadClass("org.jboss.Version"); |
||||
Object versionObject = ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(jBossVersionClass, "getInstance"), null); |
||||
Integer majorVersion = (Integer) ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(jBossVersionClass, "getMajor"), versionObject); |
||||
// For JBoss AS versions 5 and higher
|
||||
if (majorVersion >= 5) { |
||||
resourceHandlingDelegate = new VfsResourceHandlingDelegate(); |
||||
} |
||||
} |
||||
catch (Throwable ex) { |
||||
// do nothing
|
||||
} |
||||
} |
||||
|
||||
public static File getFile(URL resourceUrl) throws FileNotFoundException { |
||||
return getFile(resourceUrl, "URL"); |
||||
} |
||||
|
||||
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException { |
||||
Assert.notNull(resourceUrl, "Resource URL must not be null"); |
||||
if (useResourceHandlingDelegate(resourceUrl)) { |
||||
try { |
||||
return resourceHandlingDelegate.loadResource(resourceUrl).getFile(); |
||||
} |
||||
catch (IOException e) { |
||||
throw new FileNotFoundException(description + " cannot be resolved as a file resource"); |
||||
} |
||||
} |
||||
return ResourceUtils.getFile(resourceUrl, description); |
||||
} |
||||
|
||||
public static File getFile(URI resourceUri) throws FileNotFoundException { |
||||
return getFile(resourceUri, "URI"); |
||||
} |
||||
|
||||
public static File getFile(URI resourceUri, String description) throws FileNotFoundException { |
||||
Assert.notNull(resourceUri, "Resource URI must not be null"); |
||||
if (useResourceHandlingDelegate(resourceUri)) { |
||||
try { |
||||
return resourceHandlingDelegate.loadResource(resourceUri).getFile(); |
||||
} |
||||
catch (IOException e) { |
||||
throw new FileNotFoundException(description + " cannot be resolved as a file resource"); |
||||
} |
||||
} |
||||
return ResourceUtils.getFile(resourceUri, description); |
||||
} |
||||
|
||||
public static boolean useResourceHandlingDelegate(URL url) { |
||||
return resourceHandlingDelegate != null && resourceHandlingDelegate.canHandleResource(url); |
||||
} |
||||
|
||||
public static boolean useResourceHandlingDelegate(URI uri) { |
||||
return resourceHandlingDelegate != null && resourceHandlingDelegate.canHandleResource(uri); |
||||
} |
||||
|
||||
public static Set<Resource> findMatchingResourcesByDelegate(Resource resource, String pattern, PathMatcher matcher) throws |
||||
IOException { |
||||
return resourceHandlingDelegate.findMatchingResources(resource, pattern, matcher); |
||||
} |
||||
} |
@ -1,117 +0,0 @@
@@ -1,117 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.io.support.jboss; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.File; |
||||
import java.util.Set; |
||||
import java.util.LinkedHashSet; |
||||
import java.net.URL; |
||||
import java.net.URI; |
||||
|
||||
import org.jboss.virtual.VFS; |
||||
import org.jboss.virtual.VirtualFile; |
||||
import org.jboss.virtual.VirtualFileVisitor; |
||||
import org.jboss.virtual.VisitorAttributes; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.core.io.support.ResourceHandlingDelegate; |
||||
import org.springframework.util.PathMatcher; |
||||
|
||||
/** |
||||
* {@link org.springframework.core.io.support.ResourceHandlingDelegate} implementation |
||||
* for JBoss' Virtual File System. |
||||
* |
||||
* @author Marius Bogoevici |
||||
* @author Ales Justin |
||||
* |
||||
* Note: Thanks to David Ward from Alfresco for indicating a fix for path matching. |
||||
*/ |
||||
public class VfsResourceHandlingDelegate implements ResourceHandlingDelegate { |
||||
|
||||
public boolean canHandleResource(URL url) { |
||||
return url.getProtocol().startsWith("vfs"); |
||||
} |
||||
|
||||
public boolean canHandleResource(URI uri) { |
||||
return uri.getScheme().startsWith("vfs"); |
||||
} |
||||
|
||||
|
||||
public Set<Resource> findMatchingResources(Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException { |
||||
VirtualFile root = VFS.getRoot(rootResource.getURL()); |
||||
PatternVirtualFileVisitor visitor = new PatternVirtualFileVisitor(root.getPathName(), locationPattern, pathMatcher); |
||||
root.visit(visitor); |
||||
return visitor.getResources(); |
||||
} |
||||
|
||||
public Resource loadResource(URL url) throws IOException{ |
||||
return new VfsResource(VFS.getRoot(url)); |
||||
} |
||||
|
||||
public Resource loadResource(URI uri) throws IOException { |
||||
return new VfsResource(VFS.getRoot(uri)); |
||||
} |
||||
|
||||
|
||||
protected static class PatternVirtualFileVisitor implements VirtualFileVisitor |
||||
{ |
||||
private final String subPattern; |
||||
private final Set<Resource> resources = new LinkedHashSet<Resource>(); |
||||
private final PathMatcher pathMatcher; |
||||
private final String rootPath; |
||||
|
||||
private PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) |
||||
{ |
||||
this.subPattern = subPattern; |
||||
this.pathMatcher = pathMatcher; |
||||
this.rootPath = rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/"; |
||||
} |
||||
|
||||
public VisitorAttributes getAttributes() |
||||
{ |
||||
return VisitorAttributes.RECURSE; |
||||
} |
||||
|
||||
public void visit(VirtualFile vf) |
||||
{ |
||||
if (pathMatcher.match(subPattern, vf.getPathName().substring(rootPath.length()))) |
||||
resources.add(new VfsResource(vf)); |
||||
} |
||||
|
||||
public Set<Resource> getResources() |
||||
{ |
||||
return resources; |
||||
} |
||||
|
||||
public int size() |
||||
{ |
||||
return resources.size(); |
||||
} |
||||
|
||||
public String toString() |
||||
{ |
||||
StringBuffer buffer = new StringBuffer(); |
||||
buffer.append("sub-pattern: ").append(subPattern); |
||||
buffer.append(", resources: ").append(resources); |
||||
return buffer.toString(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue