Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Ready, Set, Go - Getting started with Tuscany

This article will show you how to download a Tuscany distribution, add the distribution as a user library in Eclipse, create the Store sample as a Java project, and then run the Store and use it from a web browser.

Info

Although this guide show some images based on the Java SCA 1.3.1 2 release, you should be able to use latest Java SCA release while going to the necessary steps for this guide.

...

The first thing you do is to create a file system folder on you disk into which you will download the TUSCANY distribution.

...

Code Block
<html>
<head>
<title>Store</title>

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

<script language="JavaScript">

	//@Reference
	var catalog = new tuscany.sca.Reference("catalog");
	
	//@Reference
	var shoppingCart = new tuscany.sca.Reference("shoppingCart");

	//@Reference
	var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
	
	var catalogItems;

	function catalog_getResponse(items) {
		var catalog = "";
		for (var i=0; i<items.length; i++) {
			var item = items[i].name + ' - ' + items[i].price;
			catalog += '<input name="items" type="checkbox" value="' + 
						item + '">' + item + ' <br>';
		}
		document.getElementById('catalog').innerHTML=catalog;
		catalogItems = items;
	}
	
	function shoppingCart_getResponse(feed) {
		if (feed != null) {
			var entries = feed.getElementsByTagName("entry");              
			var list = "";
			for (var i=0; i<entries.length; i++) {
				var content = entries[i].getElementsByTagName("content")[0];
				var name = content.getElementsByTagName("name")[0].firstChild.nodeValue;
				var price = content.getElementsByTagName("price")[0].firstChild.nodeValue;
				list += name + ' - ' + price + ' <br>';
			}
			document.getElementById("shoppingCart").innerHTML = list;

			if (entries.length != 0) {			
				shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
			}
		}
	}
	
	function shoppingTotal_getTotalResponse(total) {
		document.getElementById('total').innerHTML = total;
	}
	
	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>item</title><content type="text/xml">' +
                	'<Item xmlns="http://services/">' +
                	'<name xmlns="">' + catalogItems[i].name + '</name>' + '<price xmlns="">' + catalogItems[i].price + '</price>' +
                 	'</Item>' + '</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">'+
					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 = "";	
	}	

	function init() {
		catalog.get(catalog_getResponse);
		shoppingCart.get("", shoppingCart_getResponse);
	}
	
</script>

</head>

<body onload="init()">
<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/Cart/">(feed)</a>
	</form>    
  </div>
</body>
</html>

...

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


 

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

Use Services

Launch Services

In this step you create the code to launch the Tuscany runtime with the new store composite service In this step you launch and use the store composite service application you created.

First select Select the "Launch" class in the "launch" package of your "store" project. Right click to get the
context menu, select Run As, and then Java application. The Tuscany runtime will start up adding
the store composition to its domain.

The Eclipse console will show the following messages.

 Image Removed

 Next Launch your Web browser and enter the following address:

http://localhost:8080/store/store.html 

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

Image Removed

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.

Image Removed

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 Image Removed. You get the browsers default rendering for ATOM feeds.

 Image Removed

 Use the browser back button to get back to the Store page.

Image Removed

And then you can Checkout to complete your order.

Image Removed

Explore the Samples from the Tuscany Distribution

The sample folder of the Tuscany distribution provides a rich set of samples ready for you to explore.

Image Removed
 
In Eclipse create a New Java Project, specify the project name, select Create project from
existing source, and specify the folder that contains the sample source.

Image Removed
 
Use Next to get to the next page in the New Java Project dialog. There go to the Libraries tab, use
the Add Library... pushbutton to add the JUnit library and the user library TUSCANY.

Image Removed
 
Finish the New Java Project dialog. You now have the sample project available in the Eclipse workbench.

Image Removed
 
For the calculator sample that we've chosen go to its CalculatorClient class and select Run As ->
Java Application. You will see the following output in the console.

store" project and click on the New Java Package button Image Added in the toolbar to start the package creation dialog. Use the dialog to create a new package named "launch".

Select the "launch" package. Select the New Java Class button  Image Added . In the dialog enter "Launch" as the Name of the class, check the checkbox for creating a main method stub, 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 Block


package launch;

import org.apache.tuscany.sca.host.embedded.SCADomain;

public class Launch {
    public static void main(String[] args) throws Exception {
        System.out.println("Starting ...");
        SCADomain scaDomain = SCADomain.newInstance("store.composite");
        System.out.println("store.composite ready for big business !!!");
        System.in.read();
        System.out.println("Stopping ...");
        scaDomain.close();
        System.out.println();
    }
}

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

Use Services

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

First select the "Launch" class in the "launch" package of your "store" project. Right click to get the
context menu, select Run As, and then Java application. The Tuscany runtime will start up adding
the store composition to its domain.

The Eclipse console will show the following messages.

 Image Added

 Next Launch your Web browser and enter the following address:

http://localhost:8080/store/store.html 

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

Image Added

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.

Image Added

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 Image Added. You get the browsers default rendering for ATOM feeds.

 Image Added

 Use the browser back button to get back to the Store page.

Image Added

And then you can Checkout to complete your order.

Image Added

Congratulations, you have created an accessible, flexible, reusable Store application using Tuscany SCA technology. Image Removed