The basics of Eclipse plug-in development (5) Making OSGi bundles

Source: Internet
Author: User

Reprint Source: Http://www.cnblogs.com/liuzhuo

1. Creating an OSGi Project

First open the New Project Wizard and choose Create Eclipse plug-in project. SELECT [OSGi Framework] in the [Target Platform] option.

Figure 5-1 Creating a plug-in project

Select when selecting a template, and click Done after the Hello OSGi bundle.

Figure 5-2 Selecting an OSGi template

After the project is generated, click [Launch Frame (Launch the framework)] in the manifest editor to run the newly generated OSGi Bundle. There may be a large number of errors after execution. The reason is that the osgibundle in Eclipse is registered in the OSGi framework, but the UI-related parts do not start. We go to [run Configuration], remove all bundle bindings, and select only the bundles we are currently testing. (My own project even canceled the operation also error: (~ ~)

When the framework is running, enter the start and stop instructions to the console window to see the effect.

Figure 5-4 Hello OSGi

2. OSGi Service and Tracker

In the previous section we made a simple bundle that outputs messages at startup and stop. The code is a simple implementation of the SART () and Stop () methods of the Bundleactivator interface, in which the information is typed using SYSTEM.OUT.PRINTLN.

Code 1

public class Activator implements Bundleactivator {public void start (Bundlecontext context) throws Exception {System.out. println ("Hello world!!");} public void Stop (Bundlecontext context) throws Exception {System.out.println ("Goodbye world!!");}}

The OSGi bundle works by providing a start and stop approach to lifecycle management in the OSGi framework. The OSGi framework has a service feature that can be used as a common (Common) for multiple bundles, called OSGi service.

Code 2 The simplest OSGi Service

public class Osgiservice {public void dosomething () {System.out.println ("Running Common Service");}}

Code 3 performing OSGi Service

public class Activator implements Bundleactivator {public void start (Bundlecontext context) throws Exception {System.out. println ("Hello world!!"); /Registered OSGi Servicecontext.registerservice (OSGiService.class.getName (), New Osgiservice (), New Properties ());} public void Stop (Bundlecontext context) throws Exception {System.out.println ("Goodbye world!!"); /Generate service Trackerservicetracker tracker = new Servicetracker (context,osgiservice.class.getname (), NULL); Tracker.open ();//Get Serviceosgiservice service = (osgiservice) tracker.getservice ();//Execute serviceservice.dosomething ( );}}

Let's look at the effect of the above bundle execution.

Code 4

3. Installation and update of Bundel

The OSGi framework can also install and unload bundles without restarting the JAVAVM. Below we make a new bundle,newbundle output a line of messages at start and stop.

Code 5 Newbundle

public class Activator implements Bundleactivator {public void start (Bundlecontext context) throws Exception {System.out. println ("New bundle start!!");} public void Stop (Bundlecontext context) throws Exception {System.out.println ("New bundle stop!!");}}

Export the project to the C packing directory and launch the OSGi console input install File:\\\c:\plugins\newbundle_1.0.0.201008182238.jar. Then confirm the bundle installation through the SS command.

Code 6 installation and uninstallation of bundles

Osgi> Install File:\\\c:\plugins\newbundle_1.0.0.201008182238.jarbundle ID is 321osgi> SS NewFramework is Launched.id      State       Bundle321     installed   newbundle_1.0.0.201008182238osgi> start newbundlenew Bundle start!! osgi> Uninstall Newbundlenew Bundle stop!! osgi> SS Newframework is launched.id      State       bundleosgi>

The above code shows that the Stop method is called first when the bundle is unloaded. A simple summary of the description of the bundle's manifest file is given in the following table:

Project name Description
Manifest-version The manifest file for the jar package specifies the version number, which is typically 1.0
Bundle-manifestversion The bundle's manifest file specifies the version, usually 2
Bundle-name The name of the bundle
Bundle-symbolicname The Synbo name of the bundle, which is registered with this name in OSGi
Bundle-version The version of the bundle. It is possible in OSGi that multiple versions of the same bundle coexist
Bundle-activator Managing the class name of the bundle life cycle
Bundle-vendor Defines the name of the organization that made the bundle
Bundle-activationpolicy Specifies the policy that the bundle starts.
Import-package Packages referenced by the bundle are specified
Export-package The bundle is assigned to the package exposed by his bundle.
Required-bundle Bundles referenced by the bundle are specified

The basics of Eclipse plug-in development (5) Making OSGi bundles

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.