Reputation: 3448
I am trying to write an OSGi based desktop application. I have a Swing JFrame and I want to add possibilities to add modules (other bundles). I walked through EclipseZone OSGi at JSig tutorial, but every application is started from OSGi Framework (in this case, Knopflerfish OSGi Desktop).
So my question is, whether there is an option to start the application without visible OSGi frame? I knwo, that from code, it is possible to change properties of a bundle, but how to change properties of a framework that way? (e.g. default bundle storage location, default action when bundle is in location etc.?)
public class MainFrame extends ServiceTracker implements BundleActivator {
public MainFrame(BundleContext context, JToolBar toolBar) {
// select, which services is the bundle tracking
super(context, JMenu.class.getName(), null);
}
@Override
public void start(BundleContext context) throws Exception {
//display a JFrame
}
@Override
public void stop(BundleContext context) throws Exception {
//hide a JFrame
}
@Override
public Object addingService(ServiceReference reference) {
// Process a Service and return a JMenu
return new JMenu();
}
@Override
public void removedService(ServiceReference reference, Object service) {
// remove a JMenu from a JFrame
}
public static void main(String[] args) {
// ????????????????????????????????????????????
// ????????????????????????????????????????????
}
}
I've written a class above (I've posted just a sketch), but I have no idea, what to write in a main() function. This bundle works fine in Knopflerfish OSGi Desktop, but I want it to run without it.
Upvotes: 6
Views: 6561
Reputation: 23948
Your code is basically okay, but it sounds like you want more control over the OSGi framework itself. In other words, you want to know how to launch an OSGi framework and start your bundle. The problem you have currently is that you're using somebody else's launcher (the Knopflerfish one) which includes the KF GUI Console, and you are using that to install and start your bundle. However none of that is necessary.
In AValchev's answer he talks about starting Equinox with java -jar org.eclipse.osgi.jar -console
. The trouble with that approach is that it gives you an empty OSGi framework, so you will have to type commands into the OSGi shell in order to install and start your bundle... also not ideal!
I think you should write your own launcher. This is actually very simple and can be done in a way that is completely independent of any particular OSGi framework implements. I wrote about this in a blog post a little while ago.
In pseudo-code, your launcher application should look something like this:
public static void main() {
1. get a FrameworkFactory using java.util.ServiceLoader.
2. create an OSGi framework using the FrameworkFactory
3. start the OSGi framework
4. Install your bundle(s).
5. Start all the bundles you installed.
6. Wait for the OSGi framework to shutdown.
}
In your question you specifically ask about setting the bundle storage location. This can be done by setting the Constants.FRAMEWORK_STORAGE
property in the Map that you pass into FrameworkFactory.newFramework
method.
Upvotes: 13
Reputation: 24262
Eclipse RCP is a good option for writing application clients. It is entirely based on OSGi, and provides a rich set of tools for UI programming as well. Eclipse also provides the development environment for building them.
Once built, you will have a standalone application that just happens to be based on OSGi, but requires no interaction with OSGi to run, which is what I believe you are trying to accomplish.
Upvotes: 1
Reputation: 38132
I'm not sure what exactly you want to do, but if you're looking for a modular way to write Swing applications then have a look at the NetBeans Platform. The core module system isn't OSGi but it can "talk" to OSGi modules if needed.
Upvotes: -2
Reputation: 1530
If you want to use Swing components you don't need Knopflerfish OSGi Desktop at all. Just download EclipseRT Starter Kit and put your plugins there.
Another very simple way of using OSGi is:
java -jar org.eclipse.osgi.jar -console
With this command you've started equinox and you can install your bundles from the console.
You can find very good tutorial here
In your case just put JFrame initalzation in start() method :
@Override
public void start(BundleContext context) throws Exception {
JFrame jf = new JFrame();
.....
}
Upvotes: 1