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.
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.
The steps describe 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.
We can create and access SDO HelperContext in both OSOA and OASIS SCA applications.
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 alternative 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, we can access the default HelpContext instance using the API mechanism. However, we cannot access the instance using the annotation injection mechanism.
- Add a public or protected field or setter method of the commonj.sdo.helper.HelperContext type to the 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.
- 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);
- Use the injected HelperContext in the implementation logic.
When the component starts, the container will inject this field, or call this setter, with the default HelperContext instance for this component so we can use it in the implementation.
import com.ibm.websphere.soa.sca.sdo.DefaultHelperContext; import commonj.sdo.helper.HelperContext; // FOR OSOA import org.osoa.sca.annotations.Service; // FOR OASIS, commented out // import org.oasisopen.sca.annotation.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.
Related concepts
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
Related tasks
Create and accessing SDO HelperContext objects Use SDO 2.1.1 in SCA applications
Related information: