Example using Jersey (REST resource), OSGi (Apache Felix), and Maven (build and deploy)? - maven

I seem to be really struggling here. What I want to do (in this order) is:
1) Build a RESTful resource using a Jersey application and resource w/annotations (this is not the issue).
2) Package, install, and start that bundle into the Felix OSGi container, as an HTTP service, including dependencies.
3) Package, install, and start a WAR in the OSGi container that may incorporate #2 as a dependency.
And I would like to be able to do all of this using Maven.
I cannot seem to find a working example of even the individual steps, especially involving Maven, that work let alone the combination of those steps. I have tried cobbling together various q&a from across the web with varying levels of success but not an end-to-end working example yet.
Any pointers would be appreciated...

I do not have an example for Jersey but I have a tutorial for CXF with Apache Karaf (which uses Felix). It shows how to create a Rest service and build it with maven. Using Apache Karaf you can then deploy the bundle directly from the maven repo. Moving this to Jersey probably just means to exchange the lib and use another blueprint config to initialize the rest service.
Apache Karaf also allows to deploy wars and wabs but I have not yet tested them.

Apache Stanbol does most or all of this (not sure if the war packaging is included out of the box) to implement its RESTful services.
You'll have to dig through its codebase but searching for Jax-RS annotations in there should point you to the right places.

I also am really struggling with exactly what you are attempting to do. So far, I seem to be really close but alas not quite there, here's what I've been doing:
creating a War with Maven that defines my Jersey Resource's
bundling it with maven bundle plugin ( see section Adding OSGi metadata to existing projects without changing the packaging type ). Which allows me to run the restlets in Tomcat and test.
define a target in Eclipse that includes resources from my locally defined p2 site which I create with the p2-maven-plugin plugin. In this way I can gather up any of the dependencies from the WAR project into a p2 site, which I can deploy to an Eclipse defined target
Where I am stuck is trying to register the Jersey Resource's as services, for which I've tried:
using the JAX-RS OSGI connector, for which I eventually gave up on because it uses glassfish jersey which seems to export a version 2 API of Jersey when Jersey hasn't even defined a version 2 API yet. This caused package resolution problems when I wanted to use version 1.17 of Jersey libs.
registering the Resources using a Whiteboard a la Apache Felix HTTP Service, my current approach which doesn't seem to work yet.
And, finally, if the preceding doesn't work I'll try Amdatu
Another route I might try is from the Jersey project OSGI chapter

I tried this combination and made it up and running -
1- Modularity Specification-->OSGi specification
2- OSGi implementation-->Apache Felix 4.4.0
3- OSGi Runtime-->Apache Karaf 3.0.3
4- Software Architecture Specification of REST – JAX-RS
5- JAX-RX implementation-->Apache CXF – 2.7.5
You can refer this nice tutorial - http://java.dzone.com/articles/building-cxf-rest-service-osgi
Now, I am also planning to move to Jersey from CXF as the Jersey is the light wait reference implementation of JAX-RS. Now I am planning to use Jersey with Apache Felix/Apache Karaf. You can install Jersey in Karaf and deploy your Jersey Rest Web Services as Bundle. You can refer this tutorial - https://vzurczak.wordpress.com/2014/09/30/web-applications-with-osgi-working-with-jersey/

Here's a good example on github : https://github.com/ddragosd/jax-rs-on-karaf

Related

Is there quickstart archetype for jboss fuse using spring di

Since blueprint seems to be a pain in the ass (as a dependency injection framework) we want use Spring DI (Java based) for future service implementaions (Camel based). At the moment we deploy all services on a fuse 6.3 in fabric mode.
Red_Hat_JBoss_Fuse-6.3-Developing_and_Deploying_Applications-en-US.pdf states:
A typical Red Hat JBoss Fuse application is based on a specific development model, which is based
around the Java language, Spring or blueprint dependency injection frameworks...
Is there a good maven archetype to start with? I found those fabric8 archetypes but they seem to target fabric8 v2 - a complete different world, isn't it?
You can use camel-archetype-spring archetype with org.apache.camel.archetypes group.
example:
mvn archetype:generate -DgroupId=com.test -DartifactId=camel-spring-app -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring -DinteractiveMode=false
In meantime i found
https://github.com/FuseByExample/
this examples helped much. the most tricky part is the configuration of bundle plugin and fabric8.

How to develop a JAX-RS web service as a jar

I am looking out a sample examples where i can develop as Rest based web services and bundled them as a jar not as war/ear etc.
These Jars are deployed in server containers(JBoss) and consumed.
Also, if i can develop using Spring with Rest Based seems to have a nice feature.
I am not getting any examples/approach to start off with. I looked into Apache CXF,Axis 2. All these are bundled as war files. But i need it as a jar file

CQ5 Spring integration

Is the any way to perform integration CQ5 platform with Spring framework?
I would like to use Spring IoC capabilities to make my code more clear and efficient.
UPDATE
Hello againg, seems that I found solution.
Guys here developed Slice framework that really redices amound of code and made CQ5 development easier.
You probably want to check out Eclipse Gemini Blueprint, or it's original incarnation Spring Dynamic Modules. The Blueprint project basically gives you an easy way to create Spring enabled bundles in an OSGi environment.
As shsteimer mentions, Spring distributions before 3.2.0 were OSGi bundles, so could be dropped into an OSGi environment and you could probably use them directly. Spring 3.2.0 bundles and above are now available through the SpringSource ERB. However, Blueprint gets around or helps with some of the boilerplate OSGi stuff that you would otherwise have to do.
On a past project, I was able to get Spring JDBC working inside of CQ (to support some legacy code so we didn't have to re-write it). My memory is that the spring jar files already come "OSGI-ified" and so it was just a matter of figuring out all the layers of dependency needed for JDBC to work inside of CQ, and adding all the jars to the repository in an /apps/myApp/install folder.
Long story short, I'm not sure about IoC, but you might check to see if it's already packaged as an OSGI version which you can simply use without too much hassle.

How to deploy and access a Jersey REST OSGi bundle into Adobe CQ?

I have an OSGi bundle (running under Felix) using Jersey and providing a RESTful resource. This all works fine and I can invoke the service through my browser and get back a JSON response.
Now, what I would like to do is deploy that same bundle into Adobe CQ and through CQ access the resource (i.e. /mycq/services/my-service) similarly.
Any pointers on how to deploy an existing OSGi bundle(s) into CQ and more specifically 'access' the Jersey REST resource in the bundle?
The Maven Sling Plugin allows you to deploy an OSGi bundle to a local or remote running instance of CQ.
Source: Deploying an OSGi Bundle
Apache Stanbol is using Jersey in an OSGi environment that's fairly similar to Apache Sling's on which CQ5 is based. Looking at how Jersey is integrated there might help.
https://issues.apache.org/jira/browse/SLING-2192 also has some experimental code that might help.
Note that Sling expects to take over the whole URI space, which is needed especially for access control, so some bridging code is probably needed.

Basic Apache Camel FileRoute deployed as OSGi bundle

I'm trying to do some "hello world" example with Apache Camel. Just a simple route, deployed as OSGi bundle to Apache Karaf. And it's crazy, too many things to learn at once, Camel itself, integration with spring, integration with OSGi and Karaf...ugh.
I'm trying to follow the idea from here: http://www.andrejkoelewijn.com/wp/2008/10/19/simple-camel-dsl-osgi-bundle-example/.
I've created the bundle, the package with FileRoute.java file, etc, but I can't produce valid manifest.mf file, it's generated by "bundle plugin for maven" - so the POM file is used to generate the manifest file.
What's the problem is this in the manifest file, line with import packages:
Import-Package: com.company.foo.bar.demo;version="0.1",org.apache.ca
mel.builder;version="2.10",org.apache.camel.model;version="2.10",org.
apache.commons.logging
With error: "No available bundle exports package 'org.apache.camel.builder'" (and .model package).
Well, I understand what is the problem, but…How to expose these packages for the bundle?
Next I have there another bundle with Apache Camel context, so I thought that good idea is to expose the packages from there, through "export package", but it doesn't work, the camel bundle can't be build then.
I'm quite lost :( I'd really appreciate help or hint.
Thanks!
If you use Apache Camel 2.10 or better then the Maven archetypes is a good place to start a new Camel project. We have archetypes that can create OSGi based project for either spring-dm or OSGi blueprint. You chose which one you want to use. The project is then ready for OSGi as it setup a maven plugin that generated the needed osgi stuff for you. Only in more advanced cases you would need to tweak this plugin.
See details at: http://camel.apache.org/camel-maven-archetypes.html
If you use Maven 3.0.x then its much easier as you can just type
mvn archetype:generate
Then it runs in interactive mode, then type camel to filter only camel archetypes.
And then select the number for the archetype. eg for example the spring-dm to use OSGi with Spring.

Resources