2、 扩展Binding
SCA规范中对Binding的定义有两种,一种为Service Binding,一种为Reference Binding。Service Binding定义了外部客户用来访问Service的入口,Reference Binding描述了引用外部服务的方式,换句简单的话说就是,Reference Binding为引进(与贸易名词‘进口’对应),Service Binding为导出(与贸易名词‘出口’对应)。
目前已实现的binding方式有:rmi,ejb,jms,sca,webservice,其中有些binding实现还不支持service binding,仅支持reference binding。
Binding的装配时运行图如下:
下面详细说明一下在Tuscany中实现一种扩展Binding的方式。
a. 定义Reference,Service Binding上的扩展模型
b. 定义模型Binding的接口及实现,在其中可以定义用户自己的一些属性,接口必须要实现org.apache.tuscany.sca.assembly.Binding,此接口中定义了Binding中的一些基本属性如URI等。
c. 定义binding的解析器,实现接口StAXArtifactProcessor,其中包含了read,write,resolve方法,分别负责读自定义的节点,Binding实例保存,扩展解析生成的Binding对象。
d. 定义Binding提供者的工工厂类,实现接口BindingProviderFactory,在其中要实现ReferenceBindingProvider,ServiceBindingProvider的创建,分别负责Reference Binding,Service Binding的具体动作。
e. 实现ModuleActivator,在其start方法中注册binding解析器和BindingProviderFactory
f. 在系统注册Binding(在com.primeton.sca.runtime.IModelLoade文件中注册ModuleActivator)
示例
扩展Binding示例,实现echo的功能,reference binding引用service binding提供的服务。
a. 在composite定义文件中binding的使用方式如下
- <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
- targetNamespace="http://sample/echo"
- xmlns:se="http://sample/echo"
- xmlns:e="http://echo"
- name="EchoBinding">
-
- <service name="EchoService" promote="EchoComponent">
- <interface.java interface="echo.Echo"/>
- <e:binding.echo uri="http://tempuri.org" />
- </service>
-
- <component name="EchoComponent">
- <implementation.java class="echo.EchoComponentImpl"/>
- </component>
-
- <reference name="EchoReference" promote="EchoComponent/echoReference">
- <interface.java interface="echo.Echo"/>
- <e:binding.echo uri="http://tempuri.org" />
- </reference>
-
- </composite>
其schema如下:
- <element name=“binding.echo" type="sca:EchoBinding" substitutionGroup="sca:binding" />
- <complexType name=“EchoBinding">
- <complexContent>
- <extension base="sca:Binding“/>
- </complexContent>
- </complexType>
b.定义EchoBinding接口,扩展了org.apache.tuscany.sca.assembly.Binding
- /**
- *AmodelforthesampleEchobinding.
- */
- publicinterface EchoBinding extends Binding {
- }
- c.定义EchoBindingFactory接口,通过此工厂可以生成EchoBinding实例
- publicinterface EchoBindingFactory {
-
- /**
- *CreatesanewEchobinding.
- *
- *@returnanewEchobinding
- */
- EchoBinding createEchoBinding();
- }
c.定义EchoBinding 的实现EchoBindingImpl
- /**
- *ImplementationoftheEchobindingmodel.
- */
- publicclass EchoBindingImpl implements EchoBinding {
-
- private String name;
- private String uri;
-
- public String getName() {
- returnname;
- }
-
- public String getURI() {
- returnuri;
- }
-
- publicvoid setName(String name) {
- this.name = name;
- }
-
- publicvoid setURI(String uri) {
- this.uri = uri;
- }
-
- public List<PolicySet> getPolicySets() {
- // The sample binding does not support policies
- return Collections.emptyList();
- }
-
- public List<Intent> getRequiredIntents() {
- // The sample binding does not support policies
- return Collections.emptyList();
- }
-
- public List<Object> getExtensions() {
- // The sample binding does not support extensions
- return Collections.emptyList();
- }
-
- publicboolean isUnresolved() {
- // The sample binding is always resolved
- returnfalse;
- }
-
- publicvoid setUnresolved(boolean unresolved) {
- // The sample binding is always resolved
- }
-
- }
d.定义EchoBindingFactory 的实现DefaultEchoBindingFactory
- publicclass DefaultEchoBindingFactory implements EchoBindingFactory {
-
- public EchoBinding createEchoBinding() {
- returnnew EchoBindingImpl();
- }
-
- }
e.定义节点的解析器EchoBindingProcessor,负责解析binding.echo,并生成对象EchoBinding实例,采用XMLStreamReader进行解析。
f.定义binding的具体动作,在接口ReferenceBindingProvider和ServiceBindingProvider实现。
ServiceBindingProvider在其start方法中进行对外服务的提供,提供监听外部的调用。Stop方法中销毁对外的监听。
ReferenceBindingProvider在其createInvoker方法中实现引用外部服务的具体实现.在本例中只是把输入的值直接返回。
g. 实现ModuleActivator,对解析器及调用ProviderFactory进行注册。并在org.apache.tuscany.sca.core.ModuleActivator文件中对ModuleActivator进行注册到系统运行环境中。
|