This section explains how to modify an existing Maven project for a Apache Camel application, so that the project generates an OSGi bundle suitable for deployment in the Fuse ESB OSGi container. To convert the Maven project, you need to modify the project POM file.
To configure a Maven POM file to generate a bundle, there are essentially two
changes you need to make: change the POM's package type to bundle; and
add the Maven bundle plug-in to your POM. For details, see Modifying an Existing Maven Project.
In addition to the basic POM changes required for a generating bundle, Apache Camel
requires you to customize the instructions for importing packages in the
maven-bundle-plugin configuration. The Apache Camel application has an
implicit dependency on the org.apache.camel.osgi package and this
dependency must be declared using the Import-Package element.
For example, the following sample POM file shows how to define the
Import-Package element in the instructions for the Maven bundle
plug-in:
<project ...>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Import-Package>*,org.apache.camel.osgi</Import-Package>
<Private-Package>org.apache.servicemix.examples.camel</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>Where the Import-Package element contains the instructions that
determine which Java packages are imported by the bundle. The wildcard,
*, instructs the Maven bundle plug-in to import all of the packages
explicitly referenced by the current bundle code and the explicitly listed
org.apache.camel.osgi package is then added to the calculated list
of packages.
There are two kinds of file that you can use to configure your project:
Spring configuration—in the standard Maven
directory layout, Spring XML configuration files are located under
.ProjectDir/src/main/resources/META-INF/spring
Blueprint configuration—in the standard Maven
directory layout, blueprint XML configuration files are located under
.ProjectDir/src/main/resources/OSGI-INF/blueprint
If you decide to use the blueprint configuration, you can embed
camelContext elements in the blueprint file, as described in Blueprint configuration file.
If you decide to configure your Apache Camel application using blueprint, you must
ensure that the camel-blueprint feature is installed (it is not
installed by default). If necessary, install it by entering the following console
command:
karaf@root> features:install camel-blueprint
The OSGi Configuration Admin service defines a mechanism for passing configuration settings to an OSGi bundle. You do not have to use this service for configuration, but it is typically the most convenient way of configuring bundle applications. Spring DM provides support for OSGi configuration, enabling you to substitute variables in a Spring XML file using values obtained from the OSGi Configuration Admin service.
Example 6.1 shows how to pass the value
of the prefix variable to the constructor of the
MyTransform bean, where the value of prefix can be set
by the OSGi Configuration Admin service:
Example 6.1. Using OSGi Configuration Properties in Spring XML
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:osgi="http://camel.apache.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
... >
...
<bean id="myTransform" class="org.fusesource.example.MyTransform">
<property name="prefix" value="${prefix}"/>
</bean>
<osgix:cm-properties id="preProps" persistent-id="org.fusesource.example">
<prop key="prefix">MyTransform</prop>
</osgix:cm-properties>
<ctx:property-placeholder properties-ref="preProps" />
</beans>The syntax, ${prefix}, substitutes the value of the
prefix variable into the Spring XML file. The OSGi properties are
set up using the following XML elements:
osgix:cm-propertiesTo integrate Spring properties with the properties from the OSGi
Configuration Admin service, insert an osgix:cm-properties
element into the Spring XML file. This element creates a bean that gets
injected with all of the properties from the OSGi
ManagedService instance that is identified by the
persistent-id attribute. The minimal configuration
consists of an empty osgix:cm-properties element that sets
the persistent-id attribute and the id
attribute—for example:
<osgix:cm-properties id="preProps" persistent-id="org.fusesource.example"/>
For an example of how the persistent ID relates to OSGi configuration settings, see the example in Add OSGi configurations to the feature.
If you want to define defaults for some of the properties in the
Spring XML file, add prop elements as children of the
osgix:cm-properties element, as shown in Example 6.1.
ctx:property-placeholderProperty placeholder is a Spring mechanism that enables you
to use the syntax, ${,
to substitute variables in a Spring XML file. By defining a
PropName}ctx:property-placeholder element with a reference to
the preProps bean (as in Example 6.1), you enable the
property placeholder mechanism to substitute any of the variables from
the preProps bean (which encapsulates the OSGi
configuration properties) into the Spring XML file.