Chapter 3. Getting Started with Developing

Abstract

This chapter explains how to get started with Maven-based development, with a two-part project that illustrates how to develop applications using Apache CXF and Apache Camel.

3.1. Create a Web Services Project

Overview

This section describes how to generate a simple Web services project, which includes complete demonstration code for a server and a test client. The starting point for this project is the karaf-soap-archetype Maven archetype, which is a command-line wizard that creates the entire project from scratch. Instructions are then given to build the project, deploy the server to the Red Hat JBoss Fuse container, and run the test client.

Prerequisites

In order to access artifacts from the Maven repository, you need to add the fusesource repository to Maven's settings.xml file. Maven looks for your settings.xml file in the following standard location:
  • UNIX: home/User/.m2/settings.xml
  • Windows: Documents and Settings\User\.m2\settings.xml
If there is currently no settings.xml file at this location, you need to create a new settings.xml file. Modify the settings.xml file by adding the repository element and the pluginRepository element for the Maven Red Hat repository, as shown in the following example:
<?xml version="1.0"?>
<settings>
    
  <profiles>
    <profile>
      <id>extra-repos</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
       <repository>
            <id>redhat-ga-repository</id>
            <url>https://maven.repository.redhat.com/ga</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>redhat-ea-repository</id>
            <url>https://maven.repository.redhat.com/earlyaccess/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
          <id>jboss-public</id>
          <name>JBoss Public Repository Group</name>
          <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
            <id>redhat-ga-repository</id>
            <url>https://maven.repository.redhat.com/ga</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>redhat-ea-repository</id>
            <url>https://maven.repository.redhat.com/earlyaccess/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>jboss-public</id>
          <name>JBoss Public Repository Group</name>
          <url>https://repository.jboss.org/nexus/content/groups/public</url>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  
  <activeProfiles>
    <activeProfile>extra-repos</activeProfile>
  </activeProfiles>
    
</settings>

Create project from the command line

You can create a Maven project directly from the command line, by invoking the archetype:generate goal. First of all, create a directory to hold your getting started projects. Open a command prompt, navigate to a convenient location in your file system, and create the get-started directory, as follows:
mkdir get-started
cd get-started
You can now use the archetype:generate goal to invoke the karaf-soap-archetype archetype, which generates a simple Apache CXF demonstration, as follows:
mvn archetype:generate \
-DarchetypeGroupId=io.fabric8.archetypes \
-DarchetypeArtifactId=karaf-soap-archetype \
-DarchetypeVersion=1.2.0.redhat-630187 \
-DgroupId=org.fusesource.example \
-DartifactId=cxf-basic \
-Dversion=1.0-SNAPSHOT \
-Dfabric8-profile=cxf-basic-profile
Note
The arguments of the preceding command are shown on separate lines for readability, but when you are actually entering the command, the entire command must be entered on a single line.
You will be prompted to confirm the project settings, with a message similar to this one:
[INFO] Using property: groupId = org.fusesource.example
[INFO] Using property: artifactId = cxf-basic
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = org.fusesource.example
[INFO] Using property: fabric8-profile = cxf-basic-profile
Confirm properties configuration:
groupId: org.fusesource.example
artifactId: cxf-basic
version: 1.0-SNAPSHOT
package: org.fusesource.example
fabric8-profile: cxf-basic-profile
 Y: :
Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in the get-started/cxf-basic directory.

Customize the Web client test message

Customize the sample client test message, so that it uses the correct XML namespace. Edit the cxf-basic/src/test/resources/request.xml file, replacing the xmlns:ns2="http://soap.quickstarts.fabric8.io/" namespace setting by xmlns:ns2="http://example.fusesource.org/".
After editing the request.xml file, the contents should look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:sayHi xmlns:ns2="http://example.fusesource.org/">
            <arg0>John Doe</arg0>
        </ns2:sayHi>
    </soap:Body>
</soap:Envelope>

Build the Web services project

Build the Web services project and install the generated JAR file into your local Maven repository. From a command prompt, enter the following commands:
cd cxf-basic
mvn install

Initialize container security

If you have not already done so, create one (or more) users by adding a line of the following form to the InstallDir/etc/users.properties file:
Username=Password[,RoleA][,RoleB]...
At least one of the users must have the Administrator role, to enable administration of the fabric. For example:
admin=secretpassword,Administrator

Start up the container

Start up the JBoss Fuse container. Open a new command prompt and enter the following commands:
cd InstallDir/bin
./fuse
You will see a welcome screen similar to this:
      _ ____                  ______
     | |  _ \                |  ____|             
     | | |_) | ___  ___ ___  | |__ _   _ ___  ___
 _   | |  _ < / _ \/ __/ __| |  __| | | / __|/ _ \
| |__| | |_) | (_) \__ \__ \ | |  | |_| \__ \  __/
 \____/|____/ \___/|___/___/ |_|   \__,_|___/\___|

  JBoss Fuse (6.3.0.redhat-187)
  http://www.redhat.com/products/jbossenterprisemiddleware/fuse/

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.

Open a browser to http://localhost:8181 to access the management console

Hit '<ctrl-d>' or 'osgi:shutdown' to shutdown JBoss Fuse.

JBossFuse:karaf@root>

Deploy and start the WS server

To install and start up the cxf-basic Web service as an OSGi bundle, enter the following console command:
JBossFuse:karaf@root> install -s mvn:org.fusesource.example/cxf-basic/1.0-SNAPSHOT
Note
If your local Maven repository is stored in a non-standard location, you might need to customize the value of the org.ops4j.pax.url.mvn.localRepository property in the InstallDir/etc/org.ops4j.pax.url.mvn.cfg file, before you can use the mvn: scheme to access Maven artifacts.
If the bundle is successfully resolved and installed, the container responds by giving you the ID of the newly created bundle—for example:
Bundle ID: 265

Check that the bundle has started

To check that the bundle has started, enter the list console command, which gives the status of all the bundles installed in the container:
JBossFuse:karaf@root> list
Near the end of the listing, you should see a status line like the following:
[ 265] [Active      ] [Created     ] [       ] [   80] JBoss Fuse Quickstart: soap (1.0.0.SNAPSHOT)
Note
Actually, to avoid clutter, the list command only shows the bundles with a start level of 50 or greater (which excludes most of the system bundles).

Run the WS client

The cxf-basic project also includes a simple WS client, which you can use to test the deployed Web service. In a command prompt, navigate to the cxf-basic directory and run the simple WS client as follows:
cd get-started/cxf-basic
mvn -Ptest
If the client runs successfully, you should see output like the following:
Running org.fusesource.example.SoapTest
the response is ====> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://example.fusesource.org/"><return>Hello John Doe</return></ns2:sayHiResponse></soap:Body></soap:Envelope>
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.184 sec - in org.fusesource.example.SoapTest

Troubleshooting

If you have trouble running the client, there is an even simpler way to connect to the Web service. Open your favourite Web browser and navigate to the following URL to contact the JBoss Fuse Jetty container:
http://localhost:8181/cxf
To query the WSDL directly from the HelloWorld Web service, navigate to the following URL:
http://localhost:8181/cxf/HelloWorld?wsdl