Oracle9i Application Server Web Services Developer's Guide Release 2 (9.0.3) Part Number B10004-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:
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. A Servlets handle 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/webservices/demo/basic/stateless_ejb
on UNIX %ORACLE_HOME%\webservices\demo\basic\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.
package demo; 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 Service 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 Java Beans are used as parameters to Oracle9iAS Web Services, the client-side code should use the generated Bean included with the downloaded client-side proxy. This is because the generated client-side proxy code translates Simple Object Access Protocol (SOAP) structures to and from Java Beans by translating SOAP structure namespaces to and from fully qualified Bean class names. If a Bean with the specified name does not exist in the specified package, the generated client code will fail.
However, there is no special requirement for clients using Web Services Description Language (WSDL) to form calls to Oracle9iAS Web Services, rather than the client-side proxy. The generated WSDL document describes SOAP structures in a standard way. Application development environments, such as JDeveloper, which work directly from WSDL documents can correctly call Oracle9iAS Web Services with Java Beans as parameters.
The WebServicesAssembler
supports the <wsdl-gen>
and <proxy-gen>
tags to allow a Web Service developer to generate WSDL files and client-side proxy files. You can use these tags to control whether the WSDL file and the client-side proxy are generated. Using these tags you can also specify that the generated WSDL file or a WSDL file that you write is packaged with the Web Service J2EE .ear.
A client-side developer either uses the WSDL file that is obtained from a deployed Web Service, or the client-side proxy that is generated from the WSDL to build an application that uses the Web Service.
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 includes the ejb.jar that supplies the Java implementation. This section describes how to use the Oracle9iAS Web Services tool, WebServicesAssembler
. WebServicesAssembler
takes an XML configuration file that describes the stateless session EJB Web Service and produces a J2EE .ear file that can be deployed under Oracle9iAS Web Services.
This section contains the following topics.
The Oracle9iAS Web Services assembly tool, WebServicesAssembler
, assists in assembling Oracle9iAS Web Services. This section describes how to create a configuration file to use with stateless session EJB Web Services.
Create WebServicesAssembler
configuration file by adding the following:
Table 4-2 describes the top level WebServicesAssembler
configuration file tags. Add these tags to provide top level information describing the Java Stateless Web Service or a Java Stateful Web Service. These tags are included within a <web-service>
tag in the configuration file.
Example 4-4 shows a complete config.xml
file, including the top level tags.
WebServicesAssembler
Configuration Tags
Tag | Description |
---|---|
|
Specifies the context root of the Web Service. This tag is required. |
|
Specifies the datasource associated with the Web Service. |
|
Provides a simple description of the Web Service. This tag is optional. |
|
Specifies the name of the generated J2EE .ear file output. The dest_path specifies the complete path for the output file. This tag is required. |
|
Specifies the Web Service display name. This tag is optional. |
|
Includes a specified file in the output .ear file. Use this option to include Java resources. The path specifies the path to the file to include. This tag is optional. |
|
Use this tag to add a stateless session EJB Web Service. See Table 4-3 for a description of the valid sub-tags. |
|
Specifies a directory where the assembler can store temporary files. This tag is optional. |
Prepare Stateless Session EJB Web Services using the WebServicesAssembler
<stateless-session-ejb-service>
tag. This tag is included within a <web-service>
tag in the configuration file. Add this tag to provide information required for generating a stateless session EJB Web Service.
Table 4-3 shows the <stateless-session-ejb-service>
sub-tags.
Example 4-4 shows a complete config.xml
file, including <stateless-session-ejb-service>
.
Tag | Description |
---|---|
|
Specifies the name of the stateless session EJB. This tag is required |
|
This is a backwards compatibility tag.
See Also: the top level This tag is optional |
|
This is a backwards compatibility tag.
See Also: the top level This tag is optional |
|
Specifies servlet mapping pattern for the Servlet that implements the Web Service. The path specified as the URI is appended to the This tag is required. |
<web-service> <display-name>EJB Web Services Demo</display-name> <destination-path>tmp/HelloService.ear</destination-path> <temporary-directory>tmp</temporary-directory> <context>/sejb_webservices</context> <stateless-session-ejb-service> <path>tmp/Hello.jar</path> <uri>/HelloService</uri> <ejb-name>HelloService</ejb-name> </stateless-session-ejb-service> </web-service>
The WebServicesAssembler
supports the <wsdl-gen>
and <proxy-gen>
tags to allow a Web Service developer to generate WSDL files and client-side proxy files. You can use these tags to control whether the WSDL file and the client-side proxy are generated. Using these tags you can also specify that the generated WSDL file or a WSDL file that you write is packaged with the Web Service J2EE .ear.
A client-side developer either uses the WSDL file that is obtained from a deployed Web Service, or the client-side proxy that is generated from the WSDL to build an application that uses the Web Service.
After you create the WebServicesAssembler
configuration file, you can generate a J2EE .ear file for the Web Service. The J2EE .ear file includes the stateless session EJB Web Service servlet configuration information.
Run the Oracle9iAS Web Services assembly tool, WebServicesAssembler
as follows:
java -jar WebServicesAssembler.jar -config config_file
Where: config_file is the configuration file that contains the <stateless-session-ejb-service>
tag.
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. |
|