< Previous | Next >

Lesson 6: Develop the AccountService component

The AccountService component is the front end to the application you are developing. It will provide a Web service that external clients can access in order to perform banking operations. This lesson demonstrates creating an implementation based on the operations described in a WSDL interface; the previous two lessons used Java™ interfaces. You will create a WSDL interface, generate a JAX-WS interface from the WSDL, create a Java implementation that implements the methods of the interface, and configure a service with a Web service binding on the component. Because the AccountService component relies on services provided by the SavingsAccount and ChequingAccount components that you developed earlier, you will configure references on the AccountService component in order to access those services. Finally, you will wire the components together. By connecting the components by wires, you explicitly indicate the components that will provide the services that the AccountService component relies on.

Create the AccountService component

  1. Create the component. In the AccountServices composite, click a blank area in the composite editor and wait a moment for the action bar to display. Click the Add Component icon. A new component is added to the composite.
  2. Name the component. Click the label in the component until it allows editing. Change the default name to AccountService.
    Create AccountService component

Create a WSDL interface

WSDL files can be used as interfaces for SCA services and as a starting point for generating Java implementations. The previous components, SavingAccount and ChequingAccount, were created with Java interfaces. In this section, you will create a WSDL interface the describes the operations that will be provided by the AccountServices component.
  1. Right-click the AccountServices project and select New > Other > Web Services > WSDL. Click Next.
  2. In the wizard, set the File name as AccountService.wsdl. Click Next.
  3. On the Options page, the values should automatically be set as shown below. Click Finish. The AccountService.wsdl file is created in the AccountServices project.
    New WSDL File dialog
  4. Double-click the AccountService.wsdl file. The file opens in the editor. Select the Source tab and replace the source with the XML code below. This code creates four operations in the WSDL: deposit, withdraw, transfer, getBalance. These are the operations that will be provided via a Web service by the AccountService component.
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions name="AccountService"
    	targetNamespace="http://example.org/AccountService/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    	xmlns:tns="http://example.org/AccountService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    	<wsdl:types>
    		<xsd:schema targetNamespace="http://example.org/AccountService/">
    			<xsd:element name="deposit">
    				<xsd:complexType>
    					<xsd:sequence>
    						<xsd:element name="type" type="xsd:int" />
    						<xsd:element name="amount" type="xsd:double"></xsd:element>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="depositResponse">
    				<xsd:complexType>
    					<xsd:sequence>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="withdraw">
    				<xsd:complexType>
    					<xsd:sequence>
    
    						<xsd:element name="type" type="xsd:int"></xsd:element>
    						<xsd:element name="amount" type="xsd:double"></xsd:element>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="withdrawResponse">
    				<xsd:complexType>
    					<xsd:sequence>
    
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="transfer">
    				<xsd:complexType>
    					<xsd:sequence>
    
    						<xsd:element name="sourceType" type="xsd:int"></xsd:element>
    						<xsd:element name="destType" type="xsd:int"></xsd:element>
    						<xsd:element name="amount" type="xsd:double"></xsd:element>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="transferResponse">
    				<xsd:complexType>
    					<xsd:sequence>
    
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="getBalance">
    				<xsd:complexType>
    					<xsd:sequence>
    
    						<xsd:element name="type" type="xsd:int"></xsd:element>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    			<xsd:element name="getBalanceResponse">
    				<xsd:complexType>
    					<xsd:sequence>
    
    						<xsd:element name="amount" type="xsd:double"></xsd:element>
    					</xsd:sequence>
    				</xsd:complexType>
    			</xsd:element>
    		</xsd:schema>
    	</wsdl:types>
    	<wsdl:message name="depositRequest">
    		<wsdl:part element="tns:deposit" name="parameters" />
    	</wsdl:message>
    	<wsdl:message name="depositResponse">
    		<wsdl:part element="tns:depositResponse" name="parameters" />
    	</wsdl:message>
    	<wsdl:message name="withdrawRequest">
    		<wsdl:part name="parameters" element="tns:withdraw"></wsdl:part>
    	</wsdl:message>
    	<wsdl:message name="withdrawResponse">
    		<wsdl:part name="parameters" element="tns:withdrawResponse"></wsdl:part>
    	</wsdl:message>
    	<wsdl:message name="transferRequest">
    		<wsdl:part name="parameters" element="tns:transfer"></wsdl:part>
    	</wsdl:message>
    	<wsdl:message name="transferResponse">
    		<wsdl:part name="parameters" element="tns:transferResponse"></wsdl:part>
    	</wsdl:message>
    	<wsdl:message name="getBalanceRequest">
    		<wsdl:part name="parameters" element="tns:getBalance"></wsdl:part>
    	</wsdl:message>
    	<wsdl:message name="getBalanceResponse">
    		<wsdl:part name="parameters" element="tns:getBalanceResponse"></wsdl:part>
    	</wsdl:message>
    	<wsdl:portType name="AccountService">
    		<wsdl:operation name="deposit">
    			<wsdl:input message="tns:depositRequest" />
    			<wsdl:output message="tns:depositResponse" />
    		</wsdl:operation>
    		<wsdl:operation name="withdraw">
    			<wsdl:input message="tns:withdrawRequest"></wsdl:input>
    			<wsdl:output message="tns:withdrawResponse"></wsdl:output>
    		</wsdl:operation>
    		<wsdl:operation name="transfer">
    			<wsdl:input message="tns:transferRequest"></wsdl:input>
    			<wsdl:output message="tns:transferResponse"></wsdl:output>
    		</wsdl:operation>
    		<wsdl:operation name="getBalance">
    			<wsdl:input message="tns:getBalanceRequest"></wsdl:input>
    			<wsdl:output message="tns:getBalanceResponse"></wsdl:output>
    		</wsdl:operation>
    	</wsdl:portType>
    	<wsdl:binding name="AccountServiceSOAP" type="tns:AccountService">
    		<soap:binding style="document"
    			transport="http://schemas.xmlsoap.org/soap/http" />
    		<wsdl:operation name="deposit">
    			<soap:operation soapAction="http://www.example.org/AccountService/deposit" />
    			<wsdl:input>
    				<soap:body use="literal" />
    			</wsdl:input>
    			<wsdl:output>
    				<soap:body use="literal" />
    			</wsdl:output>
    		</wsdl:operation>
    		<wsdl:operation name="withdraw">
    			<soap:operation soapAction="http://www.example.org/AccountService/withdraw" />
    			<wsdl:input>
    				<soap:body use="literal" />
    			</wsdl:input>
    			<wsdl:output>
    				<soap:body use="literal" />
    			</wsdl:output>
    		</wsdl:operation>
    		<wsdl:operation name="transfer">
    			<soap:operation soapAction="http://www.example.org/AccountService/transfer" />
    			<wsdl:input>
    				<soap:body use="literal" />
    			</wsdl:input>
    			<wsdl:output>
    				<soap:body use="literal" />
    			</wsdl:output>
    		</wsdl:operation>
    		<wsdl:operation name="getBalance">
    			<soap:operation soapAction="http://www.example.org/AccountService/getBalance" />
    			<wsdl:input>
    				<soap:body use="literal" />
    			</wsdl:input>
    			<wsdl:output>
    				<soap:body use="literal" />
    			</wsdl:output>
    		</wsdl:operation>
    	</wsdl:binding>
    	<wsdl:service name="AccountService">
    		<wsdl:port binding="tns:AccountServiceSOAP" name="AccountServiceSOAP">
    			<soap:address
    				location="http://localhost:9080/AccountService/AccountService/wsBnd" />
    		</wsdl:port>
    	</wsdl:service>
    </wsdl:definitions>
  5. If necessary, change the port on the soap:address element to match the HTTP address of your server. To check the HTTP port of your server, right-click the server in the servers view and select Properties. In the properties, select the WebSphere Application Server section and note the HTTP port value for the server. If it is different than the default value used in AccountService.wsdl, change the port value of the following line to match your port:
    <soap:address location="http://localhost:<port>/AccountService/AccountService/wsBnd" />
  6. Click the Design tab to see the operations that have been added.
    WSDL Design view with new operations
  7. Save your work.

Generate a JAX-WS interface

In this section you will generate a JAX-WS interface based on the AccountService.wsdl file that you created.
  1. Launch the generate JAX-WS interface from WSDL wizard. Right-click the AccountService.wsdl file and select Service Component Architecture 1.1 > Generate JAX-WS Interface. The generate JAX-WS interface from WSDL wizard opens.
  2. Generate the interface. Accept the default values in the wizard and click OK.
    Generate JAX-WS interface
  3. Check the output. The interface files will be generated under the src folder in a package called org.example.accountservice. Expand that package to ensure that the files were generated. Note that there are files generated to match the operations defined in the AccountService.wsdl file.
    JAX-WS interface files

Create a Java implementation

In this section you will create a Java implementation for the AccountService component that implements the operations described in the AccountService.wsdl file.
  1. Create the implementation class AccountServiceImpl. In the AccountServices project, right-click the org.example.accountservice package in the src folder and select New > Class. The Java class wizard opens. In the Name field enter AccountServiceImpl. Click Finish. The initial class is created.
  2. Replace the source in AccountServiceImpl.java with the following code and save the file.
    package org.example.accountservice;
    
    import org.example.components.ChequingService;
    import org.example.components.SavingsService;
    import org.oasisopen.sca.annotation.Reference;
    import org.oasisopen.sca.annotation.Service;
    
    @Service(AccountService.class)
    public class AccountServiceImpl implements AccountService {
    
    	public final int ACCOUNT_TYPE_SAVING = 0;
    	public final int ACCOUNT_TYPE_CHEQUING = 1;
    
    	@Reference
    	public SavingsService savingsReference;
    
    	@Reference
    	public ChequingService chequingReference;
    
    	public void transfer(int sourceAccountType, int destAccountType,
    			double amount) {
    		if (sourceAccountType == ACCOUNT_TYPE_CHEQUING) {
    			savingsReference.modifyBalance(amount);
    			chequingReference.modifyBalance(-amount);
    		} else {
    			savingsReference.modifyBalance(-amount);
    			chequingReference.modifyBalance(amount);
    		}
    	}
    
    	public double getBalance(int type) {
    		if (type == ACCOUNT_TYPE_CHEQUING)
    			return chequingReference.getBalance();
    		else
    			return savingsReference.getBalance();
    	}
    
    	public void withdraw(int accountType, double amount) {
    		if (accountType == ACCOUNT_TYPE_CHEQUING)
    			chequingReference.modifyBalance(-amount);
    		else
    			savingsReference.modifyBalance(-amount);
    	}
    
    	public void deposit(int accountType, double amount) {
    		if (accountType == ACCOUNT_TYPE_CHEQUING)
    			chequingReference.modifyBalance(amount);
    		else
    			savingsReference.modifyBalance(amount);
    	}
    
    }
    Note: The key things to note in this code are the @Service and @Reference annotations. The line:
    @Service (AccountService.class)
    indicates that this class provides a service and identifies that the interface AccountService describes the methods provided by the service. The AccountService interface file was generated when you ran the generate JAX-WS interface process on the AccountService.wsdl file;.
    Note the @Reference lines:
    @Reference
    public SavingsService savingsReference;
    
    @Reference
    public ChequingService chequingReference;
    These lines indicate that this code requires processing that will be done by other components and so SCA references are required. In the composite editor, you will later add references to the AccountServiceComponent that correspond to these @Reference annotations and connect them to the services on the SavingsAccount and ChequingAccount components. The objects that are annotated with @Reference: savingsReference and chequingReference, are instances of the interfaces SavingsService and ChequingService. These are the same interfaces that are used to describe the methods provided by the services on the SavingsAccount and ChequingAccount components. By annotating these objects with @Reference and later wiring the references on the component, you can use the operations that are provided by other components.

Configure the AccountService component

In this section you will add references to the AccountService component to access the services on the ChequingAccount and SavingsAccount components, set the implementation to the AccountServiceImpl class, and add a service so that the AccountService component can be accessed as a Web service.
  1. Add the chequingReference. Click the AccountService component and wait a moment for the action bar to appear. In the action bar, click the Add ComponentReference icon. A new reference is added to the component. To name the reference, right-click the reference, select Show Properties View, and select the General tab. In the Name field, enter chequingReference.
    Note: Interfaces can also be set on references. If you want to be explicit about the operations that will satisfy the requirements of the reference, you can select the Interface tab and set the interface to be the same as the interface configured for the ChequingService on the ChequingAccount component. Note, though, that in the next section of this lesson, the reference will be wired directly to the service. Because of the wiring, the reference interface is inferred from the service that it is being wired to.
  2. Add the savingsReference. Click the AccountService component and wait a moment for the action bar to appear. In the action bar, click the Add ComponentReference icon. A new reference is added to the component. To name the reference, right-click the reference, select Show Properties View, and select the General tab. In the Name field, enter savingsReference.
    Note: Interfaces can also be set on references. If you want to be explicit about the operations that will satisfy the requirements of the reference, you can select the Interface tab and set the interface to be the same as the interface configured for the SavingsService on the SavingsAccount component. Note, though, that in the next section of this lesson, the reference will be wired directly to the service. Because of the wiring, the reference interface is inferred from the service that it is being wired to.
  3. Set the implementation to AccountServiceImpl. Right-click the AccountService component and select Show Properties View. In the properties view, select the Implementation tab. For Implementation type select Java. The Class field will display. Click Browse next to the Class field to open the select a Java implementation dialog. In the dialog enter AccountServiceImpl then select the AccountServiceImpl class from the matching items list and click OK.
    Choose implementation
  4. Add a service to the AccountService component. The AccountService component will be accessed as a web service. In this step you will add the service. Click the AccountService component and wait a moment for the action bar to appear. In the action bar, click the Add ComponentService icon. A new service is added to the component. To name the service, right-click the service, select Show Properties View, and select the General tab. In the Name field, enter AccountService.
  5. Set the interface on the service. In the properties view for the service, click the Interface tab. For Interface type, choose WSDL. Then click the Browse button next to the Interface field to open the select a WSDL port type dialog. In the Matched port types list, select the AccountService interface. This will set AccountService.wsdl as the interface for the service. Click OK to close the dialog.
    Set WSDL interface

    This image shows the interface set in the properties view for the service:

    Show WSDL interface

  6. Add a web service binding. The AccountService will be exposed as a web service. To add a web service binding, in the properties view, click the Binding tab, select the Bindings node, and click the Add button. In the add new binding dialog, click Web Service and click OK.
  7. Configure the binding. In the properties section for the binding, set the Name as wsBnd. This name matches the value that was set in the AccountService.wsdl file. In the WSDL Element section, uncheck the Use Generated WSDL box. For Element type choose Binding. Now click the ... browse icon next to the Element type field to open the select a WSDL binding dialog. In the Matched bindings section of the dialog, choose AccountServiceSOAP and click OK. The rest of the fields are updated automatically. The configured binding should look like the following:
    Configured web service binding
  8. Check your work This image shows the configured AccountService component in the composite editor canvas:
    Implementation set on the component

Wire the components

The AccountService component relies on services provided by the SavingsAccount and ChequingAccount components. The component has references that correspond to the @Reference annotations in your code to indicate that the component uses services provided by other components. Connect the references to services by wiring them in the composite editor using drag-and-drop operations in the GUI. By wiring references to services on specific components, you are indicating the components that will satisfy the requirements of the references.
  1. Wire the chequingReference to the ChequingService on the chequingAccount component. Use drag-and-drop to do the wiring. To do this, click the reference, wait for the wire to display, click the wire and then drag it over to the service that you want to connect it to.
    Click and drag to wire or promote the Reference
  2. Wire the savingsReference to the SavingsService on the SavingsAccount component. Follow the same drag-and-drop procedure you used in the previous step to do the wiring.
  3. Your wired components in the composite editor should now look like the following image. Click the save icon to save your work.
    Wired components

Lesson checkpoint

You have now completed developing the AccountServices application. In the next lessons you will create and deploy an SCA contribution and test the application with a web services client.
< Previous | Next >

Feedback