關(guān) 注 開 源 (王鋒的Blog)

          Spring Hibernate Jboss Tomcat SCA OSGI

          Tuscany SCA擴(kuò)展機(jī)制研究(續(xù))

          2、 擴(kuò)展Binding

          SCA規(guī)范中對(duì)Binding的定義有兩種,一種為Service Binding,一種為Reference Binding。Service Binding定義了外部客戶用來(lái)訪問(wèn)Service的入口,Reference Binding描述了引用外部服務(wù)的方式,換句簡(jiǎn)單的話說(shuō)就是,Reference Binding為引進(jìn)(與貿(mào)易名詞‘進(jìn)口’對(duì)應(yīng)),Service Binding為導(dǎo)出(與貿(mào)易名詞‘出口’對(duì)應(yīng))。

          目前已實(shí)現(xiàn)的binding方式有:rmi,ejb,jms,sca,webservice,其中有些binding實(shí)現(xiàn)還不支持service binding,僅支持reference binding。

          Binding的裝配時(shí)運(yùn)行圖如下:

          下面詳細(xì)說(shuō)明一下在Tuscany中實(shí)現(xiàn)一種擴(kuò)展Binding的方式。

          a. 定義Reference,Service Binding上的擴(kuò)展模型

          b. 定義模型Binding的接口及實(shí)現(xiàn),在其中可以定義用戶自己的一些屬性,接口必須要實(shí)現(xiàn)org.apache.tuscany.sca.assembly.Binding,此接口中定義了Binding中的一些基本屬性如URI等。

          c. 定義binding的解析器,實(shí)現(xiàn)接口StAXArtifactProcessor,其中包含了read,write,resolve方法,分別負(fù)責(zé)讀自定義的節(jié)點(diǎn),Binding實(shí)例保存,擴(kuò)展解析生成的Binding對(duì)象。

          d. 定義Binding提供者的工工廠類,實(shí)現(xiàn)接口BindingProviderFactory,在其中要實(shí)現(xiàn)ReferenceBindingProvider,ServiceBindingProvider的創(chuàng)建,分別負(fù)責(zé)Reference Binding,Service Binding的具體動(dòng)作。

          e. 實(shí)現(xiàn)ModuleActivator,在其start方法中注冊(cè)binding解析器和BindingProviderFactory

          f. 在系統(tǒng)注冊(cè)Binding(在com.primeton.sca.runtime.IModelLoade文件中注冊(cè)ModuleActivator)

          示例

          擴(kuò)展Binding示例,實(shí)現(xiàn)echo的功能,reference binding引用service binding提供的服務(wù)。

          a. 在composite定義文件中binding的使用方式如下

          1. <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
          2.       targetNamespace="http://sample/echo"
          3.       xmlns:se="http://sample/echo"
          4.       xmlns:e="http://echo"
          5.       name="EchoBinding">
          6. <service name="EchoService" promote="EchoComponent">
          7. <interface.java interface="echo.Echo"/>
          8. <e:binding.echo uri="http://tempuri.org" />
          9. </service>
          10. <component name="EchoComponent">
          11. <implementation.java class="echo.EchoComponentImpl"/>
          12. </component>
          13. <reference name="EchoReference" promote="EchoComponent/echoReference">
          14. <interface.java interface="echo.Echo"/>
          15. <e:binding.echo uri="http://tempuri.org" />
          16. </reference>
          17. </composite>

          其schema如下:

          1. <element name=“binding.echo" type="sca:EchoBinding" substitutionGroup="sca:binding" />
          2. <complexType name=“EchoBinding">
          3. <complexContent>
          4. <extension base="sca:Binding“/>
          5. </complexContent>
          6. </complexType>

          b.定義EchoBinding接口,擴(kuò)展了org.apache.tuscany.sca.assembly.Binding

          1. /**
          2. *AmodelforthesampleEchobinding.
          3. */
          4. publicinterface EchoBinding extends Binding {
          5. }
          6. c.定義EchoBindingFactory接口,通過(guò)此工廠可以生成EchoBinding實(shí)例
          7. publicinterface EchoBindingFactory {
          8. /**
          9. *CreatesanewEchobinding.
          10. *
          11. *@returnanewEchobinding
          12. */
          13. EchoBinding createEchoBinding();
          14. }

          c.定義EchoBinding 的實(shí)現(xiàn)EchoBindingImpl

          1. /**
          2. *ImplementationoftheEchobindingmodel.
          3. */
          4. publicclass EchoBindingImpl implements EchoBinding {
          5. private String name;
          6. private String uri;
          7. public String getName() {
          8. returnname;
          9. }
          10. public String getURI() {
          11. returnuri;
          12. }
          13. publicvoid setName(String name) {
          14. this.name = name;
          15. }
          16. publicvoid setURI(String uri) {
          17. this.uri = uri;
          18. }
          19. public List<PolicySet> getPolicySets() {
          20. // The sample binding does not support policies
          21. return Collections.emptyList();
          22. }
          23. public List<Intent> getRequiredIntents() {
          24. // The sample binding does not support policies
          25. return Collections.emptyList();
          26. }
          27. public List<Object> getExtensions() {
          28. // The sample binding does not support extensions
          29. return Collections.emptyList();
          30. }
          31. publicboolean isUnresolved() {
          32. // The sample binding is always resolved
          33. returnfalse;
          34. }
          35. publicvoid setUnresolved(boolean unresolved) {
          36. // The sample binding is always resolved
          37. }
          38. }

          d.定義EchoBindingFactory 的實(shí)現(xiàn)DefaultEchoBindingFactory

          1. publicclass DefaultEchoBindingFactory implements EchoBindingFactory {
          2. public EchoBinding createEchoBinding() {
          3. returnnew EchoBindingImpl();
          4. }
          5. }

          e.定義節(jié)點(diǎn)的解析器EchoBindingProcessor,負(fù)責(zé)解析binding.echo,并生成對(duì)象EchoBinding實(shí)例,采用XMLStreamReader進(jìn)行解析。

          f.定義binding的具體動(dòng)作,在接口ReferenceBindingProvider和ServiceBindingProvider實(shí)現(xiàn)。

            ServiceBindingProvider在其start方法中進(jìn)行對(duì)外服務(wù)的提供,提供監(jiān)聽外部的調(diào)用。Stop方法中銷毀對(duì)外的監(jiān)聽。

            ReferenceBindingProvider在其createInvoker方法中實(shí)現(xiàn)引用外部服務(wù)的具體實(shí)現(xiàn).在本例中只是把輸入的值直接返回。

          g. 實(shí)現(xiàn)ModuleActivator,對(duì)解析器及調(diào)用ProviderFactory進(jìn)行注冊(cè)。并在org.apache.tuscany.sca.core.ModuleActivator文件中對(duì)ModuleActivator進(jìn)行注冊(cè)到系統(tǒng)運(yùn)行環(huán)境中。

          (未完待續(xù))

          posted on 2007-08-05 11:08 wangfeng 閱讀(493) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 苏州市| 包头市| 乌鲁木齐县| 湖北省| 肥城市| 阜新| 汉阴县| 克拉玛依市| 盘山县| 东乡族自治县| 会同县| 石渠县| 图木舒克市| 马鞍山市| 怀远县| 尉氏县| 启东市| 兴化市| 铜山县| 博客| 保靖县| 泗洪县| 洛南县| 泰安市| 曲阳县| 且末县| 漳州市| 宁蒗| 宁国市| 吉木乃县| 丁青县| 松原市| 巧家县| 建平县| 北海市| 安顺市| 华池县| 台中市| 沧州市| 上杭县| 蚌埠市|