Environment Module

The Environment Module provides an abstraction of the execution environment and is complemented by the Container Module. The purpose is to allow using XWiki libraries that require a Container to be executed transparently in environments such as a Servlet or a Portlet environment.

The source code is available on GitHub.

Usage

To access the Environment interface, use the below code:

@Inject
private Environment environment;

You will also need to add a dependency:

  • on the Standard Environment JAR for JavaSE
  • on the Servlet Environment JAR for the Servlet environment

Default Values for Environment Directories

Temporary Directory

For the Temporary Directory we use:

  • the java.io.tmpdir System property for the Standard Environment
  • the javax.servlet.context.tempdir System property for the Servlet Environment

You can change the default value by using the dedicated setter, public void setTemporaryDirectory(File temporaryDirectory); of the AbstractEnvironment interface:

StandardEnvironment environment = (StandardEnvironment) componentManager.lookup(Environment.class);
environment.setTemporaryDirectory(//);

Permanent Directory

If the Permanent Directory is not set, the location if the Temporary Directory is used. You can specify a value by using the dedicated setter: public void setPermanentDirectory(File permanentDirectory);:

StandardEnvironment environment = (StandardEnvironment) componentManager.lookup(Environment.class);
environment.setPermanentDirectory(//);

Resources Directory

If the default value of the Resources Directory is not set, the resources in the Class Loader will be used to load the Standard Environment component implementation class. Otherwise, if the resource cannot be found, we also fallback on the Class Loader.

You can specify another value by using the dedicated setters: public void setResourceDirectory(File resourceDirectory); and public void setResourceClassLoader(ClassLoader classLoader);:

StandardEnvironment environment = (StandardEnvironment) componentManager.lookup(Environment.class);
environment.setResourceDirectory(//);
environment.setResourceClassLoader(//);

 

Initialize XWiki when Using the XWiki Standalone Libraries

When using the XWiki libraries in an application, you first need to initialize the XWiki Component Manager as explained in the Component Module documentation. You will also need to initialize the Environment in order for it to know what temporary, permanent and resource directories to use.

In order and to make it easy to initialize both the XWiki Component Manager and the Environment, you can use a System helper class. The available signatures of the System.initialize() method are:

public static ComponentManager initialize(ClassLoader classLoader)
public static ComponentManager initialize()
public static ComponentManager initialize(File permanentDirectory)
public static ComponentManager initialize(File permanentDirectory, File resourceDirectory)
public static ComponentManager initialize(File permanentDirectory, File resourceDirectory, File temporaryDirectory)
public static ComponentManager initialize(File permanentDirectory, File resourceDirectory, File temporaryDirectory,         
 ClassLoader classLoader)

Related Pages

Search this space