Runtime components : Core components : Contexts : Tasks : Working with contexts
  
Working with contexts
The com.ibm.dse.base.Context class provides methods for managing the resources in a context (such as adding and removing data elements and services), managing the context tree (such as chaining and unchaining to contexts and getting the parent context), and for retrieving specific context instances.
Avoid using "currentContext" and "currentChild" contexts: having several processes chain contexts in parallel may return inconsistent results to the application. Make sure that you do confuse this with the current context for a client/server session identifier, which actually returns the corresponding instance of the context related to that session.
For examples of how you can work with contexts, see the following sections.
See
Updating the data elements of a context's keyed collection
Working with services defined in the context
Resetting context keyed collections
See also
Tasks
Updating the data elements of a context's keyed collection
To show how to update a data element of a context's keyed collection, the example code assumes that the definition files contain the following definitions:
Context definition
<context id="myContext" type="test" parent="nil">
  <refKColl refId="contextCollection">
  </refKColl>
</context>
Data definition
<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
</kColl>
To do the update, use code like the following example:
Context ctxt = (Context) Context.readObject("myContext");
ctxt.setValueAt("field1","Hello");
See also
Working with contexts
Working with services defined in the context
To show how to work with a service defined in the context, the example code assumes that the definition files contain the following definitions:
Context definition
<context id="myContext" type="test" parent="nil">
  <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"/>
</kColl>
To work with a service (in this case, associating a session with the context), use code like the following example:
Context ctxt = (Context) Context.readObject("myContext");
ctxt.setValueAt("field1","Hello");
CSClientService myClient;
myClient = ((CSClientService) getService("clientsess"));
myClient.establishSession();
See also
Working with contexts
Resetting context keyed collections
To reset a context keyed collection so that part or all of the keyed collection re-initializes with values from external definition files, invoke the clearKeyedCollection method in the Context class. This method resets the current context's keyed collection based on the following rules:
If the dsedata.xml file contains the definition for the context keyed collection, the method uses the DataElement.readObject method to reinitialize the keyed collection.
If the dsetype.xml file contains the definition for the context keyed collection defined as a Type, the method uses the DSEType externalizer to re-initialize the type.
If neither the dsedata.xml file nor the dsetypes.xml file contains the definition and any of the two parameters or both are set to false, the method throws an IOException. Otherwise, the method does the following depending on the values of the parameters:
How clearKeyedCollection re-initializes the context keyed collection
keepKCollStatus = true
clearOnlyValues = true
Re-initializes only the values of the data elements inside the keyed collection. The method does not affect the data element attributes. If the keyed collection has dynamically added elements, this re-initialization does not change them in any way.
If the method does not find an external definition for the keyed collection, the method considers all the data elements as dynamic data elements and does not change them in any way. The method also retains the keyed collection attributes.
keepKCollStatus = true
clearOnlyValues = false
Re-initializes the attributes and values of the data elements in the keyed collection. The method does not re-initialize keyed collection attributes such as ErrorInfo, State, and Parameters. If the keyed collection has dynamically added elements, this re-initialization removes them.
If the method does not find an external definition for the keyed collection, it throws an IOException.
keepKCollStatus = false
clearOnlyValues = true
Re-initializes the attributes of the keyed collection and the data element values without changing the rest of the data element attributes. If the keyed collection has dynamically added elements, this re-initialization does not change them in any way.
If the method does not find an external definition for the keyed collection, it throws an IOException.
keepKCollStatus = false
clearOnlyValues = false
Re-initializes the context without preserving attributes and values. If the keyed collection has dynamically added elements, this re-initialization removes them.
If the method does not find an external definition for the keyed collection, it throws an IOException.
See also
Working with contexts