Hi all! Today we going to talk about OSGi, namely Apache Felix implementation, and creating JAX-RS web-services using Apache CXF implementation. I am not a fan of heavyweight solutions, but learning these technologies is a job necessity, so I decided to write this small tut just to save my sandbox experiments.
Note: you can find this tut sources on GitHub
Note: you can find this tut sources on GitHub
[Tools]
For this tut you will need the following tools installed
- Java SDK of version 1.6
- Apache Maven
- Apache Karaf
[Installing Karaf]
Download and unpack Apache Karaf distribution. Run Karaf console by invoking {KARAF_DIR}/bin/karaf (or {KARAF_DIR}\bin\karaf.bat for Windows). Issue the following command:
karaf@root> features:install webconsoleThis command will install Karaf web-console, which is a convinient web-interface for managing karaf server. You can visit http://localhost:8181/system/console/bundles to access it (default creds are karaf/karaf). Now issue the two following commands:
karaf@root> features:chooseurl cxf-dosgi 1.4.0
karaf@root> features:install cxf-dosgi-discovery-distributed
These commands will install CXF feature (all necessary CXF bundles will be installed). Now, if you didn't get any errors during execution of the commands above, then your Karaf is ready for the tut.
[Exploring Maven Pax Plugin]
OSGI development tools are scarce at the present moment and IDEs lack adequate integration. Too bad. Well, at least we have several pretty decent Maven plugins, which can be a lot of help, namely: Pax Plugin, Bundle Plugin and Karaf Plugin. In this tutorial we will use Pax plugin to create our project structure and to create our test bundle.
Lets start by issuing the following command:
mvn org.ops4j:maven-pax-plugin:create-project \
-DgroupId=ogsicxf \
-DartifactId=osgicxf \
-Dversion=1.0-SNAPSHOT
For those, who have used Maven before, it should be self explanatory: this command uses pax plugin from maven repo to create an OSGi project with group ID "osgicxf" and artifact ID "osgicxf" of version 1.0-SNAPSHOT.
OK, now let's cd into directory of our newly created OSGi project where root pom.xml file is situated and issue the following command
mvn pax:create-bundle \
-Dpackage=osgicxf \
-DbundleName=osgicxf-bundle \
-Dversion=1.0-SNAPSHOT
This command will create OSGi bundle with BundleActivator and sample OSGi service.
[Writing RESTful web-service]
Let's now write a sample "Hello World" web-service using JAX-RS spec. First, let's add mentioned spec as maven dependency in our bundle's pom.xml:
javax.ws.rs
javax.ws.rs-api
2.0-m10
This will add a jar file containing JAX-RS annotations to our classpath. Now we can write our service interface:
@WebService
@Produces(MediaType.TEXT_PLAIN)
public interface IHelloWorldService {
@GET
@Path("/")
String sayHello();
}
The implementation of this interface will be brain-dead simple:
public class HelloWorldService implements IHelloWorldService {
public HelloWorldService() {
System.out.println(HelloWorldService.class.getName()+" is up and running");
}
@Override
public String sayHello() {
return "Hello World from "+HelloWorldService.class.getSimpleName();
}
}
[Bringing to life]
One of the convinient ways to bring our service to life (read, instantiate it, connect it to CXF servlet which in turn will be mapped in Karaf's Jetty server) is to create blueprint XML context file, which will be parsed by Apache Aries services in Karaf. Now, blueprint -- is an OSGi alliance spec, that describes dependecy injection framework in OSGi container and Apache Aries is a blueprint implementation. The good thing is that blueprint xml config can contain JAX-RS and CXF specific namespaces, which will greatly simplify configuring web services. This is how our blueprint.xml will look like:
That's pretty much it.
[Deploying into Karaf]
Deployment now should be extremely easy: just issue mvn clean install on bundle's pom.xml and copy jar file to {KARAF_DIR}/deploy. You can now navigate to Karaf web-console and see you bundle installed.
Now head to http://localhost:8181/cxf/hello to call the only method of our web service.
So this concludes our simple tut. Hope you will put this information to good use.
