Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OSGi in a desktop standalone application

Tags:

java

swing

osgi

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.

like image 951
Benjamin Avatar asked Dec 15 '11 10:12

Benjamin


People also ask

What are the benefits of using OSGi?

Benefits of OSGi OSGi modularity provides standard mechanisms to address the issues faced by Java EE applications. The OSGi framework provides the following benefits: Applications are portable, easier to re-engineer, and adaptable to changing requirements.

What is an OSGi application?

The OSGi (Open Service Gateway Initiative) specification is a Java framework for developing and deploying modular software programs and libraries. The framework was originally managed by the OSGi Alliance, an open standards organization.

How does OSGi container work?

How does OSGi work? OSGi is a set of specifications that define a dynamic component system for Java. These specifications allow for a development model in which an application is composed of several components, and then packed into bundles. These components communicate locally and through the network via services.

What is difference between JAR and OSGi bundle?

The key difference with OSGi is that a JAR is now all private, adding metadata in the manifest makes it a bundle that can safely share with other bundles. OSGi makes sure violations are detected ahead of time. yes, it is a normal JAR. However, in a JavaEE app it will of course act as a JAR and not as a bundle.


2 Answers

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.

like image 55
Neil Bartlett Avatar answered Oct 19 '22 20:10

Neil Bartlett


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();
    .....
}
like image 44
Valchev Avatar answered Oct 19 '22 21:10

Valchev