95-702 Organization Communication and Distributed Object Technologies Homework 3 Using Axis2 for web services Part I. Prerequisite Installations ---------------------------------- 1) Tomcat is installed and is available through Eclipse. 2) The Axis2 Standard Binary Distribution has been installed from the following site: http://ws.apache.org/axis2/download/1_2/download.cgi. 3) The file named "axis2.war" has been placed in Tomcat's directory structure at .../apache-tomcat-6.0.10/webapps/axis2.war. 4) Axis2.war has been expanded into a directory called axis2 at .../apache-tomcat-6.0.10/webapps/axis2. 5) The expansion of axis2.war into the directory axis2 is performed by Tomcat. 6) The axis2 directory contains META-INF, WEB-INF and axis2-web subdirectories. 7) A visit to http://localhost:8080/axis2 shows services, a validation tool and an administration tool. 8) Two axis2 plugins for Eclipse have been installed. These are: The code generator wizard at http://ws.apache.org/axis2/0_94/CodegenTools-EclipsePlugin.html The archiver wizard at http://ws.apache.org/axis2/tools/1_1/eclipse/servicearchiver-plugin.html 9) These wizards are available from within Eclipe with file/new/other/axis2 wizards. Part 2 Writing a simple web service =================================== Start Eclipse. Choose any workspace name. Create a Java Project with separate source and output folders: file/new/project/java project/next/ Project name: HelloWorldWS Create separate source and output folders Finish Create a package called ws.first: file/new/package name: ws.first finish This ws.first should be shown under src. Create Java class called HelloWorldWebService.java. This class will be in the ws.first package: select ws.first file/new/class name: HelloWorldWebService finish Enter the following code in the file HelloWorldWebService.java: package ws.first; public class HelloWorldWebService { public String hello(String name) { return "Hi " + name; } public String goodBye(String name) { return "goodbye " + name; } } Create the services.xml file ---------------------------- Select the project. File/new/file Name: services.xml Use the following code: My Cool Hello world web service ws.first.HelloWorldWebService Placing axis2 jar files in your classpath ========================================= There are two possibilities here. If this is the first time you have done this you need to associate a variable name with all of the axis2 jar files. These instructions follow: If this is the first time do { Select project HelloWorldWS. Right click/ Properties/ Java Build Path. Libraries/add library/user library/next/user libraries/new. Enter the user libray name axis2_lib. Select axis2_lib and select addJars. This is a very tedious process but needs to be done only once. Browse to axis2-1.1.1/lib and select one jar at a time. When two jars have a different version number select only the one with the latest version. The pattern is select axis_lib, click add jar, double click an axis2 jar file, repeat. All the jars should be under the name axis_lib. When complete, choose to export this jar list to the file system. You'll need a file name. When you finish the Java Build Path will contain the JRE System Library and the axis2_lib. Both of these jar lists should appear under your project. } else (you already have performed this activity) do { Right click project / properties Select Libraries/ add library Select user library next/ User Libraries/ import / browse to the old library Your project should have two large lists of jars One from the JRE and the other from Axis2. } Build the archive file to be placed under Tomcat ------------------------------------------------ Select the project. File/New/Other/Axis2 Wizards/Service Archiver/next Class File Location Browse to this project's bin directory Check include class files only select next Select Skip WSDL select next Add libraries? No, just select next. Browse to the services.xml file. Select next. The output file location is under Tomcat. Browse to ...tomcat/webapps/axis2/WEB-INF/services The output file name is HelloWorld Select Finish and the service archive is now behind Tomcat. Tomcat Preparation ================== Window/preferences/tomcat Version 5.x Browse to Tomcat Home (mine is /usr/local/apache-tomcat-6.0.10) Advanced Tomcat Base browse to Tomcat Home Do Not Check add java projects to Tomcat Classpath JVM Settings: Set JRE Source Path: Do Not check add java .... HelloWorldWS Manager: User name and password Run Tomcat ========== Click the cat. No exceptions are thrown. Visit http://localhost:8080/axis2 Look for services: HelloWorld View the wsdl at http://localhost:8080/axis2/services/HelloWorld?wsdl Client Side Coding ================== We'll work within the same project to create a client. First we need to create a stub class. And for fun, we'll generate a wsdl. File/New/Other Axis2 Code Generation wizard Generate WSDL from Java source next. Postpone entering the file name. Select add folder. Browse to this project's bin folder (not Tomcat bin). Enter the qualified class name ws.first.HelloWorldWebService. Click Test Class Loading (this may erase an error message.) Select Next. Take the defaults select Next again. Browse and add wsdl to project on current eclipse workspace. Be sure to actually browse to the workspace and select the project name folder. Hit Finish. File refresh. You should see the wsdl under the project. Generate a stub from the wsdl to be used by a client. Select the project. File/New/Other/Axis2 Codegen Generate Java source code from a WSDL Browse to the wsdl under the project folder Use the defaults select next Select Browse and select a project on current eclipse workspace Select finish Create the client HelloWorldWebServiceClient in the package ws.first package ws.first; import java.rmi.RemoteException; import org.apache.axis2.AxisFault; public class HelloWorldWebServiceClient { public static void main(String[] args) { HelloWorldWebServiceStub stub; try { String c_value = "Mike"; stub = new HelloWorldWebServiceStub ("http://localhost:8080/axis2/services/HelloWorld"); System.out.println("Stub created"); HelloWorldWebServiceStub.Hello x = new HelloWorldWebServiceStub.Hello(); x.setName(c_value); System.out.println("set name called with " + c_value); HelloWorldWebServiceStub.HelloResponse res = stub.hello(x); System.out.println("Returned"); System.out.println("C Value : "+c_value+ "\tResult : " +res.get_return()); } catch (AxisFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } } Run the client. If all goes well you should see Stub created set name called with Mike Returned C Value : Mike Result : Hi Mike