Prof. Dr. Th. Letschert

CS5233 Components – Models and Engineering (Komponententechnologien) Master of Science (Informatik)

OSGI - Introduction

Seite 1 OSGI – what, why and who

What OSGi stands for once: OSGi : Open Service Gateway Initiative now: OSGi is a name for a Dynamic Module System for Java

Slides on OSGi are based on – OSGi Alliance: OSGi Service Platform Core Specification OSGi 2009 – J. McAffer, P. Vanderlei, S. Archer: OSGi and Equinox, Addison-Wesley 2010 – Wütherich/Hartmann/Kolb/Lübken, Die OSGI Service Platform – Dpunkt Verlag, 2008 Seite 2 OSGI – what, why and who

What OSGi is: OSGi is about building systems from components

OSGi - Components work together in a system can be managed (installed / removed) … at runtime

Seite 3 OSGI – what, why and who

What OSGi is: OSGi is a component framework for Java

OSGi-components are called bundles are units of resources can be installed have managed mutual dependencies are independent units can export services and run processes run in an OSGi-container

Seite 4 OSGI – what, why and who http://www.osgi.org What OSGi is:

OSGi is a (a set of) specification(s) Alcatel-Lucent Aplix Corporation by the OSGi-Alliance (founded 1999) Deutsche Telekom Ericsson Mobile Platforms AB with wide adoption outside the alliance Hitachi, Ltd. IBM Corporation developed in expert groups LinkedIn Makewave Core platform (CPEG) Mitsubishi Electric Corporation (VEG) OSGi in vehicles NEC Corporation Vehicle NTT Mobile (MEG) OSGi in mobile devices Oracle Corporation Paremus, Ltd. Enterprise (EEG) OSGi in enterprise solutions Progress Software ProSyst Software GmbH Residential (REG) OSGi in home automation Qualcomm Red Hat structured into 2 documents SAP AG Siemens AG Core Specification : framework Siemens Enterprise Communications Service Compendium : standard services Software AG Sonatype Inc. actual version 4.1 SpringSource (a Division of VMware) Sun Microsystems, Inc. Telcordia Technologies, Inc. TIBCO Software Inc. Westell Inc. Seite 5 OSGI – what, why and who

Where OSGi came from Requirement for a component model for devices Installable Software services  Software deployment and management  Security Originally aimed at home automation  Set top boxes, DSL-Modems, ··· building equipment and appliances Used in several areas for years  automotive  mobile devices Adopted by for Eclipse 3.0 OSGi Scenario: Refrigerator that runs Currently widespread use software form different vendors. Each of them may independently install new version of software modules.

Seite 6 OSGI – what, why and who

OSGi and Java OSGi has a focus on Java Bundles usually contain class-files Bundles may contain JNI code e.g. SWT-bundles in Eclipse Communication with non-Java devises is possible Script language support may be provided Java execution environment Requirements on a JVM to support OSGi framworks fulfilled by several JVMs (SE und ME)

related JSRs: JSR 232: Mobile operational Management JSR 291: Dynamic component support for Java SE

Seite 7 OSGI – what, why and who

OSGi and Eclipse Eclipse is a main adopter of OSGi Eclipse and OSGi in 2003  OSGi: a consortium of vendors in the area of embedded systems with the aim to provide a platform for inter-vendor compatible components called bundles.

 Eclipse: a consortium of vendors in the area of software tools with the aim to provide a platform for inter-vendor compatible components called plugins. 2003: Eclipse Project Equinox initiated  Aim: Resolve problems of Eclipse's runtime behavior Leverage of (then) current work on management and provisioning  Available technologies considered  JMX Java Management Extensions  Avalon Component framework : Apache project, 1999 – 2004, still active follower projects eg. Excalibur . OSGi Decision to use OSGi

 A number of changes and extension of the OSGi specification where developed  Based on IBMs OSGi-Impl. SMF (Service Management Framework), Equinox runtime was developed and replaced Eclipse runtime

Seite 8 OSGI – what, why and who

OSGi and Eclipse Eclipse and OSGi together changed the (software) world Eclipse ~> OSGi  OSGi: from niche to mainstream: a mature good technology became the marketing it deserved

 OSGi got a complete new market: all mayor Java application server are (or are in the process of being) ported to OSGi OSGi ~> Eclipse  gain of a mature component model  Eclipse/Equinox adoption outside Eclipse (embedded systems) OSGi + Eclipse

 Proof for everyone: Working runtime component systems can exist

 OSGi is not the first component system but it is the first with wide range of followers

 OSGi's original target market - “home gateways” - never where of any relevance but the strict constraints of them forced the development of a technology of widespread usage

Seite 9 OSGI – what, why and who

OSGi Survey Service platform: Container, Bundles, Services

OSGi Service Platform: Component technology consisting of Bundle ~ Component Set of classes and resources that have defined interfaces (provided / used services) may be installed (deployed) / de-installed at runtime Service ~ Component interface Services are provided by bundles that register them with the container Services are used by bundles that request them from the container Some Services are standard-services provided by the container Container ~ OSGi Framework Runtime environment for bundles Life-cycle management of bundles Service-management Provider of standard services

Seite 10 OSGI – what, why and who

OSGi Survey

OSGi application A set of bundles each bundle may contribute in any way: provide libraries, start threads, open sockets, open DBs, ···· no “main”-bundle: all bundles are peers no hierarchy: no bundle within a bundle

Bundle B Bundle A

Bundle D Bundle C

OSGi-Application

Seite 11 OSGI – what, why and who

OSGi Survey OSGi application vs Java application

OSGi-Application: A set of bundles with mutual dependencies declared and resolved through defined interfaces Java application: Dependencies are between (more granule) classes. They are declared using class-names and resolved using names, the position of a package / jar within the classpath, the loader that was used to load the using class, the loader that was used to load the used class, ···

Jar

Bundle B package Bundle A

class

Bundle D Bundle C

OSGi-Application Java-Application

Seite 12 OSGI – what, why and who

OSGi Survey Logical Layers according to the OSGi Specification Module Layer Defines bundles: the basic components Life-cycle Management Layer Life-cycle of bundles: states, actions, state transitions Service Layer Service definition, how to provide or use services Security Layer Security related definitions OSGi Framework Services S e Service Layer c u Lifecycle Mngmt r t Module Layer y

Execution Environment

Seite 13 OSGI – what, why and who

OSGi adoption

OSGi is a technology for enterprise servers

Enterprise servers based on OSGi ➢ IBM WebSphere Application Server ➢ Sun Glassfish V3 ➢ Oracle WebLogic Server ➢ Redhat JBoss ➢ SAP Netweaver Application Server (announced) ➢ ···

Seite 14 OSGI – what, why and who

OSGi adoption

OSGi is a technology for automotive electronics

➢ BMW ➢ Bombardier ➢ Siemens VDO ➢ Volvo ➢ Windriver ➢ ···

Seite 15 OSGI – what, why and who

What do you need:

an OSGi container (open source examples): http://eclipse.org/equinox/ ➢ Equinox OSGi-container implementation developed and used by Eclipse http://felix.apache.org/

OSGi-container implementation http://www.knopflerfish.org/ by the Apache Foundation

➢ Knopflerfish OSGi-container implementation developed by Makewave

➢ ...

Seite 16 OSGI – what, why and who

OSGi with Equinox / The console

see Equinox QuickStart Guide http://eclipse.org/equinox/documents/quickstart.php

Equinox is part of Eclipse and can be activated without further downloads – Start the Equinox -console: java -jar /path/to/eclipse/plugins/org.eclipse.osgi_3.5.2.jar -console osgi> – This starts the equinox implementation of the OSGi container and opens a console – The console may also be used with eclipse itself: /path/to/eclipse/eclipse -console

Seite 17 OSGI – what, why and who

OSGi with Equinox / The console Commands (Examples)

– help List all available commands. – ss List of all bundles together with their state and id. – ss List all bundles with names containing that string. – start Start up the bundle with a given id. – stop Stop the bundle with the given id. – install Install the bundle that the URL refers to. – uninstall Uninstall the bundle with the given id. – diag Show resolution problems for bundle with given id. – exit Exit

For more information use the help command

Seite 18 Th Letschert OSGI – what, why and who

OSGi with Equinox / The console

Usage Example: install http-server as eclipse bundles

> java -jar org.eclipse.osgi_3.5.2.jar -console osgi> install file:/path/to/eclipse/plugins/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar Bundle id is 1 osgi> install file:/path/to/eclipse/plugins/org.eclipse.equinox.ds_1.1.1.R35x_v20090806.jar Bundle id is 2 osgi> install file:/path/to/eclipse/plugins/javax.servlet_2.5.0.v200806031605.jar Bundle id is 3 osgi> install file:/path/to/eclipse/plugins/org.eclipse.equinox.util_1.0.100.v20090520-1800.jar Bundle id is 4 osgi> install \ file:/path/to/eclipse/plugins/org.eclipse.equinox.http.servlet_1.0.200.v20090520-1800.jar Bundle id is 5 osgi> install file:/path/to/eclipse/plugins/org.mortbay..util_6.1.15.v200905182336.jar Bundle id is 6 osgi> install file:/path/to/eclipse/plugins/org.mortbay.jetty.server_6.1.15.v200905151201.jar Bundle id is 7 osgi> setprop org.osgi.service.http.port=8888 Setting Properties: org.osgi.service.http.port = 8888

Seite 19 Th Letschert OSGI – what, why and who

Usage Example: install eclipse bundles (cont)

osgi> start 1

osgi> start 2 osgi> start 3 osgi> start 4 osgi> start 5 osgi> start 6 osgi> start 7 osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.5.2.R35x_v20100126 1 ACTIVE org.eclipse.osgi.services_3.2.0.v20090520-1800 2 ACTIVE org.eclipse.equinox.ds_1.1.1.R35x_v20090806 3 ACTIVE javax.servlet_2.5.0.v200806031605 4 ACTIVE org.eclipse.equinox.util_1.0.100.v20090520-1800 5 ACTIVE org.eclipse.equinox.http.servlet_1.0.200.v20090520-1800 6 ACTIVE org.mortbay.jetty.util_6.1.15.v200905182336 7 ACTIVE org.mortbay.jetty.server_6.1.15.v200905151201 osgi>

now a http server is up and running (of course it has no files to serve)

Seite 20 Th Letschert OSGI – what, why and who http://osgi.dzone.com/articles/osgi-survey-results

OSGi adoption :

OSGi survey report 2009 / Java developers on OSGi

➢ Are you familiar with the OSGi Service Platform? 92% have heard of OSGi

➢ Are you currently developing systems to be deployed to an OSGi runtime? 70.9% were using OSGi

➢ Do you have plans to develop systems that are to be deployed to an OSGi runtime within the next 6 – 12 months? 82.7% are planning to use it in the next 6 - 12 months

➢ Are you familiar with the available OSGi products and services 83.5% were familar with OSGi products and services

➢ Which OSGi implementations are you currently using or evaluating? Equinox, Felix are the leaders

Seite 21 Th Letschert OSGI – what, why and who

OSGi adoption : OSGi survey report 2009 / Java developers on OSGi

Which of the following types of systems are you planning to deploy within an OSGi runtime? 64.8% web apps 17.2% embedded development 15.6% mobile development 50.4% for rich clients 13.6% aren't using OSGi at all. What are the benefits you hope to achieve using OSGi? 83.9% greater modularity 56.6% hot deployment 51.4% manageable runtime environment 68.6% better dependency management 57.3% bundle versioning 63.5% plugin architecture 10.2% nothing

http://osgi.dzone.com/articles/osgi-survey-results

Seite 22 Th Letschert OSGI – what, why and who

OSGi adoption : OSGi survey report 2009 / Java developers on OSGi (cont.)

Which of the following types of systems are you planning to deploy within an OSGi runtime? 64.8% web apps 17.2% embedded development 15.6% mobile development 50.4% for rich clients 13.6% aren't using OSGi at all. What are the benefits you hope to achieve using OSGi? 83.9% greater modularity 56.6% hot deployment 51.4% manageable runtime environment 68.6% better dependency management 57.3% bundle versioning 63.5% plugin architecture 10.2% nothing

http://osgi.dzone.com/articles/osgi-survey-results

Seite 23 Th Letschert OSGI – Bundle

Bundle

 Bundles are runtime components, i.e. plugins  Eclipse plugins are (special) bundles  Incarnation : Jar-file containing: ➢ Content: classes, resources ➢ Meta-information: Bundle-specific meta-information  Content ➢ mainly packages identified by name and version ➢ hidden from other bundles ➢ unless explicitly exported and explicitly imported by other bundles  Meta-information . Manifest file (mandatory) ➢ as Jar manifest file META-INF/MANIFEST.MF ➢ file-content structured following the jar-specification ➢ with specific OSGi headers  Optional meta-information in optional directories e.g.: ➢ OSGi-INF: XML-files describing (declarative services) ➢ OSGi-OPT: security / i18n related specifications

Seite 24 Th Letschert OSGI – Bundle

Example: Bundle org.eclipse.core.runtime-v20090525.jar and its manifest file

Manifest-Version: 1.0 Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: CDC-1.0/Foundation-1.0,J2SE-1.3 Bundle-SymbolicName: org.eclipse.core.runtime; singleton:=true Require-Bundle: org.eclipse.osgi;bundle-version="[3.5.0,4.0.0)";visibi lity:=reexport,org.eclipse.equinox.common;bundle-version="[3.5.0,4.0. 0)";visibility:=reexport,org.eclipse.core.jobs;bundle-version="[3.2.0 ,4.0.0)";visibility:=reexport,org.eclipse.equinox.registry;bundle-ver sion="[3.4.0,4.0.0)";visibility:=reexport,org.eclipse.equinox.prefere nces;bundle-version="[3.2.0,4.0.0)";visibility:=reexport,org.eclipse. core.contenttype;bundle-version="[3.3.0,4.0.0)";visibility:=reexport, org.eclipse.core.runtime.compatibility.auth;bundle-version="[3.2.0,4. 0.0)";resolution:=optional,org.eclipse.equinox.app;bundle-version="[1 .0.0,2.0.0)";visibility:=reexport Bundle-Activator: org.eclipse.core.internal.runtime.PlatformActivator Export-Package: org.eclipse.core.internal.preferences.legacy;x-interna l:=true,org.eclipse.core.internal.runtime;x-friends:="org.eclipse.cor e.runtime.compatibility",org.eclipse.core.runtime;version="3.4.0" Bundle-Version: 3.5.0.v20090525 Bundle-ActivationPolicy: lazy Bundle-Vendor: %providerName Bundle-Name: %pluginName · · ·

Seite 25 Th Letschert OSGI – Bundle

Manifest file contains information about the bundle in headers:

 Identification: bundles identify themselves header: Bundle-SymbolicName header: Bundle-Version

 Export: Bundles may export packages  Exported packages form the bundle's API  Packages not exported are private  Packages are exported with their name and a optional version number (packages are versioned independently) header: Export-Package  Example:

Export-Package: org.eclipse.core.internal.preferences.legacy;x-interna l:=true,org.eclipse.core.internal.runtime;x-friends:="org.eclipse.cor e.runtime.compatibility",org.eclipse.core.runtime;version="3.4.0"

package org.eclipse.core.runtime is exported with version number 3.4.0

Seite 26 Th Letschert OSGI – Bundle

Manifest file contains information about the bundle in headers:

 Import

 Bundles may use packages exported packages only if they explicitly import them using the package name

 Version: Each import may be qualified with a version or a version range : lower and upper bound of versions for the imported package Version range spec. mathematically “[a,b)” “(“ ~ exclusive “[“ ~ inclusive

 header: Import-Package

. Example (from org.eclipse.equinox.http.servlet_1.0.200.v20090520-1800.jar)

Import-Package: javax.servlet;version="2.3", javax.servlet.http;version="2.3", org.osgi.framework;version="1.3.0", org.osgi.service.http;version="1.2.0" all imports are labeled with exact versions

Seite 27 Th Letschert OSGI – Bundle

Manifest file contains information about the bundle in headers:

 Requirement  Bundles may specify a dependency on entire bundles  Simplifies imports  May be used to express dependency on on-Java resources  Widely used in Eclipse because it maps to the import statements of eclipse's original plugin concept  Version: Requirements may be qualified with a version or a version range  header: Required-Bundle . Example (from org.eclipse.core.runtime-v20090525.jar)

Require-Bundle: org.eclipse.osgi;bundle-version="[3.5.0,4.0.0)";visibi lity:=reexport,org.eclipse.equinox.common;bundle-version="[3.5.0,4.0. 0)";visibility:=reexport,org.eclipse.core.jobs;bundle-version="[3.2.0 ,4.0.0)";visibility:=reexport,org.eclipse.equinox.registry;bundle-ver sion="[3.4.0,4.0.0)";visibility:=reexport,org.eclipse.equinox.prefere nces;bundle-version="[3.2.0,4.0.0)";visibility:=reexport,org.eclipse. core.contenttype;bundle-version="[3.3.0,4.0.0)";visibility:=reexport, org.eclipse.core.runtime.compatibility.auth;bundle-version="[3.2.0,4. 0.0)";resolution:=optional,org.eclipse.equinox.app;bundle-version="[1 .0.0,2.0.0)";visibility:=reexport

Seite 28 Th Letschert OSGI – Bundle

Resolving dependencies between bundles Dependencies are resolved using language rules and classloaders

 Dependencies between bundles are resolved at runtime

 Visibility follows usual Java language rules (public, protected, ···)

 Mechanism: Each bundle is equipped with it's own class loader when being installed

class name

+ class class bundle class loader

Seite 29 Th Letschert OSGI – Bundle

Lifecycle Instances of a bundle are created by installing them in a container / framework. Installed bundles have a lifecycle manged by the framework.

 The difference between a bundle and its installment is usually not made explicit. The noun “bundle” may refer to a bundle or to an installed bundle (a bundle instance). OSGi Spec differentiates between Bundle - deployed instance - and bundle - jar file.

 For each Bundle (an installed bundle) there is a Bundle-object that is managed by the framework

 Each Bundle goes through a lifecycle controlled by the framework

 A Bundle has to listen and react on lifecycle events

lifecycle events deploy / install, Bundle Bundle activate, stop bundle update, uninstall OSGi framework

Seite 30 Th Letschert OSGI – Bundle

Bundle Bundle

OSGi framework

Bundle lifecycle deploy / install, activate, stop update, uninstall

bundle

Diagr: OSGi Service Platform Spec. 4.2

Seite 31 Th Letschert OSGI – Bundle

Lifecycle

 installed: initial state of an (installed) bundle

 resolved: all dependencies are resolved, all classes are available, bundle is ready to be started

 starting: the bundle is in the process of starting, method BundleActivator.start is called but did not return

 active: bundle is active and running, method BundleActivator.start was called and did return

 stopping: the bundle is in the process of being stopped, method BundleActivator.stop was called but did not yet return

 uninstalled: the bundle has been uninstalled, it is no longer available for use of the framework

Seite 32 Th Letschert OSGI – Bundle

Bundle Context

 Upon activation a bundle gets a bundle context

 The bundle context represents the framework as seen by the bundle

 The bundle context is used by the bundle to:

 get information about the framework

 install other bundles

 information about other bundles  access persistent storage  access the service registry

 access the event subscription

Bundle Context (proxy to the framework)

Bundle Bundle Bundle

Framework

Seite 33 Th Letschert OSGI – Bundle

Events

 Types of events

 Bundle events: report state changes of a bundle  Service events: report service registration, un-registration, modification  Framework events: report changes in the framework (starting, stopping)

 Bundle listener

 registered by the bundle at the bundle context  called when the bundles stage changes

 Framework listener . registered by the bundle at the bundle context  called when a framework event occurs

Bundle Context Event Bundle Bundle Listener Framework

Seite 34 Th Letschert OSGI – Service

Services

 Types of bundle interaction

 Via direct references to classes provided by other bundles  Via Services: Access mediated by a service registry  Via Extension points (only Equinox)

 Service

 Service name: name of an interface  Service definition: a public interface defined in an exported package of a bundle . Service implementation: Object of a class that implements the interface (within the same or a different bundle) The service class usually is not exported.

 Service registration:  Service implementer registers the object under the name of the interface

 Service usage: A service consumer  imports the package containing the interface . looks up – using the name – the service registry for a matching object . uses the service object

Seite 35 Th Letschert Bundle / Equinox – Extensions

Extension and Extension Point

Extension Point: a complementary bundle collaboration mechanism. Bundle-local registration mechanism.

 Defined and implemented in Equinox

 Inheritance of Eclipse's pre-OSGi time

 Bundles declare extension points

 Other bundles provide extensions to an extension point

 Extensions and extension points are declared in XML (file plugin.xml)

Bundle Bundle Bundle Bundle

Seite 36 Th Letschert OSGi – Framework

Framework framework = runtime = container = platform

Specified in “OSGi Service Platform Core Specification” as

 part of an OSGi-compliant device  responsible for managing bundles, services and their dependencies

 Structured into “layers”:

 Execution Environment (EE)  Module  Lifecycle  Service  Security

Framework layers, their interaction and responsibilities (OSGi Service Platform Spec. 4.2 )

Seite 37 Th Letschert OSGi – Framework

Life cycle state diagram

Diagr.: OSGi Service Platform Spec. 4.2

Seite 38 Th Letschert OSGi – Framework

Life cycle of a framework

Outside of the specification: Classes that implement a framework must be made available through the class path or a special class loader. Launcher: a launcher (outside the specification) launches the framework by using the factory and a framework object:

 Factory: A framework implementation must provide a factory class which can create a framework object

 Framework life cycle refers to a framework object  installed: the framework has been created  starting: the framework has been initialized, none of its bundles are active  active: services and event handling is active, framework object has a bundle context object, framework services are available, all bundles are started  stopping: shutdown is initiated

Seite 39 Th Letschert OSGi – Framework

Launching a framework

Launching a framework (OSGi Service Platform Spec. 4.2 )

Seite 40 Th Letschert