How to use OSGi from java application
I am new to OSGi but this is interesting. Is communication possible between osgi packages and java applications? If this is possible, how? thanks!
The context is that I have a large Java SE application (authored by another programmer) with many dependencies. My goal first is to add new functionality and a second change architecture. I will try to use OSGi, but I don't want to write the code twice, so I want to write new code now as packages. But use this new functionality from your old app.
a source to share
I see OSGi as a structuring technology. You can use it to define the structure of a component in your application. So your whole application is a collection of OSGi packages. Hence, interoperability is not an issue, and the normal bits of your application interact in the usual way.
[The following explanation of the comment has been edited.]
Do you have a foundational solution: Will your OSGi code run in the same process as the original, or in a separate process?
Separation implies the freedom to structure new code as you wish using OSGi, but at the expense of interprocess communication and performance complexity. It is likely that you will eventually make significant changes to your existing application to support some form of remote access. I don't see this as a great approach unless your OSGi code is some kind of reusable service that other remote clients can use.
If in the same process I say that you have to bite the bullet and say that it will be an OSGi app. The amount of effort to use an existing application and run it in OSGi should not be overwhelming.
Suppose you viewed an existing application as one huge OSGi package? There will be some initialization work, but the rest "just works"? If you do this as a first step, the actual restructuring and modulation of the existing application is deferred. Then you simply expose the interfaces your new modules need and, where necessary, consume the services provided by the new modules. You immediately get the benefits of OSGi by structuring your dependencies.
a source to share
Yes! Yes! and yes! This is the perfect way to get started using OSGi and move on to an app-based service.
It's trivial to create a framework with Launch API 4.2 without even knowing which framework implementation you are using. You get a Framework object, which is actually an OSGi package and can provide you with a BundleContext. This can be used to install packages. This is all covered in the spec, but you can find many specific and great examples on Felix: http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html . Felix has clearly advertised built-in apps from day one.
The hard part of this approach is getting used to modularity and its limitations. To be useful, you will have to share classes between OSGi packages and your application; this requires explicitly exporting those 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 packages within frameworks is not possible due to the class loading model in Java. This means that your application code can only use services from the environment where the packages for those services are in the application's classpath.
The result of this is that new functionality tends to drift towards packages where there is full visibility: both the exported application packages and any packages. However, this is probably exactly what you want.
Be aware of this potential pitfall. Paste and then eventually move all your code into packages so that your application becomes only an OSGi launcher. However, be very aware of your packages being shared between the two environments.
Good luck and let us know how it goes.
a source to share
Building an OSGi application can interact in the same way as two regular (Java) applications. Thus, loading / saving files. Or when one of them is created as an OSGi HTTP server, then just communicate via HTTP to that server (OSGi). Think about it like before without OSGi enabled.
a source to share