The OSGi Service Framework

AMDéveloppement de logiciels

12 déc. 2011 (il y a 3 années et 21 jours)

638 vue(s)

OSGi Service Platform OSGi Framework OSGi Service Framework – Bundle, Fragments, Services Inter-bundle Communication Programming Examples ServiceTracker

Lotus Worldwide Technical Sales
©2007 IBM Corporation
The OSGi Service Framework
Max Muster
Product Manager, Notes Client
IBM Lotus
Lotus WTS Enablement2007
2
Agenda
OSGi Service Platform
OSGi Framework
OSGi Service Framework –Bundle, Fragments, Services
Inter-bundle Communication
Programming Examples
ServiceTracker
Lotus WTS Enablement2007
3
What is the OSGi Service Platform?
A Java framework for developing remotely deployed service
applications requiring:–Reliability
–Large scale distribution
–Support for a wide range of devices
–Collaboration
OSGi Service Platform Release (SPR) 4.0 specifications are available
at www.osgi.org
Lotus WTS Enablement2007
4
Essentials
Reliable
–Large-scale deployments fail without extremely high reliability
Portable
–Attract third-party developers to create essential innovative services
Dynamic–Allow configuration to dynamically adapt to user & operator needs over time
Secure
–Protect service providers from each other
–Guarantee a prescribed quality of service
Scalable
–Members have very different configurations for their deployment of OSGi
frameworks
Lotus WTS Enablement2007
5
OSGi Service Platform Contents
Framework Specification
Package Admin Service Specification
Permission Admin Service Specification
Service Tracker Specification
Log Service Specification
HTTP Service Specification
Device Access Specification
Configuration Admin Service Specification
Metatype Specification
Preferences Service Specification
User Admin Service Specification
Lotus WTS Enablement2007
6
OSGi Framework
Eclipse 3.2.1 implements (and is based on) the OSGi SPR4
Framework
Eclipse does not supply optional services –e.g. Configuration Admin Service, Log Service, Preferences Service
Eclipse also adds its own extensions to the framework –e.g. Extension Points, Eclipse-LazyStartattributes, Require-Bundle/Provide-
Package support
Lotus WTS Enablement2007
7
Framework Functions
Allows applications to share a single Java VM
Manages applications (bundles)
–Life cycle, Java Packages, Security, Dependencies between applications
Provides a service registry for collaboration
–Central location to find services
Provides an extensive notification mechanism
–Services can be notified when other services are available
Java Virtual Machine
OSGi Service Framework
Application Bundles
(User-Written)
System Bundle
(OSGi Services)
Support Bundles
(Platform-Dependent)
Lotus WTS Enablement2007
8
Managed Client Services
JVM (IBM J9 2.3 Technology or IBM JSE 1.5 )
Class Libraries ( jclDesktopor JSE 1.5 )
OSGi Service Framework R4
ICU4J
JSR47 Logging
XML Parsers
(JSE SAX & DOM)
OSGi Log Service
OSGi
MetaTypeService
OSGi
Configuration Admin
OSGi
Device Access
OSGi User
Admin Service
Eclipse Logging
OSGi Services
(service i/fdefinitions)
OSGi Utilities
(Measurement, Position)
Core
Eclipse
Core Extension
Point Framework
OSGi
Event Admin
Eclipse Preferences
Lotus WTS Enablement2007
9
OSGi Service Framework
Allows applications to share a single
JVM instance
Separate class loaders
Enables applications to share
services and packages
Separates service
interface from service implementation
Enables dynamic, “on the fly”life-
cycle management without a VM
restart
Integrated into Eclipse starting with
Eclipse 3.0
–Plug-ins run as bundles
–Bundles can run as plug-ins
Hardware
Driver
Driver
Driver
Operating System
JVM
OSGi
Fr
a
m
e
w
o
r
k
Bundle (App. Component)
=service interface exported and
imported by bundles
Lotus WTS Enablement2007
10
Bundle
Packaging of a Java library/service/application for deployment
into an OSGi framework
–Dynamic life-cycle
–Life-cycle event notification
–Supports any resources (Java, Native, Images, etc.)
–OSGi-specific manifest headers
Bundles are made locally resident
Plug-ins are Bundles
Bundle
Lotus WTS Enablement2007
11
Bundle Specifics
A bundle is the deliverable
application
A bundle registers zero or more
services
–A service is specified in a Java interface
and may be implemented by classes in
multiple bundles
Searches can be used to find
services registered by other
bundles
The Framework itself is
represented as the system bundle
A bundle is a JAR containing:
–Resources to implement zero or more
services
–A manifest with bundle information
–Dependencies on other resources (Java
packages)
–Name, description, copyrights, vendor,
profile, etc.
–An optional class that can start/stop the
bundle (BundleActivator)
–A bundle can also act like a library (only
exporting packages)
–BundleActivators can register services the
bundle will provide
Lotus WTS Enablement2007
12
Stopping
Bundle Lifecycle
Install
Uninstall
Start
Stop
Update
Uninstall
Automatic
Explicit
Installed
Uninstalled
Starting
Active
Resolved
Lotus WTS Enablement2007
13
MANIFEST.MF
Plug-in identity (Bundle-SymbolicName)
Classpath(Bundle-Classpath)
Startup class (Bundle-Activator)
Dependencies
Bundle-Name: Sample
Bundle-SymbolicName: com.ibm.pvc.sample
Bundle-Activator: com.ibm.pvc.impl.Activator
Bundle-Version: 1.5.7
Bundle-Classpath: com.ibm.pvc.jar
Import-Package: org.osgi.service.log
Export-Package: com.ibm.pvc
Provide-Package: com.ibm.pvc
Import-Service: com.ibm.osg.service.log.LogService
Export-Service: com.ibm.pvc.SomeService
Require-Bundle: org.eclipse.osgi.services
Defines packages required
by this bundle
Defines packages exported
by this bundle
Defines services required
by this bundle
Defines services exported
by this bundle
Defines packages provided
by this bundle
Defines bundles required by
this bundle
Bundle
Lotus WTS Enablement2007
14
Fragments
Extends bundles to add capability
Associates with one and only one host bundle
Contains MANIFEST.MF file
No Bundle-Activator
Follows life cycle of host bundle
Host bundle may have multiple fragments
Typical uses
–National Language Versions
–Operating System specific implementations
Fragment
Lotus WTS Enablement2007
15
Fragment MANIFEST.MF
Fragment Identity (Bundle-SymbolicName)
Host plug-in identity (Fragment-Host)
Host classpathadditions (Bundle-Classpath)
Dependencies (same as bundle)
Manifest-Version: 1.0
Bundle-Name: HTTP Service for Web Container
Bundle-SymbolicName: com.ibm.pvc.webhttpservice
Bundle-Version: 1.0.0.20050908
Bundle-ClassPath: webhttpservice.jar
Bundle-Vendor: IBM
Fragment-Host: com.ibm.osg.service.http;bundle-version="[2.1.3,2.1.4)"
Import-Package: com.ibm.pvc.webcontainer.listeners; specification-
version=1.0,
org.osgi.service.webapplication
Import-Service: com.ibm.osg.webcontainer.WebContainer
Export-Service: org.osgi.service.http.HttpService
Fragment
Lotus WTS Enablement2007
16
Services
Java object –Registered by a bundle
–Used by other bundles
Specified by a Java interface
–Clean separation of service specification and service implementation
–Different bundles can register different implementations of the same service
Service
Lotus WTS Enablement2007
17
Framework Service Example
Log Service
Specification
Log to Disk
Log to
Circular Queue
Log to
Flash RAM
Log to
Printer
Log to
Bit Bucket
Lotus WTS Enablement2007
18
Java
OSGi
Service Registry
Dynamic registry of available services –Secured by permissions
Service object registered –Under one or more interface names
–With optional properties
Lookup mechanism
Service Factory –Callback to create custom service objects
Life cycle event notification
–Registered, Modified, Unregistering
Lotus WTS Enablement2007
19
Java
OSGi Framework
Collaboration
Bundles can collaborate
–Import/Export Services
–Import/Export Packages
Dependencies managed
Bundle
Bundle
Lotus WTS Enablement2007
20
Package Sharing
Each bundle has its own ClassLoader
–Bundles have separate namespaces
Java classes are grouped into packages –Bundles can offer to share a package –export
–Bundles can request to use a package –import
Global shared package namespace
–Class Loaders connected to allow classes to be shared
Versioning
–Only one version of a package may be shared at a time -defined by
specification version
Lotus WTS Enablement2007
21
Eclipse in Lotus Expeditor
Based on:–Eclipse 3.2.1 release including the Eclipse Rich Client Platform(RCP)
–Updates/patches to some Eclipse plug-ins
–Additional plug-ins from the SDK
Customers could add other Eclipse 3.2.1 compatible plug-ins (not
supported)
Lotus WTS Enablement2007
22
Eclipse and plugin.xml
Eclipse uses plugin.xml files –Extension points
–Legacy
–Plug-in identity
–Classpath
–Dependencies
–Startup Class
Legacy Information overlaps with OSGi MANIFEST.MF file
contents
–Framework always uses MANIFEST.MF
–Legacy information in plugin.xml translated into MANIFEST.MF file
–If MANIFEST.MF and plugin.xml contain conflicting information, MANIFEST.MF
has precedence
plugin.xml information cached when first processed
Lotus WTS Enablement2007
23
Eclipse and fragment.xml
Eclipse uses fragment.xmlfiles–Extension points
–Legacy
–Fragment identity
–Host identify
–Classpath
–Dependencies
Legacy Information overlaps with OSGi MANIFEST.MF file
contents
–Framework always uses manifests
–Legacy information in fragment.xmltranslated into MANIFEST.MF file
–If MANIFEST.MF and fragment.xmlcontain conflicting information,
MANIFEST.MF has precedence
Lotus WTS Enablement2007
24
Logging
Applications can use the following methods for logging messages
–Eclipse logging interface
–OSGi LogServiceinterface
–java.util.logginginterface (JSR47)
–Apache Commons Logging API
–Standard error and standard out
logRedirectorcollects all logged messages into a single, persistent
log file
Lotus WTS Enablement2007
25
Preferences
Preferences are Key/Value pairs
–Key describes the name of the preference
–Value is one of several different types, including Boolean, double, float, int, long
and string
Eclipse platform stores plug-in preferences and supports showing
them in Preferences dialog box
Client platform adds OSGi Configuration Admin Service
–Persistently stores preference information
–Enables system administrators to query and update configuration values via
Enterprise Management Agent
–Enables use of the OSGi MetatypeService to provide a metadata description of
the parameter types, default values and validation logic to be applied for each
parameter
Lotus WTS Enablement2007
26
Other OSGi Services
OSGi Utilities –Provides a consistent way to handle a diverse
range of measurements and geographic positions
OSGi User Admin Service -Supports authentication and
authorization of the users of an OSGi platform
OSGi Device Access -Supports the coordination of automatic
detection and attachment of devices on platform
Lotus WTS Enablement2007
27
Additional Services
International Components for Unicode for Java (ICU4J) –
Supports globalization of applications –Support multiple locales
–Support bidirectional text layouts
–Create translatable components
XML Parsers
–Use standard Java API for XML Parsing (JAXP)
–Use XML Parsing Service I/F to dynamically select parser at runtime –
requires additional code
Lotus WTS Enablement2007
28
OSGi Framework Programming Notes
The BundleContext
Registering A Service
–Offering to provide a service
Using A Service
–Finding other services
Lotus WTS Enablement2007
29
The BundleContext
A bundle's execution context within the Framework
–Created by the Framework itself
–Used to grant access to other methods so that this bundle can interact with the
Framework
–Passed to the BundleActivator start() and stop() methods
–Valid from BundleActivator.start() to BundleActivator.stop()
If other classes (service implementations) need the BundleContext, pass
it into a custom constructor
If a web application’s servlets or JSPs need the BundleContext, have the
BundleActivator store the BundleContext in a location that is accessible
by the web application's servlet code
Lotus WTS Enablement2007
30
Registering A Service
Register a service with the BundleContext
–ServiceRegistration reg= context.registerService(“service name“, implementation,
properties);
Registration ties an implementation class to the service interface
definition, so if you have the following:
–com.ibm.demo.MyService–an interface
–com.ibm.demo.MyServiceDemo–a class that implements MyService
You would have the following in the BundleActivator class:
public void start(BundleContextcontext) {
–ServiceRegistration reg= context.registerService(MyService.class.getName(),
new MyServiceDemo(),
null);
}
Lotus WTS Enablement2007
31
Using A Service
Requires a reference to the bundle context
Obtain a reference to the service (a ServiceReference object) from
the BundleContext:–ServiceReference ref = context.getServiceReference(“servicename");
Obtain the service from BundleContext using the ServiceReference
–myService= (MyService) context.getService(ref);
Invoke the service methods as needed
–myService.updateDatabase();
Lotus WTS Enablement2007
32
ServiceTracker
Registers with the Framework to receive notifications when required
services change states
Use in a separate thread to track any services your bundle needs
–The BundleActivator start() method should run quickly, since it will hold up the rest of
the system
–The start() method should not wait for a dependency service to start
–Spin a separate thread with a ServiceTracker which will be notified when the other
service is started or stopped
–Can create a reference to the other service and store it when the service becomes
available
Lotus WTS Enablement2007
33
ServiceTrackerClass Example
public class MyServiceTrackerimplements ServiceTrackerCustomizer
{ private BundleContextcontext;
private MyServiceservice;
public MyServiceTracker(BundleContextcontext) {
this.context=context; }
public Object addingService(ServiceReference ref) {
service = (MyService)context.getService(ref);
return service;
}
public void removedService(ServiceReference ref) {
service = null;
}
public void modifiedService() {}
}
Lotus WTS Enablement2007
34
Using a ServiceTracker
public void start(BundleContextcontext)
{
ServiceTrackerstDB= new ServiceTracker(context,
NewsDatabaseService.class.getName(),
new MyServiceTracker(context));
stDB.open();
}
Lotus WTS Enablement2007
35
Summary
OSGi Service Platform
OSGi Framework
OSGi Service Framework –Bundle, Fragments, Services
Inter-bundle Communication
Programming Examples
ServiceTracker
Lotus Worldwide Technical Sales
©2007 IBM Corporation
End of Presentation