Introduction to OSGi(Java Modular

)
OSGi Alliance is the governing body of this stranded and it was started at 1999. their initial goal was create open stranded for network devices. Based on this idea this specification introduced for Java also. Eclipse was first in Java. they introduced OSGi based Eclipse IDE at 2004 June. OSGi is way to define dynamic module in java. There are main three OSGi container implemented for Java,such as Apache Felix, Eclipse Equinox and Knopflefish. Why OSGi? Because OSGi provide ability to divided application in to multiple module and those module easy to manage with other dependencies. other than that is very easy to install, update,stop and delete module without stop engine(Ex: Tomcat web application container). We can have multiple version of implementation with effecting to other references. There are main 3 layers in web based Java framework(Presentation , Business layer and DAO layer). There we can divide it into three OSGi based module. then we can very easily fixed bug in one layer with out effecting to others and restarting our Web container. Just we need to update out module. in OSGi world output is bundle, it can be either Jar or War file. A bundle consists of Java classes and other resources that with some additional metadata (providing services and packages to other bundles). I am going to use Eclipse IDE for create my first bundle. Because Eclipse IDe has built in Equinox container(every eclipse plugins are OSGi bundles) . Create Eclipse Plug-In-Project 1. Go to New--> Other --> Plug-In-Project and click on Next then new project creation dialog will be appeared 2. Provide project name and Target platform as below. and Click on Next Project name : com.chandana.Hello.HelloWorld Target platform : select Stranded OSGi

1

MF and I will give details information later) then click on Next button.There select Hello OSGi Bundle and click on Finish 2 . In next screen you can change bundle information(These information available in MANIFEST. 4. After that OSGi project template selection dialog will be appear.3.

java ? 1 package com.hello. 2 3 import org.chandana.framework.After few second Eclipse will generate Hello World Plug-In-Project(I had Not responding few second :) ) In my project structure is like this: Activator.osgi.BundleActivator. 3 .helloworld.

BundleC 10 ontext) 11 */ 12 public void start(BundleContext context) throws Exception { 13 System. We can use this start method for initialized database connection.out.osgi.BundleContext. It has stop and start methods. 14 } 15 16 /* 17 * (non-Javadoc) 18 * @see 19 org.framework. 4 public class Activator implements BundleActivator { 5 6 /* * (non-Javadoc) 7 * @see 8 9 org.println("Goodbye World!!").import org.framework.framework.out.osgi. 24 } } Activator is class which implement BundleActivator interface.BundleActivator#stop(org. those methods are called when a bundle is started or stopped. This bundle activator class specify in MENIFEST. Start Method: The OSGi container calls start method when bundle is starting.framework.MF file(Bundle-Activator entry).println("Hello World!!").osgi.framework. We can use this method for remove services form service registry like clean up process 4 .BundleActivator#start(org.osgi.BundleCo 20 ntext) 21 */ 22 public void stop(BundleContext context) throws Exception { 23 System. Stop Method: The OSGi container calls stop method when bundle is stopping. register a service for other bundle use.osgi.

0.Activator 7 Bundle-Vendor: CHANDANA 8 Bundle-RequiredExecutionEnvironment: JavaSE-1. what we called dependency.chandana. Bundle-Vendor Bundle-Vendor header is description of the vendor(foe example it's my name). Bundle-Name Bundle-Name header defines a short readable name for bundle.osgi.qualifier 6 Bundle-Activator: com.Hello. Run the Bundle: 5 .helloworld. Bundle-SymbolicName Bundle-SymbolicName header specifies a unique name for the bundle. Import-Package Import-Package is indicate what are the other Java bundle(OSGi) required for this bundle.framework.MANIFEST.0. Bundle-Version Bundle-Version header is the version number of the bundle. A value of 2 means that the bundle is compliant with OSGi specification Release 4.0 2 Bundle-ManifestVersion: 2 3 Bundle-Name: HelloWorld 4 Bundle-SymbolicName: com.chandana.0" Bundle-ManifestVersion Bundle-ManifestVersion header show the OSGi container that this bundle follows the rules of the OSGi specification. a value of 1 means that it is compliant with Release 3 or earlier.6 9 Import-Package: org.version="1.hello.HelloWorld 5 Bundle-Version: 1.3.MF 1 Manifest-Version: 1. This is the name you will use while referring a given bundle from other bundles. Export-Package Export-Package is indicate what are public packages in bundle and those Export-Package can import from other bundle.

OSGi Terminal Commands: start stop uninstall update refresh b 6 . In OSGi Framework Right click and create new Run Configuration. First unchecked the all target platform and Click on Add Required Bundles. After that Apply the changes and Run the project by click in Run button. 4.1. 3. After Run the project OSGi console display like below. 2. For Run this project click on Run --> Run Configuration .

version="1. Service Bundle: In service bundle you need to export your service and need to register it via service registry.chandana. I have created a sample OSGi project called HelloServcie MANIFEST. 2 3} Service Implementation: ? 1 public class HelloServiceImpl implements HelloService { 2 @Override 3 public String helloMethods() { 4 String retValue = "Inside Hello Service method". BundleActivator is to be invoked on start and stop.osgi.hello.0" 10 Export-Package: com.framework.0 2 Bundle-ManifestVersion: 2 3 Bundle-Name: HelloService 4 Bundle-SymbolicName: com.chandana. As usual that is to hide the implementation from the other bundles.HelloService 5 Bundle-Version: 1. 5 return retValue.service 11 Bundle-ActivationPolicy: lazy Service Interface: ? 1 public interface HelloService { public String helloMethods(). When BundleActivator call start method we are going to register our service. Services can be accessed via service registry(performed via the class BundleContext).0 6 Bundle-Activator: com.helloservice.chandana.3.hello.Activator 7 Bundle-Vendor: CHANDANA 8 Bundle-RequiredExecutionEnvironment: JavaSE-1.6 9 Import-Package: org. 6 } 7} 7 .0.headers services - display registered service details An OSGi Service is a java object instance which is registered with OSGi framework with set of attributes. When we are exporting service we export interface package only. After that any bundle can access that service.hello.MF ? 1 Manifest-Version: 1.

version="1.framework..!!!!!").service After importing the bundle..registerService(HelloService.BundleCo 18 ntext) 19 */ 20 public void stop(BundleContext context) throws Exception { 21 System.Boundle Activater: ? public class Activator implements BundleActivator { ServiceRegistration serviceRegistration. So need to create another Plug-In-Project for HelloClient Bundle Context Bundle context is the context of a single bundle within the OSGi runtime and it is created when Bundle get started. 12 } 13 14 /* 15 * (non-Javadoc) 16 * @see 17 org.hello..com. we can import it from another Bundle.!!!!!")..osgi.osgi. /* * (non-Javadoc) 1 * @see 2 3 org.. } } When we are using published services.osgi.framework.println("Bundle Started... serviceRegistration. 11 service.osgi.framework. 7 HelloService service = new HelloServiceImpl().chandana.framework. MANIFEST.class.framework.getName(). Important thing is service can be accessed only 8 .3.osgi.BundleActivator#stop(org.unregister(). Obtain registered services by other bundles and Register services in the framework.null).out.. Bundle context can be used to Install new bundles.0".MF ? Import-Package: 1 org.BundleC 4 ontext) */ 5 public void start(BundleContext context) throws Exception { 6 System.out.BundleActivator#start(org. 8 serviceRegistration = 9 10 context.println("Bundle Stoped. you can access the service.

framework.getName()). In OSGi Framework Right click and create new Run Configuration.through bundle context.BundleC 3 4 ontext) */ 5 public void start(BundleContext context) throws Exception { 6 serviceReference= 7 8 context.getService() method .getServiceReference() method return the HelloService OSGi service reference and Using that service reference can access the actual service object. /* * (non-Javadoc) 1 * @see 2 org. You can get actual service object via BundleContext.framework.framework.println(helloService. 12 } 13 14 /* 15 * (non-Javadoc) 16 * @see 17 org. Activator class: public class Activator implements BundleActivator { ServiceReference serviceReference.getService(serviceReference). HelloService helloService 9 10 =(HelloService)context.osgi.osgi.BundleCo 18 ntext) 19 */ 20 public void stop(BundleContext context) throws Exception { context.out.ungetService(serviceReference).BundleActivator#stop(org.helloMethods()). Make sure HelloService and HelloClient .framework. 11 System.getServiceReference(HelloService.class.osgi. For Run this project click on Run --> Run Configuration . Issues: What happened if the service is not started when client is accessing the service? What happened if you have stopped the service bundle? 9 .osgi.BundleActivator#start(org. } } context.

Sign up to vote on this title
UsefulNot useful