By Matteo Redaelli

http://www.redaelli.org/matteo/

Apache ServiceMix4 Setup


Install and start ServiceMix 4.0

Add necessary bundles

features/install cxf-osgi
osgi/install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.2.2_3/
osgi/install -s wrap:mvn:org.springframework/spring-jdbc/2.5.6/
osgi/install -s wrap:mvn:mysql/mysql-connector-java/5.1.6

Deploying Datasource service

As suggesting in this google doc, create a file moodleDS.xml and copy it in "deploy" folder of servicemix

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel-osgi="http://activemq.apache.org/camel/schema/osgi"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://activemq.apache.org/camel/schema/spring
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
http://activemq.apache.org/camel/schema/osgi
http://activemq.apache.org/camel/schema/osgi/camel-osgi.xsd">

    <bean id="moodleDS"
                class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url"
                        value="jdbc:mysql://localhost:3306/moodle" />
                <property name="username" value="root" />
                <property name="password" value="" />
    </bean>

   <osgi:service id="moodleDSOsgiService" ref="moodleDS" interface="javax.sql.DataSource" />
</beans>

Creating Maven project

mvn archetype:create \
        -DarchetypeGroupId=org.apache.servicemix.tooling \
        -DarchetypeArtifactId=servicemix-osgi-cxf-wsdl-first-archetype  \
        -DarchetypeVersion=2008.01-SNAPSHOT \
        -DgroupId=com.yourcompany \
        -DartifactId=test-wsdl-first-osgi \
        -Dversion=1.0-SNAPSHOT \
        -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository/

Edit pom.xml

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>2.5.6</version>
        </dependency>
...
                                    <extraargs>
                                        <extraarg>-impl</extraarg>  <!-- necessary to generate src/main/java/org/apache/servicemix/samples/wsdl_first/PersonImpl.java  -->


                                    </extraargs>
                                </wsdlOption>

...
                        <Import-Package>
...
                            org.springframework.jdbc,
                            org.springframework.jdbc.core,
                            org.springframework.jdbc.datasource,
                            com.mysql.jdbc,
                            javax.sql

Edit src/main/resources/META-INF/spring/beans.xml

...
        xmlns:osgi="http://www.springframework.org/schema/osgi"
...
<osgi:reference id="moodleDS" interface="javax.sql.DataSource" bean-name="moodleDS" />

 <bean id="person" class="org.apache.servicemix.samples.wsdl_first.PersonImpl">
                <property name="dataSource" ref="moodleDS" />
</bean>

 <jaxws:endpoint id="HTTPEndpoint"
        implementor="#person"
        address="/PersonService"
        wsdlLocation="wsdl/person.wsdl"
        endpointName="e:soap"
        serviceName="s:PersonService"
        xmlns:e="http://servicemix.apache.org/samples/wsdl-first"
            xmlns:s="http://servicemix.apache.org/samples/wsdl-first">
 </jaxws:endpoint>

Edit src/main/java/org/apache/servicemix/samples/wsdl_first/PersonImpl.java

import java.util.*;         
import javax.sql.DataSource;    
import org.springframework.jdbc.core.JdbcTemplate;
...
public class PersonImpl implements Person {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
      this.dataSource = dataSource;
      this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
...
        try {
        List result = this.jdbcTemplate.queryForList("select id, firstname, lastname, url, skype from mdl_user where username = ?", new Object[]{personId.value});
            if(result.size() != 1)
           throw new UnknownPersonFault("Got a different number of records, expected 1"  );

            Map record=(Map)result.get(0);
            name.value = record.get("lastname").toString();
            ssn.value = record.get("firstname").toString();
        } catch (Exception ex) {

Deploying and testing

Copy the file target/test-wsdl-first-osgi-1.0-SNAPSHOT.jar
in "deploy" folder

Test in a browser the url
http://localhost:8080/cxf/PersonService?wsdl

You can also test the webservice with the file client.html got in example/cxf-wsdl-first