Class FileLoader


  • public class FileLoader
    extends java.util.Observable
    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.
    Since:
    MicroStrategy Web 7.5.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ABSOLUTE_PREFIX
      Represents the prefix added to paths to indicate their absolute.
      static char PLUGIN_PREFIX
      Represents the first character of a relative path that indicates the path to points to a file on a specific plugin
      static java.lang.String RELATIVE_PREFIX
      Represents the prefix added to paths to indicate their relative to the application's root folder.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileLoader​(ContainerServices cs)
      Deprecated.
      FileLoader​(ContainerServicesContext cs)
      This constructor takes the container services that will be used to resolve relative paths and create InputStream instances.
      FileLoader​(java.lang.String basePath)
      This constructor takes the basePath that shall be used as root folder to resolve relative paths.
      FileLoader​(java.lang.String basePath, java.lang.String plugInsPath)
      This constructor takes the basePath and the plugInsPath that shall be used as root folder to resolve relative paths.
    • Field Detail

      • ABSOLUTE_PREFIX

        public static final java.lang.String ABSOLUTE_PREFIX
        Represents the prefix added to paths to indicate their absolute.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        Constant Field Values
      • RELATIVE_PREFIX

        public static final java.lang.String RELATIVE_PREFIX
        Represents the prefix added to paths to indicate their relative to the application's root folder.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        Constant Field Values
      • PLUGIN_PREFIX

        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
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        getRelativePath(String), Constant Field Values
    • Constructor Detail

      • FileLoader

        public FileLoader​(ContainerServicesContext cs)
        This constructor takes the container services that will be used to resolve relative paths and create InputStream instances.
        Since:
        MicroStrategy Web 9.0.1
      • FileLoader

        public FileLoader​(java.lang.String basePath)
        This constructor takes the basePath that shall be used as root folder to resolve relative paths.
        Since:
        MicroStrategy Web 9.0.0
      • FileLoader

        public FileLoader​(java.lang.String basePath,
                          java.lang.String plugInsPath)
        This constructor takes the basePath and the plugInsPath that shall be used as root folder to resolve relative paths.
        Since:
        MicroStrategy Web 9.0.0
    • Method Detail

      • getInstance

        public static FileLoader getInstance()
        The method to return the singleton instance of this class.
        Since:
        MicroStrategy Web 9.0.0
      • init

        @Deprecated
        public void init​(ContainerServices cs)
        Deprecated.
        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.
        Since:
        MicroStrategy Web 9.0.0
      • init

        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.
        Since:
        MicroStrategy Web 9.0.1
      • setBasePath

        public void setBasePath​(java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        FileLoader
      • getBasePath

        public java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        FileLoader
      • setDTDsPath

        public void setDTDsPath​(java.lang.String path)
        Sets the path of the folder where dtd files are located. This value is used by the getDTDInputStream(java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        getDTDsPath()
      • getDTDsPath

        public java.lang.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(java.lang.String), then this method will read the dtdsLocation (ApplicationParameters.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.

        Since:
        MicroStrategy Web 9.0.0
      • setPlugInsPath

        public void setPlugInsPath​(java.lang.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 !
        Since:
        MicroStrategy Web 9.0.0
      • getPlugInsPath

        public java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        setPlugInsPath(java.lang.String)
      • clearCaches

        public void clearCaches()
        This method clears all internal caches
        Since:
        MicroStrategy Web 9.0.0
      • getFileContents

        public java.lang.String getFileContents​(java.lang.String filePath)
        Returns the contents of a file as a String object
        Parameters:
        filePath - A relative file location.
        Returns:
        the contents of the file
        See Also:
        getRelativePath(java.lang.String)
      • getFileContents

        public java.lang.String getFileContents​(java.lang.String filePath,
                                                java.lang.String encoding)
        Returns the contents of a file as a String object
        Parameters:
        filePath - A relative file location.
        Returns:
        the contents of the file
        See Also:
        getRelativePath(java.lang.String)
      • getFileInputStream

        public java.io.InputStream getFileInputStream​(java.lang.String filePath)
        Returns the contents of a file as an InputStream object
        Parameters:
        filePath - A relative file location.
        Returns:
        the contents of the file
        See Also:
        getRelativePath(java.lang.String)
      • getDTDInputStream

        public java.io.InputStream getDTDInputStream​(java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
      • getLastModifiedTime

        public long getLastModifiedTime​(java.lang.String path)
        Returns the last time the resource was modified in milliseconds.
        Parameters:
        path - String
        Returns:
        long
        Since:
        MicroStrategy Web 8.0.1
      • exists

        public boolean exists​(java.lang.String path)
        Returns whether the given relative path exists in the file system.
        Since:
        MicroStrategy Web 9.0.0
      • getRealPath

        public java.lang.String getRealPath​(java.lang.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.
        See Also:
        getRelativePath(java.lang.String)
      • getApplicationPath

        public java.lang.String getApplicationPath​(java.lang.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.
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        getRelativePath(java.lang.String)
      • getRelativePath

        public java.lang.String getRelativePath​(java.lang.String absolutePath)
        Returns a relative path for the given absolute path. The path returned is relative to the applications root folder.
        Since:
        MicroStrategy Web 9.0.0
      • getRelativePath

        public java.lang.String getRelativePath​(java.lang.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:".
          Since:
          MicroStrategy Web 9.0.0
        • getCustomizationPrefix

          public static java.lang.String getCustomizationPrefix​(java.lang.String name)
          Returns the prefix to be used in a relative path, that identifies that path to belong to the given customization.
          Since:
          MicroStrategy Web 9.0.0
        • getConnection

          public static java.net.URLConnection getConnection​(java.net.URL url)
                                                      throws java.io.IOException
          Throws:
          java.io.IOException