Runtime tools : Core components : Contexts : Tasks : Customizing contexts : Creating contexts using code
  
Creating contexts using code
To create a context dynamically, use code similar to the following example:
Context ctxt = new com.ibm.dse.base.Context();
ctxt.setName("myContext");
ctxt.chainTo(wksContext);
/* Assumes that this is the instance of the workstation context */
ctxt.setType("test");
ctxt.setKeyedCollection(aCollection);
/* Assumes that this collection is defined in the data definition file */
ctxt.addService(aService,"clientsess","cs");
/* Assumes that aService is the service, "clientsess" is the alias */
/* for that service, and "cs" the service's type */
When creating instances of contexts, the context constructor automatically creates an instance of its keyed collection and services, if they are not already instantiated. To do so, the context constructor uses the constructors defined in the externalizer class variable of the DataElement or Service class.
The code in the example uses the addService method to dynamically add a service to the context. You can add any number of services to a context.
To dynamically add data to a context, use the addElement method to add a data element to the context's keyed collection.
Note While a context has a reference to only one keyed collection of data, you can add an unlimited number of data elements within that keyed collection.
If you were using external definitions to define this context, the definition would look as follows:
Context definition
<context id="myContext" type="test" parent="workstation">
  <refKColl refId="contextCollection">
  </refKColl>
  <refService refId="myCSService" alias="clientsess" type="cs"/>
</context>
Service definition
<CSClient id="myCSService" serverName="http://127.0.0.1:80"/>
Data definition
<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
  <refData refId="indexCollection"/>
</kColl>
<kColl id="indexCollection">
  <field id="field1"/>
</kColl>
Go up to
Customizing contexts