This section explains how to generate, build, and deploy a simple OSGi service in the OSGi container. The service is a simple Hello World Java class and the OSGi configuration is defined using a blueprint configuration file.
In order to generate a project using the Maven Quickstart archetype, you must have the following prerequisites:
Maven installation—Maven is a free, open source build tool from Apache. You can download the latest version from http://maven.apache.org/download.html (minimum is 2.0.9).
Internet connection—whilst performing a build, Maven dynamically searches external repositories and downloads the required artifacts on the fly. In order for this to work, your build machine must be connected to the Internet.
The maven-archetype-quickstart
archetype creates a generic Maven
project, which you can then customize for whatever purpose you like. To generate a
Maven project with the coordinates,
org.fusesource.example:osgi-service
, enter the following
command:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=osgi-service
The result of this command is a directory,
, containing
the files for the generated project.ProjectDir
/osgi-service
![]() | Note |
---|---|
Be careful not to choose a group ID for your artifact that clashes with the group ID of an existing product! This could lead to clashes between your project's packages and the packages from the existing product (because the group ID is typically used as the root of a project's Java package names). |
You must customize the POM file in order to generate an OSGi bundle, as follows:
Follow the POM customization steps described in Modifying an Existing Maven Project.
In the configuration of the Maven bundle plug-in, modify the bundle instructions to export the
org.fusesource.example.service
package, as follows:<project ... > ... <build> ... <plugins> ... <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName> <Export-Package>org.fusesource.example.service</Export-Package> </instructions> </configuration> </plugin> </plugins> </build> ... </project>
Create the
sub-directory. In this directory, use your favorite text editor to create the file,
ProjectDir
/osgi-service/src/main/java/org/fusesource/example/serviceHelloWorldSvc.java
, and add the code from Example 10.3 to it.
Example 10.3. The HelloWorldSvc Interface
// Java package org.fusesource.example.service; public interface HelloWorldSvc { public void sayHello(); }
Create the
sub-directory. In this directory, use your favorite text editor to create the file,
ProjectDir
/osgi-service/src/main/java/org/fusesource/example/service/implHelloWorldSvcImpl.java
, and add the code from Example 10.4 to it.
Example 10.4. The HelloWorldSvcImpl Class
package org.fusesource.example.service.impl; import org.fusesource.example.service.HelloWorldSvc; public class HelloWorldSvcImpl implements HelloWorldSvc { public void sayHello() { System.out.println( "Hello World!" ); } }
The blueprint configuration file is an XML file stored under the
OSGI-INF/blueprint
directory on the class path. To add a blueprint
file to your project, first create the following sub-directories:
ProjectDir
/osgi-service/src/main/resourcesProjectDir
/osgi-service/src/main/resources/OSGI-INFProjectDir
/osgi-service/src/main/resources/OSGI-INF/blueprint
Where the src/main/resources
is the standard Maven location for all
JAR resources. Resource files under this directory will automatically be packaged in
the root scope of the generated bundle JAR.
Example 10.5 shows a sample blueprint file
that creates a HelloWorldSvc
bean, using the bean
element,
and then exports the bean as an OSGi service, using the service
element.
Under the
directory, use your favorite text editor to create the file,
ProjectDir
/osgi-service/src/main/resources/OSGI-INF/blueprintconfig.xml
, and add the XML code from Example 10.5.
Example 10.5. Blueprint File for Exporting a Service
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="hello" class="org.fusesource.example.service.impl.HelloWorldSvcImpl"/> <service ref="hello" interface="org.fusesource.example.service.HelloWorldSvc"/> </blueprint>
To install and run the osgi-service
project, perform the following
steps:
Build the project—open a command prompt and change directory to
. Use Maven to build the demonstration by entering the following command:ProjectDir
/osgi-servicemvn install
If this command runs successfully, the
directory should contain the bundle file,ProjectDir
/osgi-service/targetosgi-service-1.0-SNAPSHOT.jar
.Install and start the osgi-service bundle—at the Fuse ESB console, enter the following command:
karaf@root> osgi:install -s file:
ProjectDir
/osgi-service/target/osgi-service-1.0-SNAPSHOT.jarWhere
ProjectDir
is the directory containing your Maven projects and the-s
flag directs the container to start the bundle right away. For example, if your project directory isC:\Projects
on a Windows machine, you would enter the following command:karaf@root> osgi:install -s file:C:/Projects/osgi-service/target/osgi-service-1.0-SNAPSHOT.jar
Note On Windows machines, be careful how you format the
file
URL—for details of the syntax understood by thefile
URL handler, see File URL Handler.Check that the service has been created—to check that the bundle has started successfully, enter the following Fuse ESB console command:
karaf@root> osgi:list
Somewhere in this listing, you should see a line for the
osgi-service
bundle, for example:[ 236] [Active ] [Created ] [ ] [ 60] osgi-service (1.0.0.SNAPSHOT)
To check that the service is registered in the OSGi service registry, enter a console command like the following:
karaf@root> osgi:ls 236
Where the argument to the preceding command is the
osgi-service
bundle ID. You should see some output like the following at the console:osgi-service (236) provides: ---------------------------- osgi.service.blueprint.compname = hello objectClass = org.fusesource.example.service.HelloWorldSvc service.id = 272 ---- osgi.blueprint.container.version = 1.0.0.SNAPSHOT osgi.blueprint.container.symbolicname = org.fusesource.example.osgi-service objectClass = org.osgi.service.blueprint.container.BlueprintContainer service.id = 273