Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.
OSGi open source containers
More
Archived Discussions (Read only)