| Context and Deployment Descriptor > The Application Context > Initializing a Context > Using the API |
Using the API |
INDEX
PREVIOUS
NEXT
|
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).
IlpContext context = IltSystem.GetDefaultContext();
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).
IlpContext context = new IltDefaultContext(); context.addService (IlpTypeConverter.class, new IlpDefaultTypeConverter(context));
You may want to modify an existing context to add your own services or properties.
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.
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.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |