Karaf is lightweight container for OSGI. In karaf, beside OSGI Core, we have other additional feature for support our application, in other words. Karaf is osgi container that ready to used. Not like Felix or Equinox, they are just OSGI core runtime, if you want to used it with integration for example with apache camel or apache cxf, you have to add those feature manually. Here is picture that i take from karaf website
JAXB stands for Java Architecture for XML Bindings. In a nutshell, JAXB used for convert Java Object to XML and vice versa. We called it marshalling to convert Java object to XML and unmarshalling to convert XML to Java object. JAXB bundled in Java 1.6++, so we do not need another external libraries (but sometimes I loved to adding testing and logging framework). First, you have to know the technology that I used in this tutorial are :
- Maven 3
- TestNG (for unit test)
Some of you would frown and said “hmm, why you put commons-io on this tutorial?”. Simple, I used it in unit-test to get xml file from resource folder and convert to String, I know it can be done with the classic inputstream ways. but remember to not reinventing the wheel 🙂
By default, CXF will mapping CXFServlet to /cxf context path. I used Servicemix 4.5 which using CXF 2.7.3 inside. its define static in osgiservlet.xml in cxf-rt-transport-http (FYI, for CXF 2.7.3, they are using transport-http ver. 2.6.3). So, based on default configuration, all our CXF service will result like http://localhost:8181/cxf/myservice, this is not good. because sometimes you want define service itself.
I ever had a problem when want to using Jaxb in Servicemix 4.x which used OSGI. Like documentation describe on this page, we will know how to use Jaxb with apache camel. What we have to do is adding line below :
DataFormat jaxb = new JaxbDataFormat("com.foo.bar"); from("activemq:My.Queue"). unmarshal(jaxb). to("mqseries:Another.Queue");
Above endpoint will route the jaxb unmarshal result to another queue. JaxbDataFormat will automatically search entity with JaxB @XMLRootElement inside package. But you will get a problem when implemented those code in OSGI. here is the error message :
Have you ever get confused when trying TestNG for the first time? especially when want to make a unit test with some execution order? I have :). So, I made a little example to encounter those problem. As we know, TestNG has some annotation to point our method/class and indicate when the test will running. Some of which are :
- @BeforeSuite: The annotated method will be run before all tests in this suite have run.
- @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
- @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
- @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
- @BeforeMethod: The annotated method will be run before each test method.