User login

How Can I Store and Access Parameters for My Web Service?

Story :

Level : Realm : Project :

Web service implementations might need to be provided with additional parameters when they initialize and start serving requests. This article explains how and where these parameters can be kept so that the Web service will be able to access them at runtime. Eran Chinthaka explains..

 

Applies To

Apache Axis2/Java 1.0 or later
JDK 1.4 or later

 

Web Services Parameters 

Some Web services need configuration parameters when they are initializing within Axis2 or when they start serving requests. For example, if the service is accessing a database or a remote service, the service will require parameters like username, password, location, etc. Let's see how these parameters can be given to the service.

The easiest way to do this, is to leave these parameters inside the services.xml file. When you deploy your Web services as an axis2 .aar file, you will have a services.xml file included within the services archive. You can encode your parameters inside an XML element and include them in the services.xml.

e.g.

<service name="axisversion">
	<description>My service which takes parameters</description>
	<parameter name="ServiceClass">path.to.my.ServiceClass</parameter>     ......
	<parameter name="MyParameter">MyParameterValue</parameter>
 	<parameter name="MyXMLParameter">
<DatabaseConfig>
<Username>Siripala</Username>
<Password>Kurumba</Password>
<Host>http://atuwa.mydomain.org/database</Host>
</DatabaseConfig>
</parameter>
....... <operation name="myOperation"/> </service>

Once you have set the parameters like this, you can access them using the MessageContext object at runtime. If your service does not have access to a MessageContext object, use the following static method in the MessageContext class to get a reference to the current MessageContext.

MessageContext msgContext = MessageContext.getCurrentMessageContext();
Parameter myParameter = msgContext.getParameter("MyParameter");
Parameter myXMLParameter = msgContext.getParameter("MyXMLParameter");

System.out.println("My Parameter " + myParameter.getParameterElement().getText());
System.out.println("My Parameter " + myXMLParameter.getParameterElement());

When you call msgContext.getParameter(parameterName).getParameterElement(), you will get access to the whole parameter element in the form of an OMElement (We use OMElements inside Axis2 to represent XML information items). If the value of the parameter is an XML element itself, like the "MyXMLParameter" in the above example, you will see the whole XML fragment returned inside the OMElement.

 

More Information

For more information on setting and accessing parameters within the Axis2 context hierarchy, refer to "Configuring Properties within Axis2" on the WSO2 Oxygen Tank.

 

Summary

This article showed you how to provide configuraition parameters to an Axis2 Web service.

 

Author 

Eran Chinthaka, Member - Apache Software Foundation. eranXchinthakaYgmail.com, where X=. and Y=@

0
No votes yet
Tags :