32
Plan Introduction Fonctionnement Packaging Conclusion Master 2 Traitement de l’Information et Web Lionel Médini Octobre 2021 Intergiciels et Services Frameworks de composants dynamiques (OSGi)

Intergiciels et Services Frameworks de composants

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Master 2 Traitement de l’Information et Web

Lionel Médini

Octobre 2021

Intergiciels et Services

Frameworks de composants

dynamiques (OSGi)

Page 2: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Frameworks de

composants dynamiques

• Définition

– Framework : IoC, gestion des dépendances, services transversaux

– Composant : code exécutable packagé

– Dynamique : déploiement (chargement / déchargement) d’applications « à chaud »

• Exemples

– Java : OSGi

– MS : .Net + UPnP

– Android / Java : Kalimucho

>

Page 3: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

OSGi : historique

• Origine du nom (1998)

– Open Services Gateway Initiative

• Définie par l’OSGi Alliance

– Fondée en 1999

• Caractéristiques

– Open source

– Langage : Java

– Évolution en « Releases »

• Actuellement : R6

>

Page 4: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

OSGi : historique

• Fonctionnalités (©2008 S. Frénot, D. Donsez, N. Le Sommer)

Source (R5) : http://www.slideshare.net/bosschaert/the-os-gir5enterpriserelease

>

« Applications »

supporting services

- Subsystem service

- Repository service

- Resolver service

Auxiliary services

- Service loader mediator

- Common namespaces

Enhancements / updates

- JMX management

model

- Configuration admin

- Declarative services

- Coordinator services

2012

R5 (juin 2012)

Page 5: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

OSGi : applications ciblées

• À l’origine : mise à jour de modems / passerelles

• Puis : essor dans le domaine des applications

– Dynamiques : chargement des drivers via l’API UPnP

– Mobiles / ressources limitées : J2ME/CDC

– Embarquées : mBedded Server

• Depuis :

– Toute application décomposable : Java Platform 1.5…

– Tout système multi-applications : Java EE, ESB…

>

Page 6: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

OSGi : implémentations

• Frameworks OSGi

– Eclipse Equinox

– Apache Felix

– mBedded server…

• Autres frameworks Java

– Serveurs d’applications Java EE

• JBoss (WildFly / JBoss EAP), GlassFish, Jonas, iPOJO…

– ESB

• Fuse, ServiceMix…

>

Page 7: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Premières spécifications : JavaTM ME

– JSR 246: Device Management API

• Draft, août 2005, état « dormant »

– JSR 232: Mobile Operational Management

• Release 4, octobre 2006, état : « maintenance »

• 2 specs :

– « OSGi Service Platform Core Specification »

– « OSGi Service Platform Mobile Specification »

• Spécifications du framework, des 4 couches et de l’API

>

The framework also provides for multiple applications to coordinate the

use of single-access resources such the device display. In order to ensure

a safe environment, these components are controlled via a mandatory

security model based on the Java™ 2 Platform security model.

Page 8: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Version courante : JavaTM SE

– JSR 291: Dynamic Component Support

• Release 4, avril 2007, état : final

• Reprise de « OSGi Service Platform Core Specification »

(retravaillée)

• Spécifications du framework, des 4 couches et de l’API

>

The specification enables components to be declared through metadata

and be assembled at runtime using a class loader delegation network.

The specification also allows components to be dynamically life cycle

managed (install, start, stop, update, uninstall).

Page 9: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Version courante :

– Bundles : Bundles are the OSGicomponents made by developers.

– Services : The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects.

– Life-Cycle : The API to install, start, stop, update, and uninstall bundles.

– Modules : The layer that defines how a bundle imports and exports code.

– Security : The layer that handles the security aspects.

– Execution Environment : Defines what methods and classes are available in a specific platform.

Source : https://www.osgi.org/developer/architecture/

>

2012

Page 10: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Patterns utilisés

– GRASP, Modularité

• Bundle, Service

– Design by contract

• Interfaces

– Inversion de contrôle

• Execution Environment

– Annuaire

– Gestion du cycle de vie

• Activator

>

Page 11: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Principes

– Architecture orientée composants

• Composants = services = « bundles »

– Couplage faible

• Approche par interface

– Late binding (chargement à chaud)

• Reconfiguration dynamique

• Ex. : plugins, services techniques

– S’appuie sur un annuaire de composants

• Capable de localiser les composants

• Capable d’envoyer du code au client

>

Page 12: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Avantages / spécificités

– Modularité des applications

• Chargement/déchargement de code dynamique

• Déploiement dynamique d’applications

sans interruption de la plate-forme

– Installation, Lancement, Mise à jour, Arrêt, Retrait

– « No reboot »

• Résolution des dépendances versionnées de code

– Légereté

• Ne charge que le code nécessaire

• Vise des systèmes à mémoire restreinte

>

Page 13: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Le framework OSGi

• Objectifs du framework

– Instancier des objets dynamiquement en dehors de

l’initialisation des classes

– Pouvoir obtenir une nouvelle implémentation

• À chaque invocation de méthode

• À chaque mise à jour distante de la classe

– Instanciation en Java « classique »

Approche par interface

exemple1.QuelqueChose objet = new exemple2.AutreChose();

>

Page 14: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Le framework OSGi

• Fonctionnement du chargement dynamique

– Couplage léger entre demandeur et fournisseur de la

classe

• Seule l’interface du service est couplée au client

• L’implémentation est chargée à l’exécution

• Remarques

– Plus de constructeur injection de dépendances

– Nécessite de savoir où trouver les implémentations

URLClassLoader myCL = new URLClassLoader(“http://www.somewhere.biz”);

Class myClass = myCl.load(“foo.AServiceImpl”);

test.Service exemple = (test.Service)myClass.newInstance();

>

Page 15: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

FournisseurClient

Registre

de service

Fournisseur

Acme.com

Enregistrement

Fournisseur

Acme.com

notification

?

• Programmation orientée services (notification)

Le framework OSGi >

Page 16: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Le framework OSGi

• Caractéristiques d’un composant

– Nom OSGi : « bundle »

– Téléchargé à partir d’une URL

– Packagé dans un jar avec

• Un point de lancement : interface Activator

• Des bibliothèques de code

• Des ressources (code natif, documents…)

public void start(BundleContext bc) throws BundleException;

public void stop(BundleContext bc) throws BundleException;

>

Page 17: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Hello World

package hello;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

public class HelloWorld implements BundleActivator {

public void start(BundleContext bc){

System.out.println("Bonjour");

}

public void stop(BundleContext bc){

System.out.println("Au revoir");

}

}

Bundle-Name: Hello World

Bundle-Description: The simple bundle

Bundle-Activator: hello.HelloWorld

200 Tue Jun 15 16:20:00 CEST 2004 META-INF/MANIFEST.MF

723 Tue Jun 15 16:20:00 CEST 2004 hello/HelloWorld.class

Le Bundle est

“démarré”

Le Bundle est

« arrété »

>

Page 18: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Démarrage d'un bundle

1) Décompactage de l'archive dans un cache

2) Ouverture du manifest

3) Identification de la clé : Bundle-Activator

4) Création d'un chargeur de classe dédié

5) Instanciation explicite de la classe d'activation

6) Invocation de la méthode start dessus

BundleClassLoader myCL = new BundleClassLoader(“file:/tmp/hello.jar”);

String clazzName=myCL.getManifest().getBundleActivatorName();

Class myClass = myCl.load(clazzName”);

BundleActivator act = (BundleActivator)myClass.newInstance();

act.start(myCl.getBundleContext());

...

>

Page 19: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Cycle de vie :

États d’un bundle

INSTALLED RESOLVED

STOPPINGSTARTING

ACTIVEUNINSTALLED

unknown

start()

stop()

install()

>

Page 20: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Cycle de vie :

Transitions

INSTALLED RESOLVED

STOPPINGSTARTING

ACTIVEUNINSTALLED

unknown

start()

stop()

install()

Import/Export

ne sont pas

résolus

Le Bundle est

arrêté

Le Bundle est

démarré

Le Bundle

est

désinstallé

>

Page 21: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Exemple : service de log

Une Interface de service

Plusieurs implantations possibles...

package logsystem;

public interface Log{

public void log(String level, String message);

}

package frenot;

public LeLog implements logsystem.Log{

public void log(String level, String message){

System.out.println(message);

}

}

>

Page 22: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Exemple :

un bundle de service de log

package logsystem;

public interface Log{

public void log(String level,

String message);

}

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import logsystem.Log;

package frenot;

public LeLog implements Log, BundleActivator{

public void log(String level, String message){

System.out.println(message);

}

public void start(BundleContext bc){

bc.registerService("logsystem.Log", this, null);

}

public void stop(BundleContext bc){}

}

Bundle-Name: logger

Bundle-Description: un logger

Bundle-Activator: frenot.LeLog

Import-Package: org.osgi.framework

Export-Package: logsystem

200 Tue Jun 15 16:20:00 CEST 2099 META-INF/MANIFEST.MF

723 Tue Jun 15 16:20:00 CEST 2099 logsystem/Log.class

825 Tue Jun 15 16:20:00 CEST 2099 frenot/LeLog.class

>

Page 23: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Exemple : un autre bundle de

service de log

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import logsystem.Log;

package superlog;

public class SuperLog implements Log,

BundleActivator{

public void log(String level, String message){

System.out.println(level+ " : "+message");

}

public void start(BundleContext bc){

bc.registerService(logsystem.Log.class.getName(),

this, props);

}

public void stop(BundleContext bc){}

}

Bundle-Name: superlogger

Bundle-Description: un autre logger

Bundle-Activator: superlog.SuperLog

Import-Package: logsystem,

org.osgi.framework

200 Tue Jun 15 16:20:00 CEST 2099 META-INF/MANIFEST.MF

825 Tue Jun 15 16:20:00 CEST 2099 superlog/SuperLog.class

Des propriétés

distinguent les deux

implantations

Le package du log est fourni

par un autre bundle

Une implantation radicalement

différente

>

Page 24: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Lien entre service et bundles

Un service peut être fourni par 1 bundle

2 bundles

Séparation de l'interface et de l'implantation

N bundles

1 pour chaque implantation

Il y a indépendance complète entre interface

de service, implantations de services et bundles

>

Page 25: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Un service est fourni par

Appel direct :ServiceReference sr=

bc.getServiceReference("logsystem.Log");

Log lelog=(Log)bc.getService(sr);

lelog.log("DEBUG", "it works !");

Programmation réactivebc.addServiceListener(this,

"(ObjectClass=javax.log.LogInterface"));

public void serviceChanged(ServiceEvent serviceevent) {

ServiceReference servicereference=

serviceevent.getServiceReference();

switch (serviceevent.getType()) {

case ServiceEvent.REGISTERED :

this.log=(Log) serviceEvent.getService();

break;

case ServiceEvent.UNREGISTERING :...

}

>

Page 26: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Packaging

• Deux types d’APIs

– Services OSGi

• Services du framework (administration, events...)

• Services applicatifs

– Services Java EE

• Class A : composants dans un conteneur Java EE (EJB...)

• Class B : services annexes (JNDI, JMS...)

• Class C : utilitaires (JAXB, JPA...)

>

Page 27: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Packaging

• Plusieurs types d’applications

– Applications OSGi

• Services OSGi

– Applications hybrides

• Services OSGi et Java EE

• Packaging « classique »

• Ajout de métadonnées OSGi

Esprit Platform As A Service

– Web Application Bundles (WAB)

• Enterprise OSGi Service Platform Enterprise

Specification, Version 4.2 [3].

>

Page 28: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

La spécification OSGi

• Plusieurs spécifications (R6)

– Core

• Framework

– Compendium

• Services « built-in »

– Enterprise

• Java Web, JEE, distribution, scaling…

– Residential

• Gateways, smart home, IoT

>

Page 29: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

iPOJO

• Sous-projet de Felix (depuis fin 2014)

• But : simplifier le développement de bundles

• Caractéristiques

– Les composants sont des POJOs

– Déclaration et configuration par

• Annotations, XML, programmation

– Génération automatique du code “boilerplate”

– Modèle extensible et customizable

• http://felix.apache.org/documentation/subprojects/apache-felix-ipojo.html

>

Page 30: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Conclusion

• Quelques avantages d’OSGi

– Source : Benefits of Using OSGi

• Limites

– Complexité de l’API (hors iPOJO)

– Choix du langage / objectifs initiaux…

Reduced Complexity, Reuse, Real World, Easy Deployment, Non

Intrusive, Dynamic Updates, Adaptive, Transparency, Versioning,

Simple, Lazy, Secure, Small, Humble, Runs Everywhere, Widely Used,

Supported by Key Companies

>

Page 31: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Références

• OSGi

– Cours de Stéphane Frénot (CITI, INSA Lyon), donné à

l’école Intergiciel et Construction d’Applications

Réparties

– Projet Apache Felix : http://felix.apache.org/

– Site officiel OSGi : http://www.osgi.org/

• Repositories

– Référence sur les bundles disponibles :

http://www.osgi.org/Repository/HomePage

http://felix.apache.org/downloads.cgi

>

Page 32: Intergiciels et Services Frameworks de composants

PlanIntroductionFonctionnementPackagingConclusion

Références

• Ressources

– OSGi Learning Resources & Tutorials :

https://www.osgi.org/developer/resources/learning-

resources-tutorials/

– OSGi enRoute :

http://enroute.osgi.org/

>