Title: Handling Web Services with Apache AXIS
1Handling Web Services with Apache AXIS
2Web-Service Review
3POST /wsprimegenerator.exe/soap/IPrimeGenerator
HTTP/1.0 Content-Type text/xml
charsetutf-8 Host www.jusufdarmawan.com SOAPActi
on "urnUnitIPrimeGenerator-IPrimeGeneratorprime
generator" Content-Length 527 lt?xml
version"1.0" encoding"UTF-8"?gt ltsoapenvEnvelope
xmlnssoapenv"http//schemas.xmlsoap.org/s
oap/envelope/" xmlnsxsd"http//www.w3.org/2
001/XMLSchema" xmlnsxsi"http//www.w3.org/2
001/XMLSchema-instance"gt ltsoapenvBodygt
ltns1primegenerator soapenvencodingStyle
"http//schemas.xmlsoap.org/soap/encoding/"
xmlnsns1"urnUnitIPrimeGenerator-IPrim
eGenerator"gt ltvalstart
xsitype"xsdstring"gt12lt/valstartgt
ltvalend xsitype"xsdstring"gt120lt/valendgt
lt/ns1primegeneratorgt lt/soapenvBodygt lt/soape
nvEnvelopegt
A request to www.jusufdarmawan.com
4HTTP/1.0 200 OK Date Mon, 09 May 2005 205835
GMT Content-Length 619 Content-Type
text/xml Server Microsoft-IIS/5.0 X-Powered-By
ASP.NET lt?xml version"1.0" encoding'UTF-8'?gt
ltSOAP-ENVEnvelope xmlnsSOAP-
ENV"http//schemas.xmlsoap.org/soap/envelope/"
xmlnsxsd"http//www.w3.org/2001/XMLSchema
" xmlnsxsi"http//www.w3.org/2001/XMLSche
ma-instance" xmlnsSOAP-ENC"http//schemas
.xmlsoap.org/soap/encoding/"gt
ltSOAP-ENVBodygt ltNS1primegeneratorRespons
e xmlnsNS1"urnUnitIPrimeGenerator
-IPrimeGenerator"
SOAP-ENVencodingStyle"http//schemas.xmlsoap.org
/soap/encoding/"gt ltreturn
xsitype"xsdstring"gt13,17,19,23,29,31, 37,
41,43, 47, 53,59,61,67,71,73,7
9,83,89,97,101,103,107,109,113lt/returngt
lt/NS1primegeneratorResponsegt
lt/SOAP-ENVBodygt lt/SOAP-ENVEnvelopegt
The Response
5POST /soap HTTP/1.0 SOAPAction
"" Content-Length 520 lt?xml version"1.0"
encoding"UTF-8"?gt ltsoapenvEnvelope
xmlnssoapenv"http//schemas.xmlsoap.org/soap/e
nvelope/" xmlnsxsd"http//www.w3.o
rg/2001/XMLSchema"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce"gt ltsoapenvBodygt ltns1getRate
soapenvencodingStyle"http//schemas.xmlsoap.o
rg/soap/encoding/" xmlnsns1"urnxmethod
s-CurrencyExchange"gt ltcountry1
xsitype"xsdstring"gtEurolt/country1gt
ltcountry2 xsitype"xsdstring"gtIsraellt/country2gt
lt/ns1getRategt lt/soapenvBodygt lt/soapenvEnvel
opegt
A request to services.xmethods.net
6HTTP/1.0 200 OK Date Sat, 07 May 2005 232621
GMT Content-Length 492 Content-Type
text/xml lt?xml version'1.0' encoding'UTF-8'?gt lt
soapEnvelope xmlnssoap'http//schemas.xml
soap.org/soap/envelope/'
xmlnsxsi'http//www.w3.org/2001/XMLSchema-instan
ce' xmlnsxsd'http//www.w3.org/2001/XMLS
chema' xmlnssoapenc'http//schemas.xmlsoap
.org/soap/encoding/' soapencodingStyle'htt
p//schemas.xmlsoap.org/soap/encoding/'gt
ltsoapBodygt ltngetRateResponse
xmlnsn'urnxmethods-CurrencyExchange'gt
ltResult xsitype'xsdfloat'gt5.5825lt/Res
ultgt lt/ngetRateResponsegt
lt/soapBodygt lt/soapEnvelopegt
The Response
7A WSDL Example
lt?xml version"1.0"?gt ltdefinitions
name"CurrencyExchangeService"
targetNamespace"http//www.xmethods.net/sd/Curren
cyExchangeService.wsdl" xmlnstns"http//www.x
methods.net/sd/CurrencyExchangeService.wsdl"
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
xmlnssoap"http//schemas.xmlsoap.org/wsdl/soap/"
xmlns"http//schemas.xmlsoap.org/wsdl/"gt
ltmessage name"getRateRequest"gt ltpart
name"country1" type"xsdstring"/gt ltpart
name"country2" type"xsdstring"/gt
lt/messagegt ltmessage name"getRateResponse"gt
ltpart name"Result" type"xsdfloat"/gt
lt/messagegt ltportType name"CurrencyExchangePort
Type"gt ltoperation name"getRate"gt
ltinput message"tnsgetRateRequest"
name"getRate"/gt ltoutput
message"tnsgetRateResponse" name"getRateRespo
nse"/gt lt/operationgtlt/portTypegt
8ltbinding name"CurrencyExchangeBinding"
type"tnsCurrencyExchangePortType"gt
ltsoapbinding style"rpc" transport"http//schema
s.xmlsoap.org/soap/http"/gt ltoperation
name"getRate"gt ltsoapoperation
soapAction""/gt ltinput name"getRate"gt
ltsoapbody use"encoded" namespace"urnxmethods-
CurrencyExchange"
encodingStyle"http//schemas.xmlsoap.org/soap/enc
oding/"/gt lt/inputgt ltoutput
name"getRateResponse"gt ltsoapbody
use"encoded" namespace"urnxmethods-CurrencyExch
ange" encodingStyle"http//schemas.
xmlsoap.org/soap/encoding/"/gt lt/outputgt
lt/operationgt lt/bindinggt ltservice
name"CurrencyExchangeService"gt
ltdocumentationgtReturns the exchange rate between
the two currencieslt/documentationgt ltport
name"CurrencyExchangePort" binding"tnsCurrencyE
xchangeBinding"gt ltsoapaddress
location"http//services.xmethods.net80/soap"/gt
lt/portgt lt/servicegt lt/definitionsgt
9WSDL Elements
Service
Port
Binding
Input
Output
Port Type
Operation
10Apache EXtensible Interaction System(Axis)
11What is AXIS
- Axis is essentially a SOAP engine a framework
for constructing SOAP processors - clients side
- server side
- Axis implements the interfaces of JAX-RPC
(XML-based remote procedure calls in Java) - AXIS Apache Extensible Interaction System
12Remote Method Invocation is not a New Idea
- java.rmi has been in Java since Javas early
versions - In Java RMI, objects can invoke methods of
objects that reside on a remote computer - (RMI Remote Method Invocation)
- So, what has been changed?
- Using HTTP for communication
- Using agreed protocols, Java can invoke methods
that were not written in Java (e.g., .NET
methods) and vice versa - A complex registry procedure has been required in
RMI
13AXIS Includes
- Axis package includes
- a simple stand-alone server
- a server which plugs into servlet engines such as
Tomcat - tools for Web service invocation
- extensive support for the Web Service Description
Language (WSDL) - emitter tooling that generates Java classes from
WSDL - and more
14What We Would Like to Create
- Client applications applications that can call a
remote Web service - Services methods that can be called by remote
applications - Service descriptions WSDL files that describe
our methods
15Client Applications
16Calling Web Services
- By now, we already know how to invoke a remote
Web service in Java - Open a socket to the remote server
- Through the socket, send a SOAP request wrapped
by a HTTP request - Parse the response (e.g., using SAX/DOM)
- However, this approach is cumbersome, and most of
it can be automated
17Invoking Services with Apache
- Axis includes comfortable tools for managing
calls to Web services - The programmer configures the request for the Web
service using a friendly API - with, or without considering the WSDL
- According to the configuration, a method
invocation is transformed into a SOAP request
that is sent to the remote server
18Invoking Services with Axis
- To invoke an operation of a Web service, do the
following - Construct a Service instance
- Using the Service instance, generate a Call
instance - Configure the Call instance
- Invoke the call
19Example Currency Exchange
import org.apache.axis.client. import
javax.xml.namespace.QName import
javax.xml.rpc.ParameterMode import
javax.xml.rpc.encoding.XMLType public class
CurrencyExchange1 public static void
main(String args) throws Exception
System.setProperty("http.proxyHost","wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort","
8080") String endpointUrl
"http//services.xmethods.net80/soap" String
nsuri "urnxmethods-CurrencyExchange"
Service service new Service() Call call
(Call) service.createCall()
20Example Currency Exchange (cont)
call.setTargetEndpointAddress(endpointUrl)
call.setOperationName(new QName(nsuri,"getRate
")) call.addParameter("country1",
XMLType.SOAP_STRING,
ParameterMode.IN)
call.addParameter("country2", XMLType.SOAP_STRING,
ParameterMode.IN) call.setReturnType(XMLTyp
e.SOAP_FLOAT) Object ret
call.invoke(new Object "Euro","Israel")
System.out.println(ret)
21Using the WSDL
- Axis can read a given WSDL and configure the
service as much as possible from that WSDL - Instead of using the default constructor,
construct the service using the following
constructor - Service(String wsdlLocation, QName serviceName)
- Also, supply the port to get a prefilled Call
object - In this approach, you usually need to know the
following about the service - the WSDL URL, the service name, the namespace
uri, the operation name and port and the expected
arguments
22public class CurrencyExchange2 public static
void main(String args) throws Exception
System.setProperty("http.proxyHost","wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort",
"8080") String wsdl "http//www.xmethods.n
et/sd/2001/CurrencyExchangeService.wsdl"
String nsuri "http//www.xmethods.net/sd/Currenc
yExchangeService.wsdl" String sName
"CurrencyExchangeService", String oName
"getRate" Service service new
Service(wsdl, new QName(nsuri,sName))
String port service.getPorts().next().toString()
Call call (Call)service.createCall
(new QName(nsuri,port), new QName(nsuri,oName))
System.out.println(call.invoke(new Object
"UK","Israel"))
23The WSDL2Java Application
- Axis provides a mechanism for communicating with
a Web service using stubs - That is, generation of regular Java classes that
have an interface similar to that of the Web
service and implementation that wraps Web service
management - Invoke class WSDL2Java in order to create the
required Java classes
24An Example
25Using The Generated Classes
import net.xmethods.www.sd.CurrencyExchangeService
_wsdl. public class CurrencyExchange3
public static void main(String args) throws
Exception CurrencyExchangeService ce new
CurrencyExchangeServiceLocator()
CurrencyExchangePortType cep
ce.getCurrencyExchangePort() float rate
cep.getRate("USA","Israel")
System.out.println("Rate " rate)
26Generated Classes
- WSDL2Java generates the following
- A service interface and a service implementation
(locator) for each service - A stub class for each binding
- An interface for each port type
- This interface contains methods that correspond
to the operations of the port type - A class for each complex type
27Server Applications
28AXIS Installation
- Axis works inside a Servlet container (e.g.,
Tomcat) - You should add to your Tomcat libs some jar
files axis.jar, saaj.jar, wsdl4j.jar, - You need to include several jar files in your
CLASSPATH - This has already been done for you!
- The needed CLASSPATH definitions where added to
dbi.cshrc - The needed jar files are in CATALINA_BASE/shared/
lib/
29AXIS Installation (cont)
- You need to copy the Axis application to your
Tomcat's application directory - cp -r dbi/tomcat/axis/ CATALINA_BASE/webapps
CATALINA_BASE
webapps
shared
myapplication
axis
lib
classes
30Creating a Web Service
- Next, we will see how we can create and publish a
Web service using the Axis plugin in Tomcat
311. Generate the Implementing Class
package myws public class Power
public int power(int a, int n)
return (int)Math.pow(a,n)
CATALINA_BASE/webapps/axis/classes/myws/Power.cla
ss
322. Deploy the Service using Web Service
Deployment Descriptor
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org/a
xis/wsdd/providers/java"gt ltservice
name"mypower" provider"javaRPC"gt
ltparameter name"className" value"myws.Power"/gt
ltparameter name"scope" value"application"
/gt ltparameter name"allowedMethods"
value""/gt lt/servicegt lt/deploymentgt
services.wsdd
java org.apache.axis.client.AdminClient
-hlocalhost -p8090 services.wsdd
33That's it. Call the Service.
import org.apache.axis.client. public class
PowerClient public static void
main(String argv)throws Exception
String endpoint
"http//mangal8090/axis/services/mypower"
Call call (Call) new Service().createCall(
) call.setTargetEndpointAddress(endpo
int) call.setOperationName("power")
Object value call.invoke(new
Object new Integer(2), new
Integer(5))
System.out.println(2""5 "" value)
34How Does it Work?
- The AXIS plugin is simply a Web application that
resides in Tomcat (under webapps/) - The Servlet AxisServlet of this application is
responsible for invoking services - All URLs of the form /services/ are mapped to
the AxisServlet - Where is that written?
35How Does it Work? (cont)
- The wsdd file defines mappings between a Web
service elements to a Java class elements - I.e., names, methods, etc.
- The class AdminClient sends a request to the
application to register the service based on the
wsdd content - When a SOAP request arrives, the AxisServlet
object parses the request and invokes the
corresponding method of the class associated with
the service URL
36The Deployment Descriptor
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org/a
xis/wsdd/providers/java"gt ltservice
name"mypower" provider"javaRPC"gt
ltparameter name"className" value"myws.Power"/gt
ltparameter name"scope" value"application"
/gt ltparameter name"allowedMethods"
value""/gt lt/servicegt lt/deploymentgt
services.wsdd
37The Scope of the Object
- Request (the default) a new object is created
for each request, the service instance is
available for the duration of the request,
regardless of forwarding - Session a new object is created for each new
session and the service instance is available for
the entire session - Application a singleton shared service instance
is used to serve all invocations
38Undeploying a Service
ltundeployment xmlns"http//xml.apache.org/axis/ws
dd/"gt ltservice name"mypower"/gt lt/undeploymen
tgt
undeploy.wsdd
java org.apache.axis.client.AdminClient
-hlocalhost -p8090 undeploy.wsdd
39Implementing Classes
- The class that implements the Web service must be
accessible to the Axis Servlet - Hence, this class should reside in a package in
one of the directories - CATALINA_BASE/webapps/axis/WEB-INF/classes
- CATALINA_BASE/shared/classes (classes that are
required for other applications besides Axis) - Of course, all helper classes should be
accessible to the Axis application too
40The Service WSDL
- Axis automatically provides a WSDL for each
deployed service - To get the WSDL, use the service URL with the
empty argument wsdl - http//localhost8090/axis/services/mypower?wsdl
41Power-Service WSDL
lt?xml version"1.0" encoding"UTF-8"?gt ltwsdldefin
itions targetNamespace"http//localhost/axis/serv
ices/mypower" xmlns"http//schemas.xmlsoap.or
g/wsdl/" xmlnsapachesoap"http//xml.apache.org
/xml-soap" xmlnsimpl"http//localhost/axis/ser
vices/mypower" xmlnsintf"http//localhost/axis
/services/mypower" xmlnssoapenc"http//schemas
.xmlsoap.org/soap/encoding/" xmlnswsdl"http//
schemas.xmlsoap.org/wsdl/" xmlnswsdlsoap"http
//schemas.xmlsoap.org/wsdl/soap/"
xmlnsxsd"http//www.w3.org/2001/XMLSchema"gt
ltwsdlmessage name"powerResponse"gt
ltwsdlpart name"powerReturn" type"xsdint"/gt
lt/wsdlmessagegt ltwsdlmessage
name"powerRequest"gt ltwsdlpart name"a"
type"xsdint"/gt ltwsdlpart name"n"
type"xsdint"/gt lt/wsdlmessagegt
42ltwsdlportType name"Power"gt ltwsdloperation
name"power" parameterOrder"a n"gt
ltwsdlinput message"implpowerRequest"
name"powerRequest"/gt ltwsdloutput
message"implpowerResponse" name"powerResponse"
/gt lt/wsdloperationgt lt/wsdlportTypegt
43ltwsdlbinding name"mypowerSoapBinding"
type"implPower"gt ltwsdlsoapbinding
style"rpc" transport"http//schemas.x
mlsoap.org/soap/http"/gt ltwsdloperation
name"power"gt ltwsdlsoapoperation
soapAction""/gt ltwsdlinput
name"powerRequest"gt ltwsdlsoapbody
encodingStyle"http//schemas.xmlsoap.org/
soap/encoding/" namespace"http//myws"
use"encoded"/gt lt/wsdlinputgt
ltwsdloutput name"powerResponse"gt
ltwsdlsoapbody
encodingStyle"http//schemas.xmlsoap.org/soap/enc
oding/" namespace"http//loc
alhost/axis/services/mypower" use"encoded"/gt
lt/wsdloutputgt lt/wsdloperationgt lt/wsdl
bindinggt
44 ltwsdlservice name"PowerService"gt
ltwsdlport binding"implmypowerSoapBinding"
name"mypower"gt ltwsdlsoapaddress
location"http//localhost/axis/s
ervices/mypower"/gt lt/wsdlportgt
lt/wsdlservicegt lt/wsdldefinitionsgt
45jws Files
- There is a fast and easy way of crating a
service - Create a Java class myClass.java
- Rename your class to end with jws myClass.jws
- Put the jws file directly under the directory
CATALINA_BASE/webapps/axis/ - That is all. Now you can call the service!
46Example a Calculator Service
public class SimpleCalculator public int
add(int i1, int i2) return i1 i2
public int subtract(int i1, int i2) return
i1 - i2
CATALINA_BASE/webapps/axis/SimpleCalculator.jws
Service URL http//serverport/axis/SimpleCalcula
tor.jws
47Example a Calculator Service
public class CalculatorClient public static
void main(String argv)throws Exception
String endpoint
"http//localhost80/axis/SimpleCalculator.jws"
Call call (Call) new Service().createCal
l() call.setTargetEndpointAddress(endpoi
nt) call.setOperationName("add")
Object value call.invoke(new Object new
Integer(4), new
Integer(6)) System.out.println(v
alue)
48How does it Work?
- On the first time the jws file is being called,
it is compiled into a class - WEB-INF/jwsClasses/SimpleCalculator.class
- Axis then considers the URL of the jws as one of
a regular web service - Default configurations are assumed
- Sounds like any other technology we know?
49When not to Use jws Files
- When you do not have the Java source code
- When you dont want to expose your code
- When you want to use custom type mappings
- When you want to use other configuration options
50Axis Type Mappings
51Axis Type Mappings
- Axis uses mappings between SOAP types and Java
classes and primitive types - In client side SOAP ? Java
- In server side Java ? SOAP
- Serialization and deserialization are executed by
objects of the interfaces Serializer and
Deserializer (provided by Axis), respectively - For example, SimpleSerializer,
SimpleDeserializer, DateDeserializer,
ArraySerializer
Axis Type Mapping
52Complex Type Example - Service Side
package myws import java.util. public class
VectorService public Vector
getAsVector(int array) Vector result
new Vector() for(int i0 iltarray.length
i) result.add("Number "
arrayi) return result
VectorService.java
53Input in The WSDL
ltcomplexType name"ArrayOf_xsd_int"gt
ltcomplexContentgt ltrestriction
base"soapencArray"gt ltattribute
ref"soapencarrayType" wsdlarrayType"xsdint"
/gt lt/restrictiongt lt/complexContentgt lt/com
plexTypegt
ltwsdlmessage name"getAsVectorRequest"gt
ltwsdlpart name"array" type"implArrayOf_xsd_int
"/gt lt/wsdlmessagegt
54Output in The WSDL
ltcomplexType name"Vector"gt ltsequencegt
ltelement maxOccurs"unbounded" minOccurs"0"
name"item" type"xsdanyType"/gt
lt/sequencegt lt/complexTypegt
ltwsdlmessage name"getAsVectorResponse"
ltwsdlpart name"getAsVectorReturn"
type"apachesoapVector"/gt lt/wsdlmes
sagegt
55Complex Type Example - Client Side
import java.util. import org.apache.axis.client.
public class VectorClient public static
void main(String argv)throws Exception
String endpoint
"http//localhost/axis/services/tovector"
Call call (Call) new Service().createCall()
call.setTargetEndpointAddress(endpoi
nt) call.setOperationName("getAsVecto
r") int array 3,7
Vector value (Vector)call.invoke(new
Object array) printVector(value)
VectorClient.java
56A Snapshot from the Request
ltgetAsVector soapenvencodingStyle"http//s
chemas.xmlsoap.org/soap/encoding/"gt ltarg0
xsitype"soapencArray"
soapencarrayType"xsdint2"
xmlnssoapenc"http//schemas.xmlsoap.org/soap/enc
oding/"gt ltitemgt3lt/itemgt
ltitemgt7lt/itemgt lt/arg0gt lt/getAsVectorgt
57A Snapshot from the Response
ltgetAsVectorResponse soapenvencodingStyle"h
ttp//schemas.xmlsoap.org/soap/encoding/"gt
ltgetAsVectorReturn href"id0"/gt lt/getAsVectorResp
onsegt ltmultiRef id"id0" soapencroot"0"
soapenvencodingStyle"http//schemas.xmlsoap.org
/soap/encoding/" xsitype"ns1Vector"
xmlnssoapenc"http//schemas.xmlsoap.org/soap/en
coding/" xmlnsns1"http//xml.apache.org/x
ml-soap"gt ltitem xsitype"soapencstring"gtN
umber 3lt/itemgt ltitem xsitype"soapencstri
ng"gtNumber7lt/itemgt lt/multiRefgt
58Java Beans
- Java Beans are simply objects of classes that
follow some (natural) coding conventions - An empty constructor
- A readable property has a matching getter
- A writable property has a matching setter
- A property has name and a type
- The getter of property prop type getProp()
- The setter of property prop void setProp()
59An Example
package myws public class Person private
String firstName, lastName private int
personID public String getFirstName()
return firstName public void
setFirstName(String firstName)
this.firstName firstName public String
getLastName() return lastName public void
setLastName(String lastName)
this.lastName lastName public void
setId(int id) personID id public int
getId(int id) return personID
60Using Beans in Web Services
- A Web service can use beans in its definition
- either in its input or output
- For that, you need to tell the Axis engine that
the corresponding object needs to be serialized
as a bean - In this serialization, every readable property
(i.e., one that has a getter) is specified - Where, in the file system, should the bean class
reside?
61A Service Example
package myws public class PersonService
public Person createPerson() Person
person new Person() person.setFirstName("
f") person.setLastName("l")
person.setId(2) return person
62The WSDL
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org
/axis/wsdd/providers/java"gt ltservice
name"person" provider"javaRPC"gt
ltparameter name"className"
value"myws.PersonService"/gt
ltparameter name"scope" value"application"/gt
ltparameter name"allowedMethods" value""/gt
ltbeanMapping qname"nsperson"
xmlnsns"http//www.cs.huji.ac.il/dbi/xsd"
languageSpecificType"javamyws.Person"/gt
lt/servicegt lt/deploymentgt
63Using Beans in Clients
- To use a bean on the client side, you need to
configure service to deserialize the SOAP type
into a Java bean - For that, we use the class BeanSerializer
provided by Axis - Note that client and server beans need not be the
class - What should they have in common?
64public class PersonClient public static void
main(String argv) throws Exception String
endpoint "http//localhost/axis/services/person"
Call call (Call) new Service().createCall()
call.setTargetEndpointAddress(endpoint)
call.setOperationName("createPerson") QName
qn new QName("http//www.cs.huji.ac.il/
dbi/xsd", "person") BeanDeserializerFactory
bdf new BeanDeserializerFactory(MyP
erson.class,qn) call.registerTypeMapping(MyPer
son.class, qn, null, bdf) MyPerson value
(MyPerson)call.invoke(new Object )
System.out.println(value)
Identical to Person
65Exception Handling
66Exception in Service Calls
- Several problems can cause exception to be thrown
when a service is being invoked - For example
- the server cannot be connected
- the server does not find the requested URL
- the request is inappropriate (no such operation,
invalid arguments, etc.) - the implementing service method has thrown an
exception
67Service Exception
- When the implementing service throws an
exception, a SOAP fault response is returned by
the server - The client invocation method translates this
response to a java.rmi.RemoteException after
parsing the response
68An Example
package myws public class ExceptionService
public void throwException() throws
IllegalStateException throw new
IllegalStateException ("I
only throw an exception!")
ltservice name"exception" provider"javaRPC"gt
ltparameter name"className"
value"myws.ExceptionService"/gt
ltparameter name"allowedMethods"
value""/gt lt/servicegt
services.wsdd
69public class ExceptionClient public static
void main(String argv) String endpoint
"http//localhost80/axis/services/exception"
try Call call (Call) new
Service().createCall() call.setTargetEndpoin
tAddress(endpoint) call.setOperationName("th
rowException") Object value
call.invoke(new Object )
System.out.println(value)
catch(RemoteException exp)
System.err.println("WS invocation error "
exp.getMessage()) catch(Exception exp)
System.err.println("Error "
exp.getMessage())
70A Snapshot from the Response
ltsoapenvBodygt ltsoapenvFaultgt
ltfaultcodegtsoapenvServer.userExceptionlt/faultcode
gt ltfaultstringgtjava.lang.IllegalStateExcept
ion I only throw an
exception!lt/faultstringgt ltdetailgt...lt/detail
gt lt/soapenvFaultgt lt/soapenvBodygt
71Sharing Information with Web Applications
72The Scenario
- In your project, you will have two Web
applications under Tomcat 5.0 - Your Web site
- Axis application for publishing Web services
- The above two applications need to share
information - e.g., the database connection parameters
- How will your Web application pass that
information to the service method?
73A Possible Solution
- Tomcat uses one class loader for all classes
under CATALINA_BASE/share/ - Hence, a static variable in one of those classes
will be accessible to every class running under
Tomcat
74A Possible Solution
CATALINA_BASE
webapps
shared
myapplication
axis
lib
classes
SharedClass
- Put the the shared class under shared
- Use a static field of the shared class to hold
the shared object - The first time a you discover the parameter,
store it the shared class