OSGi Framework
Download
Report
Transcript OSGi Framework
OSGi & UPnP Technology
2009 Summer
Ya-Lin Huang
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
2
What 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
3
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
4
Alliance
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…
5
Specifications
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
6
Specifications
OSGi framework
Standard service definitions
Log Service
Http Service
Device Service
Package Administration Service
Permission Administration Service
Configuration Administration Service
Preferences Service
User Administration Service
7
8
Key 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.
9
Key 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.
10
Key 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.
11
Key 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.
12
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Terminology
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
13
OSGi Framework
Services gateway
Generic application framework
Lightweight framework
Simple component model
Service registry
Support for deployment
14
OSGi Platform
Multimedia Bundles
Health-Care Bundles
System Services
applicaion
OSGi Framework
e.g. J2ME, J2SE, J2EE
Java Runtime Environment
Drivers
e.g. Windows, Linux, VxWorks, QNX
Operating System
Hardware
15
OSGi 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.
16
OSGi 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
OSGi 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.
18
OSGi 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.
19
OSGi Framework
20
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Terminology
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
21
Service & Bundle
Services
Provide 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
22
Bundle Life Cycle
23
Bundle Acting
Progress(1/3)
24
Bundle Acting
Progress(2/3)
25
Bundle Acting
Progress(3/3)
26
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Terminology
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
27
Typical Use Cases
Internet Access
Personal Communications
Home Automation
Home Security
Home health care monitoring
Entertainment
Information management
Automobiles
28
Typical Use Cases
29
Commercial Use Cases
Nokia implements OSGi on 800
Ricoh
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
30
Outline
What is OSGi Technology
Introduction
Alliance
Specifications
Key Benefits
OSGi Terminology
OSGi Framework
Service & Bundle
Use Cases
OSGi Implementation
31
Framework Implementation
IBM Lotus Expeditor
ProSyst Software
Knopflerfish OSGi
Makewave (formerly Gatespace Telematics)
Equinox OSGi (Eclipse project)
Objectweb Oscar
Apache Felix
32
Knopflerfish Installation (1)
Software Environment
JRE(Java Runtime Environment)
Window XP
Download Knopflerfish jar file
http://www.knopflerfish.org/download.html
Knopflerfish support OSGi R4 from 2.0.5
Download knopflerfish_osgi_<version>.jar
Knopflerfish Installation (2)
Install JRE
Execute knopflerfish_osgi_<version>.jar
double-clicking on the distribution file
use the command: java -jar knopflerfish_osgi_<version>.jar
Or just extract the JAR file
Startup Knopflerfish
Bundle Action
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.o
rg/eclipse_install.html
Output
Input
Bundle State
and Information
BUNDLE DEVELOPMENT
36
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.
37
Bundle Activator(2/3)
Simple 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");
}
}
38
Bundle Activator(3/3)
MANIFEST file
Manifest-Version: 1.0
Bundle-Vendor: ISMP
Bundle-Version: 1.0.0
Bundle-Name: HelloOSGi
Bundle-Activator: ismp.smart.HelloOSGi.Activator
Bundle-Description: FirstBundle
Import-Package: org.osgi.framework
Bundle-SymbolicName: HelloOSGi
39
MANIFEST header
Header Name
Description
Bundle-Actiovator
Bundle-ClassPath
Bundle-ContactAddress
Bundle-Description
Bundle-DocURL
Bundle-Name
Bundle-NativeCode
Bundle’s Activator class
Bundle Class Path
Bundle Developer’s E-mail
Bundle usage and function description
Bundle Document Address
Bundle Name. Display for user.
Make it be able to use self-generated Native Code
(JNI NativeCode)
Bundle Update Location
Vendor Name
Bundle Version Information
Export Package
Export Service
Import Package
Import Service
Bundle-UpdateLocation
Bundle-Vendor
Bundle-Version
Export-package
Export-Service
Import-package
Import-Service
40
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()
41
Service Export(2/4)
Service interface and implementation
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);
}
}
42
Service Export(3/4)
Register 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");
}
...
}
43
Service Export(4/4)
Manifest file
Manifest-Version: 1.0
Bundle-Vendor: ISMP
Bundle-Version: 1.0.0
Bundle-Name: HelloOSGiServiceProvider
Bundle-Activator:
ismp.smart.helloOSGiServices.impl.Activator
Bundle-Description: FirstBundleService
Import-Package: org.osgi.framework
Bundle-SymbolicName: HelloOSGiServiceProvider
Export-Package: ismp.smart.helloOSGiServices
44
Service Import(1/3)
Dynamic 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]));
}
}
45
Service Import(2/3)
Must implemented method
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;
}
}
46
Service Import(3/3)
Manifest file
Manifest-Version: 1.0
Bundle-Vendor: ISMP
Bundle-Version: 1.0.0
Bundle-Name: HelloOSGiServiceClient
Bundle-Activator:
ismp.smart.helloOSGiServicesClient.impl.Activator
Bundle-Description: FirstBundleServiceClient
Import-Package: org.osgi.framework,
ismp.smart.helloOSGiServicesClient
Bundle-SymbolicName: HelloOSGiServiceClient
47
Export Jar
Eclipse Export
Install Bundle
Drag 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 Bundle
Click “Bundle Action” buttons - [Start Bundle]
Stop Bundle
Click “Bundle Action” buttons - [Stop Bundle]
Uninstall Bundle
Click “Bundle Action” buttons - [Uninstall Bundle]
48
DEMO
49
Homework
50