001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.io; 018 019import java.io.File; 020import java.io.IOException; 021import java.io.InputStream; 022import java.net.URI; 023import java.net.URL; 024 025import org.springframework.core.NestedIOException; 026import org.springframework.util.Assert; 027 028/** 029 * JBoss VFS based {@link Resource} implementation. 030 * 031 * <p>As of Spring 4.0, this class supports VFS 3.x on JBoss AS 6+ 032 * (package {@code org.jboss.vfs}) and is in particular compatible with 033 * JBoss AS 7 and WildFly 8+. 034 * 035 * @author Ales Justin 036 * @author Juergen Hoeller 037 * @author Costin Leau 038 * @author Sam Brannen 039 * @since 3.0 040 * @see org.jboss.vfs.VirtualFile 041 */ 042public class VfsResource extends AbstractResource { 043 044 private final Object resource; 045 046 047 /** 048 * Create a new {@code VfsResource} wrapping the given resource handle. 049 * @param resource a {@code org.jboss.vfs.VirtualFile} instance 050 * (untyped in order to avoid a static dependency on the VFS API) 051 */ 052 public VfsResource(Object resource) { 053 Assert.notNull(resource, "VirtualFile must not be null"); 054 this.resource = resource; 055 } 056 057 058 @Override 059 public InputStream getInputStream() throws IOException { 060 return VfsUtils.getInputStream(this.resource); 061 } 062 063 @Override 064 public boolean exists() { 065 return VfsUtils.exists(this.resource); 066 } 067 068 @Override 069 public boolean isReadable() { 070 return VfsUtils.isReadable(this.resource); 071 } 072 073 @Override 074 public URL getURL() throws IOException { 075 try { 076 return VfsUtils.getURL(this.resource); 077 } 078 catch (Exception ex) { 079 throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex); 080 } 081 } 082 083 @Override 084 public URI getURI() throws IOException { 085 try { 086 return VfsUtils.getURI(this.resource); 087 } 088 catch (Exception ex) { 089 throw new NestedIOException("Failed to obtain URI for " + this.resource, ex); 090 } 091 } 092 093 @Override 094 public File getFile() throws IOException { 095 return VfsUtils.getFile(this.resource); 096 } 097 098 @Override 099 public long contentLength() throws IOException { 100 return VfsUtils.getSize(this.resource); 101 } 102 103 @Override 104 public long lastModified() throws IOException { 105 return VfsUtils.getLastModified(this.resource); 106 } 107 108 @Override 109 public Resource createRelative(String relativePath) throws IOException { 110 if (!relativePath.startsWith(".") && relativePath.contains("/")) { 111 try { 112 return new VfsResource(VfsUtils.getChild(this.resource, relativePath)); 113 } 114 catch (IOException ex) { 115 // fall back to getRelative 116 } 117 } 118 119 return new VfsResource(VfsUtils.getRelative(new URL(getURL(), relativePath))); 120 } 121 122 @Override 123 public String getFilename() { 124 return VfsUtils.getName(this.resource); 125 } 126 127 @Override 128 public String getDescription() { 129 return "VFS resource [" + this.resource + "]"; 130 } 131 132 @Override 133 public boolean equals(Object obj) { 134 return (obj == this || (obj instanceof VfsResource && this.resource.equals(((VfsResource) obj).resource))); 135 } 136 137 @Override 138 public int hashCode() { 139 return this.resource.hashCode(); 140 } 141 142}