Context and Deployment Descriptor > The Application Context > Initializing a Context

You can initialize a context either by loading a deployment descriptor file or through the API.

It is strongly recommend that you use the deployment descriptor alternative as it enables you to configure a context easily without having to write a single line of code. For more information, see Writing a Deployment Descriptor and Loading a Deployment Descriptor.

In more complex cases--if you need to add your own properties or services to the context--you will have to use the context API. For details, see Creating a New Context and Modifying a Context.

Note
You can use the context API in conjunction with a deployment descriptor. In other words, you can extend the syntax of the deployment descriptor and process added XML elements with a dedicated handler. For more information, see Extending the Deployment Descriptor.

Using a Deployment Descriptor

You can initialize the default context by loading a deployment descriptor when calling the method IltSystem.Init. There are several versions of the Init method, each with a different number of parameters.

The version with no parameter will attempt to load a default deployment parser named cpldeploy.xml by looking in the following places in this order:

  1. The current directory
  2. The user's home directory
  3. The user.home system property
  4. The classpath

For more information, see the class IltSystem.

How to Configure the Default Context Through a Deployment Descriptor
if (isApplet())
        IltSystem.Init(this, "deploy.xml");
else
        IltSystem.Init("deploy.xml");

You can only call IltSystem.Init once, when you initialize the application for the first time. Any further call to this method is discarded.

If the method IltSystem.Init has not been called before IltSystem.GetDefaultContext is called, the latter will implicitly call IltSystem.Init() (the version with no parameter) to initialize the default context. This has the following two effects, one a drawback and one a feature:

Writing a Deployment Descriptor

The deployment descriptor is defined by an XML file that describes how to initialize the contextual information and services.

Following is a basic example that shows you what a deployment descriptor file looks like. For a more detailed description of its syntax, see the class IlpDeploymentParser.

<?xml version="1.0"?>
<deployment xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/deploy.xsd">
   <urlAccess verbose="true">
        <relativePath>data/images</relativePath>
   </urlAccess>
   <classManager>
        <file>model.xml</file>
   </classManager>
</deployment>

A deployment descriptor is delimited by the XML element <deployment>. This element can contain a number of subelements each corresponding to the contextual information and services as described in The Concepts.

The elements that you can use to define your deployment descriptor in XML format can be found in the XML schema file deploy.xsd.

Note
XML elements are not necessarily read in the order in which they appear in the file. For example, the <urlAccess> element will be systematically read before the <classManager> element even if it is placed after it in the deployment descriptor file. Therefore, you can arrange these subelements in any order.

In the example, the deployment descriptor is composed of two subelements:

Loading a Deployment Descriptor

Once you have created a context, you can initialize it from an XML deployment descriptor using the parse method of IlpDeploymentParser.

How to Load a Deployment Descriptor Using the parse Method

The following example shows how to use this method to load a deployment descriptor:

IlpDefaultURLAccessService urlBootService = new IlpDefaultURLAccessService();
URL url = urlBootService.getFileLocation("deploy.xml");
IlpDeploymentParser parser = new IlpDeploymentParser(url);
IltDefaultContext context = new IltDefaultContext();
try {
  parser.parse(null, context);
} catch (Exception e) {
  e.printStackTrace();
}

In this code, IlpDefaultURLAccessService retrieves the URL to the deploy.xml file, then a new context is created and passed to the parse method. Note that the first parameter of the parse method is not used here.

Extending the Deployment Descriptor

If you want the deployment descriptor to perform specific initialization that is not part of the JViews TGO contextual information and services, you can extend its XML format with custom elements and write a handler to process these elements. Custom elements will be passed to the handler as DOM elements.

Here is a very basic example of a deployment descriptor with only one custom element:

<?xml version="1.0"?>
<deployment xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/deploy.xsd">
  <custom handlerClass="UserHandler"/>Joe Williams</custom>
</deployment>

Following is a basic handler class that sets the user name and stores it as a context property:

public class UserHandler implements IlpDeploymentParser.IlpCustomHandler {
  public UserHandler() {}
  public void handleCustomElement(Element customElement,
    IlpDefaultContext context) { 
      String name = customElement.getTextTrim();
      context.setProperty("userName",name);
  }
}

The deployment parser processes <custom> elements in the order in which they appear, after dealing with standard elements. It tries to load the Java class specified in the handlerClass attribute and instantiate it using the default constructor. Then it calls the method handleCustomElement. Note that the custom handler must implement IlpDeploymentParser.IlpCustomHandler and provide a default constructor (with no parameter).