HelloWorld
In general, learning a new technology, programmers are accustomed to first development of a Hello World application, which seems to be an "industrial standard." OK, let's get started, it's not difficult to develop a simple OSGi application: Create a plug-in project, File > New > Project, select Plug-in development > Plug-in PROJEC T
Figure 1. New Plug-in Project
In establishing the project's first wizard, fill in the project name: Osgi.test.helloworld, using the default engineering path. Note the choice of the target platform, because our project is a generic OSGi bundle, so choose Equinox.
Figure 2. Fill in Project name and select target platform
In the next wizard interface, fill in some of the plug-in information you need (note that the plug-in concept in Eclipse is basically similar to the concept of bundle in OSGi), where you need to fill in the OSGi provider (vendor) and classpath. If you don't have a special design, you can generally ignore these two fields. The last part about Activator is that if it's not a fragment bundle it needs to be filled in, which doesn't seem to be necessary unless your bundle to implement the framework's event monitor yourself. Therefore, it is recommended that you use the default settings, as shown in Figure 3:
Figure 3. Use default settings
Activator: This is the program entry that was first invoked when the bundle started, equivalent to the main function in the Java module. The difference is that main needs to be invoked through the command line, and OSGi's Activator is passively accepting calls from the OSGi framework, which starts only when the message is received.
Best Practice: Do not write too many startup code in the Activator, otherwise it will affect the bundle boot speed and the associated service startup can be placed in the service listener. The last step is not to use any of the templates, so check out the default options, click Finish, as shown in Figure 4:
Figure 4. Check out the default options
Complete, the basic plug-in view as shown in Figure 5,eclipse will create the same path in the project name Java Package, which contains the Activator class, and the configuration information of the plug-in are also placed in MANIFEST. MF file, we have quite a lot of work to do in the future. &NBSP
Figure 5. Basic Plug-in View
Edit Activator.java, enter the Hello World statement with the following code: &NBSP
Listing 1. Edit Activator.java
Package Osgi.test.helloworld;
Import Org.osgi.framework.BundleActivator;
Import Org.osgi.framework.BundleContext;
public class Activator implements Bundleactivator {
/* * (Non-javadoc)
* @see Org.osgi.framework.BundleActivator
* #start (org.osgi.framework.BundleContext) */public
void Start (Bundlecontext context) throws Exception {
System.out.println ("Hello World");
}
* *
(non-javadoc) *
@see org.osgi.framework.BundleActivator
* #stop ( Org.osgi.framework.BundleContext)
*
/public void Stop (Bundlecontext context) throws Exception {
}
} |
We can see that each Activator actually implements the Bundleactivator interface, which enables Activator to accept the invocation of the framework. After the frame is started, each bundle is invoked to invoke each bundle's Activator.
Note: The bundle Activator must contain parameterless constructors so that the framework can use Class.newinstance () to reflect the Activator instance of the constructed bundle.
Here we fill in the Hello World string we want to output in the Start method. So, how can we start this bundle? Executive: Select Run > Open run Dialog, go to the running menu, right-click in the OSGi framework and select New OSGi runtime Environment, as shown in:
Figure 6. New OSGi Runtime Environment
In the right side of the running Environment dialog box, enter the name of the running environment, start level and dependent plug-ins, because we do not need other third-party plug-ins, so we only need to check the system Org.eclipse.osgi Plug-ins, if not selected this plugin, Hello World Will not run. As shown in Figure 7, only when you click on the Validate Bundles button, and the hint is no problem, it shows that your operating environment is basically OK.
Figure 7. Select Org.eclipse.osgi Plugin
Dependent plug-in selection:
Figure 8. Choice of dependent Plug-ins
OK, if your running environment is OK, click Run.
Figure 9. Running an OSGi project
Congratulations on your success.
Back to the top of the page
OSGi Console
The OSGi console is still relatively new to developers who are accustomed to developing common Java applications. In general, through the OSGi console, you can manage the lifecycle of all bundle in the system, as well as view the system environment, start, stop the entire frame, set the startup level, and so on. As shown in Figure 10, you can view the status of all bundle by typing SS:
Figure 10. View the status of all bundle
The main console commands are listed below:
Table 1. Equinox OSGi's main console command table
Category |
Command |
Meaning |
Control framework |
Launch |
Start frame |
Shutdown |
Stop frame |
Close |
Close, exit frame |
Exit |
Exit immediately, equivalent to System.exit |
Init |
Uninstall all bundle (provided it is already shutdown) |
SetProp |
Set properties that are performed at run time |
Control bundle |
Install |
Installation |
Uninstall |
Unloading |
Start |
Start |
Stop |
Stop it |
Refresh |
Refresh |
Update |
Update |
Show status |
Status |
Show installed bundle and registered services |
Ss |
Show the simple state of all bundle |
Services |
Show details of the registration service |
Packages |
Show the status of import and export packages |
Bundles |
Show the status of all installed bundles |
Headers |
Displays the bundles header information, namely the MANIFEST. What's in MF |
Log |
Display LOG Entry Information |
Other |
Exec |
Executes a command (blocking state) in another process |
Fork |
Unlike EXEC, it doesn't cause obstruction. |
Gc |
Promote garbage collection |
Getprop |
Get a property, or a property |
Control startup level |
Sl |
Get the start level information for a bundle or the entire frame |
Setfwsl |
Set the start level of a frame |
Setbsl |
Set the start level of the bundle |
Setibsl |
Sets the start level of the initialization bundle |
Back to the top of the page
MANIFEST. Mf
MANIFEST. MF may appear in any Jar package that includes main class information, typically located in the Meta-inf directory, so this file is not an OSGi-specific thing, but simply adds some attributes, which just keeps the OSGi environment and the common Java environment consistent and easy to deploy on older systems. Table 2 lists the important attributes in this file and their implications:
Table 2. MANIFEST. MF file Properties
Attribute name |
Meaning |
Bundle-activator |
The Bundle launcher |
Bundle-symbolicname |
Name, typically using a name similar to the JAVA package path |
Bundle-version |
Version, note that different versions of the same name bundle can be deployed online simultaneously |
Export-package |
Exported package declarations, other bundle can be referenced directly |
Import-package |
Imported Package |
Eclipse-lazystart |
Is it only started when it is referenced? |
Require-bundle |
Full reliance on bundle, not recommended |
Bundle-classpath |
The class path of this bundle can contain some other resource paths |
Bundle-requiredexecutionenvironment |
The required execution environment for this bundle, such as JDK version declarations |
Category: Notes, OSGI