Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The first thing you do is to create a folder on you disk into which you will download the TUSCANY distribution.
Next you download the latest release distribution. Launch your browser and enter one of the
following URL's.
Latest Release - http://cwiki.apache.org/TUSCANY/sca-java-releases.html
Download both the bin zip as well as the src zip to the folder that you created on your disk. Once
you completed the download you should see the following on your disk.
Image Removed !release_zips.png!Next you unzip the bin zip in place, you should see the following folder file structure on your disk
after unzip is complete.

...

Code Block
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 "?";
	}
}

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

...

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


 

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.

...

Code Block
<html>
	<head>
	<title>Store</title>
	<script type="text/javascript" src="binding-atom.js"></script>
	<script type="text/javascript" src="binding-jsonrpc.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;
			if (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="/ufs/store.html">'+
			document.getElementById('shoppingCart').innerHTML+
			'<br>'+
			document.getElementById('total').innerHTML+
			'<br>'+
			'<br>'+
			'<input type="submit" value="Continue Shopping">'+
			'</form>';
		shoppingCart.delete("", null);
	}
	function deleteCart() {
		shoppingCart.delete("", null);
		document.getElementById('shoppingCart').innerHTML = "";
		document.getElementById('total').innerHTML = "";
	}
	window.onload = function() {
			catalog.get(catalog_getResponse);
			shoppingCart.get("", shoppingCart_getResponse);
		}
	</script>
	<link rel="stylesheet" type="text/css" href="style.css" />
	</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/">
		<img src="http://projects.apache.org/images/blue-icon.png" border="0">
		</a>
	</form>
	</div>
	</body>
</html>

Next select the "ufservices" package again. Right click to get the context menu, select New, and
then File. In the New File dialog enter "binding-jsonrpc.js" for the File name, and then select
Finish to complete the dialog.

The Text editor will open on the new created javascript file. Replace the content of the editor by
copy-paste of the javascript snippet you find here:

http://svn.apache.org/repos/asf/incubator/tuscany/java/sca/modules/binding-jsonrpc/src/main/resources/org/apache/tuscany/sca/binding/jsonrpc/jsonrpc.js
Next select the "ufservices" package again. Right click to get the context menu, select New, and
then File. In the New File dialog enter "binding-atom.js" for the File name, and then select Finish
to complete the dialog.

The Text editor will open on the new created javascript file. Replace the content of the editor by
copy-paste of the following javascript snippet.

Code Block

 function AtomClient(uri) {
	this.uri=uri;
	this.get = function(id, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null)
						responseFunction(xhr.responseXML);
				} else {
				alert("get - Error getting data from the server");
				}
			}
		}
		xhr.open("GET", uri + id, true);
		xhr.send(null);
	}
	this.post = function (entry, responseFunction) {
	var xhr = this.createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 201) {
				if (responseFunction != null)
					responseFunction(xhr.responseXML);
				} else {
					alert("post - Error getting data from the server");
				}
			}
		}
		xhr.open("POST", uri, true);
		xhr.setRequestHeader("Content-Type", "application/atom+xml");
		xhr.send(entry);
	}
	this.put = function (id, entry, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null)
						responseFunction(xhr.responseXML);
				} else {
					alert("put - Error getting data from the server");
				}
			}
		}
		xhr.open("PUT", uri + id, true);
		xhr.setRequestHeader("Content-Type", "application/atom+xml");
		xhr.send(entry);
	}
	this.delete = function (id, responseFunction) {
		var xhr = this.createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (responseFunction != null) responseFunction();
				} else {
					alert("delete - Error getting data from the server");
				}
			}
		}
		xhr.open("DELETE", uri + id, true);
		xhr.send(null);
	}
	this.createXMLHttpRequest = function () {
		try {return new XMLHttpRequest();} catch(e) {}
		try {return new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {}
		try {return new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {}
		alert("XML http request not supported");
		return null;
	}
}

Note: That we have to have the bindig-jsonrpc.js, and binding-atom.js local in our project is only
temporary, so this step will be removed in the future.

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

 Image Added

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 Block

 <?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"
	name="store">
	<component name="ufs">
		<t:implementation.resource location="ufservices"/>
		<service name="Resource">
			<t:binding.http/>
		</service>
	</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="Collection">
			<t:binding.atom/>
		</service>
	</component>
	<component name="CurrencyConverter">
		<implementation.java class="services.CurrencyConverterImpl"/>
	</component>
</composite>

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

Launch Services

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

Select the "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.out.println();
		System.in.read();
		scaDomain.close();
	}
}

 After completing these steps the content of the "store" project will look as follows.Image Added
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/ufs/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

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 Added
 
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 Added
 
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 Added
 
Finish the New Java Project dialog. You now have the sample project available in the Eclipse
workbench.Image Added
 
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.

 Image Added<<Under construction >>