Tuesday, October 26, 2010

OSGi Bundle of Hello World



This is "Hello, OSGi" series of the second part. OSGi has been introduced prior to what will continue on the following article describes the content, about a simple OSGi Bundle: Hello World are developed.

51CTO Editors: OSGi entry and practice of all captures

Develop a simple Hello World of OSGi Bundle (OSGi binding package)

In OSGi, the software is released in the form of Bundle. A Bundle from the Java classes and other resources of its Bundle for other services, you can also import other Bundle in Java packages; Meanwhile, OSGi's Bundle where the equipment can provide some of its features. Eclipse for the development of OSGiBundle provides excellent support, which not only provides a wizard to create the OSGi Bundle, and also provides embedded Equinox container, you can use the OSGi container implementation and debugging plug-ins. Please note that each Eclipse plug-in essence, is an OSGi Bundle, just add this OSGiBundle some Eclipse-specific code only. Let us look at how to use Eclipse to develop a simple OSGi the HelloWorld Bundle.

3.1. New Bundle

1) In Eclipse, click on "File-> New-> Project" menu, you will see a new project to create dialog boxes;

2) In the New Project dialog box, select the "Plug-inProject (plug-in project)" and click "Next (Next)" button, you will see the plug-in project dialog box;

3) In the plug-in project dialog box, type the following values:

Project Name (Project Name): com.javaworld.sample.HelloWorld

Target Platform (target platform): an OSGiFramework-> Standard (OSGi Framework -> standard)

4) the requirements of other input values using default values and click "Next (Next)" button, you will see the plug-in context of the dialog box;

5) In the plug-in the context of the dialog box, select the default values and click "Next (Next)" button;

6) In the Template dialog box, select the "Hello OSGiBundle (Hello, OSGi package)" template, then click "Finish (complete)" button to complete the project.

Eclipse will take a few seconds Clock Generation HelloWorld Bundle template code, it built two files: Activator.java and MANIFEST.MF, the following, let us look at these two files:

3.1.1. Activator.java file

Source List 1.Activator.java

package com.javaworld.sample.helloworld; importorg.osgi.framework.BundleActivator; importorg.osgi.framework.BundleContext; publicclass Activator implements BundleActivator (public void start (BundleContext context) throws Exception (System.out.println ("Helloworld" );) public void stop (BundleContext context) throws Exception (System.out.println ("GoodbyeWorld");))

If you want your Bundle developed in their own startup or shutdown notice, you should create a new class, make it happen BundleActivator interface, the same time, you also need to comply with the following rules:

This class implements BundleActivator interface must have a public, non-constructor with parameters, so, OSGi framework will call the class of Class.newInstance () method to create this BundleActivator object;

Activator class vessel will call start () method to start the Bundle, therefore, we can start () method to perform initialization of the operation some resources, for example, we can get in the way the database connection, for later use. The start () method the only parameter is a BundleObject object, Bundles can be the object and the OSGi framework for communication, we can get from the object information related to OSGi container; If a Bundle thrown, the container will be the home for the "stopped (stopped)" status, this time, this Bundle can not provide services outside.

If we are to close a Bundle, the container will call the Activator class in the stop () method. Therefore, we can stop () method to perform clean-up tasks some resources, such as the release of the database connection.

Once in place Activator class, you can MANIFEST.MF file to the package to pass the legal name of the container. Here we look at the MANIFEST.MF file.

3.1.2. MANIFEST.MF file

Bundle the file is the deployment descriptor, its format and the normal JAR file in the MANIFEST.MF file in the same package, it consists of a series of attributes and values corresponding to the composition of these attributes, the attribute name in the beginning of each line, we can say its head for the property. OSGi specification, you can use the property description of your head to the container Bundle. Your HelloWorld Bundle of MANIFEST.MF file should look as shown in Listing 2:

Source Code Listing 2. Hello World Bundle in the MANIFEST.MF file

Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: HelloWorld Plug-in Bundle-SymbolicName: com.javaworld.sample.HelloWorld Bundle-Version: 1.0.0 Bundle-Activator: com.javaworld.sample.helloworld.Activator Bundle -Vendor: JAVAWORLD Bundle-Localization: plugin Import-Package: org.osgi.framework; version = "1.3.0"

We take a look at the properties used in this document head:

Bundle-ManifestVersion

The property first told OSGi container, the Bundle will follow the OSGi specification, value of 2 indicates the Bundle and the OSGi specification, version 4 compatible; if the attribute value is 1, then said of the package and the OSGi version 3 or earlier versions.

Bundle-Name

Bundle-based head of the property defines a short, can read the name;

Bundle-SymbolicName

This property defines a head-centered Bundle unique, non-localized name; When you need to access from another a specified Bundles Bundle, you must use the name.

Bundle-Version

The first property gives the version number of this Bundle.

Bundle-Activator

The first gives the property used in this Bundle listener class name, the attribute value is optional. Listeners will Activator in the start () and stop () method of monitoring. In Listing 2, the value of the property first com.javaworld.sample.helloworld.Activator.

Bundle-Vendor

The first property is the expression of the issuers Bundle.

Bundle-Localization

The attribute header contains the localization files in this Bundle location, our HelloWorld Bundle of documents is not localized, but the Eclipse IDE automatically generates the property is still the first

Import-Package

The first attribute defines the Bundle package introduced in Java, I will manage this dependence later sections explain in detail the problem. Now, HelloWorld Bundle is ready, let's run and see its output.

3.2. Run Bundle

I mentioned earlier, Eclipse IDE has a built-in EquinoxOSGi container, you can use it to execute or debug OSGi Bundle. Follow these steps just HelloWorld Bundle:

1) Click Run脿Run ... menu (translators note, in Eclipse3.3, click Run脿Open Run Diglog ... menu);

2) Eclipse will open the "Create, manage and run configuration (new, management and operation of configuration)" dialog box, double-click "EquinoxOSGi Framework" button, Eclipse will open the run-time configuration dialog;

3) In the above dialog, the Name (name) values of the input box to HelloWorld Bundle;

4) You will notice that in the Workspace plug-in directory, there is a plugin called com.javaworld.sample.HelloWorld, select it; in TargetPlatform (target platform), please ensure that the org.eclipse.osgi plug-in is selected . Your Run (Run) dialog box should appear as shown in Figure 1:






Figure 1. HelloWorld Bundle run configuration

5) Now, click the Run (Run) button, you should see the console view, print out the "HelloWorld". In fact, Eclipse is OSGi console to open the console view.

3.2.1. OSGi Panel

OSGi OSGi container console is a command line interface, you can in the console to start, stop, install, update and delete Bundles. In EclipseIDE, please click on the console view has focus, then press the Enter key, then you can see the OSGi prompt, as shown in Figure 2: (translators note, in Eclipse3.3, if you do not see to the OSGi prompt, run the configuration in Figure 1, click the Arguments tab, and then ProgramArguments (process parameters) the input box, type "-console", and then re-run the Bundle).






Figure 2. OSGi console and HelloWorldActivator.java

Here are a few frequently used OSGi command, you can use these commands to interact with the OSGi container.

ss: This command displays all installed Bundles and their status, it will display the Bundle ID, Bundle and Bundle short name of the state; start : This command will start a Bundle; stop : This command will stop A Bundle; update : This command uses the new JAR file to update a Bundle; install : This command will install a new Bundle OSGi container; uninstall : OSGi container unloaded from an installed Bundle.

Please note that these commands are provided for OSGi specification, so you can use them to interact with any OSGi container.

Read here, I hope you OSGi Bundle development with a general understanding.









相关链接:



Taste CorelDRAW10: color docker



Using "replace" one-time elimination of the hard carriage return



Hot Multimedia Creation Tools



Assembly LANGUAGE instruction and by Example



MKV To PSP



ESSO Card From The City To Look Into The Need For Integration



How to convert protected wma to mp3 for ipod



Qingdao Shengli Oilfield Nursing Home Use Maxima



Enjoys a bandwidth 10M 20M 20M exclusive exclusive special price



Storage Inventory And Barcoding



MP4 To WMP



If THE turmoil on Wall Street later to 30 years



AVI To Zune



Samba Can Be The First To Support GPLv3 Break With Concerns



Introduction Games Card



No comments:

Post a Comment