Implementing Contextual Web Services, Page 2
Accessing Context Using Standard JAX-RPC API
The use of WebServiceContext API has been deprecated in WebLogic 9.2 onwards and the support for this API may be withdrawn at any time. From version 9.2 onwards, WebLogic has moved towards a Servlets-based JAX-RPC runtime; therefore, support for standard APIs are available now. Another important note is that the Web Service development model has been drastically changed in 9.2 when compared to its previous versions. It has adopted a Web Services annotations model to develop portable Web Services. For more information, please read JSR 181: Web Services Metadata for the JavaTM Platform and JSR 921: Implementing Enterprise Web Services.
The JAX-RPC specification provides an API, javax.xml.rpc.server.ServiceLifecycle, that defines the lifecycle of an endpoint. If the service end point implements this interface, the container is expected to manage the life cycle of the corresponding service end points. This interface has life cycle methods to initialize and destroy the service end points. Please find the method signature for the life cycle methods below:
/**
* Used to initialize the service end point
*/
public void init ( Object contextObject ) throws ServiceException {
}
/**
* JAX-RPC runtime system ends the lifecycle of a service
* endpoint instance by invoking the destroy method
*/
public void destroy() {
}
To illustrate the use of standard APIs, the earlier restaurant example has been recoded as below:
package service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import weblogic.jws.WLHttpTransport;
@WebService( name="Restaurant", serviceName="Restaurant",
targetNamespace="http://location.com/webservices" )
@SOAPBinding( style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL,
parameterStyle = SOAPBinding.ParameterStyle.WRAPPED )
@WLHttpTransport( contextPath="Restaurant",
serviceUri="Restaurant" )
public class RestaurantService implements ServiceLifecycle
{
@WebMethod( operationName = "getRestaurants" )
public String getRestaurants( int latitude, int longitude ) {
if( servletEndpointContext != null ) {
MessageContext mcontext =
servletEndpointContext.getMessageContext();
//javax.Servlet.http.HttpSession session =
mcontext.getHttpSession();
if( mcontext.containsProperty( "userId" ) ) {
String userId = (String) mcontext.getProperty( "userId" );
String userType = "Classic";
//String userType=UserDAO.getUserType(userId);
if( userType.equals( "Premium" ) ) {
return "<Restaurants>" +
"<Restaurant>Italiano</Restaurant>" +
"<Restaurant>Pizza Hut</Restaurant>" +
"<Restaurant>Chili's</Restaurant>" +
"<Restaurant>Sushi Bistro</Restaurant>" +
"<Restaurant>Naan-N-Curry</Restaurant>" +
"</Restaurants>";
}
}
}
return "<Restaurants>" +
"<Restaurant>Italiano</Restaurant>" +
"<Restaurant>Pizza Hut</Restaurant>" +
"<Restaurant>Mona Lisa</Restaurant>" +
"</Restaurants>";
}
private ServletEndpointContext servletEndpointContext = null;
public void destroy() {
servletEndpointContext = null;
System.out.println("Destroyed ServletEndpointContext");
}
public void init( Object contextObject )
throws ServiceException {
System.out.println(
"Initializing ServletEndpointContext...");
this.servletEndpointContext =
(ServletEndpointContext) contextObject;
System.out.println("Initialized ServletEndpointContext");
}
}
Page 2 of 3
