org.jboss.virtual
Class VFSUtils

java.lang.Object
  extended by org.jboss.virtual.VFSUtils

public class VFSUtils
extends java.lang.Object

VFS Utilities

Version:
$Revision: 1.1 $
Author:
Adrian Brock, Ales Justin, Kabir Khan

Field Summary
static java.lang.String CASE_SENSITIVE_QUERY
           
static java.lang.String FILE_PROTOCOL
          Constant representing the URL file protocol
static java.lang.String FORCE_CANONICAL
          Key used to force canonical lookup
static java.lang.String FORCE_CASE_SENSITIVE_KEY
          Key used to force case sensitive path checking in vfsfile
static java.lang.String FORCE_COPY_KEY
          The system no force copy key / query
static java.lang.String FORCE_NO_REAPER_KEY
          Key used to turn off reaper mode in vfszip - forcing synchronous (slower) handling of files
static java.lang.String FORCE_VFS_JAR_KEY
          Key used to force fallback from vfszip (default) to vfsjar
static java.lang.String IS_TEMP_FILE
          The temp marker flag
static java.lang.String JAR_URL_SEPARATOR
          Standard separator for JAR URL
static java.lang.String NO_REAPER_QUERY
           
static java.lang.String OPTIMIZE_FOR_MEMORY_KEY
          Key used to turn on memory optimizations - less cache use at the expense of performance
static java.lang.String USE_COPY_QUERY
           
static java.lang.String VFS_CACHE_KEY
          Key used to determine VFS Cache impl
static java.lang.String VFS_LINK_INFIX
          The link
static java.lang.String VFS_LINK_NAME
          The link name
static java.lang.String VFS_LINK_PROPERTIES_SUFFIX
          The link properties
static java.lang.String VFS_LINK_TARGET
          The link target
 
Constructor Summary
VFSUtils()
           
 
Method Summary
static void addManifestLocations(VirtualFile file, java.util.List<VirtualFile> paths)
          Add manifest paths
protected static VirtualFile copy(VirtualFile file, CopyMechanism mechanism)
          Create temp.
static void copyStream(java.io.InputStream is, java.io.OutputStream os)
          Copy input stream to output stream without closing streams.
static void copyStreamAndClose(java.io.InputStream is, java.io.OutputStream os)
          Copy input stream to output stream and close them both
static void disableCaseSensitive(VFS vfs)
          Disable case sensitive for vfs param.
static void disableCaseSensitive(VirtualFile file)
          Disable case sensitive for file param.
static void disableCopy(VFS vfs)
          Disable copy for vfs param.
static void disableCopy(VirtualFile file)
          Disable copy for file param.
static void disableNoReaper(VFS vfs)
          Disable reaper for vfs param.
static void disableNoReaper(VirtualFile file)
          Disable reaper for file param.
protected static void disableOption(VFS vfs, java.lang.String optionName)
          Disable option.
protected static void disableOption(VirtualFile file, java.lang.String optionName)
          Disable option.
static void enableCaseSensitive(VFS vfs)
          Enable case sensitive for vfs param.
static void enableCaseSensitive(VirtualFile file)
          Enable case sensitive for file param.
static void enableCopy(VFS vfs)
          Enable copy for vfs param.
static void enableCopy(VirtualFile file)
          Enable copy for file param.
static void enableNoReaper(VFS vfs)
          Enable reaper for vfs param.
static void enableNoReaper(VirtualFile file)
          Enable reaper for file param.
protected static void enableOption(VFS vfs, java.lang.String optionName)
          Enable option.
protected static void enableOption(VirtualFile file, java.lang.String optionName)
          Enable option.
static VirtualFile explode(VirtualFile file)
          Force explode.
static java.lang.String fixName(java.lang.String name)
          Fix a name (removes any trailing slash)
static java.net.URI getCompatibleURI(VirtualFile file)
          Get spec compatilbe uri from virtual file.
static java.net.URL getCompatibleURL(VirtualFile file)
          Get spec compatilbe url from virtual file.
static java.lang.String getKey(VFSContext context)
          Get path key.
static java.util.jar.Manifest getManifest(VFS archive)
          Get a manifest from a virtual file system, assuming the root of the VFS is the root of an archive
static java.util.jar.Manifest getManifest(VirtualFile archive)
          Get a manifest from a virtual file, assuming the virtual file is the root of an archive
static java.lang.String getName(java.net.URI uri)
          Get the name.
static java.lang.String getOption(VFS vfs, java.lang.String key)
          Get the option.
static
<T> T
getOption(VFS vfs, java.lang.String key, java.lang.Class<T> exactType)
          Get the option.
static java.lang.String getOption(VirtualFile file, java.lang.String key)
          Get the option.
static
<T> T
getOption(VirtualFile file, java.lang.String key, java.lang.Class<T> exactType)
          Get the option.
protected static java.lang.String getPath(java.net.URI uri)
          Get path.
static java.lang.String getPathsString(java.util.Collection<VirtualFile> paths)
          Get the paths string for a collection of virtual files
static java.net.URL getRealURL(VirtualFile file)
          Get real url.
static java.lang.String getRelativePath(VFSContext context, java.net.URI uri)
          Get relative path.
static boolean isLink(java.lang.String name)
          Does a vf name contain the VFS link prefix
static boolean isNestedFile(VirtualFile file)
          Is file handle nested.
static boolean isTemporaryFile(VirtualFile file)
          Is the virtual file temporary.
static java.lang.String outputContents(VirtualFile file)
          Returns a string of the contents of the virtual file, showing the names of all the nested directories and names, useful for debugging
static void outputContents(VirtualFile file, java.io.Writer writer)
          Output contents to writer.
static void parseLinkProperties(java.io.InputStream is, java.util.List<LinkInfo> info, java.util.Properties props)
          Parse a properties link file
static java.util.Map<java.lang.String,java.lang.String> parseURLQuery(java.lang.String query)
          Take a URL.getQuery string and parse it into name=value pairs
static java.util.List<LinkInfo> readLinkInfo(java.io.InputStream is, java.lang.String name, java.util.Properties props)
          Read the link information from the stream based on the type as determined from the name suffix.
static java.util.jar.Manifest readManifest(VirtualFile manifest)
          Read the manifest from given manifest VirtualFile.
static java.net.URL sanitizeURL(java.net.URL url)
          Ensure the url is convertible to URI by encoding spaces and percent characters if necessary
static void stopCache()
          Stop cache.
static java.lang.String stripProtocol(java.net.URI uri)
          Strip protocol from url string.
static java.util.List<java.lang.String> stripProtocolToTokens(java.net.URI uri)
          Strip protocol from url string, return tokens.
static VirtualFile temp(VirtualFile file)
          Create temp.
static java.net.URI toURI(java.net.URL url)
          Deal with urls that may include spaces.
static VirtualFile unjar(VirtualFile file)
          Unjar.
static VirtualFile unpack(VirtualFile file)
          Unpack the nested artifact under file param.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VFS_LINK_INFIX

public static final java.lang.String VFS_LINK_INFIX
The link

See Also:
Constant Field Values

VFS_LINK_PROPERTIES_SUFFIX

public static final java.lang.String VFS_LINK_PROPERTIES_SUFFIX
The link properties

See Also:
Constant Field Values

VFS_LINK_NAME

public static final java.lang.String VFS_LINK_NAME
The link name

See Also:
Constant Field Values

VFS_LINK_TARGET

public static final java.lang.String VFS_LINK_TARGET
The link target

See Also:
Constant Field Values

FORCE_COPY_KEY

public static final java.lang.String FORCE_COPY_KEY
The system no force copy key / query

See Also:
Constant Field Values

USE_COPY_QUERY

public static final java.lang.String USE_COPY_QUERY
See Also:
Constant Field Values

FORCE_VFS_JAR_KEY

public static final java.lang.String FORCE_VFS_JAR_KEY
Key used to force fallback from vfszip (default) to vfsjar

See Also:
Constant Field Values

FORCE_NO_REAPER_KEY

public static final java.lang.String FORCE_NO_REAPER_KEY
Key used to turn off reaper mode in vfszip - forcing synchronous (slower) handling of files

See Also:
Constant Field Values

NO_REAPER_QUERY

public static final java.lang.String NO_REAPER_QUERY
See Also:
Constant Field Values

FORCE_CASE_SENSITIVE_KEY

public static final java.lang.String FORCE_CASE_SENSITIVE_KEY
Key used to force case sensitive path checking in vfsfile

See Also:
Constant Field Values

CASE_SENSITIVE_QUERY

public static final java.lang.String CASE_SENSITIVE_QUERY
See Also:
Constant Field Values

FORCE_CANONICAL

public static final java.lang.String FORCE_CANONICAL
Key used to force canonical lookup

See Also:
Constant Field Values

OPTIMIZE_FOR_MEMORY_KEY

public static final java.lang.String OPTIMIZE_FOR_MEMORY_KEY
Key used to turn on memory optimizations - less cache use at the expense of performance

See Also:
Constant Field Values

VFS_CACHE_KEY

public static final java.lang.String VFS_CACHE_KEY
Key used to determine VFS Cache impl

See Also:
Constant Field Values

FILE_PROTOCOL

public static final java.lang.String FILE_PROTOCOL
Constant representing the URL file protocol

See Also:
Constant Field Values

JAR_URL_SEPARATOR

public static final java.lang.String JAR_URL_SEPARATOR
Standard separator for JAR URL

See Also:
Constant Field Values

IS_TEMP_FILE

public static final java.lang.String IS_TEMP_FILE
The temp marker flag

See Also:
Constant Field Values
Constructor Detail

VFSUtils

public VFSUtils()
Method Detail

stopCache

public static void stopCache()
Stop cache.


getPathsString

public static java.lang.String getPathsString(java.util.Collection<VirtualFile> paths)
Get the paths string for a collection of virtual files

Parameters:
paths - the paths
Returns:
the string
Throws:
java.lang.IllegalArgumentException - for null paths

addManifestLocations

public static void addManifestLocations(VirtualFile file,
                                        java.util.List<VirtualFile> paths)
                                 throws java.io.IOException
Add manifest paths

Parameters:
file - the file
paths - the paths to add to
Throws:
java.io.IOException - if there is an error reading the manifest or the virtual file is closed
java.lang.IllegalStateException - if the file has no parent
java.lang.IllegalArgumentException - for a null file or paths

getManifest

public static java.util.jar.Manifest getManifest(VirtualFile archive)
                                          throws java.io.IOException
Get a manifest from a virtual file, assuming the virtual file is the root of an archive

Parameters:
archive - the root the archive
Returns:
the manifest or null if not found
Throws:
java.io.IOException - if there is an error reading the manifest or the virtual file is closed
java.lang.IllegalArgumentException - for a null archive

readManifest

public static java.util.jar.Manifest readManifest(VirtualFile manifest)
                                           throws java.io.IOException
Read the manifest from given manifest VirtualFile.

Parameters:
manifest - the VF to read from
Returns:
JAR's manifest
Throws:
java.io.IOException - if problems while opening VF stream occur

getManifest

public static java.util.jar.Manifest getManifest(VFS archive)
                                          throws java.io.IOException
Get a manifest from a virtual file system, assuming the root of the VFS is the root of an archive

Parameters:
archive - the vfs
Returns:
the manifest or null if not found
Throws:
java.io.IOException - if there is an error reading the manifest
java.lang.IllegalArgumentException - for a null archive

fixName

public static java.lang.String fixName(java.lang.String name)
Fix a name (removes any trailing slash)

Parameters:
name - the name to fix
Returns:
the fixed name
Throws:
java.lang.IllegalArgumentException - for a null name

getName

public static java.lang.String getName(java.net.URI uri)
Get the name.

Parameters:
uri - the uri
Returns:
name from uri's path

parseURLQuery

public static java.util.Map<java.lang.String,java.lang.String> parseURLQuery(java.lang.String query)
Take a URL.getQuery string and parse it into name=value pairs

Parameters:
query - Possibly empty/null url query string
Returns:
String[] for the name/value pairs in the query. May be empty but never null.

isLink

public static boolean isLink(java.lang.String name)
Does a vf name contain the VFS link prefix

Parameters:
name - - the name portion of a virtual file
Returns:
true if the name starts with VFS_LINK_PREFIX, false otherwise

readLinkInfo

public static java.util.List<LinkInfo> readLinkInfo(java.io.InputStream is,
                                                    java.lang.String name,
                                                    java.util.Properties props)
                                             throws java.io.IOException,
                                                    java.net.URISyntaxException
Read the link information from the stream based on the type as determined from the name suffix.

Parameters:
is - - input stream to the link file contents
name - - the name of the virtual file representing the link
props - the propertes
Returns:
a list of the links read from the stream
Throws:
java.io.IOException - on failure to read/parse the stream
java.net.URISyntaxException - for an error parsing a URI

parseLinkProperties

public static void parseLinkProperties(java.io.InputStream is,
                                       java.util.List<LinkInfo> info,
                                       java.util.Properties props)
                                throws java.io.IOException,
                                       java.net.URISyntaxException
Parse a properties link file

Parameters:
is - - input stream to the link file contents
info - the link infos
props - the propertes
Throws:
java.io.IOException - on failure to read/parse the stream
java.net.URISyntaxException - for an error parsing a URI

toURI

public static java.net.URI toURI(java.net.URL url)
                          throws java.net.URISyntaxException
Deal with urls that may include spaces.

Parameters:
url - the url
Returns:
uri the uri
Throws:
java.net.URISyntaxException - for any error

sanitizeURL

public static java.net.URL sanitizeURL(java.net.URL url)
                                throws java.net.URISyntaxException,
                                       java.net.MalformedURLException
Ensure the url is convertible to URI by encoding spaces and percent characters if necessary

Parameters:
url - to be sanitized
Returns:
sanitized URL
Throws:
java.net.URISyntaxException - if URI conversion can't be fixed
java.net.MalformedURLException - if an error occurs

getOption

public static java.lang.String getOption(VirtualFile file,
                                         java.lang.String key)
Get the option.

Parameters:
file - the file
key - the option key
Returns:
key's option

getOption

public static java.lang.String getOption(VFS vfs,
                                         java.lang.String key)
Get the option.

Parameters:
vfs - the vfs
key - the option key
Returns:
key's option

getOption

public static <T> T getOption(VirtualFile file,
                              java.lang.String key,
                              java.lang.Class<T> exactType)
Get the option.

Type Parameters:
T - exact type
Parameters:
file - the file
key - the option key
exactType - the exact type
Returns:
key's option

getOption

public static <T> T getOption(VFS vfs,
                              java.lang.String key,
                              java.lang.Class<T> exactType)
Get the option.

Type Parameters:
T - exact type
Parameters:
vfs - the vfs
key - the option key
exactType - the exact type
Returns:
key's option

enableOption

protected static void enableOption(VirtualFile file,
                                   java.lang.String optionName)
Enable option.

Parameters:
file - the file
optionName - option name

disableOption

protected static void disableOption(VirtualFile file,
                                    java.lang.String optionName)
Disable option.

Parameters:
file - the file
optionName - option name

enableOption

protected static void enableOption(VFS vfs,
                                   java.lang.String optionName)
Enable option.

Parameters:
vfs - the vfs
optionName - option name

disableOption

protected static void disableOption(VFS vfs,
                                    java.lang.String optionName)
Disable option.

Parameters:
vfs - the vfs
optionName - option name

enableCopy

public static void enableCopy(VirtualFile file)
Enable copy for file param.

Parameters:
file - the file

disableCopy

public static void disableCopy(VirtualFile file)
Disable copy for file param.

Parameters:
file - the file

enableCopy

public static void enableCopy(VFS vfs)
Enable copy for vfs param.

Parameters:
vfs - the vfs

disableCopy

public static void disableCopy(VFS vfs)
Disable copy for vfs param.

Parameters:
vfs - the vfs

enableNoReaper

public static void enableNoReaper(VirtualFile file)
Enable reaper for file param.

Parameters:
file - the file

disableNoReaper

public static void disableNoReaper(VirtualFile file)
Disable reaper for file param.

Parameters:
file - the file

enableNoReaper

public static void enableNoReaper(VFS vfs)
Enable reaper for vfs param.

Parameters:
vfs - the vfs

disableNoReaper

public static void disableNoReaper(VFS vfs)
Disable reaper for vfs param.

Parameters:
vfs - the vfs

enableCaseSensitive

public static void enableCaseSensitive(VirtualFile file)
Enable case sensitive for file param.

Parameters:
file - the file

disableCaseSensitive

public static void disableCaseSensitive(VirtualFile file)
Disable case sensitive for file param.

Parameters:
file - the file

enableCaseSensitive

public static void enableCaseSensitive(VFS vfs)
Enable case sensitive for vfs param.

Parameters:
vfs - the vfs

disableCaseSensitive

public static void disableCaseSensitive(VFS vfs)
Disable case sensitive for vfs param.

Parameters:
vfs - the vfs

isTemporaryFile

public static boolean isTemporaryFile(VirtualFile file)
Is the virtual file temporary.

Parameters:
file - the file
Returns:
true if temporary, false otherwise

unpack

public static VirtualFile unpack(VirtualFile file)
                          throws java.io.IOException,
                                 java.net.URISyntaxException
Unpack the nested artifact under file param.

Parameters:
file - the file to unpack
Returns:
unpacked file
Throws:
java.io.IOException - for any io error
java.net.URISyntaxException - for any uri error

explode

public static VirtualFile explode(VirtualFile file)
                           throws java.io.IOException,
                                  java.net.URISyntaxException
Force explode. Explode archives or nested entries.

Parameters:
file - the file to explode
Returns:
exploded file
Throws:
java.io.IOException - for any io error
java.net.URISyntaxException - for any uri error

temp

public static VirtualFile temp(VirtualFile file)
                        throws java.io.IOException,
                               java.net.URISyntaxException
Create temp.

Parameters:
file - the file to temp
Returns:
temp file
Throws:
java.io.IOException - for any io error
java.net.URISyntaxException - for any uri error

unjar

public static VirtualFile unjar(VirtualFile file)
                         throws java.io.IOException,
                                java.net.URISyntaxException
Unjar.

Parameters:
file - the file to unjar
Returns:
temp file
Throws:
java.io.IOException - for any io error
java.net.URISyntaxException - for any uri error

copy

protected static VirtualFile copy(VirtualFile file,
                                  CopyMechanism mechanism)
                           throws java.io.IOException,
                                  java.net.URISyntaxException
Create temp.

Parameters:
file - the file to unpack/explode
mechanism - the copy mechanism
Returns:
temp file
Throws:
java.io.IOException - for any io error
java.net.URISyntaxException - for any uri error

isNestedFile

public static boolean isNestedFile(VirtualFile file)
                            throws java.io.IOException
Is file handle nested.

Parameters:
file - the file handle to check
Returns:
true if file/dir is nested otherwise false
Throws:
java.io.IOException - for any error

copyStreamAndClose

public static void copyStreamAndClose(java.io.InputStream is,
                                      java.io.OutputStream os)
                               throws java.io.IOException
Copy input stream to output stream and close them both

Parameters:
is - input stream
os - output stream
Throws:
java.io.IOException - for any error

copyStream

public static void copyStream(java.io.InputStream is,
                              java.io.OutputStream os)
                       throws java.io.IOException
Copy input stream to output stream without closing streams. Flushes output stream when done.

Parameters:
is - input stream
os - output stream
Throws:
java.io.IOException - for any error

getCompatibleURL

public static java.net.URL getCompatibleURL(VirtualFile file)
                                     throws java.lang.Exception
Get spec compatilbe url from virtual file.

Parameters:
file - the virtual file
Returns:
spec compatible url
Throws:
java.lang.Exception - for any error

getCompatibleURI

public static java.net.URI getCompatibleURI(VirtualFile file)
                                     throws java.lang.Exception
Get spec compatilbe uri from virtual file.

Parameters:
file - the virtual file
Returns:
spec compatible uri
Throws:
java.lang.Exception - for any error

getRealURL

public static java.net.URL getRealURL(VirtualFile file)
                               throws java.io.IOException,
                                      java.net.URISyntaxException
Get real url. The closest thing that doesn't need the vfs url handlers.

Parameters:
file - the virtual file
Returns:
real url
Throws:
java.io.IOException - for any error
java.net.URISyntaxException - for any uri syntac error

getRelativePath

public static java.lang.String getRelativePath(VFSContext context,
                                               java.net.URI uri)
Get relative path.

Parameters:
context - the vfs context
uri - the uri
Returns:
uri's relative path to context's root

getPath

protected static java.lang.String getPath(java.net.URI uri)
Get path. Apply Carlo's fix as well.

Parameters:
uri - the uri
Returns:
uri's path

stripProtocolToTokens

public static java.util.List<java.lang.String> stripProtocolToTokens(java.net.URI uri)
Strip protocol from url string, return tokens.

Parameters:
uri - the uri
Returns:
uri's path string tokens

stripProtocol

public static java.lang.String stripProtocol(java.net.URI uri)
Strip protocol from url string.

Parameters:
uri - the uri
Returns:
uri's path string

getKey

public static java.lang.String getKey(VFSContext context)
Get path key.

Parameters:
context - the vfs context
Returns:
contex's root path w/o protocol

outputContents

public static java.lang.String outputContents(VirtualFile file)
Returns a string of the contents of the virtual file, showing the names of all the nested directories and names, useful for debugging

Parameters:
file - the virtual file to display the contents of
Returns:
A string containing the contents of the virtual file
Throws:
java.lang.RuntimeException - if an error happened

outputContents

public static void outputContents(VirtualFile file,
                                  java.io.Writer writer)
Output contents to writer.

Parameters:
file - the file
writer - the writer