SCA(Tascany)环境搭建和使用范例

1. 概述
本文档描述了基于Eclipse3.5和Apache Tascany基础之上的开发环境搭建以及快速入门的使用范例。
文中的内容基本上来源于对网络信息的搜集,大多数内容源自于http://tuscany.apache.org。在编写过程中希望通过更加快捷、更加简便的方式,并部分结合我们的开发现状,使读者能够快速上手。如有问题请联系MSN:huangyungang@hotmail.com,并注明意图。
2. 开发环境
2.1. 环境准备
为了在实践中完成下面文中描述的内容,您需要以下工具:

[img]http://dl.iteye.com/upload/attachment/237361/79a25012-2866-341b-85c7-2d24904414fd.png[/img]
2.2. 环境搭建
2.2.1. 使用Tuscany Eclipse Plugin
从表1所示的环境中,获取tuscany1.6 Eclipse Plugin,将其放置到Eclipse3.5目录下,如下图所示:

[img]http://dl.iteye.com/upload/attachment/237363/8f5c3b47-de7b-3144-8284-9ee3dbf5cc9d.png[/img]
图1-Tuscany Eclipse插件放置到Eclipse3.5目录中
接下来我们通过建立一个Link文件,找Eclipse3.5找到,并加载这个Tuscany插件。
首先,如果您的Eclipse3.5中没有Link这个文件夹,您需要手动建一个。然后在这个文件夹下建立一个名为“com.ruijie.tuscany.eclipse.link”文件(文件名可任意)。如下图所示

[img]http://dl.iteye.com/upload/attachment/237365/6c16b59d-abd8-370e-a1e1-b3caf5097f17.png[/img]

图2-使用link文件加载Tuscany Eclipse插件
文件中的内容如下:

path=d:\\eclipse3.5\\tuscany1.6 Eclipse Plugin\\

下面我们来打开Eclipse3.5看看会有什么变化。打开Eclipse3.5的工作区后,在“New Wiazrd”对话框中可以看到如下图所示的Tuscany的插件加载情况。

[img]http://dl.iteye.com/upload/attachment/237367/58f67caf-57d0-3c2c-bc1b-011245d42332.png[/img]
图3-Tuscany Plugin加载

2.2.2. 使用OSOA工具包
下载如表1中的apache-tuscany-sca-1.6.zip文件,并对其解压。解压后的效果如下图所示:

[img]http://dl.iteye.com/upload/attachment/237369/392f3a1a-fba0-3bb9-b562-5fb18e87cbf2.png[/img]


图-4 解压后的apache-tuscany-sca-1.6.zip
在实际开发过程中我们需要使用lib目录下的jar包,因此在工程中,需要引用lib下的包。(初始环境中加载所有的jar包)。
2.3. 示例程序一
接下来我们开发一个基于Apache Tuscany并成功发布WebService的示例。
2.3.1. 创建工程
首先我们在d:\TuscanyWorkspace\工作区下,创建一个名为Web的工程。并按照如下图的方式加载TuscanyLibrary。

[img]http://dl.iteye.com/upload/attachment/237372/736d51c1-5d13-3839-8b5c-54e652106cd0.png[/img]
图5 加载Tuscany Library

第2步:我们需要加载OSOA的工具包。找到前面的下载apache-tuscany-sca-1.6.zip的解压文件,并加载所有的.jar文件。

[img]http://dl.iteye.com/upload/attachment/237375/97ef976c-2bfa-3eb1-a067-c5a2594318c9.png[/img]
图5-加载OSOA工具包
以上是一个Java Application中对OSOA包的使用。当然对于Java Web Application需要将这些包加到Web Application的lib目录下。
2.3.2. 代码
下面的我们要实现的是基于Apache Tomcat来发布一个tuscany的SCA组件。
程序代码的包结构如下图所示:

[img]http://dl.iteye.com/upload/attachment/237377/7f007606-6427-3582-86e0-3cc7c8e8926e.png[/img]
图6-示例程序的包结构
定义Service接口
package user.service;

import java.util.List;

import org.osoa.sca.annotations.Remotable;
@Remotable
public interface IUserService {
//if service is distribute,it need to decalre "@Remotable"
List <User>getAll();
}

定义接口实现
package user.service;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements IUserService{

private static List<User> users = null;
static {
users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId("id" + i);
user.setName("name" + i);
user.setPassword("password" + i);
Address address = new Address();
address.setStreet("street" + i);
user.setAddress(address);
users.add(user);
}
}

public List<User> getAll() {
return users;
}
}

User Pojo类代码
package user.service;

public class User {
private String id;
private String name;
private String password;
private Address address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}


Address POJO类代码
package user.service;

public class Address {
private String street;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}
}

META-INF/sca-deployables下的default.compostie
<?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" name="test"
targetNamespace="http://serivce.web.com">
<component name="UserServiceImpl">
<implementation.java class="user.service.UserServiceImpl"></implementation.java>
</component>
<service name="UserService" promote="UserServiceImpl">
<interface.java interface="user.service.IUserService" />
<binding.ws></binding.ws>
</service>
</composite>


WebContent/META-INF/sca-contribution.xml
<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://www.osoa.org/xmlns/sca/1.0"

xmlns:Web="http://serivce.web.com">
<deployable composite="Web:test"/>
</contribution>

Web.xml中的tuscany过滤器设置
<filter>
<filter-name>tuscany</filter-name>
<filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>tuscany</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.3.3. 运行效果
查看http://localhost:8080/Web/UserService?wsdl得到如下画面


[img]http://dl.iteye.com/upload/attachment/237380/2136056b-529d-3243-a602-2f76aec7a0cc.png[/img]
图7-发布后的SCA组件
2.4. 示例程序二
下面我们根据tuscany的calculator Sample,来实现从页面前端对SCA组件的引用。本示例基于示例程序一的工程之上建立。新建一个classpath,并建立如同下图的包结构的类代码。


[img]http://dl.iteye.com/upload/attachment/237384/04309ae3-2277-3f57-9243-5546ced78da3.png[/img]
2.4.1. 代码
关键类的代码,如CalculatorService代码如下:
package calculator;
/**
* The Calculator service interface.
*/
public interface CalculatorService {

double add(double n1, double n2);

double subtract(double n1, double n2);

double multiply(double n1, double n2);

double divide(double n1, double n2);

}

CalculatorServiceImpl.java
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package calculator;

import org.osoa.sca.annotations.Reference;


/**
* An implementation of the Calculator service.
*/
public class CalculatorServiceImpl implements CalculatorService {

private AddService addService;
private SubtractService subtractService;
private MultiplyService multiplyService;
private DivideService divideService;

@Reference
public void setAddService(AddService addService) {
this.addService = addService;
}

@Reference
public void setSubtractService(SubtractService subtractService) {
this.subtractService = subtractService;
}

@Reference
public void setDivideService(DivideService divideService) {
this.divideService = divideService;
}

@Reference
public void setMultiplyService(MultiplyService multiplyService) {
this.multiplyService = multiplyService;
}

public double add(double n1, double n2) {
return addService.add(n1, n2);
}

public double subtract(double n1, double n2) {
return subtractService.subtract(n1, n2);
}

public double multiply(double n1, double n2) {
return multiplyService.multiply(n1, n2);
}

public double divide(double n1, double n2) {
return divideService.divide(n1, n2);
}
}

Calculator.composite文件
<?xml version="1.0" encoding="UTF-8"?>

<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
targetNamespace="http://Web"
xmlns:sample="http://Web"
name="Calculator">

<component name="CalculatorServiceComponent">
<implementation.java class="calculator.CalculatorServiceImpl"/>
<reference name="addService" target="AddServiceComponent"></reference>
<reference name="subtractService" target="SubtractServiceComponent"></reference>
<reference name="multiplyService" target="MultiplyServiceComponent"></reference>
<reference name="divideService" target="DivideServiceComponent"></reference>
</component>

<component name="AddServiceComponent">
<implementation.java class="calculator.AddServiceImpl"/>
</component>

<component name="SubtractServiceComponent">
<implementation.java class="calculator.SubtractServiceImpl"/>
</component>

<component name="MultiplyServiceComponent">
<implementation.java class="calculator.MultiplyServiceImpl"/>
</component>

<component name="DivideServiceComponent">
<implementation.java class="calculator.DivideServiceImpl"/>
</component>

</composite>


sca-contribution.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://www.osoa.org/xmlns/sca/1.0"

xmlns:Web="http://serivce.web.com">
<deployable composite="Web:test"/>
<deployable composite="Web:Calculator"/>
</contribution>


Calc.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.osoa.org/sca/sca_jsp.tld" prefix="sca" %>
<sca:reference name="CalculatorServiceComponent" type="calculator.CalculatorService" />
<html>
<head><title>Calculator sample</title></head>
<body>
<table>
<tr>
<th>Expression</th><th>Result</th>
</tr>
<tr>
<td>2 + 3</td><td><%= CalculatorServiceComponent.add(2, 3) %></td>
</tr>
<tr>
<td>3 - 2</td><td><%= CalculatorServiceComponent.subtract(3, 2) %></td>
</tr>
<tr>
<td>3 * 2</td><td><%= CalculatorServiceComponent.multiply(3, 2) %></td>
</tr>
<tr>
<td>3 / 2</td><td><%= CalculatorServiceComponent.divide(3, 2) %></td>
</tr>
</table>
</body>
</html>

2.4.2. 运行效果

[img]http://dl.iteye.com/upload/attachment/237388/752f2576-6b48-38a4-8f15-cc1b0a5b1812.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值