Accessing default HelperContext objects in SCA applications

A Service Component Architecture (SCA) application can access a Service Data Objects (SDO) 2.1.1 HelperContext object. This object either uses DefaultHelperContext annotation or implements a application programming interface (API) that uses the commonj.sdo.helper.SDO class.

Before you begin

Install the product and enable SDO. Instructions on enabling the SDO feature are in the installation topics for your operating system.

Read SDO data binding for SCA applications to better understand how to work with SDO in SCA Java clients and implementations.

Develop one or more SCA composites that use SDO following a top-down or bottom-up approach.

About this task

This topic describes how to create and access SDO HelperContext in SCA applications. For information about accessing SDO HelperContext in non-SCA applications, see Creating and accessing SDO HelperContext objects.

Because the SCA run time manages the HelperContext objects and identifiers when using SDO in SCA applications, the method used to create and access SDO HelperContext in SCA applications is different from that used in non-SCA applications. An SCA application can access SDO HelperContext using a DefaultHelperContext annotation, @DefaultHelperContext.

Alternatively, an SCA application can implement an API that uses the commonj.sdo.helper.SDO class, to obtain the same SCA-managed HelperContext instance. This approach is an alterative to the annotation. To use this approach, pass the String ID of the SCA-managed HelperContext, which is sca-default, into the SDO.getHelperContext method.

You can access the default HelperContext programmatically in a Java or Java Platform, Enterprise Edition (Java EE) component implementation type using either annotation injection or an API. You cannot access the default HelperContext programmatically in a Spring component implementation type. When you use an OSGI application as an implementation of an SCA component, you can access the default HelpContext instance using the API mechanism. However, you cannot access the instance using the annotation injection mechanism.

Procedure

  1. Add a public or protected field or setter method of the commonj.sdo.helper.HelperContext type to your Java implementation class.

    The method can either be a Java component implementation, such as <implementation.java>, or a Java EE implementation class, such as an EJB implementation class.

  2. Annotate the field or setter method with @com.ibm.websphere.soa.sca.sdo.DefaultHelperContext.

    The following example shows a field annotation:

    @DefaultHelperContext
    protected HelperContext myDefaultHC;

    The following example shows an annotation of a setter method:

    private HelperContext helperContext;
    
    @DefaultHelperContext
    public void setHelperContext(HelperContext hc) {
       this.helperContext = hc;
    }
    Alternative step: The following example uses the API rather than annotation:
    import commonj.sdo.helper.SDO;
    import com.ibm.websphere.sdox.SDOUtil;
    
    HelperContext helperContext = SDO.hetHelperContext("sca-default")
    // Or the following line is equivalent to using the string value directly
    HelperContext helperContext = SDO.getHelperContext(SDOUtil.SCA_DEFAULT_SCOPE);
  3. Use the injected HelperContext in your implementation logic.

    When your component starts, the container will inject this field, or call this setter, with the default HelperContext instance for this component so you can use it in your implementation.

    import com.ibm.websphere.soa.sca.sdo.DefaultHelperContext;
    import commonj.sdo.helper.HelperContext;
    import org.osoa.sca.annotations.Service;
    …
    
    // This is a Java implementation of an SCA component
    @Service(AccountService.class)
    public class AccountServiceImpl implements AccountService {
    
        private HelperContext helperContext;
    
        @DefaultHelperContext
        public void setHelperContext(HelperContext hc) {
            this.helperContext = hc;
        }
    
        @Override
        public DataObject accountMethod(DataObject account, String name) {
            // ….
    
            // Get dataFactory to create return object
            DataFactory dataFactory = this.helperContext.getDataFactory();
            DataObject retVal = dataFactory.create("http://myns", "Response");  
            
            retVal.set(..)    // … Set properties on return object
            
            return retVal;
        }

Results

You have written code that accesses the default HelperContext.

What to do next

Develop one or more SCA composites that use SDO following a bottom-up or top-down approach.




In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms of Use | Feedback

Last updatedLast updated: Sep 19, 2011 3:08:41 PM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=matt&product=was-nd-zos&topic=tsca_sdo_use_helpercontext_sca
File name: tsca_sdo_use_helpercontext_sca.html