Environment Module

Version 1.2 by Vincent Massol on 2012/02/09 15:19

cogProvides a simple abstraction of the execution environment
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

A lot XWiki modules can be used as Java libraries that can be used in one's own applications and thus run in various environments. However some of these libraries need to save temporary files or access configuration data that can be stored on the filesystem or even need to save data that should persist across restarts.

Thus XWiki offers this Environment abstraction that other modules can depend on whenever they need to interact with the Environment, making it possible to use them in various environment such a JavaSE, Servlet Container or other.

To use the Environment module just get the Environment component injected as in:

...
@Inject
private Environment environment
...

Note that you'll need to add a dependency on the Standard Environment JAR (for JavaSE) or the Servlet Environment JAR (for the Servlet environment).

Then you get access to the following Environment APIs:

public interface Environment
{
   /**
     * Gets the directory for storing temporary data. The content of this directory may be deleted across restarts
     * and thus is not a safe place to store permanent/important data.
     *
     * @return a {@link File} object pointing to a directory that the application can use for storing temporary files
     */

    File getTemporaryDirectory();
   
   /**
     * Gets the root directory of a location for storing persisting data. Contrary to the Temporary Directory the
     * content of this directory is guaranteed to persist across time.
     *
     * @return a {@link File} object pointing to the root folder of the permanent directory
     */

    File getPermanentDirectory();

   /**
     * @param resourceName the full name of the resource to access (eg "/somefile.properties")
     * @return the resource location as a {@link URL}
     */

    URL getResource(String resourceName);

   /**
     * @param resourceName the full name of the resource to access (eg "/somefile.properties")
     * @return the resource location as an {@link InputStream}
     */

    InputStream getResourceAsStream(String resourceName);
}

Get Connected