This article is
Good, osgi
The second part of the series. What is osgi?
, Following
This document introduces how to develop a simple osgi Bundle: Hello world.
51cto editing recommendations:
Osgi introduction and Practice
Develop a simple hello World osgi bundle (osgi binding package)
In osgi, software is released in the form of bundle. A bundle consists of a Java class and other resources. It can provide services for other bundle, or import
Java package in its bundle. At the same time, osgi bundle can also provide some functions for its devices. Eclipse provides excellent
Yes. It not only provides a wizard to create osgi
Bundle. It also provides an embedded equinox container that you can use to execute and debug the osgi plug-in. Note that every Eclipse plug-in is essentially
Osgi
Bundle, but this osgibundle adds some eclipse-specific code. Next let's take a look at how to use eclipse to develop a simple osgi
Helloworld bundle.
3. 1. Create a bundle
1) In eclipse, click "file-> New-> project" to see the new project creation dialog box;
2) in the new project dialog box, select "Plug-inproject" and click "Next". The plug-in project dialog box is displayed;
3) in the plug-in project dialog box, enter the following values:
Project name (project name): COM. javaworld. sample. helloworld
Target Platform: An osgiframework-> standard (osgi framework-> Standard)
4) for other requirements, enter the default value and click "Next". You will see the plug-in context dialog box;
5) in the plug-in context dialog box, select the default value and click "Next;
6) in the template dialog box, select the "Hello osgibundle (hello, osgi package)" template and click "finish" to complete the project.
Eclipse will generate helloworld in seconds
Bundle template code, which will create two new files: activator. Java and manifest. MF. below, let's take a look at these two files:
3.1.1. activator. Java File
Source code 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 to enable the developed bundle to notify you when it is started or closed, you should create a class to implement the bundleactivator interface. At the same time, you also need to follow
Line 1:
This class that implements the bundleactivator interface must have a public constructor without parameters, so that the osgi framework can call the class
Class. newinstance () method to create this bundleactivator object;
The container will call the START () method of the activator class to start the bundle. Therefore, we can execute some Resource Initialization operations in the START () method, for example,
We can obtain the database connection in this method for future use. The unique parameter of this start () method is a bundleobject object. Through this object and
For osgi framework communication, we can obtain some information about the osgi container from this object. If a bundle throws an exception, the container changes it to the stopped status,
In this case, this bundle cannot provide external services.
If we want to disable a bundle, the container will call the stop () method in the activator class. Therefore, we can execute some resource cleanup tasks in the Stop () method,
For example, release the database connection.
Once the activator class is ready, you can pass the valid name of the package to the container through the manifest. MF file. Next, let's take a look at this
Manifest. MF file.
3.1.2. manifest. MF File
This file is the deployment description file of bundle. Its format is the same as the manifest. MF file in the normal JAR file package. Therefore, it consists of a series of attributes and the value groups corresponding to these attributes.
Is located at the beginning of each row. We can call it the attribute header. According to the osgi specification, you can use the attribute header to describe your bundle to the container. Your helloworld
The bundle's manifest. MF file should look like Listing 2:
Source code list 2. manifest. MF file in Hello World bundle
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"
Let's take a look at the attribute headers used in this file:
Bundle-manifestversion
This attribute header tells the osgi container that this bundle complies with the osgi specification. The value 2 indicates that this bundle is compatible with the osgi Specification Version 4th. If the value of this attribute is 1
This indicates that this package is compatible with osgi Version 3 or earlier.
Bundle-name
This attribute header defines a brief and readable name for this bundle;
Bundle-symbolicname
This attribute header defines a unique, non-localized name for this bundle. You need to use this name when you need to access a specified bundle from another bundle.
Word.
Bundle-version
This attribute header provides the version number of this bundle.
Bundle-Activator
This attribute header provides the name of the listener class used in this bundle. This attribute value is optional. The listener monitors the START () and stop () methods in activator.
Listen. In program list 2, the value of this attribute header is com. javaworld. sample. helloworld. activator.
Bundle-vendor
This attribute header is a description of the bundle publisher.
Bundle-localization
This attribute header contains the location of the local file of this bundle. Our helloworld bundle does not have a local file, but eclipse
IDE still automatically generates this property Header
Import-Package
This attribute header defines the Java package introduced in this bundle. I will explain this issue in detail in the dependency management section later in this article. Now, helloworld
Bundle is ready. Let's run it and check its output result.
3.2. Run bundle
As mentioned above, Eclipse IDE has an embedded equinoxosgi container that you can use to execute or debug osgi.
Bundle. Follow these steps to execute the helloworld Bundle:
1) Click Run... In eclipse3.3, click Run à Open Run diglog... Menu );
2) eclipse will open "create, manage and run"
Configuration dialog box, double-click equinoxosgi
Framework button, eclipse will open the runtime configuration dialog box;
3) in the preceding dialog box, change the value of the Name field to helloworld bundle;
4)
You will notice that there is a plug-in named com. javaworld. sample. helloworld under the workspace plug-in directory. Please select it; In
Make sure that the org. Eclipse. osgi plug-in is selected under targetplatform (target platform. The run dialog box should look like 1:
Figure 1. Running configuration of helloworld bundle
5)
Now, click the run button. You should see "helloworld" printed in the console view ". In fact, eclipse opens the osgi console in the console view.
3.2.1. osgi Console
The osgi console is the command line interface of the osgi container. You can start, stop, install, update, and delete bundles on this console. In eclipseide, click
The console view gets the focus, and then press the Enter key. Then you can see the osgi prompt, as shown in 2: (Note: In eclipse3.3, if you do not see the osgi prompt,
In the running configuration in Figure 1, click the arguments tag, then type "-console" in the programarguments (program parameters) input box, and run
).
Figure 2. osgi console and helloworldactivator. Java
The following are several frequently used osgi commands. You can use these commands to interact with the osgi container.
1. SS: This command displays all installed bundles and their statuses. It displays bundle ID, brief name of bundle, and bundle status;
2. Start <bundleid>: This command starts a bundle;
3. Stop <bundleid>: This command stops a bundle;
4. Update <bundleid>: use the new JAR file to update a bundle;
5. install <bundleid>: This command installs a new bundle into the osgi container;
6. Uninstall <bundleid>: detach an installed bundle from the osgi container.
Note that these commands are defined in the osgi specification, so you can use them to interact with any osgi container.
I hope you have a general understanding of osgi bundle development.