DSP

eclipse stp开发sca

2019-07-13 14:52发布

环境 eclipse Helios(3.6)for java ee develope sca框架 tuscany1.6.2 下载eclipse及tuscany eclipse地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr2 tuscany1.6.2地址:http://labs.renren.com/apache-mirror//tuscany/java/sca/1.6.2/apache-tuscany-sca-1.6.2.tar.gz 打开eclipse Help---->install new software打开install窗口 点击右侧的Add...按钮弹出Add repository窗口name随意,location http://download.eclipse.org/stp/updates/helios/ 完成更新地址添加在下拉列表中查找你增加的更新项选中,稍等就会在列表中出现更新项的所有插件信息,全选,next完成进行安装 安装完成后重启eclipse 进入eclipse在new----->other的wizard中会多出一项SCA包括六项菜单。 开发sca 解压tuscany到系统目录下
  1. 在eclipse菜单选择 Window > Preference.
  2. 进入首选项 Java > Build Path > User Libraries.
  3. 选择new创建新的用户库
  4. 名称为tuscany(可以随意)
单击add jar选择tuscany目录下的lib与module下的所有jar包 module下包括不同的目录拷起很麻烦可以用如下命令解决 find ./ -name *.jar -exec cp {} /home/du/book/sca/ ;

应用实例 下图为我们将要创建的sca装配图
RestaurantServiceComponent
接受点菜并且结算订单 MenuServiceComponent
提供不同菜单的详细信息 BillServiceComponent
计算菜单价格 VATServiceComponent
计算增值税 TipServiceComponent
计算小费 创建新的java project名称Restaurant 创建SCA diagram
  1. 右键工程选择 New > Other....
  2. 在向导中选择 SCA Composite Diagram单击完成


现在你的工程目录应该如下
创建Restaurant组件 在该编辑器的右侧为图形面板 首先创建RestaurantServiceComponent,MenuServiceComponent,BillServiceComponent,VatServiceComponent,TipServiceComponent 在组件内创建相关service
  • 服务名 RestaurantService 在RestaurantServiceComponent,
  • 服务名 MenuService MenuServiceComponent,
  • 服务名 BillService 在 BillServiceComponent,
  • 服务名 VatService在VatServiceComponent,
  • 服务名 TipService在TipServiceComponent.
创建引用 
  • 引用名menuService 在 RestaurantServiceComponent,
  • 引用名billService 在 RestaurantServiceComponent,
  • 引用名 vatService 在 BillServiceComponent,
  • 引用名tipService 在 BillServiceComponent.
利用wires来连接引用项与被引用项 提升restaurant service 创建一个service名称RestaurantService利用promote将该服务与RestaurantServiceComponent中的RestaurantService连接 完成后应该如下图所示

查看Restaurant.composite文件如下这是根据图形化编辑内容生成的
定义接口与实现 创建两包如下图所示
定义服务的接口在restaurant.api包中 Restaurant Service
package restaurant.api;
 
public interface RestaurantService { 
  Menu[] getMenus();
  double getBill(Menu menu);
}
Menu Service
package restaurant.api;
 
public interface MenuService {
  Menu[] getMenu();
  double getPrice(Menu menu);
}
Bill Service
package restaurant.api;
 
public interface BillService {
  double getBill(double menuPrice);
}
Vat Service
package restaurant.api;
 
public interface VatService {
  double getPriceWithVat(double price);
}
Tip Service
package restaurant.api;
 
public interface TipService {
  double getPriceWithTip(double price);
}
Define also the interface of the Menu (Data Transfert Object):


package restaurant.api;
 
import java.io.Serializable;
 
public interface Menu extends Serializable {
  String printMenu();
}
定义实现在lib包中 Restaurant Service
package restaurant.lib;
 
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Service;
 
import restaurant.api.BillService;
import restaurant.api.Menu;
import restaurant.api.MenuService;
import restaurant.api.RestaurantService;
 
@Service(RestaurantService.class)
public class RestaurantServiceImpl implements RestaurantService {
 
  private MenuService menuService;
  private BillService billService;
 
  @Reference
  public void setMenuService(MenuService menuService) {
    this.menuService = menuService;
  }
 
  @Reference
  public void setBillService(BillService billService) {
    this.billService = billService;
  }
 
  public double getBill(Menu menu) {
    double menuPrice = this.menuService.getPrice(menu);
    return this.billService.getBill(menuPrice);
  }
 
  public Menu[] getMenus() {
    return this.menuService.getMenu();
  }  
}
Menu Service
package restaurant.lib;
 
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Service;
 
import restaurant.api.Menu;
import restaurant.api.MenuService;
 
@Service(MenuService.class)
public class MenuServiceImpl implements MenuService {
 
  private Menu[] menus;
  private double[] prices;
 
  @Init
  public void init() {
    this.menus = new Menu[] {
            new MenuImpl(0, "Grilled hamburger with French fries" ),
            new MenuImpl(1, "Roasted chicken with vegetables"),
            new MenuImpl(2, "Duck breast in an orange sauce"),
            new MenuImpl(3, "Duck foie gras & mango chutney") };
    this.prices = new double[] { 10, 15, 35, 50 };
  }
 
  public Menu[] getMenu() {
    return this.menus;
  }
 
  public double getPrice(Menu menu) {
    return this.prices[((MenuImpl) menu).getId()];
  }
}
Bill service
package restaurant.lib;
 
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Service;
 
import restaurant.api.BillService;
import restaurant.api.TipService;
import restaurant.api.VatService;
 
@Service(BillService.class)
public class BillServiceImpl implements BillService {
 
  private VatService vatService;
  private TipService tipService;
 
  @Reference
  public void setVatService(VatService vatService) {
    this.vatService = vatService;
  }
 
  @Reference
  public void setTipService(TipService tipService) {
    this.tipService = tipService;
  }
 
  public double getBill(double menuPrice) {
    double pricewithTaxRate = 
                   this.vatService.getPriceWithVat(menuPrice);
    double priceWithTipRate = 
                   this.tipService.getPriceWithTip(pricewithTaxRate);
    return priceWithTipRate;
  }
}
Vat Service
package restaurant.lib;
 
import org.osoa.sca.annotations.Service;
 
import restaurant.api.VatService;
 
@Service(VatService.class)
public class VatServiceImpl implements VatService {
 
  public double vatRate;
 
  public VatServiceImpl(){
    this.vatRate=19.6;
  }
 
  public double getPriceWithVat(double price) {
    return price * this.vatRate/100 + price;
  }  
}
Tip service
package restaurant.lib;
 
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Service;
 
import restaurant.api.TipService;
 
@Service(TipService.class)
public class TipServiceImpl implements TipService {
 
  @Property
  public double tipRate;
 
  public TipServiceImpl(){
    this.tipRate=10;
  }
 
  public double getPriceWithTip(double price) {
    return price * this.tipRate/100 + price;
  }
}
Menu
package restaurant.lib;
 
import restaurant.api.Menu;
 
public class MenuImpl implements Menu {
 
  private int id;
  private String details;
 
  MenuImpl(int idC, String detailsC) {
    this.id = idC;
    this.details = detailsC;
  }
 
  public String printMenu() {
    return this.details;
  }
 
  public int getId() {
    return this.id;
  }  
}
将接口与实现与sca的组装图结合 将实现类拖入图形化相对应的组件的service接口
组装完成后如下图所示
测试 package test;
 
import org.apache.tuscany.sca.host.embedded.SCADomain;
 
import restaurant.api.Menu;
import restaurant.api.RestaurantService;
 
public class Client {
 
  public static void main(String[] args) throws Exception {
    SCADomain scaDomain = SCADomain.newInstance("Restaurant.composite");
    RestaurantService restaurantService = scaDomain.getService(
    RestaurantService.class, "RestaurantServiceComponent");
    Menu[] menus = restaurantService.getMenus();
    System.out.println("--- Menu ---");
    for (Menu m : menus) {
      System.out.println("- " + m.printMenu());
    }
    System.out.println();   
    Menu menu = menus[3];
    System.out.println("My choice: " + menu.printMenu());
    System.out.println();
    double price = restaurantService.getBill(menu);
    System.out.println("Price (" + menu.printMenu() + "): " + price);
    scaDomain.close();
  }
}
运行Client
在TipServiceImpl中我们定义了一个tipRate的属性表示小费,可能有些国家没有小费我们可以这样 在TipServiceComponent上加一个property名称为tipRate 右键点击tipRate选择show property view显示属性视图,在value中设置为0
运行如下
js实现服务
删除vatservice上的java实现,增加js实现

js内容 var vatRate=10 function getPriceWithVat(price) { return price * vatRate/100 + price; } 运行如下