Page 3 of 5
The OSGi console is a command-line interface to the OSGi container. It allows you to do things like start, stop, install bundles, and update or delete bundles. In your Eclipse IDE, click the console view to give focus to that view, then click Enter and you will get an OSGi prompt like the one shown in Figure 2 (click to enlarge).
Here are some of the commonly used OSGi commands that you can use to interact with your OSGi container:
ss displays a list of installed bundles with the status of each bundle. It will display the bundle ID, short name, and status
of the bundle.
start <bundleid> starts a bundle.
stop <bundleid> stops a bundle.
update <bundleid> updates a bundle with a new JAR file.
install <bundleURL> installs a new bundle into the OSGi container.
uninstall <bundleid> uninstalls already installed bundles from the OSGi container.
Note that these commands are defined in the OSGi specification so you can use them to interact with any of the OSGi containers.
The OSGi specification allows you to break your application into multiple modules and then manage their dependencies on each other. It does this by adding a bundle scope. By default, none of the classes in a bundle are visible from any other bundle; inside bundles they follow the normal rules of the Java language. So, what do you do if you want to access the classes of one bundle from another bundle? The solution is to export packages from the source bundle and then import them into the target bundle. In this section we'll walk through a sample application that demonstrates this concept.
First, we will create a com.javaworld.sample.HelloService bundle, which will export a package. Then we will import the package into our com.javaworld.sample.HelloWorld bundle.
We'll start by creating the com.javaworld.sample.HelloService bundle and exporting a package from it. Follow these steps to create the bundle and export the package:
com.javaworld.sample.HelloService bundle by following the same steps used to create the com.javaworld.sample.HelloWorld bundle in the previous section.
HelloService bundle, create a com.javaworld.sample.service.HelloService.java interface, as shown in Listing 3.
public interface HelloService {
public String sayHello();
}
com.javaworld.sample.service.impl.HelloServiceImpl.java class implementing the HelloService interface, as shown in Listing 4.
public class HelloServiceImpl implements HelloService{
public String sayHello() {
System.out.println("Inside HelloServiceImple.sayHello()");
return "Say Hello";
}
}
MANIFEST.MF for the HelloService package in your Eclipse Manifest editor and go to the Runtime tab. In the Exported Packages section, click Add and select
the com.javaworld.sample.service package. The MANIFEST.MF file for the HelloService bundle should look like the one in Listing 5.
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService Plug-in
Bundle-SymbolicName: com.javaworld.sample.HelloService
Bundle-Version: 1.0.0
Bundle-Vendor: JAVAWORLD
Bundle-Localization: plugin
Export-Package: com.javaworld.sample.service
Import-Package: org.osgi.framework;version="1.3.0"
As you can see, the MANIFEST.MF file for the HelloService bundle looks very similar to that of the HelloWorld bundle. The only difference is that this MANIFEST.MF file has one Export-Package manifest header, whose value is com.javaworld.simple.service.
The Export-Package manifest header informs the OSGi container that classes in the com.javaworld.sample.service package from the HelloService bundle can be accessed from outside. Note that in the sample code we have exposed a HelloService interface but not the HelloServiceImpl implementation class.
The next step is to update the HelloWorld bundle to import the com.javaworld.simple.service package. Here are the steps to import the package:
com.javaworld.sample.HelloWorld bundle, open the MANIFEST.MF file in the Plug-in Manifest editor. Now go to the Dependencies tab, and then to Imported Packages. Add com.javaworld.sample.service as the value of Imported Packages. The MANIFEST.MF file for your HelloWorld bundle should look like the one in Listing 6.
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: com.javaworld.sample.service,
org.osgi.framework;version="1.3.0"
Import-Package header is a comma-separated list of packages that this bundle wants to import. In the sample code the HelloWorld bundle imports two packages: com.javaworld.sample.service and org.osgi.framework.org.osgi.framework package contains OSGi framework classes such as BundleContext and BundleActivator, which are used by the Activator.java class of the HelloWorld bundle.
com.javaworld.sample.helloworld.Activator.java in the Eclipse Java editor. You will notice that you are now able to access the HelloService interface but not the HelloServiceImpl class. This is because the HelloService package exports (and the HelloWorld package imports) the com.javaworld.sample.service package. HelloServiceImpl is an internal class for the HelloService bundle and no other bundle can access it.
| Subject |
|
|
OSGi open source containers
More
Visibility between bundlesBy Anonymous on January 28, 2010, 7:20 pmYou mention that Export-Package: com.javaworld.sample.service exports only the interface HelloService but not the HelloServiceImpl. However, I am able to instantiate...
Reply | Read entire comment
OSGiBy Anonymous on January 17, 2010, 8:10 amyou must set the startlevel of the bundle suppling services greater than that bundle which needs service. Eclipse Menu: Run Configuaration -> Bundles -> Start Level,...
Reply | Read entire comment
really good articleBy Anonymous on January 9, 2010, 1:12 pmthank you very much
Reply | Read entire comment
Best Intro to OSGI I have found!!By Anonymous on January 4, 2010, 6:10 amThanks very much. One small problem, when I go to create the new project as per page 2, step 2, "2.In the New Project dialog, select Plug-in Project and click Next."...
Reply | Read entire comment
ThanksBy Anonymous on December 31, 2009, 6:19 amThis really great. Thanks for posting this. I have observed a small correction on page 4, When you start the HelloWorld, not HelloService bundle, you will see the...
Reply | Read entire comment
View all comments