用JSP调用以Web应用形式部署在Tomcat 5.5中的SCA服务组件的例子

Composite是部署的基本单元。在装配文件中,composite元素是根元素。

composite元素可以包含composite、service、component、reference等其他元素,component是非常重要的元素。

component元素可以包含0...n个Service,Reference,property 和0...1个implementation。

实现component中的implementation的方式可以有Java、BPEL、Composite等,如下图。

用JSP调用以Web应用形式部署在Tomcat 5.5中的SCA服务组件的例子

在这个例子中,就是使用Composite方式实现composite中包括的component的implementation。

用JSP调用以Web应用形式部署在Tomcat 5.5中的SCA服务组件的例子

在基于Web应用的SCA服务组件的装配文件中,是这样表示composite实现component的。

文件名为default.scdl

<?xmlversion="1.0"encoding="UTF-8"?>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"
name
="CalculatorComposite">

<componentname="CalculatorServiceComponent">
<implementation.compositename="CalculatorComposite"jarLocation="lib/sample-calculator-1.0-incubator-M2.jar"/>
</component>
</composite>


在发布的web应用目录的WEB-INF中,有一个lib目录,里面保存着运行SCA应用运行需要的环境,也包括包含着当前web应用需要的代码和装配文件组成的jar包 sample-calculator-1.0-incubator-M2.jar 。这个jar包的内容就是前面举例(Tuscany SCA以独立应用方式运行的简单例子 )使用的jar包,通过default.scdl应用装配文件加载到运行环境中。

与可独立运行的SCA服务组件不同的是,web应用服务组件环境的建立和装配过程是通过web.xml中servlet的组件listener和filter来完成的。

web.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>ApacheTuscanySimpleWebappSample</display-name>

<welcome-file-listid="WelcomeFileList">
<welcome-file>calc.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>TuscanyFilter</filter-name>
<filter-class>org.apache.tuscany.runtime.webapp.TuscanyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TuscanyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.apache.tuscany.runtime.webapp.TuscanyContextListener</listener-class>
</listener>
</web-app>

web服务启动后,可以通过jsp访问SCA服务组件。

calc.jsp

<%@pageimport="calculator.CalculatorService"%>
<%@pageimport="org.osoa.sca.CompositeContext"%>
<%@pageimport="org.osoa.sca.CurrentCompositeContext"%>
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>
<%
CompositeContextcontext
=CurrentCompositeContext.getContext();
CalculatorServicecalc
=context.locateService(CalculatorService.class,"CalculatorServiceComponent");
%>
<html>
<head><title>Calculatorsample</title></head>

<body>
<table>
<tr>
<th>Expression</th><th>Result</th>
</tr>
<tr>
<td>2+3</td><td><%=calc.add(2,3)%></td>
</tr>
<tr>
<td>3-2</td><td><%=calc.subtract(3,2)%></td>
</tr>
</table>
</body>
</html>

<END>