You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by co...@apache.org on 2008/10/21 03:10:03 UTC

[CONF] Apache Tuscany: Getting Started with Tuscany (using Tuscany Eclipse Plugin) (page edited)

Getting Started with Tuscany (using Tuscany Eclipse Plugin) (TUSCANY) edited by Luciano Resende
      Page: http://cwiki.apache.org/confluence/display/TUSCANY/Getting+Started+with+Tuscany+%28using+Tuscany+Eclipse+Plugin%29
   Changes: http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=74081&originalVersion=13&revisedVersion=14






Content:
---------------------------------------------------------------------

h1. Ready, Set, Go - Getting started with Tuscany

[(*y) "Get Started with Store Demo in Eclipse video"|http://cwiki.apache.org/confluence/display/TUSCANYWIKI/Getting+Started+with+Apache+Tuscany]
This guide shows you how to get started with Tuscany in Eclipse by installing the Eclipse plugins and creating a web store shopping cart with SCA technology.

{include:Tools - Install the Latest Tuscany Eclipse Plugin}

h2. Create your 1st Composite Service Application

The following shows the composition diagram for the composite service application you are about
to create.

!first_composite.png!

The composite service application you will create is a composition of four services. The composed
service provided is that of an on-line store.
There is a Catalog service which you can ask for catalog items, and depending on its currency
code property configuration it will provide the item prices in USD or EUR. The Catalog service is not
doing the currency conversion itself it references a CurrencyConverter service to do that task. Then
there is the ShoppingCart service into which items chosen from the catalog can be added, it is
implemented as a REST service. The Catalog is bound using the JSONRPC binding, and the
ShoppingCart service is bound using the ATOM binding. Finally there is the Store user facing
service that provides the browser based user interface of the store. The Store service makes use of
the Catalog and ShoppingCart service using the JSONRPC, and ATOM binding respectively.

h2. Create a Java Project

In this step you create a Java Project in Eclipse to hold the composite service application.
Click on the *{_}New Java Project{_}* button !new_java_project.png!  in the toolbar to launch the project creation dialog.
Next you enter "store" as the *{_}Project name{_}*, and for *{_}Project Layout{_}* select *{_}Create separate{_}*
*{_}folders for sources and class files._*
!new_java_project_dlg.png!
 
!project_layout.png!
 
Hit the *{_}Next{_}* button, and on the following page go to the *{_}Libraries{_}* tab. Use the *{_}Add Library..._*
button on the right to add the *Tuscany Library* library to the project.
 
!new_java_project_lib.png!
 
 
Hit the *{_}Finish{_}* button to complete the *{_}New Java Project{_}* dialog to create the "store" java project. 
 
!new_java_project_folder.png!
 
 

h2. Construct Services

First you create two package folders into which later in this step you place service implementations.
Select the "store" project and click on the *{_}New Java Package{_}* button !new_java_pkg_btn.png! in the toolbar to launch
the package creation dialog.

Next you enter "services" as the package *{_}Name{_}*, and press the *{_}Finish{_}* button to complete the
dialog.
!new_java_pkg_dlg.png!
 
Repeat the previous step to create another package named "ufservices". The store project now
should look as follows.
 
!new_java_pkg_folder.png!
 
In the following you will place in the "services" package the regular services, and in the "ufservices"
package the user facing services of the composite service application you create.

h3. _Catalog_

In this step you create the Catalog service interface and implementation.
Select the "services" package. Next you click on the dropdown arrow next to the *{_}New Java Class{_}*
button  !class_btn.png!  and select the *{_}New Java Interface{_}*   !interface_btn.png! option from the dropdown list. In the dialog
enter "Catalog" as the *{_}Name{_}* of the interface and select the Finish button to complete the dialog.
The Java editor will open on the new created Java interface. Replace the content of the editor by
*{_}copy-paste{_}* of the following Java interface code snippet.
{code}
package services;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface Catalog {
    String[] get();
}
{code}
Select the "services" package again. Select the *{_}New Java Class{_}* button !class_btn.png! . In the dialog enter
"CatalogImpl" as the *{_}Name{_}* of the class, add "Catalog" as the interface this class implements, and
then select *{_}Finish{_}* to complete the dialog.

The Java editor will open on the new created Java class. Replace the content of the editor by
*{_}copy-paste{_}* of the following Java class code snippet.
{code}
package services;
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;
	}
}
{code}
After completing these steps the content of the "store" project will look as follows.
!store_project.png!
&nbsp;
*Note:* CatalogImpl is red x'ed because it makes use of the CurrencyConverter interface that we
have not implemented yet.
&nbsp;

h3. _CurrencyConverter_

In this step you create the CurrencyConverter service interface and implementation.
You follow the same steps that you learned previously to create the interface and implementation.
First create a Java interface in the "services" package named "CurrencyConverter" and *{_}copy-paste{_}*
the following Java interface code snippet into it.
&nbsp;&nbsp;
{code}
package services;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface CurrencyConverter {
	public float getConversion(String fromCurrenycCode,
	String toCurrencyCode, float amount);
	public String getCurrencySymbol(String currencyCode);
}
{code}
Next create a Java class in the "services" package named "CurrencyConverterImpl" and *{_}copy-paste{_}*
the following Java class code snippet into it.
{code}
package services;
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 "?";
	}
}
{code}
After completing these steps the content of the "store" project will look as follows.

!store_project_1.png!

h3. _ShoppingCart_

In this step you create the Item model object, the Cart and Total service interfaces and the ShoppingCart service implementation.
You follow the same steps that you learned previously to create the interface and implementation.

Create a Java class in the "services" package named "Item" and *{_}copy-paste{_}* the
following code snippet into it.

{code}
package services;

public class Item {
    private String name;
    private String price;
    
    public Item() {
    }
    public Item(String name, String price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPrice() {
        return price;
    }   
    public void setPrice(String price) {
        this.price = price;
    }
}
{code}

Create a Java interface in the "services" package named "Cart" and *{_}copy-paste{_}* the
following code snippet into it.
{code}
package services;

import org.apache.tuscany.sca.data.collection.Collection;
import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Cart extends Collection<String, Item> {

}
{code}

Create a Java interface in the "services" package named "Total" and *{_}copy-paste{_}* the
following code snippet into it.

{code}
package services;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface Total {
    String getTotal();
}
{code}

Create a Java class in the "services" package named "ShoppingCartImpl" and *{_}copy-paste{_}* the
following Java class code snippet into it.
{code}
package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.NotFoundException;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Scope;

@Scope("COMPOSITE")
public class ShoppingCartImpl implements Cart, Total {
    
    private Map<String, Item> cart;
    
    @Init
    protected void init() {
        cart = new HashMap<String, Item>();
    }

    public Entry<String, Item>[] getAll() {
        Entry<String, Item>[] entries = new Entry[cart.size()];
        int i = 0;
        for (Map.Entry<String, Item> e: cart.entrySet()) {
            entries[i++] = new Entry<String, Item>(e.getKey(), e.getValue());
        }
        return entries;
    }

    public Item get(String key) throws NotFoundException {
        Item item = cart.get(key);
        if (item == null) {
            throw new NotFoundException(key);
        } else {
            return item;
        }
    }

    public String post(String key, Item item) {
        if (key == null) {
            key ="cart-" + UUID.randomUUID().toString();
        }
        cart.put(key, item);
        return key;
    }

    public void put(String key, Item item) throws NotFoundException {
        if (!cart.containsKey(key)) {
            throw new NotFoundException(key);
        }
        cart.put(key, item);
    }
    
    public void delete(String key) throws NotFoundException {
        if (key == null || key.equals("")) {
            cart.clear();
        } else {
            Item item = cart.remove(key);
            if (item == null)
                throw new NotFoundException(key);
        }
    }

    public Entry<String, Item>[] query(String queryString) {
        List<Entry<String, Item>> entries = new ArrayList<Entry<String,Item>>();
        if (queryString.startsWith("name=")) {
            String name = queryString.substring(5);
            for (Map.Entry<String, Item> e: cart.entrySet()) {
                Item item = e.getValue();
                if (item.getName().equals(name)) {
                    entries.add(new Entry<String, Item>(e.getKey(), e.getValue()));
                }
            }
        }
        return entries.toArray(new Entry[entries.size()]);
    }
    
    public String getTotal() {
        double total = 0;
        String currencySymbol = "";
        if (!cart.isEmpty()) {
            Item item = cart.values().iterator().next();
            currencySymbol = item.getPrice().substring(0, 1);
        }
        for (Item item : cart.values()) {
            total += Double.valueOf(item.getPrice().substring(1));
        }
        return currencySymbol + String.valueOf(total);
    }
}
{code}
*Note:* Since the Tuscany conversational support is not ready yet the cart is realized through a hack.
The cart field is defined as static.

After completing these steps the content of the "store" project will look as follows.

!store_project_2.png!
&nbsp;

h3. *{_}Store{_}*

In this step you create the user facing Store service that will run in a Web browser and provide the
user interface to the other services you created.

Select the "ufservices" package. *{_}Right click{_}* to get the context menu, select *{_}New{_}*, and then *{_}File{_}*. In
the *{_}New File{_}* dialog enter "store.html" for the *{_}File name{_}*, and then select *{_}Finish{_}* to complete the
dialog.

The Text editor will open on the new created html file. Replace the content of the editor by *{_}copy-paste{_}*
of the following html snippet.
{code}
<html>
<head>
<title>Store</TITLE>

<script type="text/javascript" src="store.js"></script>

<script language="JavaScript">

	//@Reference
	var catalog = new Reference("catalog");

	//@Reference
	var shoppingCart = new Reference("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('shoppingCart').innerHTML = "";
		document.getElementById('total').innerHTML = "";
	}

	catalog.get(catalog_getResponse);
	shoppingCart.get("", shoppingCart_getResponse);
</script>

</head>

<body>
<h1>Store</h1>
  <div id="store">
   	<h2>Catalog</h2>
   	<form name="catalogForm">
		<div id="catalog" ></div>
		<br>
		<input type="button" onClick="addToCart()"  value="Add to Cart">
   	</form>

 	<br>

   	<h2>Your Shopping Cart</h2>
   	<form name="shoppingCartForm">
		<div id="shoppingCart"></div>
		<br>
		<div id="total"></div>
		<br>
		<input type="button" onClick="checkoutCart()" value="Checkout">
		<input type="button" onClick="deleteCart()" value="Empty">
	   	<a href="../ShoppingCart/">(feed)</a>
	</form>
  </div>
</body>
</html>
{code}
After completing these steps the content of the "store" project will look as follows.

&nbsp;!store_project_3.jpg!

h2. Compose Services

Now that you have all the required service implementations you compose them together to provide
the store composite service. The composition is stored in a .composite file.

Select the "src" folder of the "store" project. *{_}Right click{_}* to get the context menu, select *{_}New{_}*, and
then *{_}File{_}*. In the *{_}New File{_}* dialog enter "store.composite" for the *{_}File name{_}*, and then select *{_}Finish{_}*
to complete the dialog.

The Text editor will open on the new created composite file. Replace the content of the editor by
*{_}copy-paste{_}* of the following composite snippet.
{code}
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
	xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"
	xmlns:s="http://store"
        targetNamespace="http://store"
	name="store">
	<component name="store">
		<t:implementation.widget location="ufservices/store.html"/>
		<service name="Widget">
			<t:binding.http uri="http://localhost:8080/"/>
		</service>
                <reference name="catalog" target="Catalog">
		 	<t:binding.jsonrpc/>
		 </reference>
		 <reference name="shoppingCart" target="ShoppingCart/Cart">
		 	<t:binding.atom/>
		 </reference>
		 <reference name="shoppingTotal" target="ShoppingCart/Total">
		 	<t:binding.jsonrpc/>
		 </reference>
	</component>
	
	<component name="Catalog">
		<implementation.java class="services.CatalogImpl"/>
		<property name="currencyCode">USD</property>
		<service name="Catalog">
			<t:binding.jsonrpc/>
		</service>
		<reference name="currencyConverter" target="CurrencyConverter"/>
	</component>
	
	<component name="ShoppingCart">
		<implementation.java class="services.ShoppingCartImpl"/>
		<service name="Cart">
			<t:binding.atom uri="/ShoppingCart/Cart"/>
		</service>    	
		<service name="Total">
			<t:binding.jsonrpc/>
		</service>    	
	</component>

	<component name="CurrencyConverter">
		<implementation.java class="services.CurrencyConverterImpl"/>
	</component>
</composite>
{code}
After completing these steps the content of the "store" project will look as follows.

!store_project_4.jpg!
&nbsp;

Congratulations you completed your 1st composite service applications, now its time to take it into
action.

h2. Use Services

In this step you launch and use the store composite service application you created.

First select the "store.composite" file. *{_}Right click{_}* to get the context menu, select *{_}Run As{_}*, and then *{_}Tuscany{_}*. 
The Tuscany runtime will start up adding the store composition to its domain.

The Eclipse console will show the following messages.

&nbsp;!eclipse_console.png!

&nbsp;Next Launch your Web browser and enter the following address:

[http://localhost:8100/store/store.html|http://localhost:8100/store/store.html]&nbsp;

&nbsp;You get to the Store user facing service of the composite service application.

!store_page.png!

You can select items from the Catalog and add them to your Shopping Cart.

*Note:* When adding items for the first time you will be asked for *{_}userid and password{_}* by the
browser. Enter "admin" for both.

 !store_page_1.png!

Since the ShoppingCart service is bound using the ATOM binding, you can also look at the
shopping card content in ATOM feed form by clicking on the feed icon !atom_feed.png!. You get the browsers default rendering for ATOM feeds.

&nbsp;!atom_feeds_page.png!

&nbsp;Use the browser back button to get back to the Store page.

!store_page_2.png!

And then you can Checkout to complete your order.

!store_page_3.png!

---------------------------------------------------------------------
CONFLUENCE INFORMATION
This message is automatically generated by Confluence

Unsubscribe or edit your notifications preferences
   http://cwiki.apache.org/confluence/users/viewnotifications.action

If you think it was sent incorrectly contact one of the administrators
   http://cwiki.apache.org/confluence/administrators.action

If you want more information on Confluence, or have a bug to report see
   http://www.atlassian.com/software/confluence