學(xué)習(xí)Tuscany
安裝Tuscany的Eclipse插件
- 首先,啟動Eclipse,并依次點擊菜單:Help -> Software Updates -> Find and Install,選擇“Search for new features to install”,點擊下一步:
- 在接下來的對話框中,點擊“New Remote Site…”創(chuàng)建一個新的站點,命名為“Tuscany”,其地址為:http://people.apache.org/~jsdelfino/tuscany/tools/updatesite/
- 確認(rèn)選擇了我們剛剛新增的一個遠(yuǎn)程站點,并點擊“Finish”按鈕
- 選擇“Apache Tuscany SCA Tools”,并在接下來的窗口一路點擊Next,直到點擊Finish為止,接受該插件的使用協(xié)議(Plugin License)

該應(yīng)用是由四個服務(wù)組成的,這個應(yīng)用是一個在線商店,這里有一個目錄服務(wù),你可以通過它取得項目名單,依靠currencyCode屬性來判斷是以美元還是歐元提供項目價格,目錄服務(wù)本身不去做貨幣換算的工作,相反它引用了一個CurrencyConverter服務(wù)去完成這個工作。接下來,這里還有一個ShoppingCart服務(wù),從目錄選擇了的項目可以被加進(jìn)去(購物車),它是使用REST實現(xiàn)的;Catalog使用的是一個JSONRPC綁定,ShoppingCart呢則是通過ATOM進(jìn)行綁定,最后,就是提供給用戶的、基于瀏覽器的界面服務(wù)的Store組件了,Store服務(wù)組件分別通過JSONRPC和ATOM綁定使用Catalog和ShoppingCart服務(wù)。
創(chuàng)建一個Java工程
這一步你要在Eclipse中建立一個Java工程,點擊工具欄上的New Java Project按鈕
folders for sources and class files:
點擊下一步按鈕,在接下來的窗口打開Libraries選項卡,點擊Add Library...按鈕以添加Tuscany Libraray到工程中:
最后,點擊Finish按鈕結(jié)束工程創(chuàng)建。
構(gòu)建服務(wù)
首先,創(chuàng)建兩個包目錄,以便在接下來的步驟中存放服務(wù)的實現(xiàn)。選擇“store”工程,點擊工具欄上的新建Java包按鈕

記下來,會在services包內(nèi)放入普通的服務(wù),ufservices內(nèi)是界面相關(guān)服務(wù)。
Catalog
現(xiàn)在,準(zhǔn)備創(chuàng)建Catalog服務(wù)的接口和實現(xiàn)。
選擇“services”包,接下來在工具欄的下拉菜單中點擊“New Java Class” 按鈕
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface Catalog {
String[] get();
}
再次選中“services”包,點擊“New Java Class”按鈕
在Java編輯器中會打開我們新增的類,將下面的代碼復(fù)制過去:
import java.util.ArrayList;
import java.util.List;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Reference;
public class CatalogImpl implements Catalog {
@Property
public String currencyCode = "USD";
@Reference
public CurrencyConverter currencyConverter;
private List<String> catalog = new ArrayList<String>();
@Init
public void init() {
String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
catalog.add("Apple - " + currencySymbol +
currencyConverter.getConversion("USD", currencyCode, 2.99f));
catalog.add("Orange - " + currencySymbol +
currencyConverter.getConversion("USD", currencyCode, 3.55f));
catalog.add("Pear - " + currencySymbol +
currencyConverter.getConversion("USD", currencyCode, 1.55f));
}
public String[] get() {
String[] catalogArray = new String[catalog.size()];
catalog.toArray(catalogArray);
return catalogArray;
}
}
在這一步,我們將創(chuàng)建CurrencyConverter的接口和實現(xiàn),首先,按照之前的方式建立接口和類文件,并將下面的代碼復(fù)制過去:
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface CurrencyConverter {
public float getConversion(String fromCurrenycCode,
String toCurrencyCode, float amount);
public String getCurrencySymbol(String currencyCode);
}
public class CurrencyConverterImpl implements CurrencyConverter {
public float getConversion(String fromCurrencyCode,
String toCurrencyCode, float amount) {
if (toCurrencyCode.equals("USD"))
return amount;
else if (toCurrencyCode.equals("EUR"))
return amount*0.7256f;
return 0;
}
public String getCurrencySymbol(String currencyCode) {
if (currencyCode.equals("USD"))
return "$";
else
if (currencyCode.equals("EUR"))
return "?";
return "?";
}
}

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.tuscany.sca.binding.feed.collection.Collection;
import org.apache.tuscany.sca.binding.feed.collection.NotFoundException;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import com.sun.syndication.feed.atom.Link;
public class ShoppingCartImpl implements Collection {
// needs to change to instance var once conversation scope works
private static Map<String, Entry> cart = new HashMap<String, Entry>();
public Feed getFeed() {
Feed feed = new Feed();
feed.setTitle("shopping cart");
Content subtitle = new Content();
subtitle.setValue("Total : " + getTotal());
feed.setSubtitle(subtitle);
feed.getEntries().addAll(cart.values());
return feed;
}
public Entry get(String id) throws NotFoundException {
return cart.get(id);
}
public Entry post(Entry entry) {
String id = "cart-" + UUID.randomUUID().toString();
entry.setId(id);
Link link = new Link();
link.setRel("edit");
link.setHref("" + id);
entry.getOtherLinks().add(link);
link = new Link();
link.setRel("alternate");
link.setHref("" + id);
entry.getAlternateLinks().add(link);
entry.setCreated(new Date());
cart.put(id, entry);
return entry;
}
public void put(String id, Entry entry) throws NotFoundException {
entry.setUpdated(new Date());
cart.put(id, entry);
}
public void delete(String id) throws NotFoundException {
if (id.equals(""))
cart.clear();
else
cart.remove(id);
} private String getTotal() {
float total = 0;
String symbol = "";
if (!cart.isEmpty()) {
Entry entry = cart.values().iterator().next();
String item = ((Content)entry.getContents().get(0)).getValue();
symbol = item.substring(item.indexOf("-")+2, item.indexOf("-")+3);
}
for (Entry entry : cart.values()) {
String item = ((Content)entry.getContents().get(0)).getValue();
total += Float.valueOf(item.substring(item.indexOf("-")+3));
}
return symbol + String.valueOf(total);
}
}

<head>
<title>Store</TITLE>
<script type="text/javascript" src="store.js"></script>
<script language="JavaScript">
//Reference
catalog = (new JSONRpcClient("../Catalog")).Catalog;
//Reference
shoppingCart = new AtomClient("../ShoppingCart");
function catalog_getResponse(items) {
var catalog = "";
for (var i=0; i<items.length; i++)
catalog += '<input name="items" type="checkbox" value="' +
items[i] + '">' + items[i]+ ' <br>';
document.getElementById('catalog').innerHTML=catalog;
}
function shoppingCart_getResponse(feed) {
if (feed != null) {
var entries = feed.getElementsByTagName("entry");
var list = "";
for (var i=0; i<entries.length; i++) {
var item = entries[i].getElementsByTagName("content")[0].firstChild.nodeValue;
list += item + ' <br>';
}
document.getElementById("shoppingCart").innerHTML = list;
document.getElementById('total').innerHTML = feed.getElementsByTagName("subtitle")[0].firstChild.nodeValue;
}
}
function shoppingCart_postResponse(entry) {
shoppingCart.get("", shoppingCart_getResponse);
}
function addToCart() {
var items = document.catalogForm.items;
var j = 0;
for (var i=0; i<items.length; i++)
if (items[i].checked) {
var entry = '<entry xmlns="http://www.w3.org/2005/Atom"><title>cart-item</title><content type="text">'+items[i].value+'</content></entry>'
shoppingCart.post(entry, shoppingCart_postResponse);
items[i].checked = false;
}
}
function checkoutCart() {
document.getElementById('store').innerHTML='<h2>' +
'Thanks for Shopping With Us!</h2>'+
'<h2>Your Order</h2>'+
'<form name="orderForm" action="store.html">'+
document.getElementById('shoppingCart').innerHTML+
'<br>'+
document.getElementById('total').innerHTML+
'<br>'+
'<br>'+
'<input type="submit" value="Continue Shopping">'+
'</form>';
shoppingCart.del("", null);
}
function deleteCart() {
shoppingCart.del("", null);
document.getElementById('
posted on 2008-05-07 10:46 gembin 閱讀(1321) 評論(0) 編輯 收藏 所屬分類: SCA 、SOA