email:
website: http://www.siongboon.com
Step by Step Explaination
|
Steps to do |
| Developing Java & Flex 3
webservice application What you need? - Eclipse - JDK (Java development kit required by Tomcat) - Apache Tomcat (free open source server) - Axis (protocol engine for tomcat server) - Flex builder 3 Optional - Apache Tomcat plugin for Eclipse IDE - Flex builder 3 plugin for Eclipse IDE |
Software download link (you should
install them in sequence) Reference guide to install Tomcat. 4) Apache Axis2, http://ws.apache.org/axis2/ (Download: axis2.war) Reference guide to install Axis2 to Tomcat. Here is a link to a SmsService.aar file (a working archive of a sms webservice to help you test your axis environment) The following service can be seen on the browser indicating that the environment is properly setup to host the webservice. SmsService Service EPR : http://localhost:8080/axis2/services/SmsService Service Description : Please Type your service description here Service Status : Active - sendSms
5) Adobe Flex 3 builder, http://www.adobe.com/products/flex/features/flex_builder/ Other Reference http://www.vogella.de/articles/Webservice/article.html Reading Reference
|
| Webservice programming with
Apache Axis2 & Eclipse IDE A webservice is similar to a function/method call. If you are familiar with programming, function/method should be a very familiar term to you. A function is like a black box where we call it to execute some process. Sometimes we may need to pass some information to this black box. At the end of the process, we expect some return result or a process to be executed. Functions helps to organise or consolidate our codes. Design functions so that they can reuse in future. As a programmer, we should have a lazy mindset. Lazy not for now nor the past. You want a lazy life for the future. Design and artitacte your codes in a way, such that your programming effort become lesser and lesser as you progress. Programming should be getting easier & easier to write as your codes gets larger. If you are not experiencing that, it just means that there is more that needs to be understood about the concept of programming. Nowadays the codes seems so easy to write. For a line of codes, you can get a pop up window to do all the work for you. In fact, all the hardwork is already done for us, through the years of collective experience & software evolution. They have written libraries of functions that are so easy to use by many programmer. The progress is good because programming is getting less and less effort. Let us go back to webservice again. What is the difference between Webservice and function/method call? Webservice is located at a remote server. Meaning, it is a function call to a remote server. The remote server provide functions for other devices on the network. We call them services. There are many protocol standards that are design to provide such remote services (function call) across the network. One of them is call CORBA which I am aware of. There are many many more... which I do not know. The most important benefit of using webservice technology as compare to other protocols is that it is in a readable form. The protocol is formated using ascii coded XML tags. The XML format allows developers from various operating system platform or programming languages to be able to connect the shared services, quick and easy. The protocol itself is self documentated, meaning that any developer can easily read and use the webservice for their own implementation. This is similar to the HTML tags that we have for accessing and displaying our web content. Evolving and becoming popular worldwide in such a short period of time. In a summary, webservice is a function call from remote server using ascii texts in the format of XML tag. Apache Axis2 provides the open source library/method that implement the standard webservice. It is a ready written engine, a servlet application running under Tomcat server. Webservices written in module by module will be hosted under Axis 2 servlet. The following sections will demonstrate a short example to illustrate the process of building a webservice. |
|
1) Setup Axis in
Eclipse. Axis
plugin is already built into Eclipse by default, but a bit of
configuration
is still required. The axis2 module will helps to automate files
generation that are
required
by the apache axis2 to run webservice.![]() |
1) Go to Apache
website & download the apache
axis2 Standard Binary Distribution and unzip this axis2 package. . 2) Next, you got to tell eclipse where is this axis2 installed. This will tell eclipse where your axis2 is located. |
| 2) Open a new webservice project. Eclipse is a wonderful IDE (integrated development environment) and is built in with features that support webservice development work. In order for eclipse to help us automate some of the webservice setup files, we will have to open a project type "Dynamic Web Project". | 1) Go to menu File>>New>>Others...>>Web>>Dynamic Web Project 2) Please ensure the following configuration. Go to the menu Window>Preferences>Web Services>Server and Runtime " You may also like to set the default server run for your project. |
| 3) Write a new java class using the Eclipse IDE. This will be the class providing the webservice methods to be invoked over the internet. doBasicStuff will be the webservice method. | 1) To create the interface class for our weservice, right click on the project>>New>>Classpublic class DoesMagic |
| 4) Generate the folder/files for implementing the axis2 webservice. Eclipse will help us generate the files structure to deploy a webservice running on Axis2. Axis2 is a opensource java servlet program that process webservice protocol. Axis2 is run under Tomcat server. | 1) Right click the new java class file DoesMagic.java>Webservices>Create Webservice Please take note that generated webservice directory may not have overwritten the previous/old webservice content. Another important file is also generated "services.xml" by the eclipse in the process. It is located in "projectNameXXX\WebContent\WEB-INF\services\serviceNameXXX\META-INF\services.xml". This file describe about the webservice interface and is important. Axis2 need this file in order to generate its *.wsdl file. *.wsdl file provides the details (xml format) for developer to interface to this webservice. Developer from various development platform can use this *.wsdl file to automatically generate function/method calls to invoke to this webservice easily. If you are building the webservice project from Ant build, remember to backup this services.xml file and include it in your *.aar package before you deploy as a service in axis2. If you are not using Ant, then you do not need to worry too much. 2) Now that the webservice structure is generated, |
| 5) Run the Tomcat
server. This will start up all servlets install in the Tomcat server. Axis2 will run, waiting for incoming webservice request. For this project example, our service name is "DoesMagic", method to invoke will be "doBasicStuff". The keyword for the data parameter/input of this webservice method will be "message". |
|
6) The advantage of working with webservices is that the methods are self documented. The methods and the type of parameters required for any webservice will be provided. This information or documentation is found in the *.wsdl file. The *.wsdl file contains all the methods (doBasicStuff) available on this webservice (DoesMagic). It even provides detail on the parameters (message) required by the method (doBasicStuff). The wsdl file though provides the webservice details but the real context of any complex input parameters will not be available. It will not tell you what the webservice is going to do when you call the method with its parameters. Therefore when we provide our webservice, it is necessary to design simple methods with meaningful names. This is the art of doing programming, and it can be abstract. I always have problem with this, and is still learning the art. This *.wsdl file is important. With this file, your IDE is about to help you generate the methods/functions call available for your use in your client program. You might be using other programming language (for example C#, Visual C++), your own IDE should be able to help you generate the codes for used when coding your client program. This is possible because the *.wsdl contains all the information on how to invoke the webservice. For example, I wrote the DoesMagic webservice using Java. You may be writing in C# wanting to invoke my webservice. In order for you to start using my webservice, I will need to give you my webservice address. From the address, you can querry the webservice *.wsdl file to learn about what the webservice offers. The services offered by the webservice is now known. You can invoke directly by typing directly through the browser. Or you can ask your C# IDE to help you auto-generate the method interfaces; given the *.wsdl file. With this interface, your C# program will be able to invoke the webservice as if you are invoking any function call within the C#. In your programming codes, it may seems like calling a typical function, but it is calling a webservice.
|
1) To see the wsdl info: From your web
browser (Key in
the following web address), A short explaination, "http://localhost:8080/", is the place where your tomcat server is located. "axis2/", is the axis2 servlet running on the tomcat. "services/", services directory will contain all the webservices currently running under your axis2. "DoesMagic", is the webservice in this example. "DoesMagic?wsdl", is the instruction given to the DoesMagic webservice, to querry about the webservice's methods and other details. The querry will respond back with the details in xml format to tell us more about this webservice (DoesMagic) interface.
2) After the DoesMagic?wsdl querry, you should be able to see the following response. These are the information regarding DoesMagic webservice.
|
| 7) Axis2 has a service running to help us to manage all its webservices. The services are listed, and you can also start & stop individual services, just like servlets running under Tomcat. You may need to manage the services running using axis2 with some of the classes like ServiceLifeCycle and Lifecycle, which are not covered here. | 1) To see the services running in the Axis2 servlet: From web
browser (Key in
the following web address),
|
| 8) Now we will test if our webservice DoesMagic works in our Tomcat server. | 1) To test the webservice DoesMagic created From web
browser (Key in
the following for the webservice through http request), A short explaination, "http://localhost:8080/", is the place where your tomcat server is located. "axis2/", is the axis2 servlet running on the tomcat. "services/", services directory will contain all the webservices currently running under your axis2. "DoesMagic", is the webservice name. "doBasicStuff", is a method from the webservice DoesMagic. "?message=123", is the input parameter required by the doBasicStuff method. The keyword "message" must be the same as what is defined in the webservice.
|
Another example of webservice with multiple parameters:
From web
browser (Key in
the following for the webservice through http request), The parameters pass to the webservice are seperated by the '&' delimiter. |
|
| I have this idea to write a standard function call like "help" for each and every webservice. It will be used to help user to learn using the webservice, and it can also have simple example to help test/troubleshoot the service. I think this additional service is will be helpful although the wsdl file is already available. The wsdl file is technically still quite difficult to completely understand the context of the webservice in used; additional help information can be very helpful. |
| Flex 3 client access to
webservice Now that the webservice is properly setup, I am going to create a flex project to allow the client to access the webservice that we have created. |
|
| 1) Install Flex
builder 3 development software. |
|
| 2) Create a new flex project.
Under the menu File>New>Flex
project. |
|
3) Get the flex builder to generate the functions for the access to our DoesMagic webservice. Under the menu, select
Data>Import Webservice (WSDL)...> Alternative method to invoke the webservice will be to use the dynamic webservice call approach. Methods are manually defined instead of auto generated through *.wsdl file. Here is the example "HomeAutomationRpcClient.java" |
|
| 4) The example on the right demonstrate how the actionscript in a flex project access the webservice after the webservice actionscript has been auto generated programmer use. The actionscript is accessing the class's method DoesMagic.doBasicStuff, which is the webservice that we have created earlier. | Example of how
actionscript access
the webservice: <?xml version="1.0" encoding="utf-8"?> |
| 5) Although the
flex project is able to run properly in the flex builder IDE, it does
mean it can be deploy properly. There will be still many things to
troubleshoot. Next we will generate the html & flex necessary for
our web deployment. |
|
| 6) Deploy the flex
project to
the webserver choose File>Export>Release Build... Project: "your flex project name" Application: "your main *.mxml file" Export to folder: "bin-release" A new bin-release will be generated. Copy the files in the folder into the server's html hosting directory. You can test if the build is working by clicking on it's html file. If you receive an error message stating that there is a security setting violation or that the application can’t access a resource or asset, then you will need to add a compiler setting that will allow you to access resources from the local file system. Use the following steps to add the compiler setting. In Flex Builder, right click on the flex project and select Properties from the menu. • Select the Flex Compiler menu. • Add a space and then -use-network=false to the complier arguments. Redo the build process and test again. |
|
| 7) Copying the web
files to
the following Tomcat web hosting directory. C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\StarhomeWidget\pcadmin\Main.html |
Access webservice using Java though RPC call method There are many ways to access webservice. RPC is one of the methods. A Java example of RPC call is on the right. |
DoorWsClient.java |
| Deploy the html
files to the WAMPSERVER. WAMPSERVER is typically used for hosting static HTML pages for port80. It also has a php (script) engine running. This is unlike a Tomcat server where it is used for hosting static webpage & servlet. WAMPSERVER can be deployed as the front end server, and be used to conceal link/mapped to other server service accessible from other communication port. The root directory for the web hosting is located at D:\wamp\www\index.html When we access a web domain for example http://starhome.i2r.a-star.edu.sg/, the index.html file will be loaded. The default file to load can be configured in the following file, D:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf <Directory "D:/wamp/www/"> </Directory> <IfModule dir_module> DirectoryIndex index.html index.htm index.php index.php3 </IfModule> These define the root directory and also the default file to load if it is not specified. For website, the default file is usually index.html |
|
| Setting up LCDS (LiveCycle Data
Services) for Flex project 1) copy "ds-console.war" & "lcds.war" to C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ 2) Execute C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\tomcat7.exe Project folders will be setup under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ |
- ds-console.war - lcds.war |
Other references on webservice:
- http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaWebServices.html (NTU website on webservice), Java Web Services (by Chua Hock Chuan).pdf