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.lang.Nullable; 027import org.springframework.util.Assert; 028 029/** 030 * JBoss VFS based {@link Resource} implementation. 031 * 032 * <p>As of Spring 4.0, this class supports VFS 3.x on JBoss AS 6+ 033 * (package {@code org.jboss.vfs}) and is in particular compatible with 034 * JBoss AS 7 and WildFly 8+. 035 * 036 * @author Ales Justin 037 * @author Juergen Hoeller 038 * @author Costin Leau 039 * @author Sam Brannen 040 * @since 3.0 041 * @see org.jboss.vfs.VirtualFile 042 */ 043public class VfsResource extends AbstractResource { 044 045 private final Object resource; 046 047 048 /** 049 * Create a new {@code VfsResource} wrapping the given resource handle. 050 * @param resource a {@code org.jboss.vfs.VirtualFile} instance 051 * (untyped in order to avoid a static dependency on the VFS API) 052 */ 053 public VfsResource(Object resource) { 054 Assert.notNull(resource, "VirtualFile must not be null"); 055 this.resource = resource; 056 } 057 058 059 @Override 060 public InputStream getInputStream() throws IOException { 061 return VfsUtils.getInputStream(this.resource); 062 } 063 064 @Override 065 public boolean exists() { 066 return VfsUtils.exists(this.resource); 067 } 068 069 @Override 070 public boolean isReadable() { 071 return VfsUtils.isReadable(this.resource); 072 } 073 074 @Override 075 public URL getURL() throws IOException { 076 try { 077 return VfsUtils.getURL(this.resource); 078 } 079 catch (Exception ex) { 080 throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex); 081 } 082 } 083 084 @Override 085 public URI getURI() throws IOException { 086 try { 087 return VfsUtils.getURI(this.resource); 088 } 089 catch (Exception ex) { 090 throw new NestedIOException("Failed to obtain URI for " + this.resource, ex); 091 } 092 } 093 094 @Override 095 public File getFile() throws IOException { 096 return VfsUtils.getFile(this.resource); 097 } 098 099 @Override 100 public long contentLength() throws IOException { 101 return VfsUtils.getSize(this.resource); 102 } 103 104 @Override 105 public long lastModified() throws IOException { 106 return VfsUtils.getLastModified(this.resource); 107 } 108 109 @Override 110 public Resource createRelative(String relativePath) throws IOException { 111 if (!relativePath.startsWith(".") && relativePath.contains("/")) { 112 try { 113 return new VfsResource(VfsUtils.getChild(this.resource, relativePath)); 114 } 115 catch (IOException ex) { 116 // fall back to getRelative 117 } 118 } 119 120 return new VfsResource(VfsUtils.getRelative(new URL(getURL(), relativePath))); 121 } 122 123 @Override 124 public String getFilename() { 125 return VfsUtils.getName(this.resource); 126 } 127 128 @Override 129 public String getDescription() { 130 return "VFS resource [" + this.resource + "]"; 131 } 132 133 @Override 134 public boolean equals(@Nullable Object other) { 135 return (this == other || (other instanceof VfsResource && 136 this.resource.equals(((VfsResource) other).resource))); 137 } 138 139 @Override 140 public int hashCode() { 141 return this.resource.hashCode(); 142 } 143 144}