Top Description Fields Constructors Methods
io.vertx.core.file.impl

public Class FileResolverImpl

extends Object
implements FileResolver
Class Inheritance
All Implemented Interfaces
io.vertx.core.spi.file.FileResolver, java.io.Closeable, java.lang.AutoCloseable
Imports
io.netty.util.internal.PlatformDependent, io.vertx.core.VertxException, io.vertx.core.file.FileSystemOptions, io.vertx.core.impl.Utils, io.vertx.core.spi.file.FileResolver, java.io.File, .IOException, .InputStream, java.net.JarURLConnection, .URL, java.nio.file.FileVisitResult, .Files, .Path, .SimpleFileVisitor, java.nio.file.attribute.BasicFileAttributes, java.util.ArrayList, .Enumeration, .List, java.util.jar.JarFile, java.util.zip.ZipEntry, .ZipFile

Sometimes the file resources of an application are bundled into jars, or are somewhere on the classpath but not available on the file system, e.g. in the case of a Vert.x webapp bundled as a fat jar.

In this case we want the application to access the resource from the classpath as if it was on the file system.

We can do this by looking for the file on the classpath, and if found, copying it to a temporary cache directory on disk and serving it from there.

There is one cache dir per Vert.x instance and they are deleted on Vert.x shutdown.

Authors
Tim Fox, Rob Worsnop, Julien Viet

Field Summary

Modifier and TypeField and Description
private final FileCache
public static final String
private final File
public static final String
public static final String
private final boolean
private final boolean
private static final String
private static final boolean

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public String
public void
close()

Implements io.vertx.core.spi.file.FileResolver.close.

Close this file resolver, this is a blocking operation.

private void
extractFilesFromJarFile(ZipFile zip, String entryFilter)

Extract a subset of the entries to the cache.

private ClassLoader
pack-priv FileCache
private static URL
getValidClassLoaderResource(ClassLoader cl, String fileName)

Get a class loader resource that can unpack to a valid cache path.

private boolean

Returns:

if the bundle resource represented by the bundle URL is a directory
isBundleUrlDirectory
(URL
the url
url
)

It is possible to determine if a resource from a bundle is a directory based on whether or not the ClassLoader returns null for a path (which does not already contain a trailing '/') *and* that path with an added trailing '/'

private static boolean
private static boolean
private List<String>

Returns:

the list of entries
listOfEntries
(URL
the URL
url
)

Parse the list of entries of a URL assuming the URL is a jar URL.

public File
resolveFile(String
the name to resolve
fileName
)

Implements io.vertx.core.spi.file.FileResolver.resolveFile.

Resolve the file for the specified fileName.

private File

Returns:

the extracted file
unpackFromBundleURL
(URL
the url
url
,
String
the file name used to cache the content
fileName
,
boolean isDir)

bundle:// urls are used by OSGi implementations to refer to a file contained in a bundle, or in a fragment.

private File
private File
private File
unpackUrlResource(URL url, String fileName, ClassLoader cl, boolean isDir)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private final FileCache cache
CACHE_DIR_BASE_PROP_NAMEback to summary
public static final String CACHE_DIR_BASE_PROP_NAME
cwdback to summary
private final File cwd
DISABLE_CP_RESOLVING_PROP_NAMEback to summary
public static final String DISABLE_CP_RESOLVING_PROP_NAME
DISABLE_FILE_CACHING_PROP_NAMEback to summary
public static final String DISABLE_FILE_CACHING_PROP_NAME
enableCachingback to summary
private final boolean enableCaching
enableCPResolvingback to summary
private final boolean enableCPResolving
JAR_URL_SEPback to summary
private static final String JAR_URL_SEP
NON_UNIX_FILE_SEPback to summary
private static final boolean NON_UNIX_FILE_SEP

Constructor Detail

FileResolverImplback to summary
public FileResolverImpl()
FileResolverImplback to summary
public FileResolverImpl(FileSystemOptions fileSystemOptions)

Method Detail

cacheDirback to summary
public String cacheDir()
closeback to summary
public void close() throws IOException

Implements io.vertx.core.spi.file.FileResolver.close.

Close this file resolver, this is a blocking operation.

Exceptions
IOException:

Doc from java.io.Closeable.close.

if an I/O error occurs

extractFilesFromJarFileback to summary
private void extractFilesFromJarFile(ZipFile zip, String entryFilter) throws IOException

Extract a subset of the entries to the cache.

getClassLoaderback to summary
private ClassLoader getClassLoader()
getFileCacheback to summary
pack-priv FileCache getFileCache()
getValidClassLoaderResourceback to summary
private static URL getValidClassLoaderResource(ClassLoader cl, String fileName)

Get a class loader resource that can unpack to a valid cache path.

Some valid entries are avoided purposely when we cannot create the corresponding file in the file cache.

isBundleUrlDirectoryback to summary
private boolean isBundleUrlDirectory(URL url)

It is possible to determine if a resource from a bundle is a directory based on whether or not the ClassLoader returns null for a path (which does not already contain a trailing '/') *and* that path with an added trailing '/'

Parameters
url:URL

the url

Returns:boolean

if the bundle resource represented by the bundle URL is a directory

isValidCachePathback to summary
private static boolean isValidCachePath(String fileName)
isValidWindowsCachePathback to summary
private static boolean isValidWindowsCachePath(char c)
listOfEntriesback to summary
private List<String> listOfEntries(URL url)

Parse the list of entries of a URL assuming the URL is a jar URL.

  • when the URL is a nested file within the archive, the list is the jar entry
  • when the URL is a nested file within a nested file within the archive, the list is jar entry followed by the jar entry of the nested archive
  • and so on.
Parameters
url:URL

the URL

Returns:List<String>

the list of entries

resolveFileback to summary
public File resolveFile(String fileName)

Implements io.vertx.core.spi.file.FileResolver.resolveFile.

Doc from io.vertx.core.spi.file.FileResolver.resolveFile.

Resolve the file for the specified fileName. This method should never return null, it can return a file that does not exist.

Parameters
fileName:String

the name to resolve

Returns:File

a file resolved

unpackFromBundleURLback to summary
private File unpackFromBundleURL(URL url, String fileName, boolean isDir)

bundle:// urls are used by OSGi implementations to refer to a file contained in a bundle, or in a fragment. There is not much we can do to get the file from it, except reading it from the url. This method copies the files by reading it from the url.

Parameters
url:URL

the url

fileName:String

the file name used to cache the content

Returns:File

the extracted file

unpackFromFileURLback to summary
private File unpackFromFileURL(URL url, String fileName, ClassLoader cl)
unpackFromJarURLback to summary
private File unpackFromJarURL(URL url, String fileName, ClassLoader cl)
unpackUrlResourceback to summary
private File unpackUrlResource(URL url, String fileName, ClassLoader cl, boolean isDir)