public class

FileLoader

extends Observable
java.lang.Object
   ↳ java.util.Observable
     ↳ com.microstrategy.utils.FileLoader

Class Overview

The FileLoader is the class used by MicroStrategy Web classes to load files from the file-system or class-path.

Typically, an application server will take the war file and expand its contents, in which case all files can be loaded directly from the file-system, however some application servers have the capacity to run the web application without expanding the war file and provides InputStream instances for the resources inside the war file. This class encapsulates this logic so it becomes transparent to all elements in the application that need to load a file.
Differences along application servers are encapsulated by the ContainerServicesContext. If provided, the FileLoader will use a ContainerServicesContext instance to resolve relative paths and create the corresponding InputStream instances.

MicroStrategy Web always uses relative paths to specify the location of configuration files. The path is always relative to where the application was installed, and the absolute path is resolved by the ContainerServicesContext. For those cases, like Microstrategy's Eclipse plug-in, where MicroStrategy classes are used outside the context of an application server (and therefore there is associated ContainerServicesContext) the FileLoader provides a base path property, in the absence of the ContainerServicesContext the FileLoader will prefix this value to all relative paths when calculating the absolute path.

Starting with version 8.0.3, this class is used by MicroStrategy Web as a singleton. When the application is started it will take care of initializing the instance with a valid ContainerServicesContext for all future requests.

Summary

Constants
String ABSOLUTE_PREFIX Represents the prefix added to paths to indicate their absolute.
char PLUGIN_PREFIX Represents the first character of a relative path that indicates the path to points to a file on a specific plugin
String RELATIVE_PREFIX Represents the prefix added to paths to indicate their relative to the application's root folder.
Public Constructors
FileLoader(ContainerServicesContext cs)
This constructor takes the container services that will be used to resolve relative paths and create InputStream instances.
FileLoader(ContainerServices cs)
This constructor is deprecated. replaced by FileLoader(ContainerServicesContext)
FileLoader(String basePath)
This constructor takes the basePath that shall be used as root folder to resolve relative paths.
FileLoader(String basePath, String plugInsPath)
This constructor takes the basePath and the plugInsPath that shall be used as root folder to resolve relative paths.
Public Methods
void clearCaches()
This method clears all internal caches
boolean exists(String path)
Returns whether the given relative path exists in the file system.
String getApplicationPath(String relativePath)
Returns a path that its always relative to the application's root folder.
String getBasePath()
Returns the current base path for this file loader.
ContainerServices getContainerServices()
This method is deprecated. replaced by getContainerServicesContext()
ContainerServicesContext getContainerServicesContext()
Returns the underlying ContainerServicesContext.
static String getCustomizationPrefix(String name)
Returns the prefix to be used in a relative path, that identifies that path to belong to the given customization.
InputStream getDTDInputStream(String dtdFileName)
Returns an InputStream for the given dtdFileName.
String getDTDsPath()

Returns the path of the folder where dtd files, used to validate the syntax fo xml files, are located.

String getFileContents(String filePath)
Returns the contents of a file as a String object
String getFileContents(String filePath, String encoding)
Returns the contents of a file as a String object
InputStream getFileInputStream(String filePath)
Returns the contents of a file as an InputStream object
static FileLoader getInstance()
The method to return the singleton instance of this class.
long getLastModifiedTime(String path)
Returns the last time the resource was modified in milliseconds.
String getPlugInsPath()
Returns the plugInsPath.
String getRealPath(String filePath)
Resolves the real (absolute) path from the given relative path.
static String getRealPath(String filePath, ContainerServicesContext cs)
String getRelativePath(String absolutePath, CustomizationInfo ci)
Returns a relative path for the given absolute path.
String getRelativePath(String absolutePath)
Returns a relative path for the given absolute path.
void init(ContainerServicesContext cs)
Initializes this FileLoader with a ContainerServicesContext that will then be used to resolve paths and to create InputStream instances to load files.
void init(ContainerServices cs)
This method is deprecated. replaced by init(ContainerServicesContext)
void setBasePath(String basePath)
Sets the base path for this file loader.
void setDTDsPath(String path)
Sets the path of the folder where dtd files are located.
void setPlugInsPath(String path)
Sets the plugins path.
[Expand]
Inherited Methods
From class java.util.Observable
From class java.lang.Object

Constants

public static final String ABSOLUTE_PREFIX

Represents the prefix added to paths to indicate their absolute.

Constant Value: "ABSOLUTE:"

public static final char PLUGIN_PREFIX

Represents the first character of a relative path that indicates the path to points to a file on a specific plugin

Constant Value: 64 (0x00000040)

public static final String RELATIVE_PREFIX

Represents the prefix added to paths to indicate their relative to the application's root folder.

Constant Value: "RELATIVE:"

Public Constructors

public FileLoader (ContainerServicesContext cs)

This constructor takes the container services that will be used to resolve relative paths and create InputStream instances.

public FileLoader (ContainerServices cs)

This constructor is deprecated.
replaced by FileLoader(ContainerServicesContext)

This constructor takes the container services that will be used to resolve relative paths and create InputStream instances.

public FileLoader (String basePath)

This constructor takes the basePath that shall be used as root folder to resolve relative paths.

public FileLoader (String basePath, String plugInsPath)

This constructor takes the basePath and the plugInsPath that shall be used as root folder to resolve relative paths.

Public Methods

public void clearCaches ()

This method clears all internal caches

public boolean exists (String path)

Returns whether the given relative path exists in the file system.

public String getApplicationPath (String relativePath)

Returns a path that its always relative to the application's root folder.

  1. If the path is already relative to the application's root folder, returns the same value.
  2. If the path is relative to a customization folder but that folder doesn't exist, it throws an IllegalArgumentException, otherwise it returns the corresponding relative path.
  3. If the path is prefixed with "ABSOLUTE:", it returns the same value without the prefix.

public String getBasePath ()

Returns the current base path for this file loader. In the absence of a ContainerServices, the base-path is used to calculate the real location of relative paths.

See Also

public ContainerServices getContainerServices ()

This method is deprecated.
replaced by getContainerServicesContext()

Returns the ContainerServices instance currently associated with

Returns

public ContainerServicesContext getContainerServicesContext ()

Returns the underlying ContainerServicesContext.

public static String getCustomizationPrefix (String name)

Returns the prefix to be used in a relative path, that identifies that path to belong to the given customization.

public InputStream getDTDInputStream (String dtdFileName)

Returns an InputStream for the given dtdFileName. It uses the getDTDsPath() method to calculate the location of the file, if null, it just tries to load the file from the application's root folder.

Parameters
dtdFileName The name of the file. Typically specified as the SYSTEM attribute of the xml's DOCTYPE.
Returns
  • InputStream for the given dtd if found; null otherwise.

public String getDTDsPath ()

Returns the path of the folder where dtd files, used to validate the syntax fo xml files, are located. If the location of this folder has not been explicitly set through the setDTDsPath(String), then this method will read the dtdsLocation (DTD_PATH init parameter from the associated ContainerServices class, if any.

It returns null if the value has not been explicitly set or there is no ContainerServices instance associated.

public String getFileContents (String filePath)

Returns the contents of a file as a String object

Parameters
filePath A relative file location.
Returns
  • the contents of the file

public String getFileContents (String filePath, String encoding)

Returns the contents of a file as a String object

Parameters
filePath A relative file location.
Returns
  • the contents of the file

public InputStream getFileInputStream (String filePath)

Returns the contents of a file as an InputStream object

Parameters
filePath A relative file location.
Returns
  • the contents of the file

public static FileLoader getInstance ()

The method to return the singleton instance of this class.

public long getLastModifiedTime (String path)

Returns the last time the resource was modified in milliseconds.

Parameters
path String
Returns
  • long

public String getPlugInsPath ()

Returns the plugInsPath. When not null, all relative paths that point to a plug-in or customization folder are resolved to be relative this path ,regardless of the customization they belong to.

public String getRealPath (String filePath)

Resolves the real (absolute) path from the given relative path. Note that the resulting path does not necessarily point to a file/directory that exists.

The root folder used to resolve relative paths is defined as follows:

  1. If there is container services instance associated, the relative path is resolved by the container services (which typically uses the virtual directory folder as the root).
  2. If there is no container services defined but the base path has been specified, the base path is used as root folder.
  3. Otherwise, the user directory (i.e. the folder where the JVM was started from) is used as the root folder.

Parameters
filePath Always a relative location. If it is an absolute path, it must have ABSOLUTE_PREFIX.
Returns
  • An absolute path as used by the File System.

public static String getRealPath (String filePath, ContainerServicesContext cs)

Parameters
filePath Always a relative location. If it is an absolute path, it must have ABSOLUTE_PREFIX.
cs ContainerServicesContext used to resolve relative paths
Returns
  • An absolute path as used by the File System.

public String getRelativePath (String absolutePath, CustomizationInfo ci)

Returns a relative path for the given absolute path. A FileLoader supports the following relative paths:

  1. Paths relative to the application's root folder
  2. Paths relative to a specific customization's root folder.
  3. Paths outside the application's root folder.

Paths that are relative the application's root folder start either with "/" or are prefixed with "RELATIVE:", for example:

  • "/jsp/mstrWeb.jsp"
  • "RELATIVE:/WEB-INF/xml"

Paths that are relative to a specific customization's root folder are prefixed with "@{NAME}:", where "{NAME}" corresponds to the customization's name, for example:

  • "@Customizations:/jsp/mstrWeb.jsp"
  • "@MapInfo:/jsp/mstrWeb.jsp"

Paths to files that are not part of the application are prefixed with "ABSOLUTE:", for example:

  • "ABSOLUTE:Z:\Program Files\Common Files\MyFile.txt"

This method will return the best possible relative path for the given absolute path, that is:
  1. if a CustomizationInfo is provided and the file is member of the customization's root folder or one of its subfolders, it will return a path relative to the customization's root folder.
  2. else, if the file is member of the application's s root folder or one of its subfolders, it will return a path relative to the application's root folder.
  3. otherwise, it will return the same path prefix with "ABSOLUTE:".

public String getRelativePath (String absolutePath)

Returns a relative path for the given absolute path. The path returned is relative to the applications root folder.

public void init (ContainerServicesContext cs)

Initializes this FileLoader with a ContainerServicesContext that will then be used to resolve paths and to create InputStream instances to load files.

public void init (ContainerServices cs)

This method is deprecated.
replaced by init(ContainerServicesContext)

Initializes this FileLoader with the given instance of a ContainerServices. The ContainerServices will then be used to resolve relative paths and to create InputStreams to load files.

public void setBasePath (String basePath)

Sets the base path for this file loader. In the absence of a ContainerServices, the base-path is used to calculate the real location of relative paths.

See Also

public void setDTDsPath (String path)

Sets the path of the folder where dtd files are located. This value is used by the getDTDInputStream(String) to create the input stream for the given dtd. If this value is not explicitly set, it is calculated from the associated ContainerServices instance.

Parameters
path The relative path location of the folder where dtd files are located.
See Also

public void setPlugInsPath (String path)

Sets the plugins path. Normally all plugins' paths are resolved based on the plugin name and the settings on the Customizations class. When this plugins path is set, though, all relative paths that are part of a plug-in or customization are resolved to be relative to this path, regardless of the customization they belong to. For example:

FileLoader fl = new FileLoader("."); fl.setPlugInsPath("/custom"); fl.getRealPath("@A:/test.xml"); //Returns: /custom/test.xml fl.getRealPath("@B:/test.xml"); //Also returns /custom/test.xml !