Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop Data access resources > Develop data access applications > Develop data access applications > Service Data Objects version 2.1.1 > Use SDO 2.1.1 in SCA applications


Access 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 API that uses the commonj.sdo.helper.SDO class.

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.

This topic describes how to create and access SDO HelperContext in SCA applications. For information about accessing SDO HelperContext in non-SCA applications, see Create 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.

We can access the default HelperContext programmatically in a Java or Java EE component implementation type using either annotation injection or an API. We 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.getHelperContext("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.
SDO data binding for SCA applications
Service Data Objects version 2.1.1
Data access with Service DataObjects, API versions 1.0 and 2.01
Create and accessing SDO HelperContext objects
Use SDO 2.1.1 in SCA applications
Rational Application Developer documentation

+

Search Tips   |   Advanced Search