Context and Deployment Descriptor > The Application Context > Initializing a Context > Using the API

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 IltSystem.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 IltSystem.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.

Adding, Changing, or Removing Services

You can add a service to a context or replace an existing service with a new one using the addService method.

How to Change a Service in a Context

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)
Adding or Removing Properties

The default context implementation allows you to store properties. The following methods are available to help you handle the context properties: