先來看看整個項目的代碼架構
com.ajun.tuscany.server包主要是加減乘除法的java介面和計算機介面和計算機實作類別。
com.ajun.tuscany.script主要是用指令碼語言來實現加減乘法服務功能。
com.ajun.tuscany.client用戶端調用。
server包主要代碼:
IAdd.java
package com.ajun.tuscany.server;/** * 加法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午9:47:25 */public interface IAdd {double add(double n1,double n2);}
ISubtract.java
package com.ajun.tuscany.server;/** * 減法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午9:52:00 */public interface ISubtract {double subtract(double n1,double n2);}
IMultiply.java
package com.ajun.tuscany.server;/** * 乘法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午9:50:47 */public interface IMultiply {double multiply(double n1,double n2);}
IDivide.java
package com.ajun.tuscany.server;/** * 除法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午9:48:53 */public interface IDivide {double divide(double n1,double n2);}
ICalculator.java
package com.ajun.tuscany.server;/** * 電腦介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午10:02:20 */public interface ICalculator {/** * 加法 * @param n1 * @param n2 * @return */double add(double n1,double n2);/** * 減法 * @param n1 * @param n2 * @return */double subtract(double n1,double n2);/** * 除法 * @param n1 * @param n2 * @return */double divide(double n1,double n2);/** * 乘法 * @param n1 * @param n2 * @return */double multiply(double n1,double n2);}
Calculator.java
package com.ajun.tuscany.server;import org.osoa.sca.annotations.Reference;public class Calculator implements ICalculator{/** * 引用其他組件IAdd,ISubtract,IMultiply,IDivide * @Reference 說明引用其他組件 會和.composite檔案進行配置 */private IAdd add;//加法組件private ISubtract subtract;//減法組件private IMultiply multiply;//乘法組件private IDivide divide;//除法組件public IAdd getAdd() {return add;}@Referencepublic void setAdd(IAdd add) {this.add = add;}public ISubtract getSubtract() {return subtract;}@Referencepublic void setSubtract(ISubtract subtract) {this.subtract = subtract;}public IMultiply getMultiply() {return multiply;}@Referencepublic void setMultiply(IMultiply multiply) {this.multiply = multiply;}public IDivide getDivide() {return divide;}@Referencepublic void setDivide(IDivide divide) {this.divide = divide;}@Overridepublic double add(double n1, double n2) {System.out.println("Server add");return this.add.add(n1, n2);}@Overridepublic double subtract(double n1, double n2) {System.out.println("Server subtract");return this.subtract.subtract(n1, n2);}@Overridepublic double divide(double n1, double n2) {System.out.println("Server divide");return this.divide.divide(n1, n2);}@Overridepublic double multiply(double n1, double n2) {System.out.println("Server multiply");return this.multiply.multiply(n1, n2);}}
script包用於實現java介面中的方法:
AddService.js
//加法,用js實現function add(n1,n2){return n1+n2;}
DivideService.groovy
//用Groovy實現除法def divide(n1,n2){return n1/n2}
MultiplyService.py
#用python實現def multiply(n1,n2):return n1*n2
SubtractService.rb
#減法用Ruby實現def subtract(n1,n2)return n1-n2end
Calculator.composite設定檔
<?xml version="1.0" encoding="UTF-8"?><composite xmlns="http://www.osoa.org/xmlns/sca/1.0"xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"targetNamespace="http://calc"xmlns:c="http://calc"name="Calculator" ><component name="CalculatorServiceComponent"><implementation.java class="com.ajun.tuscany.server.Calculator" /><reference name="add" target="AddComponent"/><reference name="subtract" target="SubtractComponent"/><reference name="multiply" target="MultiplyComponent"/><reference name="divide" target="DivideComponent"/></component><component name="AddComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/AddService.js"/></component><component name="SubtractComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/SubtractService.rb"/></component><component name="MultiplyComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/MultiplyService.py"/></component><component name="DivideComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/DivideService.groovy"/></component></composite>
用戶端調用:
CalculatorServerClient.java
package com.ajun.tuscany.client;import org.apache.tuscany.sca.host.embedded.SCADomain;import com.ajun.tuscany.server.Calculator;import com.ajun.tuscany.server.ICalculator;/** * 服務端調用 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19 下午11:12:44 */public class CalculatorServerClient {public static void main(String [] args){SCADomain domain = SCADomain.newInstance("com/ajun/tuscany/server/Calculator.composite");ICalculator c = domain.getService(Calculator.class, "CalculatorServiceComponent");System.out.println("3 + 2 = "+c.add(3, 2));System.out.println("3 - 2 = "+c.subtract(3, 2));System.out.println("3 * 2 = "+c.multiply(3, 2));System.out.println("3 / 2 = "+c.divide(3, 2));}}
運行:
2012-7-26 21:55:32 org.apache.tuscany.sca.node.impl.NodeImpl <init>資訊: Creating node: com/ajun/tuscany/server/Calculator.composite2012-7-26 21:55:33 org.apache.tuscany.sca.node.impl.NodeImpl configureNode資訊: Loading contribution: file:/E:/springdm/Calculator_script/bin/2012-7-26 21:55:36 org.apache.tuscany.sca.node.impl.NodeImpl start資訊: Starting node: com/ajun/tuscany/server/Calculator.compositeServer add3 + 2 = 5.0Server subtract3 - 2 = 1.0Server multiply3 * 2 = 6.0Server divide3 / 2 = 1.5