Build your first Web Services with Tuscany (TUSCANY) created by Luciano Resende http://cwiki.apache.org/confluence/display/TUSCANY/Build+your+first+Web+Services+with+Tuscany
Content: --------------------------------------------------------------------- h1. Build your first Web Services with Tuscany This guide will give you step by step instructions on how to build your first web services. The first part, we will learn how we can add the Tuscany Runtime to Eclipse IDE. The second part, will show how easy is to create a webservices using Apache Tuscany. h2. Install the Tuscany Eclipse Plugin The first thing you do is to start Eclipse and go to *Help \-> Software Updates \-> Find and Install*, select "Search for new features to install" and then click next !install_update.jpg! On the next dialog, click on *"New Remote Site..."* to create a new site entry. Give it a name such as "Tuscany" and add the site URL as *http://people.apache.org/~jsdelfino/tuscany/tools/updatesite/* !install_update_new_site.jpg! Make sure the *"Remote Site"* that was just created is selected, and click *"Finish"* !install_update_finish.jpg! Select the *"Apache Tuscany SCA Tools"* and click *"Next"*, and then, on the next dialog, click *"Finish"* !install_update_search_results.jpg! Accept the *"Plugin License"* !install_update_feature_license.jpg! and next click on *"Install All"* !install_update_feature_verification.jpg! When asked to *"restart eclipse"*, click the *"yes"* button. !install_update_restart_eclipse.jpg! 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 "ws" 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 the "helloworld" package folders into which later in this step you place service implementations. Select the "ws" 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 "helloworld" as the package *{_}Name{_}*, and press the *{_}Finish{_}* button to complete the dialog. !new_java_pkg_dlg.png! h3. _HelloWorld_ In this step you create the HelloWorld service interface and implementation. Select the "helloworld" 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 "HelloWorld" 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 helloworld; import org.osoa.sca.annotations.Remotable; @Remotable public interface HelloWorld { String[] sayHello(String name); } {code} Select the "helloworld" package again. Select the *{_}New Java Class{_}* button !class_btn.png! . In the dialog enter "HelloWorldImpl" 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 helloworld; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return "Hello " + name; } } {code} After completing these steps the content of the "ws" project will look as follows. !store_project.png! *Note:* CatalogImpl is red x'ed because it makes use of the CurrencyConverter interface that we have not implemented yet. 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 "ws" project. *{_}Right click{_}* to get the context menu, select *{_}New{_}*, and then *{_}File{_}*. In the *{_}New File{_}* dialog enter "helloworld.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:c="http://helloworld" name="helloworld"> <component name="HelloWorldComponent"> <implementation.java class="helloworld.HelloWorldImpl"/> <service name="HelloWorld"> <binding.ws uri="http://localhost:8080/HelloWorld"/> </service> </component> </composite> {code} After completing these steps the content of the "store" project will look as follows. !store_project_4.jpg! 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 ws composite service application you created. First select the "helloworld.composite" file, in your "ws" project. *{_}Right click{_}* to get the context menu, select *{_}Run As{_}*, and then *{_}Tuscany{_}*. The Tuscany runtime will start up adding the helloworld composition to its domain and will make the helloworld web service live. The Eclipse console will show the following messages. !eclipse_console.png! Next Launch your Web browser and enter the following address: [http://localhost:8080/HelloWorld?wsdl] --------------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
