OSGi & UPnP Technology 2009 Summer Ya-Lin Huang. 2 Outline What is OSGi Technology Introduction...

Preview:

Citation preview

OSGi & UPnP TechnologyOSGi & UPnP Technology

2009 Summer

Ya-Lin Huang

2

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey Benefits

OSGi FrameworkService & BundleUse CasesOSGi Implementation

3

What is OSGi TechnologyWhat is OSGi Technology

It’s a module system for the Java platform

It’s dynamic

It’s service-oriented

A specification of the OSGi Alliance, a non-profit organization

4

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey Benefits

OSGi FrameworkService & BundleUse CasesOSGi Implementation

5

AllianceAlliance

OSGi - “Open Service Gateway Institute”The Alliance has been founded by Sun Microsystems, IBM, Ericsson and others in March 1999.Among its members are (as of April 2006) more than 35 companies from quite different business areas, for example Nokia, Motorola, Philips, BenQ, Siemens VDO Automotive, BMW, Gatespace Telematics, ProSyst, Samsung Electronics…

6

SpecificationsSpecifications

OSGi Release 1 (R1): May 2000

OSGi Release 2 (R2): October 2001

OSGi Release 3 (R3): March 2003

OSGi Release 4 (R4): October 2005 / September 2006

Core Specification (R4 Core): October 2005

Mobile Specification (R4 Mobile / JSR-232): September 2006

Latest release (R4.1 / JSR-291): May 2007

7

SpecificationsSpecifications

OSGi frameworkStandard service definitions

Log ServiceHttp ServiceDevice ServicePackage Administration ServicePermission Administration ServiceConfiguration Administration ServicePreferences ServiceUser Administration Service

8

9

Key BenefitsKey Benefits

Platform Independence:OSGi member companies work in many different environments and as such they suffer different constraints, resulting in widely varying product characteristics and capabilities.

Application Independence:The OSGi specifications focus on defining common APIs for service deployment. This makes the specifications suitable for Service Platforms for a variety of applications in different markets.

10

Key BenefitsKey Benefits

Multiple Service Support:OSGi environments should be capable of hosting multiple applications from different Service Providers on a single Service Platform with each application providing an independent set of services to the end user.

Service Collaboration Support:An important aspect of the OSGi deployment model is that it allows the core platform to be extended with deployed services. This is not limited to end user oriented services only, which is a limitation of some other deployment models.

11

Key BenefitsKey Benefits

Multiple Network Technology Support:OSGi Service Platforms can work with wide area technologies like xDSL, Cable modems, Satellite, ISDN and PSTN and local area networks like Bluetooth, USB, IEEE 1394 Firewire.

Popular service discovery techniques like UPnP, Jini, Salutation and several others work very well in conjunction with an OSGi Service Platform and can even potentially interwork transparently.

12

Key BenefitsKey Benefits

Security:The OSGi offers a fine grained security architecture that limits the potential harm a malicious or badly written application can do.

Simplicity:The OSGi environment offers a service environment for everybody by removing much of the complexity and putting it into the hands of professionals. The environment can be remotely managed by a professional organization, if so desired.

13

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey BenefitsOSGi Terminology

OSGi FrameworkService & BundleUse CasesOSGi Implementation

14

OSGi FrameworkOSGi Framework

Services gateway

Generic application framework

Lightweight frameworkSimple component model

Service registry

Support for deployment

15

OSGi PlatformOSGi Platform

Hardware

Drivers

e.g. Windows, Linux, VxWorks, QNXOperating System

applicaion OSGi Framework

System Services

Multimedia Bundles Health-Care Bundles

e.g. J2ME, J2SE, J2EEJava Runtime Environment

16

OSGi FrameworkOSGi Framework

Hardware: the physical part of a computer.Operating System: a software program that manages the hardware and software resources of a computer. Java Runtime Environment: a software bundle from Sun Microsystems that allows a computer system to run a java application.Application: a subclass of computer software that employs the capabilities of a computer directly to a task that the user wishes to perform.

OSGi FrameworkOSGi Framework

The Framework is divided in a number of layers:

Execution Environment

Modules

Life Cycle Management

Service Registry.

Additionally, there is a security system that is deeply intertwined with all the layers.

17

18

OSGi FrameworkOSGi Framework

Execution environment: the specification of the Java environment. Java 2 Configurations and Profiles. Modules: defines the class loading policies. The OSGi Framework is a powerful and strictly specified class loading model. It is based on top of Java but add modularization. Life Cycle Management: adds bundles that can be dynamically installed, started, stopped, updated, and uninstalled. Bundles rely on the module layer for class loading but add an API to manage the modules in run time.

19

OSGi FrameworkOSGi Framework

Service Registry: The service registry provides a cooperation model for bundles that takes the dynamics into account. The service registry provides a comprehensive model to share objects between bundles. A number of events are defined to handle the coming and going of services.

Security is based on Java and the Java 2 security model.

20

OSGi FrameworkOSGi Framework

21

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey BenefitsOSGi Terminology

OSGi FrameworkService & BundleUse CasesOSGi Implementation

22

Service & BundleService & Bundle

ServicesProvide applications

Bundles (JAR file)Provide (export) and reuse (import) services via the framework

Identify Java packages (classes)

Implement specified interface (services)

Register services with the Service Registry

23

Bundle Life CycleBundle Life Cycle

24

Bundle Acting Bundle Acting Progress(1/3)Progress(1/3)

25

Bundle Acting Bundle Acting Progress(2/3)Progress(2/3)

26

Bundle Acting Bundle Acting Progress(3/3)Progress(3/3)

27

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey BenefitsOSGi Terminology

OSGi FrameworkService & BundleUse CasesOSGi Implementation

28

Typical Use CasesTypical Use Cases

Internet Access

Personal Communications

Home Automation

Home Security

Home health care monitoring

Entertainment

Information management

Automobiles

29

Typical Use CasesTypical Use Cases

30

Commercial Use CasesCommercial Use Cases

Nokia implements OSGi on 800Ricoh

Multi-functional Products (MFPs) laser printers

BMW Research 3GT, Ertico GST

Volvo Technology (VTEC)3GT (Third Generation Telematics)GST (Global System for Telematics) CVIS (Cooperative Vehicle Infrastructure Systems)

Philips iPronto

31

OutlineOutline

What is OSGi TechnologyIntroduction

AllianceSpecificationsKey BenefitsOSGi Terminology

OSGi FrameworkService & BundleUse CasesOSGi Implementation

32

Framework ImplementationFramework Implementation

IBM Lotus Expeditor

ProSyst Software

Knopflerfish OSGiMakewave (formerly Gatespace Telematics)

Equinox OSGi (Eclipse project)

Objectweb OscarApache Felix

Knopflerfish Installation (1)Knopflerfish Installation (1)

Software EnvironmentJRE(Java Runtime Environment)

Window XP

Download Knopflerfish jar filehttp://www.knopflerfish.org/download.html

Knopflerfish support OSGi R4 from 2.0.5

Download knopflerfish_osgi_<version>.jar

Knopflerfish Installation (2)Knopflerfish Installation (2)

Install JREExecute knopflerfish_osgi_<version>.jar

double-clicking on the distribution fileuse the command: java -jar knopflerfish_osgi_<version>.jarOr just extract the JAR file

Startup KnopflerfishStartup Knopflerfish

Change the directory to the installation directory and start Knopflerfish

double-clicking on framework.jar

use the command: java -jar framework.jar

Eclipse plugin http://www.knopflerfish.org/eclipse_install.html

Bundle ActionBundle Action

Bundle Stateand Information

Bundle Stateand InformationOutputOutput

InputInput

BUNDLE DEVELOPMENTBUNDLE DEVELOPMENT

36

37

Bundle Activator(1/3)Bundle Activator(1/3)

Activator Class (implements BundleActivator)start()

This method implements what should the bundle do when it starts. The same as main() method in common Java program.

stop()This method implements what should the bundle do when it is going to stop.

38

Bundle Activator(2/3)Bundle Activator(2/3)Simple implementationSimple implementation

package ismp.smart.HelloOSGi;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception {

System.out.println("HELLO!!"); } public void stop(BundleContext context) throws Exception {

System.out.println("Byebye"); }}

package ismp.smart.HelloOSGi;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception {

System.out.println("HELLO!!"); } public void stop(BundleContext context) throws Exception {

System.out.println("Byebye"); }}

39

Bundle Activator(3/3)Bundle Activator(3/3)MANIFEST fileMANIFEST file

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiBundle-Activator: ismp.smart.HelloOSGi.ActivatorBundle-Description: FirstBundleImport-Package: org.osgi.frameworkBundle-SymbolicName: HelloOSGi

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiBundle-Activator: ismp.smart.HelloOSGi.ActivatorBundle-Description: FirstBundleImport-Package: org.osgi.frameworkBundle-SymbolicName: HelloOSGi

MANIFEST headerMANIFEST header

Header Name DescriptionBundle-Actiovator Bundle’s Activator classBundle-ClassPath Bundle Class Path

Bundle-ContactAddress Bundle Developer’s E-mailBundle-Description Bundle usage and function description

Bundle-DocURL Bundle Document AddressBundle-Name Bundle Name. Display for user.

Bundle-NativeCode Make it be able to use self-generated Native Code(JNI NativeCode)

Bundle-UpdateLocation Bundle Update LocationBundle-Vendor Vendor NameBundle-Version Bundle Version InformationExport-package Export PackageExport-Service Export Service

Import-package Import PackageImport-Service Import Service

40

41

Service Export(1/4)Service Export(1/4)

Activator Class (implements BundleActivator)start()

We have to implement a class and a service method for this class, and register this service in the start() method.

BundleContext.registerService (java.lang.String class, java.lang.Object service, java.util.Dictionary properties)

stop()Some services should be unregistered before the bundle stopped.

ServiceRegistration.unregister()

Service Export(2/4)Service Export(2/4)Service interface and implementationService interface and implementation

42

package ismp.smart.helloOSGiServices;import java.util.Date;public interface FirstService {

public String getFormattedDate(Date date);

}

package ismp.smart.helloOSGiServices;import java.util.Date;public interface FirstService {

public String getFormattedDate(Date date);

}

package ismp.smart.helloOSGiServices.impl;import java.text.DateFormat;import java.util.Date;import ismp.smart.helloOSGiServices.FirstService;public class FirstServiceImpl implements FirstService {

public String getFormattedDate(Date date) {return

DateFormat.getDateInstance(DateFormat.SHORT).format(date);}

}

package ismp.smart.helloOSGiServices.impl;import java.text.DateFormat;import java.util.Date;import ismp.smart.helloOSGiServices.FirstService;public class FirstServiceImpl implements FirstService {

public String getFormattedDate(Date date) {return

DateFormat.getDateInstance(DateFormat.SHORT).format(date);}

}

43

Service Export(3/4)Service Export(3/4)Register ServiceRegister Service

package ismp.smart.helloOSGiServices.impl;...import ismp.smart.helloOSGiServices.FirstService;public class Activator implements BundleActivator {

public static BundleContext bc = null;public void start(BundleContext bc) throws Exception {

...Activator.bc = bc;

FirstService service = new FirstServiceImpl();ServiceRegistration registration = bc.registerService( FirstService.class.getName(), service, new Hashtable());System.out.println("Service registered: FirstService");

}...

}

package ismp.smart.helloOSGiServices.impl;...import ismp.smart.helloOSGiServices.FirstService;public class Activator implements BundleActivator {

public static BundleContext bc = null;public void start(BundleContext bc) throws Exception {

...Activator.bc = bc;

FirstService service = new FirstServiceImpl();ServiceRegistration registration = bc.registerService( FirstService.class.getName(), service, new Hashtable());System.out.println("Service registered: FirstService");

}...

}

44

Service Export(4/4)Service Export(4/4)Manifest fileManifest file

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiServiceProviderBundle-Activator: ismp.smart.helloOSGiServices.impl.ActivatorBundle-Description: FirstBundleServiceImport-Package: org.osgi.frameworkBundle-SymbolicName: HelloOSGiServiceProvider

Export-Package: ismp.smart.helloOSGiServices

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiServiceProviderBundle-Activator: ismp.smart.helloOSGiServices.impl.ActivatorBundle-Description: FirstBundleServiceImport-Package: org.osgi.frameworkBundle-SymbolicName: HelloOSGiServiceProvider

Export-Package: ismp.smart.helloOSGiServices

45

Service Import(1/3)Service Import(1/3)Dynamic binding servicesDynamic binding services

public void start(BundleContext bc) throws Exception {System.out.println("start " + getClass().getName());Activator.bc = bc;String filter = "(objectclass=" + FirstService.class.getName()

+ ")";bc.addServiceListener(this, filter);ServiceReference references[] = bc.getServiceReferences(null,

filter);for (int i = 0; references != null && i < references.length;

i++){ this.serviceChanged(new

ServiceEvent(ServiceEvent.REGISTERED,references[i]));}

}

public void start(BundleContext bc) throws Exception {System.out.println("start " + getClass().getName());Activator.bc = bc;String filter = "(objectclass=" + FirstService.class.getName()

+ ")";bc.addServiceListener(this, filter);ServiceReference references[] = bc.getServiceReferences(null,

filter);for (int i = 0; references != null && i < references.length;

i++){ this.serviceChanged(new

ServiceEvent(ServiceEvent.REGISTERED,references[i]));}

}

Service Import(2/3)Service Import(2/3)Must implemented methodMust implemented method

46

public void serviceChanged(ServiceEvent event) {switch (event.getType()) {case ServiceEvent.REGISTERED:

log("ServiceEvent.REGISTERED");this.service = (FirstService)Activator.bc.getService(

event.getServiceReference());this.startUsingService();break;

case ServiceEvent.MODIFIED:log("ServiceEvent.MODIFIED received");this.stopUsingService();this.service = (FirstService) Activator.bc.getService(

event.getServiceReference());this.startUsingService();break;

case ServiceEvent.UNREGISTERING:log("ServiceEvent.UNREGISTERING");this.stopUsingService();break;

}

}

public void serviceChanged(ServiceEvent event) {switch (event.getType()) {case ServiceEvent.REGISTERED:

log("ServiceEvent.REGISTERED");this.service = (FirstService)Activator.bc.getService(

event.getServiceReference());this.startUsingService();break;

case ServiceEvent.MODIFIED:log("ServiceEvent.MODIFIED received");this.stopUsingService();this.service = (FirstService) Activator.bc.getService(

event.getServiceReference());this.startUsingService();break;

case ServiceEvent.UNREGISTERING:log("ServiceEvent.UNREGISTERING");this.stopUsingService();break;

}

}

Service Import(3/3)Service Import(3/3) Manifest fileManifest file

47

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiServiceClientBundle-Activator: ismp.smart.helloOSGiServicesClient.impl.ActivatorBundle-Description: FirstBundleServiceClientImport-Package: org.osgi.framework, ismp.smart.helloOSGiServicesClient

Bundle-SymbolicName: HelloOSGiServiceClient

Manifest-Version: 1.0Bundle-Vendor: ISMPBundle-Version: 1.0.0Bundle-Name: HelloOSGiServiceClientBundle-Activator: ismp.smart.helloOSGiServicesClient.impl.ActivatorBundle-Description: FirstBundleServiceClientImport-Package: org.osgi.framework, ismp.smart.helloOSGiServicesClient

Bundle-SymbolicName: HelloOSGiServiceClient

Export JarExport Jar

Eclipse Export

Install BundleDrag and Drop or click “Bundle Action” buttons - [Open Bundle(s)]

If all the dependencies are satisfied, the installed bundle will automatically start, or it will prompt error messages.

Start BundleClick “Bundle Action” buttons - [Start Bundle]

Stop BundleClick “Bundle Action” buttons - [Stop Bundle]

Uninstall BundleClick “Bundle Action” buttons - [Uninstall Bundle]

48

DEMODEMO

49

HomeworkHomework

50

Recommended