There have been questions raised in Axis2 user list about reading some properties defined in services.xml from service implementation class.
An easy way of doing that is as follows.
1. Suppose your services.xml is as follows and it has a parameter named, TestProperty.
<service name="ParameterService">
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass">org.test.MyParameterService</parameter>
<parameter name="TestProperty">This is a test property</parameter>
</service>
2. We need to read the value of "TestProperty" parameter from service implementation class.
It can be done using MessageContext as follows.
import org.apache.axis2.context.MessageContext;
public class MyParameterService {
public void readProperty(){
MessageContext mc = MessageContext.getCurrentMessageContext();
String prop = mc.getCurrentMessageContext().getAxisService().getParameter("TestProperty").getParameterElement().getText();
System.out.println(prop);
}
}
Now you can create a service archive with this class and copy it to AXIS2_HOME/repository/services directory. Then start axis2server.bat and go to http://localhost:8080
You will notice that the service will be deployed there. Then invoke the service by sending HTTP GET request as follows
http://localhost:8070/axis2/services/ParameterService/readProperty
Look at the Axis2server console. You will see "This is a test property" message is printed there.
No comments:
Post a Comment