7.4. Composite
7.4.1. 解释
如果说component是SCA的原子的话,composite则是分子。一些component组成composite,进而一些composite组成application。
Composite包含components,promoted services, promoted references, propterties 和wires。
Composite可以是component的implementation。就是说更高一层的composite可以包含实现是composite的component。
Composite可以通过inclusion在其他Composite中使用。当一个组合构件被另一个组合构件包含时,上级Composite的其他元素可以使用被包含Composite的全部内容。
Composite可以作为一个发布单元。当发布一个Composite到SCA domain,composite会发布其所有的元素到SCA domain,一般我们会称作这个composite为application。我们会说deploy一个application,等同于deploy那个application对应的最上一层的Composite。 被include的composite,作为component实现的Composite,也可以被发布到domain。
7.4.2. Composite的属性和元素
打开Restaurant2.composite,
<?xml version="1.0" encoding="UTF-8"?>
<sca:composite xmlns:frascati="http://frascati.ow2.org" xmlns:instance="http://www.w3.org/2004/08/wsdl-instance" xmlns:sawsdl="http://www.w3.org/ns/sawsdl" xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0" name="Restaurant2" targetNamespace="http://eclipse.org/Restaurant2/src/Restaurant2">
<sca:component name="RestaurantServiceComponent">
<sca:implementation.java class="restaurant2.lib.RestaurantServiceImpl"/>
<sca:reference name="menuService"/>
<sca:reference name="billService"/>
<sca:service name="RestaurantService">
<sca:interface.wsdl interface="http://api.restaurant2/#wsdl.interface(RestaurantService)"/>
<sca:binding.ws uri="http://localhost:8085/RestaurantService"/>
<sawsdl:semantic.sawsdl/>
</sca:service>
<sawsdl:semantic.sawsdl/>
</sca:component>
<sca:component name="MenuServiceComponent">
<sca:implementation.java class="restaurant2.lib.MenuServiceImpl"/>
<sca:service name="MenuService">
<sca:interface.java interface="restaurant2.api.MenuService"/>
</sca:service>
</sca:component>
<sca:component name="BillServiceComponent">
<sca:implementation.java class="restaurant2.lib.BillServiceImpl"/>
<sca:service name="BillService">
<sca:interface.java interface="restaurant2.api.BillService"/>
</sca:service>
<sca:reference name="vatService"/>
<sca:reference name="tipService">
<sca:interface.java callbackInterface="restaurant2.api.TipServiceCallback" interface="restaurant2.api.TipService"/>
<sca:binding.ws uri="http://localhost:8086/TipServiceComponent"/>
<sca:callback>
<sca:binding.ws uri="http://localhost:8087/TipServiceComponentCB"/>
</sca:callback>
</sca:reference>
</sca:component>
<sca:component name="VatServiceComponent">
<tuscany:implementation.script script="restaurant2/lib/VatServiceImpl.js"/>
<sca:service name="VatService">
<sca:interface.java interface="restaurant2.api.VatService"/>
</sca:service>
</sca:component>
<sca:wire source="RestaurantServiceComponent/billService" target="BillServiceComponent/BillService"/>
<sca:wire source="BillServiceComponent/vatService" target="VatServiceComponent/VatService"/>
<sca:service name="RestaurantService" promote="RestaurantServiceComponent/RestaurantService"/>
<sca:wire source="RestaurantServiceComponent/menuService" target="MenuServiceComponent/MenuService"/>
</sca:composite>
Composite包含元素和属性:
• name,必填项。在由targetNamespace属性标识的命名空间中。
• targetNamespace,声明命名空间
• local,composite的components是否必须运行于同一个进程。Local=”true”指示所有component运行于同一进程。默认是false。
• autowire,component的reference是否自动wire。默认为false
• constrainingType,
• requires,intents。
• policySets, policy sets,这些后面介绍
Composite的service是Composite对外提供的服务,可以被Composite外面的访问。Composite reference表示composite通过reference 调用其他Composite的service。Wire是描述Composite内的component之间的调用关系。
Composite service是component service 提升(promote)来的,实际是有component来提供服务。Composite reference也是由component reference 提升来的。
多个component reference可以promote到一个composite reference。 一个component service可以promote到多个composite service。当多个component reference 提升到一个composite reference,它们都共享相同的配置,包括相同的target service。
7.4.3. 例子,Component实现类型是composite
Ø 在项目Restaurant2Tip下,新建SCA Composite Diagram,名字为Testaurant2TipInner
Ø 新建component TipServiceInnerComponent,在新component上添加java implementation,service,service interface,binding。所有的属性和名称和Restaurant2Tip里的component TipServiceComponent完全相同
图7_4_1
然后编辑Restaurant2TipInner.composite文件,设置targetNamespace,这个要和Restaurant2Tip.composite里相同,并定义新的namespace tip。
<?xml version="1.0" encoding="UTF-8"?>
<sca:composite xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmlns:tip="http://eclipse.org/Restaurant2Tip/src/Restaurant2Tip" name="Restaurant2TipInner" targetNamespace="http://eclipse.org/Restaurant2Tip/src/Restaurant2Tip">
<sca:component name="TipServiceInnerComponent">
<sca:implementation.java class="restaurant2.lib.TipServiceImpl"/>
<sca:service name="TipService">
<sca:interface.java callbackInterface="restaurant2.api.TipServiceCallback" interface="restaurant2.api.TipService"/>
<sca:binding.ws uri="http://localhost:8086/TipServiceComponent"/>
</sca:service>
</sca:component>
</sca:composite>
Ø 打开Restaurant2Tip.composite_diagram,删除component上的service
图7_4_2
Ø 删除component的Java implementation
图7_4_3
图7_4_4
Ø 给component添加新的implementation,SCA implementation
图7_4_5
编辑implementation的属性,你会发现name不可编辑,但后面有个button
图7_4_6
点击那个button
图7_4_7
选择一个已存在的namespace
图7_4_8
Local part中填入之前新建的那个composite的名字,Restaurant2TipInner,点ok
图7_4_9
现在Restaurant2Tip.composite的内容为
<?xml version="1.0" encoding="UTF-8"?>
<sca:composite xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmlns:tip="http://eclipse.org/Restaurant2Tip/src/Restaurant2Tip" name="Restaurant2Tip" targetNamespace="http://eclipse.org/Restaurant2Tip/src/Restaurant2Tip">
<sca:component name="TipServiceComponent">
<sca:implementation.composite name="tip:Restaurant2TipInner"/>
</sca:component>
</sca:composite>
运行TipServiceServer和RestaurantServiceServer
使用soapUI调用ws getBill,正常得到结果
图7_4_10