Ruben Laguna’s blog

Passing Configuration Parameter to Axis2 Services

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

1
2
3
4
5
6
7
8
9
10
11
12
13
<span class='line'>&lt;?xml version="1.0" encoding="UTF-8"?>
</span><span class='line'>&lt;!-- This file was auto-generated from WSDL -->
</span><span class='line'>&lt;!-- by the Apache Axis2 version: 1.3  Built on : Aug 10, 2007 (04:45:47 LKT) -->
</span><span class='line'>&lt;serviceGroup>
</span><span class='line'>    &lt;service name="xxxxxxx" class="MyServiceLifeCycleImpl">
</span><span class='line'>
</span><span class='line'>....
</span><span class='line'>        &lt;parameter name="jdbcConnectionString">jdbc:derby:c:/demoderby2b;create=true;user=a;password=b;&lt;/parameter>
</span><span class='line'>        &lt;parameter name="jdbcDriver">org.apache.derby.jdbc.EmbeddedDriver&lt;/parameter>
</span><span class='line'>
</span><span class='line'>...
</span><span class='line'>    &lt;/service>
</span><span class='line'>&lt;/serviceGroup></span>

2) Write a ServiceLifeCycle class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<span class='line'>public class MyServiceLifeCycleImpl implements ServiceLifeCycle {
</span><span class='line'>    private Log log = LogFactory.getLog(MyServiceLifeCycleImpl.class);
</span><span class='line'>
</span><span class='line'>    public void startUp(ConfigurationContext confCtx, AxisService axisService) {
</span><span class='line'>        try {
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>            String jdbcConnectionString = (String) axisService.getParameterValue("jdbcConnectionString");
</span><span class='line'>            String jdbcDriver = (String) axisService.getParameterValue("jdbcDriver");
</span><span class='line'>            Class.forName(jdbcDriver).newInstance();
</span><span class='line'>            Connection connection = DriverManager
</span><span class='line'>                    .getConnection(jdbcConnectionString);
</span><span class='line'>            axisService.addParameter("jdbcConnection", connection);
</span><span class='line'>        } catch (Exception e) {
</span><span class='line'>            throw new RuntimeException(e);
</span><span class='line'>        }
</span><span class='line'>    }
</span><span class='line'>
</span><span class='line'>....</span>

3) Add init method to your service

1
2
3
4
5
6
7
8

1
2
3
4
5
6
7
8
<span class='line'>....
</span><span class='line'>       private ServiceContext serviceContext;
</span><span class='line'>
</span><span class='line'>  public void init(ServiceContext serviceContext) {
</span><span class='line'>    this.serviceContext = serviceContext;
</span><span class='line'>
</span><span class='line'>  }
</span><span class='line'>....</span>

4) Access the parameter jdbcConnection from your service through serviceContext

1
2
3
4
5

1
2
3
4
5
<span class='line'>....
</span><span class='line'>    Connection conn = (Connection) serviceContext.getAxisService()
</span><span class='line'>          .getParameterValue("jdbcConnection");
</span><span class='line'>
</span><span class='line'>....</span>

Comments

Copyright © 2015 - Ruben Laguna - Powered by Octopress