Ruben's blog

Nothing to say

Passing Configuration Parameter to Axis2 Services

| Comments

One way to pass parameters to you Axis2 service:

1) write a tag inside your services.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was auto-generated from WSDL -->
<!-- by the Apache Axis2 version: 1.3  Built on : Aug 10, 2007 (04:45:47 LKT) -->
<serviceGroup>
    <service name="xxxxxxx" class="MyServiceLifeCycleImpl">

....
        <parameter name="jdbcConnectionString">jdbc:derby:c:/demoderby2b;create=true;user=a;password=b;</parameter>
        <parameter name="jdbcDriver">org.apache.derby.jdbc.EmbeddedDriver</parameter>

...
    </service>
</serviceGroup>

2) Write a ServiceLifeCycle class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MyServiceLifeCycleImpl implements ServiceLifeCycle {
    private Log log = LogFactory.getLog(MyServiceLifeCycleImpl.class);

    public void startUp(ConfigurationContext confCtx, AxisService axisService) {
        try {


            String jdbcConnectionString = (String) axisService.getParameterValue("jdbcConnectionString");
            String jdbcDriver = (String) axisService.getParameterValue("jdbcDriver");
            Class.forName(jdbcDriver).newInstance();
            Connection connection = DriverManager
                    .getConnection(jdbcConnectionString);
            axisService.addParameter("jdbcConnection", connection);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

....

3) Add init method to your service

1
2
3
4
5
6
7
8
....
       private ServiceContext serviceContext;

  public void init(ServiceContext serviceContext) {
    this.serviceContext = serviceContext;

  }
....

4) Access the parameter jdbcConnection from your service through serviceContext

1
2
3
4
5
....
    Connection conn = (Connection) serviceContext.getAxisService()
          .getParameterValue("jdbcConnection");

....

Comments