skip to main content
Programmers documentation > Context and Deployment Descriptor > The application context > Initializing a context
 
Initializing a context
Describes how to initialize a context either by loading a deployment descriptor file or through the API.
*Using a deployment descriptor
*Describes how to initialize a context by loading a deployment descriptor file.
*Using the API
*Describes how to initialize a context by using the API.
Using a deployment descriptor
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 How to load a deployment descriptor using the parse method.
You can initialize the default context by loading a deployment descriptor when calling the method IltSystem. 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
 
    IltSystem.Init("deploy.xml");
You can only call IltSystem 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:
*If you call an IltSystem.Init method with parameters, you must make sure that instances of classes using the default context are not created before IltSystem.Init is invoked. For example these instances should not be created during static initialization.
*If you are working in an IDE, you can configure the default context by changing the default deployment descriptor at design time. Your context will then be automatically loaded when you instantiate the Beans at run time.
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 <installdir> /data/ilog/cpl/schema/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:
*The <urlAccess> element is read before the elements that potentially use it, that is, elements that require files to be loaded (the <classManager>, for example).
*The <classManager> element includes a list of file elements that contain filenames. These files are read in the order in which they appear in the file.
How to load a deployment descriptor using the parse method
Once you have created a context, you can initialize it from an XML deployment descriptor using the parse method of IlpDeploymentParser.
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).
Using the API
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.
You can initialize the default context through the API IltDefaultContext, that is, the default implementation of the interface IlpContext.
When you initialize the JViews TGO library (by calling IltSystem.Init ), an instance of the default context is created. You can retrieve it by using the method GetDefaultContext. This method is invoked by the default constructor of classes using contextual information and services ( IltDefaultDataSource, for example).
How to retrieve the default context through the API
 
IlpContext context = IltSystem.GetDefaultContext();
Creating a new context
Generally, you are led to create new contexts when your application is to run in a multiuser environment and you want each user to have his/her own context.
To create an instance of IltDefaultContext, simply call:
 
IltDefaultContext context = new IltDefaultContext();
To use this new context, you must pass the context parameter to the constructor of each class making use of it in order to bypass the default context returned by GetDefaultContext. For example, to create a data source, you should not use the default constructor new IltDefaultDataSource(), because using it would initialize the default context, but use instead the constructor that takes a context as its parameter, that is, new IltDefaultDataSource(context).
How to create and initialize a context through the API
 
IlpContext context = new IltDefaultContext();
context.addService (IlpTypeConverter.class, new
IlpDefaultTypeConverter(context));
Modifying a context
You may want to modify an existing context to add your own services or properties.
How to change a service in a context
You can add a service to a context or replace an existing service with a new one using the addService method.
If you want to change the synchronization strategy to use regular Java™ synchronization and not the default strategy that uses the event thread, write the following line:
 
context.addService(IlSynchronizationStrategy.class,
                   new IlSynchronizeOnLockStrategy());
The first parameter of the addService method is a class that defines the service to be changed (usually this parameter is an interface because a service is generally defined by an interface). The second parameter is an implementation of this service. In other words, the second parameter is supposed to implement the class specified by the first parameter.
How to remove a service from a context
To remove a service from the context, you use the method removeService with a single parameter to indicate the service to be removed:
 
context.removeService(IlSynchronizationStrategy.class)
The default context implementation allows you to store properties. The following methods are available to help you handle the context properties:
*public void setProperty(String name, Object property) Sets the value of a property in the context.
*public Object getProperty(String name) Returns the value of a property in the context or null if the property is not defined.
*public Map getProperties() Returns the collection of all properties defined in the context.
You can directly modify this Map by adding or removing your own context properties.
Creating a context in background
It is possible to initialize TGO in a background thread, which automatically invokes the IltSystem method along with the creation of a new context. It also initializes the TGO static resources that are usually only required at rendering time. This improves the graphic component initialization time (usually done at the Event Dispatch thread) and also provides some control over the initialized TGO static resources.
How to initialize TGO in background
When the JViews TGO application context is created (using the method IltSystem), many predefined resources are initialized, such as alarm states and default values for severities. Other resources such as the different predefined object types are initialized on an as needed basis, when they are first referenced by the application. This process takes time and can slow down your application display time, for example, when you try to display a large number of different predefined nodes for the first time.
You can prevent such slowdowns by warming up TGO with an extended context initialization that is carried out on a background thread by the IltSystemInitializer utility class. This is useful for applications that perform time consuming tasks during startup and/or initialization (such as establishing remote connections, waiting for user authentication, and so on).
The IltSystemInitializer class creates the TGO application context (by internally invoking the IltSystem.Init method) extending the initialization to some resources usually lazily initialized by the rendering of predefined objects. This is done on a separate thread, concurrently with the time consuming part of your application initialization.
The following example illustrates the process:
Before starting a routine that is time consuming, start the TGO initialization:
 
  // Start initialization on a worker thread
  IltSystemInitializer.Init("my/deployment/file.xml");
When you are ready to start your UI, check for correct TGO initialization and continue:
 
  // Wait for TGO initialization
  // This is a blocking method!
  IltSystemInitializer.WaitInitialization();
 
  // You can check for initialization errors
  Throwable error = IlpSystemInitializer.GetError());
 
  // The context is already initialized
  // There is no need to invoke IltSystem.Init()
  _context = IltSystem.GetDefaultContext();
  _tree = new IlpTree(_context);
Note that this technique is useful when the IltSystemInitializer method executes in parallel with a time consuming task.

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.