Oracle9i Application Server Web Services Developer's Guide Release 2 (9.0.2) Part Number A95453-01 |
|
This chapter describes the procedures you use to write and deploy Oracle9iAS Web Services that are implemented as stateless session Enterprise Java Beans (EJBs).
This chapter covers the following topics:
Oracle9iAS Web Services can be implemented as any of the following:
This chapter shows sample code for writing Web Services implemented with stateless session EJBs.
Oracle9iAS supplies Servlets to access the EJBs which implement a Web Service. The Servlet handles requests generated by a Web Service client, locates the EJB home and remote interfaces, runs the EJB that implements the Web Service, and returns results back to the Web Service client.
Writing EJB based Web Services involves obtaining or building an EJB that implements a service. The EJB should contain one or more methods that a Web Services Servlet running under Oracle9iAS invokes when a client makes a Web Services request. There are very few restrictions on what actions Web Services can perform. At a minimum, Web Services usually generate data that is sent to a Web Services client or perform an action as specified by a Web Services method request.
This section shows how to write a simple stateless session EJB Web Service, HelloService
that returns a string, "Hello World", to a client. This EJB Web Service receives a client request with a single String
parameter and generates a response that it returns to the Web Service client.
The sample code for the complete Web Service is supplied with Oracle9iAS Web Services installation in the following directory: $ORACLE_HOME/j2ee/home/demo/web_services\stateless_ejb
on UNIX %ORACLE_HOME%\j2ee\home\demo\web_services\stateless_ejb
on Windows.
Create a stateless session EJB Web Service by writing a standard J2EE stateless session EJB containing a remote interface, a home interface, and an enterprise bean class. Oracle9iAS Web Services runs EJBs that are deployed as Oracle9iAS Web Services in response to a request issued by a Web Service client.
Developing a stateless session EJB consists of the following steps:
When looking at the HelloService
EJB Web Service, note that the .ear file, HelloService.ear
defines the Web Service and its configuration files. In the sample directory, the file HelloService.java
provides the remote interface for the HelloService
EJB.
Example 4-1 shows the Remote
interface for the sample stateless session EJB.
public interface HelloService extends javax.ejb.EJBObject { java.lang.String hello(java.lang.String phrase) throws java.rmi.RemoteException; }
The sample file HelloServiceHome.java
provides the home interface for the HelloService
EJB.
Example 4-2 shows the EJBHome
interface for the sample stateless session EJB.
package demo; /** * This is a Home interface for the Session Bean */ public interface HelloServiceHome extends javax.ejb.EJBHome { HelloService create() throws javax.ejb.CreateException, java.rmi.RemoteException ; }
The sample file HelloServiceBean.java
provides the Bean logic for the HelloService
EJB. When you create a Bean to implement a Web Service, the parameters and return values must be of supported types. Table 4-1 lists the supported types for parameters and return values for stateless session EJBs that implement Web Services.
Example 4-3 shows the source code for the HelloService
Bean.
package demo; import java.rmi.RemoteException; import java.util.Properties; import javax.ejb.*; /** * This is a Session Bean Class. */ public class HelloServiceBean implements SessionBean { private javax.ejb.SessionContext mySessionCtx = null; public void ejbActivate() throws java.rmi.RemoteException {} public void ejbCreate() throws javax.ejb.CreateException, java.rmi.RemoteException {} public void ejbPassivate() throws java.rmi.RemoteException {} public void ejbRemove() throws java.rmi.RemoteException {} public javax.ejb.SessionContext getSessionContext() { return mySessionCtx; } public String hello(String phrase) { return "HELLO!! You just said :" + phrase; } public void setSessionContext(javax.ejb.SessionContext ctx) throws java.rmi.RemoteException { mySessionCtx = ctx; } }
The hello()
method shown in Example 4-3 returns a String
. An Oracle9iAS Web Services server-side Servlet runs the Bean that calls the hello()
method when the Servlet receives a Web Services request from a client. After executing the hello()
method, the Servlet returns a result to the Web Services client.
Example 4-3 shows that the EJB Bean writer only needs to return values of supported types to create Web Services implemented as stateless session EJBs.
When an error occurs while running a Web Service implemented as an EJB, the EJB should throw an exception. When an exception is thrown, the Web Services Servlet returns a Web Services (SOAP) fault. Use the standard J2EE and OC4J administration facilities for logging Servlet errors for a Web Service that uses stateless session EJBs for its implementation.
Parameters and results sent between Web Service clients and a Web Service implementation need to be encoded and serialized. This allows the call and return values to be passed as XML documents using SOAP.
Table 4-1 lists the supported data types for parameters and return values for Oracle9iAS Web Services.
A Bean for purposes of Web Services is any Java class which conforms to the following restrictions:
Oracle9iAS Web Services allows Beans to be returned or passed in as arguments to J2EE Web Service methods, as long as the Bean only consists of property types that are listed in Table 4-1 or are another supported Java Bean.
When writing an EJB Web Service, this step is optional. If you do not perform this step, the Oracle9iAS Web Services runtime generates a WSDL file and the client-side proxy stubs for deployed Web Services. These files allow Oracle9iAS Web Services to supply a Web Service client with the WSDL or the client-side proxy stubs that a client-side developer can use to build an application that uses a Web Service.
When you do not want to use the Oracle9iAS Web Services generated WSDL file or client-side proxy stubs, and you want to supply your own versions of these files, perform the following steps:
.wsdl
extension. The client-side proxy Jar must have an _proxy.jar
extension.The extension is placed after the service name.
For example,
simpleservice.wsdl simpleservice_proxy.jar
WEB-INF/classes
. For example, if simpleservice.wsdl
describes a service that is part of the demo package, place the file in the following directory:
WEB-INF/classes/demo/simpleservice.wsdl
WEB-INF/lib
component of the .war file that is added to the .ear file and deployed as a Web Service. For example, the file simpeservice_proxy.jar
could be added to simpleservice.jar
and placed in WEB-INF/lib
, or added to a Jar file namex.jar
in WEB-INF/lib
(
where namex is a file name).
To deploy a stateless session EJB as a Web Service you need to assemble a J2EE .ear file that includes the deployment descriptors for the Oracle9iAS Web Services Servlet and the ejb.jar file that supplies the EJB implementation. A Web Service implemented using a stateless session EJB includes a .war file that provides descriptive information for the Oracle9iAS Web Services Servlet running under Oracle9iAS Containers for J2EE (OC4J). This section describes the procedures you use to assemble the .ear file that contains a stateless session EJB and the Web Services Servlet configuration, placed in a .war file, required to run a stateless session EJB as a Web Service.
This section covers the following topics:
The Oracle9iAS Web Services assembly tool, WebServicesAssembler
, assists in assembling Oracle9iAS Web Services. The Web Services assembly tool takes a configuration file which describes the location of the J2EE/EJB Jar files and produces a J2EE .ear file that can be deployed under Oracle9iAS Web Services. This section describes how to assemble Oracle9iAS Web Services implemented as stateless session EJBs manually, without using WebServicesAssembler
.
To prepare a stateless session EJB to implement a Web Service, you need to prepare a JAR file as you would for any other standard J2EE stateless session EJB.
The JAR file should contain the following:
For the sample stateless session EJB, the EJB classes should include the classes developed for the HelloServiceBean
, organized in the standard J2EE directory format.
Example 4-4 shows the stateless session EJB deployment descriptor for the HelloServiceBean
that is included in the JAR file.
<?xml version="1.0"?> <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc. //DTD Enterprise JavaBeans 1.1 //EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'> <ejb-jar> <enterprise-beans> <session> <ejb-name>HelloService</ejb-name> <home>demo.HelloServiceHome</home> <remote>demo.HelloService</remote> <ejb-class>demo.HelloServiceBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
This section provides instructions for assembling a Web Service based on a stateless session EJB. To use an EJB as a Web Service you need to add a <servlet>
entry and corresponding <ejb-ref>
and <servlet-mapping>
entries in a web.xml
file for each stateless session EJB that is deployed as a Web Service. The resulting web.xml
file is packaged as part of a J2EE .war file that is included in the .ear file that is deployed to OC4J.
Note: All stateless session EJBs deployed as Oracle9iAS Web Services must conform to the restrictions limiting parameter and return types. See Table 4-1 for the list of supported types for parameters and return values. |
To modify web.xml
to support Web Services implemented as stateless session EJBs, perform the following steps:
To add Web Services based on stateless session EJBs, you need to modify the <servlet>
tag in the web.xml
file. This supports using the Oracle9iAS Web Services Servlet with a stateless session EJB. Table 4-2 describes the <servlet>
tag and the values that it must include to add a Web Service based on a stateless session EJB.
Example 4-5 shows the sample <servlet>
tag for the HelloService
EJB Web Service, as extracted from the web.xml
file in the .ear file, statelessejb.ear
.
<servlet> <servlet-name>stateless session bean web service - HelloService</servlet-name> <servlet-class>oracle.j2ee.ws.SessionBeanWebService</servlet-class> <init-param> <param-name>jndi-name</param-name> <param-value>ejb/HelloService</param-value> </init-param> </servlet>
To add Web Services based on stateless session EJBs, you need to modify the <ejb-ref>
tag in the web.xml
file. This tag defines the EJB reference that is used as the implementation of the Web Service.
Example 4-6 shows the <ejb-ref>
tag for the HelloServiceBean
for the HelloService
EJB, as extracted from the web.xml
file in the .ear file, HelloService.ear
.
<ejb-ref> <ejb-ref-name>ejb/HelloService</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>demo.HelloServiceHome</home> <remote>demo.HelloService</remote> <ejb-link>HelloService</ejb-link> </ejb-ref>
To add Web Services based on stateless session EJBs, you need to modify the <servlet-mapping>
tag in the web.xml
file. This tag specifies the URL for the Servlet that implements a Web Service.
Example 4-7 shows a sample <servlet-mapping>
entry corresponding to the servlet entry shown in Example 4-5.
<servlet-mapping> <servlet-name>stateless session bean web service - HelloService</servlet-name> <url-pattern>/HelloService</url-pattern> </servlet-mapping>
To add Web Services based on stateless session EJBs, you need to use a .war file to package the J2EE Servlet configuration files. After modifying the web.xml
file, add any required support classes under WEB-INF/classes or WEB-INF/lib and package all the files as a .war file.
To add Web Services based on stateless session EJBs, you need to include an application.xml
file and package the application.xml
, the .war file and the ejb.jar file containing the stateless session EJB into a J2EE .ear file.
The Oracle9iAS Web Services assembly tool, WebServicesAssembler
, assists in assembling Oracle9iAS Web Services.
After creating the .ear file containing a stateless session EJB, you can deploy the Web Service as you would any standard J2EE application stored in an .ear file (to run under OC4J).
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|