关 注 开 源 (王锋的Blog)

Spring Hibernate Jboss Tomcat SCA OSGI

Tuscany SCA扩展机制研究(Interface,DataBinding)

本节对Tuscany扩展中的Interface,DataBinding进行了探讨,说明了两种实现的目的以及如何进行扩展。Interface对提供的service进行接口的描述,服务元模型的一种描述方式,在使用是会对调用的服务进行接口匹配,来保证调用的服务的操作与服务定义的接口相一致。DataBinding提供了数据的转换机制,能够把业务数据按正确的方式传递给Component进行调用,通过实现接口DataBinding和Transformer来完成。

3、 扩展Interface

Interface扩展相对于implementation,binding来讲,是相对简单一点的扩展方式,对提供的service进行接口的描述,提供服务元模型的一种方式。Interface在使用是会对调用的服务进行接口匹配,来保证调用的服务的操作与服务定义的接口相一致。

目前已实现的interface有Java,WebService1.1两种方式。

扩展步骤如下:

a. 定义Interface上的扩展模型

b. 定义InterfaceContract的扩展接口,来扩展自己的属性

c. 实现InterfaceContract接口,生成Interface的具体定义

d. 定义对Interface节点的解析器,实现接口StAXArtifactProcessor,并在其read方法中生成InterfaceContract实例。

e. 在ModuleActivator中注册解析器,并在运行器中注册

4、Databinding扩展

DataBinding提供了数据的转换机制,能够把业务数据按正确的方式传递给Component进行调用,通过实现接口DataBinding和Transformer来完成。

DataBinding通过一字符中类型的ID进行区分,并且还可以为其指定别名,并且指定针对其ID所使用的一系列转换器(Transformer),在使用过程中,会针对指定类型把一种数据类型转换为另一种数据类型,实现接口数据的自动匹配。例如,我们定义了data1到data2数据类型的转换器,那么在应用过程中,可以使用data1数据类型,也可以使用data2数据类型,框架会此进行判断,并按转换器定义的规则进行数据类型的转换。

要扩展新的DataBinding实现,通常通过以下几个步骤:

a. 扩展接口DataBinding,通常扩展抽象类BaseDataBinding就可以了

a) 定义DataBinding的唯一标识和别名

b) 处理操作的wrapping/unwrapping

c) 定义数据的copy方式,传值引用,相于对象的clone

d) 定义正理异常的Handle

b. 增加转换器,把databinding中指定的类型转换到不同的类型数据上,在Tuscany中会把所有的Transformer放到一张拓扑图中,根据拓扑图能找到从源类型—>目标类型转换的最短路径。通过实现PullTransformer/PushTransformer接口来完成

a)接口定义了源和目标数据的类型以及其中的源到目标数据类型的转换规则。

b)Transformer有两种类型的接口PullTransformer/PushTransformer,其中有一点小小的区别,PushTransformer在数据转换方法transform中比PullTransformer多了一个参数,来表明数据的接收点(The sink to receive the data),有点像管道流的作用。

 下面以AxiomDataBinding转换的示例,把字符串类型转换为OMElement类型:

  1. public class String2OMElement extends BaseTransformer<String, OMElement> implements
  2. PullTransformer<String, OMElement> {
  3. @SuppressWarnings("unchecked")
  4. public OMElement transform(String source, TransformationContext context) {
  5. try {
  6. StAXOMBuilder builder = new StAXOMBuilder(new ByteArrayInputStream(source.getBytes()));
  7. OMElement element = builder.getDocumentElement();
  8. AxiomHelper.adjustElementName(context, element);
  9. return element;
  10. } catch (Exception e) {
  11. throw new TransformationException(e);
  12. }
  13. }
  14. public Class getTargetType() {
  15. return OMElement.class;
  16. }
  17. public Class getSourceType() {
  18. return String.class;
  19. }
  20. public int getWeight() {
  21. return 40;
  22. }
  23. }

c、DataBinding加载

其实现方式同Implementation,Binding,Interface,通过实现ModuleActivator接口完成。

DataBinding的类图如下:

资源列表:

Spec:http://www.osoa.org/display/Main/Home

Tuscany Home:http://cwiki.apache.org/TUSCANY/

Tuscany Code:https://svn.apache.org/repos/asf/incubator/tuscany/java/sca

(全文完)

posted on 2007-08-08 21:50 wangfeng 阅读(1188) 评论(1)  编辑  收藏

评论

# re: Tuscany SCA扩展机制研究(Interface,DataBinding) 2007-08-09 11:45 阿不

好,收藏起来,学习先!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: