View Javadoc

1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.core.io;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URI;
23  import java.net.URL;
24  
25  import org.springframework.core.NestedIOException;
26  import org.springframework.util.Assert;
27  
28  /**
29   * VFS based {@link Resource} implementation.
30   * Supports the corresponding VFS API versions on JBoss AS 5.x as well as 6.x and 7.x.
31   *
32   * @author Ales Justin
33   * @author Juergen Hoeller
34   * @author Costin Leau
35   * @since 3.0
36   * @see org.jboss.virtual.VirtualFile
37   * @see org.jboss.vfs.VirtualFile
38   */
39  public class VfsResource extends AbstractResource {
40  
41  	private final Object resource;
42  
43  
44  	public VfsResource(Object resources) {
45  		Assert.notNull(resources, "VirtualFile must not be null");
46  		this.resource = resources;
47  	}
48  
49  
50  	public InputStream getInputStream() throws IOException {
51  		return VfsUtils.getInputStream(this.resource);
52  	}
53  
54  	@Override
55  	public boolean exists() {
56  		return VfsUtils.exists(this.resource);
57  	}
58  
59  	@Override
60  	public boolean isReadable() {
61  		return VfsUtils.isReadable(this.resource);
62  	}
63  
64  	@Override
65  	public URL getURL() throws IOException {
66  		try {
67  			return VfsUtils.getURL(this.resource);
68  		}
69  		catch (Exception ex) {
70  			throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex);
71  		}
72  	}
73  
74  	@Override
75  	public URI getURI() throws IOException {
76  		try {
77  			return VfsUtils.getURI(this.resource);
78  		}
79  		catch (Exception ex) {
80  			throw new NestedIOException("Failed to obtain URI for " + this.resource, ex);
81  		}
82  	}
83  
84  	@Override
85  	public File getFile() throws IOException {
86  		return VfsUtils.getFile(this.resource);
87  	}
88  
89  	@Override
90  	public long contentLength() throws IOException {
91  		return VfsUtils.getSize(this.resource);
92  	}
93  
94  	@Override
95  	public long lastModified() throws IOException {
96  		return VfsUtils.getLastModified(this.resource);
97  	}
98  
99  	@Override
100 	public Resource createRelative(String relativePath) throws IOException {
101 		if (!relativePath.startsWith(".") && relativePath.contains("/")) {
102 			try {
103 				return new VfsResource(VfsUtils.getChild(this.resource, relativePath));
104 			}
105 			catch (IOException ex) {
106 				// fall back to getRelative
107 			}
108 		}
109 
110 		return new VfsResource(VfsUtils.getRelative(new URL(getURL(), relativePath)));
111 	}
112 
113 	@Override
114 	public String getFilename() {
115 		return VfsUtils.getName(this.resource);
116 	}
117 
118 	public String getDescription() {
119 		return this.resource.toString();
120 	}
121 
122 	@Override
123 	public boolean equals(Object obj) {
124 		return (obj == this || (obj instanceof VfsResource && this.resource.equals(((VfsResource) obj).resource)));
125 	}
126 
127 	@Override
128 	public int hashCode() {
129 		return this.resource.hashCode();
130 	}
131 
132 }