tags:

views:

119

answers:

3

Hi! I'm new whith OSGi, but it is interesting. Is it possible to interact between osgi bundles and java application? If it is possible, how? thanks!

The context is that I have a big Java SE application(author is another programmer) with many dependencies. First my goal is to add new functionality and second - change architecture. I'll try to use OSGi, but I don't want to write code twice, for that reason I want to write new code now as bundles. But use this new functionality from the old application.

A: 

I see OSGi as a structuring technology. You can use it to define the component structure of your application. So all of your app is effectively a collection of OSGi bundles. Hence interaction is not a problem, just different bits of your app interating in the normal way.

[Edited following comment clarification.]

You have a fundamental decision: is your OSGi code going to execute in the same process as the original or in a seperate process?

Separation implies freedom to structure the new code as you wish, exploiting OSGi, but at the cost of interprocess communication complexity and performance overheads. It's pretty likely that you will end up making substantial changes to the existing app in order to support remoting in some form. I don't see this as a great approach unless your OSGi code happens to be some kind of re-usable service that perhaps other remote clients would use.

If in the same process then I'd say that you need to bite the bullet and say that this is going to be a OSGi application. The amount of effort to take an existing app and make it run in OSGi need not be excessive.

Suppose you treated the existing application as one huge OSGi bundle? There would be some work on initialisation, but would the rest "just work"? If you do this as the first step then the real re-architecting and modularisation of the existing app is deferred. You then just expose the interfaces your new modules need, and where necessary consume services provided by the new modules. Immediately you are getting OSGi benefits by structuring the dependencies.

djna
but how to do this interaction?
EK
Please explain the problem a bit more. If you have a Java object of a class defined in one Jar that wants to use capabilities of classes defined in another Jar what do you do? OSGi doesn't change much other than some small details of getting stuff loaded - that's all documented in the OSGi tutorials. The actual method calls are just method calls. Please explain what interaction you are seeing as problematic.
djna
i'll tryi have a big java se application(author is another programmer) with many dependencies. First my goal is to add new functionality and second - change architecture. I 'll try to use OSGi, but i don't want to write code twice, for that reason i want to write new code now as bundles.But use this new functionality from the old application. just now :)
EK
Yes, it is also very interesting - to do the existing application as one huge OSGi bundle. Is it possible a piece of code of this way?Thanks!
EK
+5  A: 

Yes! Yes! and Yes! This is a perfect way to start taking advantage of OSGi and evolving towards a service based application.

It is trivial to create a framework with the 4.2 launcher API without even knowing which framework implementation you use. You get a Framework object then that is actually an OSGi Bundle and can provide you with a BundleContext. This you can use to install bundles. This all is described in the spec but you can find a lot concrete and excellent examples in Felix: http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html. Felix has been explicitly promoting embedded in apps since day one.

The hard part of this approach will be getting used to modularity and its restrictions. To be useful, you will have to share classes between OSGi bundles and your application; this requires explicit exporting of these shared packages from your application using the org.osgi.framework.systempackages.extra property. This property is the Export-Package header for your application.

Importing packages from bundles in the framework is not possible due to the class loading model in Java. This means your application code can only use services from the framework where the packages for those services are on the apps classpath.

The result of this is that new functionality tend to drift to bundles where there is full visibility: both the exported app packages as well as any bundles. However, this is probably exactly what you want.

So be aware of this potential pitfall. Embed, and then over time migrate all your code to bundles so that your application becomes only an OSGi launcher. However, be very aware of your the packages shared between the two environments.

Good luck and let us know how this goes.

Peter Kriens
thanks, i'll try to use this way
EK
A: 

An application build with OSGi can interact in the same way as just two normal (Java) applications. So, by loading / saving files. Or when one of them is created as an OSGi http server, then just communicate through http with that (OSGi) server. Just think of it as you used to do, without OSGi included.

Verhagen