Creating a new business context

All business contexts are registered in a business context configuration file. In the file, you have to specify a business context factory class to create the business context. If the SimpleBusinessContextFactory class, the default factory class, is used to create the business context, you must provide the name of the implementation class as one of the parameters to the factory. To register a business context in the system, refer to the business context configuration file.

Procedure

  • Business context factory class

    When creating the factory class for instantiating the business context object, the class must implement the BusinessContextFactory interface. There are two methods that the factory class must implement: createContextSPI() and destroyContextSPI(). These two methods are used for instantiating and removing the business contexts of a particular type.

    Instead of implementing the interface, you can also extend the AbstractBusinessContextFactory class. This abstract class provides object pooling capability which allows a business context to be reused. The createContextSPI() method will retrieve the business context from an object pool while the destroyContextSPI() method will return the business context back to the pool. To leverage this capability from the abstract class, you have to implement the getContextSPIImplClassname() abstract method to return the implementation class name of the business context object.

  • Business context implementation class

    Each business context registered to the site must implement the Context and ContextSPI interfaces. The Context interface is extended and provides methods that are used by the business logic to retrieve context information. The ContextSPI interface provides methods that the business context service can interact with to signal events such as the start or end of a request.

    To create a new business context, you must define a new interface, for example MyCustomContext, that extends from the Context interface . In the new interface, define a static constant field called CONTEXT_NAME which stores the name used to reference this context. Following HCL Commerce convention, the fully qualified package name of the business context is used, com.mycompany.context.custom.MyCustomContext. All methods that are exposed to the business logic should also be defined. The following example shows a new context interface:

    
    public interface MyCustomContext extends Context { 
    /** 
    * The name of the context that are used to reference this context. 
    */ 
    public static final String CONTEXT_NAME = "com.mycompany.context.custom.MyCustomContext"; 
    
    
    /** 
    * Method that is exposed to business logic. 
    * Returns a string that is saved in this custom context. 
    

    Now that the new business context interface is defined, the concrete class that implements this interface must be provided. This implementation class must implement the Context and ContextSPI interfaces. An AbstractContextImpl class is provided to help implement most of the methods on these interfaces. When you create a new custom implementation class, you should extend the AbstractContextImpl class and implement your new context interface. For example:

    
    public class MyCustomContextImpl extends AbstractContextImpl implements 
    MyCustomContext { 
    

    The new business context implementation must implement the getContextName() method to return the name to be used to reference this object. This should return the CONTEXT_NAME variable defined in the interface.

    
    public String getContextName() {
         return CONTEXT_NAME;
    

    } In addition to the getContextName() method, the following abstract methods from the AbstractContextImpl class must be implemented in the new implementation:

    initializeContext(ActivityData initData)
    Given a set of name value pairs defined in the initData parameter, initialize the business context. This method is called when a new activity is initiated from the client.
    preInvokeContext(ActivityData sessionData)
    Given a set of name value pairs defined in the sessionData parameter, perform context-specific tasks before request execution. This method is called when an activity has already been established and a new request is initiated from the client.
    getContextAttributes()
    Get the array of attributes to be included in the string representation of this context. This array will then be serialized and stored in the database.
    setContextAttributes(String[] ctxAttrs)
    Given an array of context attributes, populate the context accordingly.
    clearContext()
    Reset the attribute of this context to its default value.