License X:Forge Tag Reference Request Component SQL Component XSL Component | VariablesBrokerThe X:Forge VariablesBroker is a central mechanism that manages the various X:Forge contexts and the data they contain. Generally, an X:Forge context is a scoped object container with a unique contextId. This means that ...
ConfigurationThe VariablesBroker cannot be configured, it runs automatically in the background. UsageGetting the VariablesBrokerA reference to the VariablesBroker can be obtained like this: VariablesBroker broker = VariablesBroker.getBroker(); X:Forge ContextsX:Forge ships with several contexts that the VariablesBroker can manage. They differ as to how they calculate their own, unique identifier and some of them have additional features. Here are the contexts available in the core system:
Creating Custom ContextsAny class that implements the interface org.bibop.xml.xforge.XForgeContext can be used as a context with the VariablesBroker. The interface basically means that the class has to be able to calculate its own, unique contextId. Additionally, a context may implement the interface org.bibop.xml.xforge.ContextEventListener, which is good for monitoring its lifecycle. Here are the interface definitions: org.bibop.xml.xforge.XForgeContext: public String getContextId() throws XForgeException; org.bibop.xml.xforge.ContextEventListener: public void beforeStart() throws XForgeException; public void afterStart() throws XForgeException; public void beforeEnd() throws XForgeException; public void afterEnd() throws XForgeException; Using Contexts with the VariablesBrokerThe VariablesBroker supports a number of operations on contexts: MyContext myContext = new MyContext(); Object myObject = new Object(); broker.startContextScope(myContext); broker.putObject(myContext,"myKey",myObject); (MyObject)broker.getObject(new MyContext(),"myKey"); broker.endContextScope(myContext); A context's scope must be started, before objects can be stored in or retrieved from it. During the start phase the methods beforeStart() and afterStart() from the above-mentioned interface ContextEventListener are executed, during the end phase the methods beforeEnd() and afterEnd() are executed. Using Contexts in XMLIt is also possible to access objects from a context in an XML resource. Example from test/all.xml: <contexttest xmlns:thread="http://xforge.org/xforge/ 3.0/context/org.bibop.xml.xforge.helpers.ThreadContext"> A serialized object from the thread context: <xf:get name="thread:testfile"/> </contexttest> Resulting XML<contexttest xmlns:thread="http://xforge.org/xforge/ 3.0/context/org.bibop.xml.xforge.helpers.ThreadContext"> A serialized object from the thread context: test/all.xml </contexttest> ErrorsKnown Error Conditions
Error HandlingAll exceptions are thrown back to the caller. Here is an example: Exception message: context cannot be null org.bibop.xml.xforge.XForgeException: context cannot be null at org.bibop.xml.xforge.VariablesBroker.startContextScope(VariablesBroker.java:74) at org.bibop.xml.xforge.Test.test(Test.java:199) at org.bibop.xml.xforge.Test.main(Test.java:122) |