Server-side Development with Axis
Chapter 8 • Web
Services 291
The code description is as follows: Line 1: Java package where the interface class is located.
Line 2: Exception RemoteException may be thrown by the Web service methods. Lines 7–8: Method getPriceForecast takes one input argument, which is a Java
Bean used to transport information to and from the Web service. The method return type is void
, which implies that any result values will be returned in the args parameter. Lines 10–11: Method getRecommendation signature, defined similarly as for the
method getPriceForecast.
Web interaction Gathering
stock information
Pattern learning recognition
e.g., neural network stock_analyst
interact
Internet database
a
analyze collect
Internet
b
Web service proxies stubskeleton
interact
ParamsBean – parameters_ : HashMapString, Object
+ addParamString, Object + getParamString : Object
+ getParams : String, Object
ws Generated by Axis
WSDL2Java
ForecastServer + getPriceForecastargs : ParamsBean
+ getRecommendationargs : ParamsBean
ForecastServerImpl numOfTrials_ : long
maxNumOfTrials_ : long + getPriceForecastargs : ParamsBean
+ getRecommendationargs : ParamsBean
Internet
b
Web service proxies stubskeleton
interact
ParamsBean – parameters_ : HashMapString, Object
+ addParamString, Object + getParamString : Object
+ getParams : String, Object ParamsBean
– parameters_ : HashMapString, Object + addParamString, Object
+ getParamString : Object + getParams : String, Object
ws Generated by Axis
WSDL2Java
ForecastServer + getPriceForecastargs : ParamsBean
+ getRecommendationargs : ParamsBean ForecastServer
+ getPriceForecastargs : ParamsBean + getRecommendationargs : ParamsBean
ForecastServerImpl numOfTrials_ : long
maxNumOfTrials_ : long + getPriceForecastargs : ParamsBean
+ getRecommendationargs : ParamsBean ForecastServerImpl
numOfTrials_ : long maxNumOfTrials_ : long
+ getPriceForecastargs : ParamsBean + getRecommendationargs : ParamsBean
Figure 8-18: a UML package diagram for the example application. b Web-service related classes.
Ivan Marsic • Rutgers University 292
Step 2: Java2WSDL – Generate a WSDL document from the given stock-forecasting Java interface
Now that we defined the Java interface for the stock-forecasting service, it is time to generate a WSDL Web Service Definition Language file, which will describe our web service in a
standard XML format. For this, we will use an Apache Axis command line tool Java2WSDL. A detailed documentation on Java2WSDL, its usage and parameters can be found at the Axis
website.
java org.apache.axis.wsdl.Java2WSDL -o wsdlinteract.wsdl
-l http:localhost:8080axisservicesinteract -n urn:interact
-p stock_analyst.interact urn:interact stock_analyst.interact.ForecastServer
Java2WSDL tool will generate a standard WSDL file, which is an XML representation of a given interface ForecastServer.java in our case. We tell the program the information that it
needs to know as it builds the file, such as:
• Name and location of output WSDL file -o wsdlinteract.wsdl • Location URL of the Web service
-l http:localhost:8080axisservicesinteract • Target namespace for the WSDL -n urn:interact
• Map Java package to namespace stock_analyst.interact → urn:interact
get price forecast
getPriceForecast Delphi method facilitator
: Service Requestor : FcastSoapBindingImpl
initialize analyst
: ForecastServerImpl data tracking
and machine learning : Backend
http : Transport
http : Transport
wsdlinteract.wsdl : WSDL
: FcastSoapBindingStub create
obtain publish
find
do forecast
2 3
discovery agency : UDDI, WSIL
getPriceForecast
4 1
Figure 8-19: The steps in Apache Axis for establishing the interconnections of a Web service, overlaid on Figure 8-2. Note that the discovery agencyUDDI is not implemented.
Chapter 8 • Web Services
293
• The fully qualified Java interface of the Web service itself stock_analyst.interact.ForecastServer
Step 3: WSDL2Java – Generate server-side wrapper code
This step generates code for both server and client side, as seen below. Our next step is to take the WSDL, synthesized in step 2, and generate all of the glue code for
deploying the service. The WSDL2Java Axis tool comes to our aid here. Complete documentation on this tool can be found at the Axis website.
java org.apache.axis.wsdl.WSDL2Java -o src
-d Session -s
-p stock_analyst.interact.ws wsdlinteract.wsdl
Once again, we need to tell our tool some information for it to proceed: • Base output directory -o src
• Scope of deployment Application, Request, or Session • Turn on server-side generation -s — we would not do this if we were accessing an
external Web service, as we would then just need the client stub • Package to place code -p stock_analyst.interact.ws
• Name of WSDL file used to generate all this code wsdlinteract.wsdl For separating the automatically generated code from the original code, we store it a new Web
service package “stock_analyst.interact.ws”, shown in Figure 8-18b. After running the WSDL2Java code generator, we get the following files under
srcstock_analystinteractws
: • FcastSoapBindingImpl.java
This is the implementation code for our web service. We will need to edit it, to connect it to our existing ForecastServerImpl see Step 4 below.
• ForecastServer.java This is a remote interface to the stock forecasting system extends Remote, and methods
from the original ForecastServer.java throw RemoteExceptions. • ForecastServerService.java
Service interface of the Web services. • ForecastServerServiceLocator.java
A helper factory for retrieving a handle to the service. • FcastSoapBindingStub.java
Client-side stub code that encapsulates client access.
Ivan Marsic • Rutgers University 294
• ParamsBean.java A copy of our bean used to transfer data.
• deploy.wsdd Deployment descriptor that we pass to Axis system to deploy these Web services.
• undeploy.wsdd Deployment descriptor that will un-deploy the Web services from the Axis system.
As seen above, some of these files belong to the client side and will be used in Section 8.5.2 below.
Step 4: Tune-up – Modify FcastSoapBindingImpl.java to call server implementation code
We need to tweak one of the output source files to tie the web service to our implementation code ForecastServerImpl.java. Since we passed a mere interface to the Java2WSDL tool,
the generated code has left out the implementation. We need to fill out the methods to delegate the work to our implementation object ForecastServerImpl.
FcastSoapBindingImpl.java is waiting for us to add the stuff into the methods that it
created. The lines that should be added are highlighted in boldface in Listing 8-5:
Listing 8-5: FcastSoapBindingImpl.java – Java code automatically generated by Axis, with the manually added modifications highlighted in boldface.
1 package stock_analyst.interact.ws; 2
3
import stock_analyst.interact.ForecastServerImpl;
4 5 public class FcastSoapBindingImpl implements
5a stock_analyst.interact.ForecastServer { 6
7
ForecastServerImpl analyst;
8 9
public FcastSoapBindingImpl throws java.rmi.RemoteException {
10 analyst = new ForecastServerImpl;
11
}
12 13 public void getPriceForecast
14 stock_analyst.interact.ParamsBean inout0 15 throws java.rmi.RemoteException {
16
return analyst.getPriceForecast inout0 ;
17 } 18
19 public void getRecommendation 20 stock_analyst.interact.ParamsBean inout0
21 throws java.rmi.RemoteException { 22
return analyst.getRecommendation inout0 ;
23 } 24 }
Chapter 8 • Web Services
295
Step 5: Compile and deploy
We first need to compile and compress our newly generated Web service, including both the original code and the automatically generated Web service code:
javac -d bin srcstock_analyst.interact.ws.java
cd bin
jar -cvf ..stock_analyst.jar
cd .. Finally, we copy the JAR file into Tomcat library path visible by Axis and deploy it:
cp stock_analyst.jar TOMCAT_HOMEwebappsaxisWEB-INFlib
--reply=yes
java org.apache.axis.client.AdminClient
-l http:localhost:8080axisservicesAdminService srcstock_analystinteractwsdeploy.wsdd
Admin client is yet another command line tool provided by Apache Axis, which we can use to do tasks such as deployment, un-deployment, and listing the current deployments. We pass the
deployment descriptor to this program so it can do its work.
Now our Stock Forecasting Web Service is up and running on the server.