Les services
Transcription
Les services
La plate-forme dynamique de service OSGi™ Didier Donsez Université Joseph Fourier (Grenoble 1) PolyTech’Grenoble LIG/ADELE [email protected] [email protected] http://membres-liglab.imag.fr/donsez/cours/osgi.pdf Sommaire ■ Motivations et Rappels ■ Conditionnement et Service ■ Enregistrement et recherche de services ■ Composants ■ Services standards (survol) ■ Acteurs, Concurrences et Perspectives © 2001-2010, D. Donsez OSGi 3 Qu’est ce que OSGi™ ? ■ Spécification OSGi ◆ définit un canevas de déploiement et d’exécution de services Java ◆ multi-fournisseur, télé-administré ◆ Cible initiale : set top box, modem cable, ou une passerelle résidentielle dédiée. ■ OSGi Alliance ◆ Corporation indépendante ◆ Soutenus par les acteurs majeurs des IT, home/building automation, telematics (car automation), … ◆ de la téléphonie mobiles (Nokia et Motorola) ◆ et Eclipse pour les plugins de son IDE ! ◆ et maintenant Apache pour ses serveurs © 2001-2010, D. Donsez OSGi 5 Qu’est ce que OSGi™ ? ■ Histoire ◆ ◆ ◆ ◆ ◆ ◆ ◆ Mars 1999 : Fondation de l’OSGi Alliance Novembre 1999: SUN transfère le JSR008 du JCP à OSGi OPEN SERVICES GATEWAY INITIATIVE (OSGi) TO DRIVE 1.0 : Mai 2000 (189 pages) DEVELOPMENT OF GATEWAY STANDARD FOR HOMES, SOHO AND REMOTE LOCATIONS 2.0 : Octobre 2001 (288 pages) Sun's Java™ Technology Accelerates Development of 3.0 : Mars 2003 (602 pages) Specification PALO ALTO, Calif., - November 22, 1999 - Open Services 4.0: Octobre 2005 (1000 pages) Gateway Initiative (OSGi) and Sun Microsystems, Inc. announced today that Sun has transferred the effort to define an open 4.1: Juin 2007 (optimisation du core R4) gateway specification from the Java™ Community Process to the Open Services Gateway Initiative. … ■ Remarque ◆ Open Services Gateway Initiative est un terme obsolète © 2001-2010, D. Donsez OSGi 6 L’ancêtre : JSR-8 : Open Services Gateway (OSG) ■ Java Embedded Server ◆ JavaOne e-Fridge ■ Domain : SOHO / ROBO Gateway ■ EG ◆ Spec leader : Robert Mines (Sun) ◆ Sun Microsystems, IBM, Nortel, Alcatel, Cable and Wireless, EDF, Enron, Ericsson, Lucent, Motorola, NCI, Phillips, Sybase, Toshiba ■ Package names ◆ javax.osg.servicespace ◆ javax.osg.remote ◆ javax.osg.service ■ Transfered to the OSGi Alliance © 2001-2010, D. Donsez OSGi 7 Principales propriétés du canevas OSGi ■ Modularisation des applications ◆ Chargement/Déchargement de code dynamique ❖ Langage Java ◆ Déploiement dynamique d’applications sans interruption de la plateforme ❖ Installation, Lancement, Mise à jour, Arrêt, Retrait ❖ « No reboot » ◆ Résolution des dépendances versionnées de code ■ Architecture orientée service ◆ Couplage faible, late-binding ◆ Reconfiguration dynamique des applications (plugins, services techniques) ■ Vise des systèmes à mémoire restreinte ◆ s’accroche à J2ME/CDC ◆ même si de plus en plus Java Platform 1.5, 6, 7, … © 2001-2010, D. Donsez OSGi 8 Rappel sur les chargeurs de classes ■ java.lang.ClassLoader ◆ Objet (java) chargé de charger en mémoire la définition des classes (.class) ■ Principe de la délégation ◆ Tout chargeur a un chargeur parent ❖ sauf le chargeur primordial ◆ Tout chargeur vérifie si la classe à charger n’a pas déjà été chargée par un chargeur parent ■ Arbre de délégation basique ◆ ClassLoader bootstrap ou primordial ◆ sun.misc.Launcher$ExtClassLoader (extension) ◆ sun.misc.Launcher$AppClassLoader (application ou system) Possibilité de personnaliser les chargeurs © 2001-2010, D. Donsez OSGi 9 Rappel sur les chargeurs de classes Pourquoi utiliser les chargeurs de classes ■ Classes non présentes dans le CLASSPATH ou le $JAVA_HOME/lib/ext ◆ URLClassLoader, AppletClassLoader, RMIClassLoader… ❖ ex: WEB-INF/classes et WEB-INF/lib d’une WebApp ❖ ex: CODEBASE d’une applet, … ■ Emballage particulier ◆ JavaEE EAR, OSGi bundle (fichiers JAR imbriqués), java.util.jar.Pack200, Google Android DEX format … ■ Modification du ByteCode à la volée au chargement ◆ Instrumentation ❖ AOP (Aspect Oriented Programming) ❖ BCEL, ASM, … ◆ Protection ■ Chargement de ressources associées à la classe ◆ properties, images, … ■ Déchargement et Mise à jour du bytecode lors de l’exécution de la VM (runtime) ◆ Chargeurs de OSGi © 2001-2010, D. Donsez OSGi 10 Rappel sur les chargeurs de classes Pourquoi NE PAS utiliser les chargeurs de classes ■ Beaucoup trop complexe pour le commun des mortels (et également pour les autres) ■ Indispensable de comprendre le fonctionnement ! ■ Car malheureusement beaucoup bricolent avec ! © 2001-2010, D. Donsez OSGi 11 Rappel sur la programmation OO « programming in the small » ■ Un client C invoque N fois la méthode execute() d’un serveur S ■ S s=new S() ■ C c1=new C(s, N); ■ C c2=new C(s, N); ■ Problème: Architecture ? Configuration ? © 2001-2010, D. Donsez OSGi 12 Rappel sur la programmation Composant « programming in the large » ■ Un client C invoque N fois la méthode execute() d’un serveur S ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ S s=SFactory.create() C c1=SFactory.create(); C c2=SFactory.create(); c1.setProperty("nloop",N); c1.bind("S",s); c2.setProperty("nloop",N); c2.bind("S",s); s.start() c1.start(); c2.start(); … © 2001-2010, D. Donsez OSGi nloop S C BC AC LC C BC AC LC Client Server y.Y y.Y 13 Rappel sur la programmation Composant ■ … ■ S s2=SFactory.create() ■ c2.stop(); ■ c2.bind("S",s2); ■ s2.start() ■ c2.start(); ■ Problème: Multi-domaines d’administration ◆ carte GSM SIM, WS, iTV STB, passerelle domotique, … © 2001-2010, D. Donsez OSGi 14 Rappel: Architecture orienté service (SOA) « programming in the VERY large » ■ Les services (contrats) sont « invariants » Service Service Registry Registry search register Service Service Consumer Consumer bind invoke Service Service Provider Provider servant ■ WebServices, TORBA, … © 2001-2010, D. Donsez OSGi 15 Rappel: SOA Dynamique ■ Arrivée dynamique de nouveaux services Service Service Registry Registry notify register Service Service Consumer Consumer bind/invoke bind/in ■ JINI, UPnP, OpenWings v ok e Service Service Provider Provider Service Service Provider Provider Acme.com Acme.com ■ OSGi © 2001-2010, D. Donsez OSGi 16 Rappel: SOA Dynamique ■ Retrait dynamique de services utilisés Service Service Registry Registry notify unregister Service Service Consumer Consumer bind/invoke bind/in ■ JINI, UPnP, OpenWings v ok e Service Service Provider Provider Service Service Provider Provider Acme.com Acme.com ■ OSGi © 2001-2010, D. Donsez OSGi 17 Dynamic Service Platform Zoo Invocation JINI OpenWings CORBA CosTrading UPnP V1 Remote (RMI) Remote (RMI IIOP ) Remote (IIOP) Remote (HTTP/SOAP1.0) Removal Registry Type Programming Language Lease Distributed (ad-hoc) Java Connector Distributed (?) Java No Distributed (?) all Message Bye Distributed (ad-hoc) all all Web Services Remote No DPWS (HTTP/SOAP1.2) Message Bye Centralized (UDDI) WS-Discovering SLP / DNSSD / Message Bye Distributed all OSGi Locale (Référence) Java Event Centralized Java © 2001-2010, D. Donsez OSGi 18 OSGi Modèle d’administration et Domaines d’application Domaines d’application ■ Initialement, Systèmes embarqués ◆ Véhicule de transport (automotive) ◆ Passerelle résidentiel/domotique/immotique ◆ Contrôle industriel ◆ Téléphonie mobile ■ Cependant ◆ Tout concepteur d’application est gagnant à distribuer son application sous forme de plugins conditionnés dans des bundles OSGi ◆ Cela évite l’enfer du CLASSPATH ❖ CLASSPATH, lib/ext du JRE ou JavaEE, … ■ Maintenant ◆ Eclipse RCP, JavaEE, Harmony JRE pieces, … © 2001-2010, D. Donsez OSGi 20 Architecture générale Serveur Fournisseur de service (société gardiennage) Serveur Fournisseur de service (hôpital) Serveur Fournisseur de service (distributeur électricité) Usager/Agent hors foyer Contrôle climatisation Compteur électrique Console Bundles ® Adele, 2002 Réseau Camera surveillance Domotique Déploiement Passerelle OSGi Usager Centrale alarme © 2001-2010, D. Donsez Moniteur cardiaque WEB OSGi Interactions Serveur Opérateur de la Passerelle 21 Architecture générale (i) Déploiement Serveur Fournisseur de service (société gardiennage) Serveur Fournisseur de service (hôpital) Serveur Fournisseur de service (distributeur électricité) Contrôle climatisation Compteur électrique Console Bundles ® Adele, 2002 Réseau Camera surveillance Domotique WEB Déploiement Passerelle OSGi Usager/ Patient Centrale alarme © 2001-2010, D. Donsez Moniteur cardiaque OSGi Serveur Opérateur de la Passerelle 22 Architecture générale (ii) Interactions Serveur Fournisseur de service (société gardiennage) Serveur Fournisseur de service (hôpital) Serveur Fournisseur de service (distributeur électricité) Usager/Agent hors foyer Contrôle climatisation Compteur électrique Console Bundles ® Adele, 2002 Réseau Camera surveillance Domotique WEB Interactions Passerelle OSGi Usager/ Patient Centrale alarme © 2001-2010, D. Donsez Moniteur cardiaque OSGi Serveur Opérateur de la Passerelle 23 Même architecture générale (iii) Contexte différent Service Provider Service Provider Service Provider (security firm) (equipment provider) (electricity company) User/operator outdoor Operator power meter over temp. Console Bundles ® Adele, 2002 Security cam Factory Network WEB Deployment OSGi Service Gateway Interactions RFiD reader © 2001-2010, D. Donsez Burglar alarm OSGi Gateway Operator 24 HVAC control power meter ® gateway-side services Adele, 2002 Adhoc Network (UPnP,DPWS) security cam Service Provider Servers Service Provider Servers Service Provider Servers (security firm) (hospital) (electricity company) JavaEE servers JavaEE servers JavaEE servers OSGi gateway enterprise-side services Customer/ Patient burglar alarm enterprise-side services enterprise-side services ECG monitor User/operator outdoor mobile-side services Management Console JavaEE servers Operator power meter oven temp. operator-side services gateway-side services Operator Management Servers ® Adele, 2002 security cam Factory Network (DPWS, …) OSGi gateway RFiD reader © 2001-2010, D. Donsez burglar alarm OSGi 25 Même architecture générale (iv) Contexte différent power meter ® Adele, 2002 Satellite Network (1553) Satellite Mission center (imaging firm) (weather conpany) (scientific labs) Magneto meter Space Web Console Deployment OSGi Service Gateway Interactions HR cam © 2001-2010, D. Donsez Mission center over temp. Bundles IR cam Mission center OSGi Control center 26 Application à la Distribution Electrique chez Schneider Electric Fournisseurs de services à valeur ajoutée (fabricant, intégrateur) Web Superviseur Fabrique Réseau de terrain A C C AC Distributeur d’électricité A C P Intranet / VPN Administrateur P Réseau de terrain A C C C Réseau de distribution électrique © 2001-2010, D. Donsez OSGi AC C Notation P Passerelle OSGi P P C Capteur A Actionneur AC Capteur+ Actionneur 27 Diagnostic de véhicules à distance ■ Aujourd’hui la part de l’électronique dans la conception d’un véhicule est de 35%. ◆ ■ 90% des innovations se font dans le domaine électronique Cependant 50% des pannes sont provoquées par des défaillances dans ce domaine. © 2001-2010, D. Donsez OSGi 28 Personal gateway ■ Human ◆ Cardiac patient, Baby, Prisoner, Soldier on battle field ■ Gateway (cell phone, watch, bracelet …) ■ Between ◆ wearable Sensors ❖ ECG, Sphygmomanometer, GPS/GSM, … ◆ and wearable actuators ❖ PaceMaker, Heart Defibrillator, Tourniquet (garotte), ■ and providers ◆ Emergency team, Parents, Prison service, Army , … © 2001-2010, D. Donsez OSGi 30 Exemple de Scénario Le photocopieur en location ■ Le photocopieur est installé en location chez le client ■ Le loueur (est une organisme financier) facture mensuellement à la consommation ◆ Fixe mensuel + tarif par feuille ■ Le loueur sous-traite la maintenance simple à une société spécialisée ■ La société de maintenance réalise un diagnostic à distance avant d’envoyer un agent ■ L’agent de maintenance interroge sur place le logiciel de diagnostic ■ Le fabricant peut mettre à jour le logiciel embarqué ◆ RICOH (26% copier market share) inclut une passerelle OSGi dans ses photocopieurs (en 2006). ❖ © 2001-2010, D. Donsez http://www2.osgi.org/wiki/uploads/Conference/OSGiCommunityBushnaq.pdf OSGi 35 Exemple de Scénario Le photocopieur en location (c) Didier Donsez, 2004 Customer Customer install and start the new version of the diagnotic tools Manufacturer Manufacturer update the version on the panel GUI core add a custom tab (ie plugin) to the panel GUI Renter Renter start the scanner function and the mail function of the copier subscribe ($€£) to the scanner function and the mail function of my copier © 2001-2010, D. Donsez OSGi 36 Le M2M (Machine-to-Machine) OSGi © 2001-2010, D. Donsez OSGi Taken from the JES book 37 OSGi Conditionnement, Déploiement et Service Rappel : Une application Java non modulaire ■ Ensemble de jarfiles ◆ placés statiquement dans le CLASSPATH ou $JRE_HOME/lib/ext JVM + JRE © 2001-2010, D. Donsez OSGi 39 Bundle ■ Bundle ◆ Unité de livraison et de déploiement sous forme de jarfile ◆ Unité fonctionnelle (offre des services) ■ Application ◆ « Ensemble » de bundles ❖ livrés dynamiquement ❖ et éventuellement partagés avec d’autres applications OSGi JVM + JRE © 2001-2010, D. Donsez OSGi 40 Structure d’un bundle Ressources Services fournis 1011 0110 1110 Services requis .class 1011 0110 1110 .class Packages exportés 1011 0110 1110 .class manifest Packages importés Activateur + Fichier Manifest objets métiers qui implémentent les services © 2001-2010, D. Donsez OSGi 41 Cycle de vie d’un Bundle (R4) Transition explicite Transition automatique install update refresh STARTING STARTING refresh update resolve start ACTIVE ACTIVE RESOLVED RESOLVED stop uninstall uninstall INSTALLED INSTALLED © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED 3 nouveaux états dans R4.1 •START_ACTIVATION_POLICY •START_TRANSIENT •STOP_TRANSIENT STOPPING STOPPING OSGi 44 Cycle de vie d’un Bundle Transition explicite Transition automatique install Installé Installé correctement correctement STARTING STARTING refresh update resolve start ACTIVE ACTIVE RESOLVED RESOLVED stop uninstall uninstall INSTALLED INSTALLED update Le aa été refresh Le bundle bundle été © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED STOPPING STOPPING OSGi 45 Cycle de vie d’un Bundle Transition explicite Transition automatique install update refresh STARTING STARTING refresh update resolve uninstall INSTALLED INSTALLED start ACTIVE ACTIVE RESOLVED RESOLVED uninstall Toutes Toutes les les classes classes que que nécessite nécessite le le bundle bundle sont sont Disponibles, Disponibles, ilil peut peut démarrer démarrer © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED OSGi stop STOPPING STOPPING 46 Cycle de vie d’un Bundle Transition explicite Transition automatique install update refresh ilil enregistre enregistre ses ses services, services, et et obtient obtient les les services services dont dont ilil aa besoin. besoin. refresh update resolve ACTIVE ACTIVE RESOLVED RESOLVED © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED STARTING STARTING start stop uninstall uninstall INSTALLED INSTALLED Le Le bundle bundle démarre, démarre, STOPPING STOPPING OSGi 47 Cycle de vie d’un Bundle Transition explicite Transition automatique install update refresh STARTING STARTING refresh update resolve start ACTIVE ACTIVE RESOLVED RESOLVED Le Le bundle bundle est est Actif Actif stop uninstall uninstall INSTALLED INSTALLED © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED STOPPING STOPPING OSGi 48 Cycle de vie d’un Bundle Transition explicite Transition automatique install update refresh start ACTIVE ACTIVE RESOLVED RESOLVED uninstall © 2001-2010, D. Donsez STARTING STARTING refresh update resolve uninstall INSTALLED INSTALLED UNINSTALLED UNINSTALLED stop Le Le bundle bundle arrête, arrête, ilil desenregistre desenregistre ses ses services. services. OSGi STOPPING STOPPING 49 Cycle de vie d’un Bundle Transition explicite Transition automatique install update refresh STARTING STARTING refresh update resolve Retour Retour àà l’état l’état Resolved Resolved start ACTIVE ACTIVE RESOLVED RESOLVED stop uninstall uninstall INSTALLED INSTALLED © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED STOPPING STOPPING OSGi 50 Cycle de vie d’un Bundle Transition explicite Transition automatique install refresh update resolve RESOLVED RESOLVED uninstall uninstall INSTALLED INSTALLED © 2001-2010, D. Donsez UNINSTALLED UNINSTALLED update refresh STARTING STARTING start ACTIVE ACTIVE stop STOPPING STOPPING Le Le bundle bundle est est désinstallé, désinstallé, ilil ne peut neOSGi peut plus plus changer changer d’état. d’état. 51 News in R4.1 ■ By default, persistent start and stop ■ Transient start and stop ◆ Bundle.start(int transientFlag) and Bundle.stop(int transientFlag) ❖ Flag sample: do not restart after FW restart (ie do no alter the autostart settin of the bundle) ■ By default, « Eager » activation ◆ The BundleActivator is instanciated when Bundle.start() ■ Lazy Activation ◆ The bundle is activate when a exported class is required by an other bundles new bundle event (lazy activated) : useful for extender model ■ A lire ◆ http://www2.osgi.org/wiki/uploads/Conference/OSGi4.1Overview.pdf © 2001-2010, D. Donsez OSGi 52 Dépendance & Dynamisme Notification d’un nouveau service Administrateur 1 2 > start 5 > uninstall 3 5 Service fourni Service requis 4 3 Package exporté Package importé Notification Dépendance de packages Dépendance de services © 2001-2010, D. Donsez OSGi 53 Dépendance & Dynamisme Administrateur 1 2 > start 5 > uninstall 3 5 Service fourni Service requis 4 Package exporté Package importé Notification Dépendance de packages Dépendance de services © 2001-2010, D. Donsez OSGi 54 Dépendance & Dynamisme Administrateur 1 Service relaché puis relié 2 > update 4 > refresh 5 Service fourni Service requis 4 Package exporté Package importé Bundle/Service redémarré Notification Dépendance de packages Dépendance de services © 2001-2010, D. Donsez OSGi 55 Service ■ Une interface (ou plusieurs) ■ Des implémentations ◆ multiples implémentations possibles conditionnées dans les bundles. ◆ implémentation normalement non publique. ◆ se trouvent dans des packages différents ■ Qualifié par des propriétés. Classe d’implémentation Interface com.hp.printer.laserjet.impl.Driver org.device.print.PrintService org.device.print.type=laser location=4th floor Classe d’implémentation com.lexmark.printer.laser.impl.Driver Interface org.device.print.type=laser org.device.print.PrintService © 2001-2010, D. Donsez OSGi location=1st floor 56 Exemple de service Interface org.device.print.PrintService package org.device.print; public interface PrintService { public static final String TYPE="org.device.print.type"; public int print(OutputStream out, String[] printparams) throws PrintException; public Job[] list() throws PrintException; } public interface Job[] { … } public class PrintException extends Exception { … } © 2001-2010, D. Donsez OSGi 57 Exemple d’application org.device.print.PrintService Text Text Editor Editor type=bubble dpi=72,150,300,600,1200,2400 color=16777216 location=1st floor type=laser dpi=72,150,300,600,1200 location=1st floor Service fourni Service requis type=laser dpi=72,150,300,600,1200 location=4th floor HP HP Deskjet Deskjet Printer Printer Lexmark Lexmark Laser Laser Printer Printer HP HP OfficeDesk OfficeDesk FaxPrinter FaxPrinter Package exporté PrintService PrintService Interface Interface FaxService FaxService Interface Interface Package importé org.device.fax.FaxService © 2001-2010, D. Donsez OSGi 58 .mf Fichier manifest (i) ■ Informations nécessaires au framework Import-Package Packages requis (avec/sans la version de spécification) Export-Package Packages fournis (avec/sans la version de spécification) Import-Service Services requis (indicatif, n’est pas utilisé par le FW) r3 Export-Service Services fournis (indicatif, n’est pas utilisé par le FW) r3 Bundle-Activator Nom de la classe Activator Bundle-ClassPath Emplacement des classes et ressources du bundle Bundle-NativeCode Bibliothéques natives à charger en fonction du processeur, du SE, … Bundle-UpdateLocation URL des mises à jour du bundle © 2001-2010, D. Donsez OSGi 59 .mf Fichier manifest (ii) by d l i Bu en 2 v Ma ■ Informations nécessaires au framework Bundle-SymbolicName r4 Nom symbolique du bundle (sert à l’identification) Bundle-Name Nom du bundle Bundle-Description Description du bundle Bundle-Version Version du bundle Bundle-DocURL URL de la documentation du bundle Bundle-ContactAddress Coordonnée du propriétaire du bundle Bundle-Category Catégorie du bundle Bundle-RequiredExecution Environment r3 Liste d’environnement qui doivent être présents sur la plateforme ( exemple : CDC-1.0/Foundation-1.0, OSGi/Minimum-1.0 ) DynamicImport-Package Liste de package qui pourront être importés en cours d’exécution 60 OSGi (com.acme.plugin.*) © 2001-2010, D. Donsez r3 .mf Exemple de manifest (i) HP HP Deskjet Deskjet Printer Printer org.device.print.PrintService Text Text Editor Editor Export-Package: org.device.print; 1.1 Bundle-Name: PrintService Bundle-Version: 2.0.1 Lexmark Lexmark Laser Laser Export-Package: org.device.fax; 1.1 Printer Printer Bundle-Name: FaxService Bundle-Version: 3.2.0 HP HP OfficeDesk OfficeDesk FaxPrinter FaxPrinter PrintService PrintService Interface Interface FaxService FaxService Interface Interface org.device.fax.FaxService © 2001-2010, D. Donsez OSGi 61 .mf Exemple de manifest (ii) Import-Package: org.osgi.framework;1.3 org.device.print; 1.0,org.device.print.PrintService Export-Service: org.device.print.PrintService, Bundle-Activator: com.hp.printer.deskjet.impl.Activator Bundle-Name: HP Deskjet Printer Text Text HP HP Deskjet Deskjet Import-Package: org.osgi.framework;1.3 Printer Printer org.device.print; 1.1, org.device.fax; 1.1 Export-Service: org.device.print.PrintService, Lexmark org.device.fax.FaxService Lexmark Laser LaserBundle-Activator: Printer Printercom.hp.printer.officedesk.impl.Activator Bundle-Name: HP OfficeDesk FaxPrinter Editor Editor HP HP OfficeDesk OfficeDesk FaxPrinter FaxPrinter PrintService PrintService Interface Interface FaxService FaxService Interface Interface org.device.fax.FaxService © 2001-2010, D. Donsez OSGi 62 .mf Exemple de manifest (iii) HP HP Deskjet Deskjet Printer Printer org.device.print.PrintService Text Text Editor Editor Lexmark Lexmark Laser Laser Printer Printer Import-Package: org.osgi.framework;1.3 org.device.print; 1.0 Import-Service: org.device.print.PrintService Bundle-Activator: org.eclipse.texteditor.impl.Activator Bundle-Classpath: .;/lib/resources.jar Bundle-Name: TextEditor HP HP OfficeDesk OfficeDesk FaxPrinter FaxPrinter PrintService PrintService Interface Interface FaxService FaxService Interface Interface org.device.fax.FaxService © 2001-2010, D. Donsez OSGi 63 Chargement de classes (i) ■ 1 ClassLoader par Bundle ◆ Chargement, Mise à Jour, Déchargement ■ Principe de la recherche des classes ◆ La classe est dans le JRE ◆ La classe est dans un package ni importé ni exporté ❖ Utilisation de la classe chargée à partir du BUNDLE-CLASSPATH ◆ La classe est dans un package importé ❖ Utilisation de la classe chargée par le CL d’un autre bundle ◆ La classe est dans un package exporté mais déjà exporté par un autre bundle ❖ Utilisation de la classe chargée par le CL de l’autre bundle ◆ La classe est dans un package exporté mais non exporté par un autre ❖ Utilisation de la classe chargée à partir du BUNDLE-CLASSPATH © 2001-2010, D. Donsez OSGi 64 Chargement de classes (ii) D’après la release3 d’OSGi B utilise javax.servlet exporté par A A importe javax.comm © 2001-2010, D. Donsez OSGi 65 Les limites r3 ■ Pas d’activation tant que tous les imports ne sont pas résolus ■ Un service package actif à la fois ■ Compatibilité ascendance à assurer Ad vitam eternam org.device.print.PrintService Editor Editor 1.0 1.0 org.device.print; spec-version=1.0 Printer Printer 1.0 1.0 org.device.print.PrintService Service fourni Editor Editor 1.1 1.1 org.device.print; spec-version=1.1 Printer Printer 1.1 1.1 Service requis Package exporté Package importé © 2001-2010, D. Donsez OSGi 66 Les avancés r3 ■ R3 ◆ Importation dynamique r4 ■ R4 ◆ ◆ ◆ ◆ ◆ ◆ Bundle fragment Bundle requis Bundle extension Intervalle de version, Politiques sur les versions Importation et Exportation conditionnelles (attribut et filtre) Activation simultanée de plusieurs version de packages ■ La suite : le JSR 277, JSR 294 … Jigsaw ◆ Richard S. Hall, “Java modularity, OSGi, and JSRs 277, 291, and 294”, ApacheCon EU 2006 ◆ http://docs.safehaus.org/download/attachments/2995/osgi-apachecon20060628.pdf ◆ http://www.osgi.org/blog/2008/12/project-jigsaw.html © 2001-2010, D. Donsez OSGi 67 DynamicImport-Package R3 ■ Permet en cours d’exécution d’importer des packages non présents à la résolution ❖ activator.getClass().getClassLoader().loadClass(clazzname) ❖ Surtout pas Class.forName(clazzname) ■ L’entrée DynamicImport-Package du mainfeste liste les packages qui pourront être importés en cours d’exécution ■ Usage : framework à plugin ou service provider ◆ Exemple : JMF, JCE, JDBC, … © 2001-2010, D. Donsez OSGi 68 DynamicImport-Package Exemple avec JMF (i) R3 ■ Installation de bundle 2 ◆ il passe à l’état ACTIVE myClassLoader.loadClass("com.acme.mp3.MP3Decoder") throws java.lang.NoClassDefFoundError Bundle 1 implementing JMF Bundle 2 using JMF BundleClassLoader BundleClassLoader javax.media .mf Export-Package: javax.media © 2001-2010, D. Donsez Import-Package: .mf javax.media DynamicImport-Package: com.acme.* OSGi 69 DynamicImport-Package Exemple avec JMF (ii) R3 ■ Installation de bundle 3 : il passe à l’état ACTIVE myClassLoader.loadClass("com.acme.mp3.MP3Decoder") add a package dependency then return the class Bundle 1 implementing JMF Bundle 2 using JMF Bundle 3 providing JMF codecs BundleClassLoader BundleClassLoader BundleClassLoader com.acme.mp3 javax.media .mf Export-Package: javax.media © 2001-2010, D. Donsez Import-Package: .mf javax.media DynamicImport-Package: com.acme.* OSGi Import-Package: .mf javax.media Export-Package: com.acme.mp3, com.acme.wave 70 Importation et Exportation conditionnelles r4 ■ Motivation ◆ La compatibilité descendante (backward compatibility) est lourde à maintenir (surtout en embarqué). ◆ Les @deprecated disparaissent parfois lors des chargements des majeures de versions ■ Idées ◆ Intervalle de version ◆ Import-Package: javax.servlet; version="[2.0.0,2.4.0)"; resolution="optional" ■ Autres ◆ Export-Package: org.foo.service; version=1.1; vendor="org.foo", org.foo.service.bar; version=1.1; uses="org.foo.service", org.foo.service.fizz; include:="*Impl"; exclude:="Test*" ◆ Richard S. Hall, Java Modularity Support in OSGi R4, ApacheCon (San Diego), December 14th, 2005 ❖ © 2001-2010, D. Donsez http://docs.safehaus.org/download/attachments/2995/osgi-apachecon-20051214.pdf OSGi 71 Accès aux ressources et aux fichiers ■ Ressources ◆ this.getClass().getRessourceAsStream(String path) path="/" correspond à la racine du JAR (BUNDLE-CLASSPATH) ■ Support de persistance ◆ BundleContext.getDataFile(String path) path="" correspond à la racine du cache du bundle ◆ FileService accès au système de fichier local (s’il existe !) et contrôle les permissions du Bundle au passage © 2001-2010, D. Donsez OSGi 72 .mf Bundle-Classpath ■ Représente (dans le manifeste) les chemins (dans le JAR) de recherche des classes et des ressources ■ 3 cas ◆ Bundle-Classpath: . ou Pas de Bundle-Classpath ❖ Recherche dans le JAR ◆ Bundle-Classpath: .;demo/nested.jar;test/nest.jar ❖ Recherche dans le JAR puis dans le JAR inclus ◆ Bundle-Classpath: demo/nested.jar ❖ Recherche dans le JAR inclus ❖ Aucune classe ou ressource n’est recherchée dans le JAR ■ Intérêt des JAR inclus ◆ Conservation des signatures, manifestes, … ◆ Possibilité de patcher un sous ensemble des ressources/classes ! © 2001-2010, D. Donsez OSGi 73 .mf Bibliothèques natives ■ Bibliothèques de fonctions natives (C) dépendantes du processeur et de l’OS ◆ Exemple : Pilotes matériel (javax.comm), Patrimonial (codec), … ■ Bundle-NativeCode dans le MANIFEST ◆ Spécifie l’emplacement des bibliothèques dépendantes du système et du processeur, à charger dynamiquement (par le ClassLoader) ◆ Exemple ❖ Bundle-NativeCode: com/mycomp/impl/nativesample/libnat.so; osname=Solaris; processor=sparc; osversion=5.5, com/mycomp/impl/nativesample/libnat.so; osname=SunOS; processor=sparc; osversion=2.5, com/mycomp/impl/nativesample/nat.dll; osname=Windows NT; processor=x86; osversion=4.0 ■ Remarque : Propriétés du framework ◆ org.osgi.framework.processor, org.osgi.framework.language, org.osgi.framework.os.name, org.osgi.framework.os.version © 2001-2010, D. Donsez OSGi 74 La classe d’activation du bundle ■ Classe publique ◆ Implémente les 2 méthodes start() et stop() de BundleActivator ◆ qui reçoivent une référence sur un contexte. ■ start(BundleContext ctxt) ◆ recherche et obtient des services requis auprès du contexte et/ou positionne des listeners sur des événements ◆ enregistre les services fournis auprès du contexte ■ stop(BundleContext ctxt) ◆ désenregistre les services fournis ◆ relâche les services requis ❖ Cependant le FW fait ces opérations si stop() en oublie ! ■ il peut ne pas y avoir de BundleActivator dans un bundle ◆ Livraison de classes et ressources ◆ Eclipse extension points ◆ Extender model © 2001-2010, D. Donsez OSGi 75 BundleContext ■ Interface vers le framework ◆ Passé lors des invocations de start() et stop() de l’Activator ■ Permet ◆ L’enregistrement de services ◆ Le courtage de services ◆ L’obtention et la libération des services ◆ La souscription aux évènements du Framework. ◆ L’accès aux ressources du bundle ◆ L’accès aux propriétés du framework ◆ L’installation de nouveaux bundles ◆ L’accès à la liste des bundles © 2001-2010, D. Donsez OSGi 76 BundleContext et Activator Bundle Context Service Bundles Bundle Activator •registerService() •getServiceReferences() •getService() •getDataFile() •addServiceListener() •addBundleListener() •addFrameworkListener() © 2001-2010, D. Donsez OSGi •start(BundleContext bc) •stop(BundleContext bc) •serviceChanged() 77 Enregistrement de services (Lexmark Laser Printer) package com.lexmark.printer.laser.impl; public class Activator implements BundleActivator { private ServiceRegistration reg=null; private PrintService theService=null; public void start(BundleContext ctxt) throws BundleException { theService=new PrintServiceImpl(); Properties props=new Properties(); props.put("type", "laser"); props.put("dpi", "72,150,300,600,1200"); props.put("location", "1st floor"); reg=ctxt.registerService( "org.device.print.PrintService", theService, props); } public void stop(BundleContext ctxt) throws BundleException { if(reg != null) reg.unregister(); } © 2001-2010, D. Donsez OSGi } 78 Recherche de services (TextEditor) package org.eclispe.texteditor.impl import org.device.print.PrintService; class Activator implements BundleActivator { public void start(BundleContext ctxt) throws BundleException { private PrintService ser; // On va voir si quelqu'un offre un PrintService ... ServiceReference[] tempRefs =ctxt.getServiceReferences ("org.device.print.PrintService","(location=1st floor)"); if(tempRefs!=null) { System.out.println("Found a PrintService! I will use it!!!"); // On prend le premier offert! ser=(PrintService) ctxt.getService(tempRefs[0]); } … } … } D. Donsez © 2001-2010, OSGi 79 Recherche (Courtage) de services ■ Filtrage par des expressions de condition LDAP (RFC1960) sur les propriétés enregistrées par les services ■ Expressions de filtrage ◆ Expressions simples (attribut opérateur valeur) ◆ Valeurs de type String, Numerique, Character, Boolean, Vector, Array ◆ Attribut insensible aux majuscules/minuscules ◆ L’attribut objectClass représente le nom du service ◆ Opérateurs >=, <=, =, ~= (approximativement égal), =* (présent) ◆ Connecteurs logiques &, |, ! © 2001-2010, D. Donsez OSGi 80 Recherche de services ■ Tous les services d’impression refs=bundleContext.getServiceReferences("org.device.print.PrintService", null); refs=bundleContext.getServiceReferences(null, "(objectClass=org.device.print.PrintService)"); ■ Certains services d’impression refs=bundleContext.getServiceReferences("org.device.print.PrintService", "(&(!(type=laser))(capability=double-sided)(!(dpi<=300))(location=*)" ); ■ Tous les services de org.device refs=bundleContext.getServiceReferences(null,"(objectClass=org.device.*)"); ■ Le service d’impression et de fax au 3ième étage refs=bundleContext.getServiceReferences(null, "(&(objectClass=org.device.print.PrintService)(objectClass=org.device.fax.FaxService)" + "(location=4th floor))" ); © 2001-2010, D. Donsez OSGi 81 Comparaison avec le courtage de JINI ■ JINI ■ OSGi ◆ Typage fort sur le nom de l’interface et la signature de ces méthodes ◆ Typage sur le nom de l’interface ◆ Sous-typage des interfaces ◆ Non ◆ Propriétés de courtage ◆ Oui ◆ Distribué ◆ Centralisé (même JVM) ◆ Notion de groupe ◆ Non ◆ Bail (lease) d’un enregistrement ◆ Non © 2001-2010, D. Donsez OSGi 82 Événements dans le Framework ■ FrameworkEvent ◆ Notifie le démarrage et les erreurs du Framework ◆ interface FrameworkListener méthode frameworkEvent Traitement séquentiel et asynchrone des listeners (par event dispatcher) ■ BundleEvent ◆ Notifie les changements dans le cycle de vie des bundles ◆ interface BundleListener méthode bundleChanged Traitement séquentiel et asynchrone des listeners (par event dispatcher) ◆ interface SynchronousBundleListener méthode bundleChanged Traitement séquentiel et synchrone des listeners (avant le traitement du changement d’état) R2 ■ ServiceEvent ◆ Notifie l’enregistrement ou le retrait de services ◆ interface ServiceListener méthode serviceChanged Traitement séquentiel et synchrone des listeners © 2001-2010, D. Donsez OSGi 83 Service Registry Hooks (RFC 126 R4.2) org.osgi.framework.hooks ■ Hooks on the service registry operations ■ PublishHook ◆ Bundles registering this service will be called during framework service publish (register, modify, and unregister service) operations. This method is called prior to service event delivery when a publishing bundle registers, modifies or unregisters a service and can filter the bundles which receive the event. ■ FindHook ◆ Bundles registering this service will be called during framework service find (get service references) operations. This method is called during the service find operation by the finding bundle and can filter the result of the find operation. ■ ListenerHook ◆ Bundles registering this service will be called during service listener addition and removal. The hook is notified of the collection of service listeners and what they may be listening for and well as future changes to that collection. © 2001-2010, D. Donsez OSGi 84 Prendre en compte l’enregistrement et le retrait de service (i) ■ Les bundles « requesters » doivent impérativement prendre en compte l’enregistrement et le retrait de services « importés » ■ Exemple public class PrintListenerActivator implements BundleActivator { PrintServiceListener listener = null; public void start(BundleContext context) { PrintServiceListener listener = new PrintServiceListener(context); context.addServiceListener(listener); } public void stop(BundleContext context) { context.removeServiceListener(listener); } } © 2001-2010, D. Donsez OSGi 85 Prendre en compte l’enregistrement et le retrait de service (ii) ■ Exemple simpliste et inutile class PrintServiceListener implements ServiceListener { public void serviceChanged(ServiceEvent e) { ServiceReference ref = e.getServiceReference(); if(((String)ref.getProperty("objectClass").equals("org.device.print.PrintService"))){ switch (e.getType()) { case ServiceEvent.REGISTERED: println(ref + " has been registered by "+ ref.getBundle().getLocation()); break; case ServiceEvent.UNREGISTERING: println(ref + " is being unregistered"); break; Ajout de ServiceEvent.MODIFIED_ENDMATCH en R4.2 case ServiceEvent.MODIFIED: println("properties of "+ref+" have been modified:"); String[] keys = ref.getPropertyKeys(); for (int i=0; i<keys.length; i++) println(keys[i] + "=" + ref.getProperty(keys[i])); break; } }} void println(String msg) {System.out.println("events: "+msg); } } © 2001-2010, D. Donsez OSGi 86 Prendre en compte l’enregistrement et le retrait de service (iii) ■ Exemple 2 : public class Activator implements BundleActivator { final static String filterStr ="(&(objectClass=org.device.print.PrintService)(location=4th floor))"; Map/*<ServiceReference,PrintService>*/ printservices; BundleContext context; public void start(BundleContext context) throws BundleException { this.context=context; printservices=new HashMap(); BindingController ctlr=new BindingController(context,filterStr,printservices); ctlr.open(); context.addServiceListener(ctlr); } © 2001-2010, D. Donsez OSGi 87 Prendre en compte l’enregistrement et le retrait de service (iv) public class BindingController implements ServiceListener { Map/*<ServiceReference,Object>*/ services; String filterStr; Filter filter; BundleContext context; public BindingController(BundleContext context, String filterStr, Map services){ this.context=context; this.filterStr=filterStr; this.services=services; filter=context.createFilter(filterStr); } © 2001-2010, D. Donsez OSGi 88 Prendre en compte l’enregistrement et le retrait de service (v) … public void open() { // fill the services map ServiceReference[] refs=context.getServiceReferences(null,filterStr); for(int i=0;i<refs.length;i++){ Object svc = context.getService(refs[i]); if(svc!=null) services.put(refs[i],svc); } } public void close() { // release the references to service … } … © 2001-2010, D. Donsez OSGi 89 Prendre en compte l’enregistrement et le retrait de service (vi) … public void serviceChanged(ServiceEvent e) { ServiceReference servref = e.getServiceReference(); Object ref; switch (e.getType()) { case ServiceEvent.REGISTERED: if(filter.match(servref)){ println(servref + " (from "+ servref.getBundle().getLocation() + ") is added"); services.put(servref,context.getService(servref)); }; break; … © 2001-2010, D. Donsez OSGi 90 Prendre en compte l’enregistrement et le retrait de service (vii) …. case ServiceEvent.UNREGISTERING: ref=services.remove(servref); if(ref!=null) { println(servref + " is removed"); context.ungetService(servref); } break; case ServiceEvent.MODIFIED: ref=services.get(servref); if(ref!=null && !filter.match(servref)){ println(servref + " is removed since properties has changed"); services.remove(servref); context.ungetService(servref); } break; }} © 2001-2010, D. Donsez OSGi 91 Prendre en compte l’enregistrement et le retrait de service (viii) ■ Le BindingController est incomplet ◆ Pas de synchronisation ◆ Les services apparus et disparus entre le getServiceReferences et le addServiceListener ■ Mini conclusion ◆ Vous avez suivi ? ◆ Et maintenant avec 6 services dont 3 obligatoires © 2001-2010, D. Donsez OSGi 92 Prendre en compte l’enregistrement et le retrait de service (viii) ■ Mini conclusion ◆ Vous avez suivi ? ■ Solutions R2 ◆ La classe utilitaire ServiceTracker (OSGi R2) ❖ Ne gère pas le cycle de vie ◆ Service Component Runtime (OSGi R4) ❖ ~ ADL pour cycle de vie et liaison ❖ Repris de ServiceBinder (Cervantes & Hall) © 2001-2010, D. Donsez OSGi 93 ServiceTracker ■ R2 Motivation ◆ Simplifier l’usage des ServiceListeners ■ ServiceTracker ◆ Classe utilitaire pour suivre un type de service (interface + filtre LDAP) ■ ServiceTrackerCustomizer ◆ Interface de rappel ■ Remarque ◆ peut être fourni par le framework afin d’en optimiser l’implémentation © 2001-2010, D. Donsez OSGi 94 ServiceTracker : Exemple ■ Dans le start(BundleContext) serviceTracker=new ServiceTracker( bundleContext, bundleContext.createFilter( "(&(objectClass=org.device.print.PrintService)(type=laser))"), (ServiceTrackerCustomizer)null); serviceTracker.open(); ■ Dans les méthodes du service PrintService ps=(PrintService) serviceTracker.getService(); ps.print(…); … Job[] jobs=ps.list(); … ps=null; ■ Dans le stop(BundleContext) serviceTracker.close(); © 2001-2010, D. Donsez OSGi 95 ServiceTrackerCustomizer : Exemple class MyServiceTrackerCustomizer implements ServiceTrackerCustomizer { public Object addingService( ServiceReference reference) { Object obj = context.getService(reference); HttpService servant = (HttpService)obj; // Register the Servlet using servant ... return servant; } public void removedService( ServiceReference reference, Object object ){ HttpService servant = (HttpService)obj; // Unregister the Servlet using servant ... context.ungetService(reference); } } © 2001-2010, D. Donsez OSGi 96 Service Component Runtime r4 ■ Modèle simple de composants orienté service ◆ Gère le cycle de vie du composant en fonction des dépendances obligatoires de services ■ Caractéristiques ◆ Fabrique de Services ◆ Activation retardée (lazy-activation) ◆ Gestion des liaisons ❖ stratégie événementielle ❖ stratégie de recherche (via le contexte) ◆ Entrée du manifeste : Service-Component ◆ Descripteur XML ❖ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0" ◆ … © 2001-2010, D. Donsez OSGi 97 Service Component Runtime ■ Programmation SOC Dynamique ◆ Démarrage des instances de services ◆ Listeners pour gérer la liaison dynamique vers les services requis Activator Service Or Factory Object Bundle Listener Bundle callback to the framework BundleCxt © 2001-2010, D. Donsez callback to the framework OSGi Framework OSGi BundleCxt 98 Service Component Runtime ■ Gestion descriptive des instances de services et des liaisons ◆ OSGI-INF/component.xml CM: SCR Component Manager Object component .xml Service Or Factory (thread, …) Object CM Bundle Listener Bundle callback to the framework BundleCxt © 2001-2010, D. Donsez CM component .xml Listener callback to the framework OSGi Framework OSGi BundleCxt 99 Service Component Runtime <component name="editor"> <implementation class="org.eclispe.texteditor.impl.EditorComp"/> <reference name="PRINT" interface="org.device.print.PrintService" target="(&(location=*)(org.device.print.type=inkjet))" cardinality="1..n" policy="dynamic" bind="bindPrintService" unbind="unbindPrintService" /> <reference name="LOG" interface="org.osgi.service.log.LogService" cardinality="0..1" policy="dynamic" /> </component> /OSGI-INF/component.xml <component name="printer"> <implementation class="com.hp.printer.deskjet.impl.PrinterComp"/> <property name="org.device.print.type" value="inkjet" type="String"/> <property name="location" value="4st floor" type="String"/> <property name="jmx.objectName" value="printer.hp:name=DeskJet 930c 4st fl." type="String"/> <service> <provide interface="org.device.print.PrintService"/> <provide interface="com.hp.printer.ConfigMBean"/> </service> </component> Word Word Processor Processor /OSGI-INF/component.xml HPPrinter HPPrinter LOG cmp.xml cmp.xml [org.device.print.PrintService, com.hp.printer.ConfigMBean] Editor Comp Printer Comp PRINT org.device.print.type="inkjet" location="4st floor" jmx.objectName="printer.hp:name=DeskJet 930c 4st fl." © 2001-2010, D. Donsez OSGi 100 Word Word Processor Processor HPPrinter HPPrinter LOG cmp.xml cmp.xml [org.device.print.PrintService, com.hp.printer.ConfigMBean] Editor Comp Printer Comp PRINT org.device.print.type="inkjet" location="4st floor" jmx.objectName="printer.hp:name=DeskJet 930c 4st fl." public class EditorComp { private List printServices = new ArrayList() public class PrinterComp implements PrintService, ConfigMBean { public void activate(ComponentContext ctxt) { LogService log = (LogService)ctxt.locateService("LOG"); if(log!=null) log.log(LogService.LOG_INFO, “Editor starting")); log=null; // must release this reference // démarre la thread principale du traitement de texte … } public void deactivate(ComponentContext ctxt) { // arrête la thread principale … LogService log = (LogService)ctxt.locateService("LOG"); if(log!=null) log.log(LogService.LOG_INFO, “Editor stopped")); log=null; } public void bindPrintService(PrintService ref){ synchronized (printServices ){ printServices.add(ref); } } public void unbindPrintService(PrintService ref){ synchronized (printServices ){ printServices.remove(ref); } } … // méthodes de PrintService public Job[] list(){ ... } public Job print(InputStrean in, Dictionary printParams ) throws PrintException { … } // méthodes de ConfigMBean public long getPrintedPageCounter(){ … } public void setDefaultTrailer(int trailerId){ … } … } } © 2001-2010, D. Donsez OSGi 101 Word Word Processor Processor HPPrinter HPPrinter LOG cmp.xml cmp.xml [org.device.print.PrintService, com.hp.printer.ConfigMBean] Editor Comp Printer Comp PRINT org.device.print.type="inkjet" location="4st floor" jmx.objectName="printer.hp:name=DeskJet 930c 4st fl." public class EditorComp { private List printServices = new ArrayList() public class PrinterComp implements PrintService, ConfigMBean { public void activate(ComponentContext ctxt) { LogService log = (LogService)ctxt.locateService("LOG"); if(log!=null) log.log(LogService.LOG_INFO, “Editor starting")); log=null; // must release this reference // démarre la thread principale du traitement de texte … } public void deactivate(ComponentContext ctxt) { // arrête la thread principale … LogService log = (LogService)ctxt.locateService("LOG"); if(log!=null) log.log(LogService.LOG_INFO, “Editor stopped")); log=null; } public void bindPrintService(PrintService ref){ synchronized (printServices ){ printServices.add(ref); } } public void unbindPrintService(PrintService ref){ synchronized (printServices ){ printServices.remove(ref); } } … // méthodes de PrintService public Job[] list(){ ... } public Job print(InputStrean in, Dictionary printParams ) throws PrintException { … } // méthodes de ConfigMBean public long getPrintedPageCounter(){ … } public void setDefaultTrailer(int trailerId){ … } … } } © 2001-2010, D. Donsez OSGi 102 iPOJO (Apache Felix) ■ Motivations ◆ Exécuter de pur POJOs (Plain Old Java Object) ◆ pour en faire des services ou utilisés des services sans utiliser l’API org.orgi.framework ■ Principes ◆ Injection de bytecode pour instrumenter les membres (compile time) ◆ Intercepter xload/xstore et invokex pour passer le contrôle à des handlers (eux meme des iPOJO) ◆ Les handlers travaillent en fonction des metadonnées décrites dans un descripteur (.mf,.xml, @nnotations 1.5, doclet …) © 2001-2010, D. Donsez OSGi 103 iPOJO example <ipojo> <instance component="EditorType" name="Editor"/> </ipojo> <ipojo> <instance component="HPPrinterType" name="HPPrinter"> <property name="location" value="1st floor"/> </ipojo> /metadata.xml /metadata.xml Word Word Processor Processor HPPrinter HPPrinter LOG cmp.xml cmp.xml [org.device.print.PrintService, com.hp.printer.ConfigMBean] Editor POJO Printer POJO PRINT org.device.print.type="inkjet" location="4st floor" jmx.objectName="printer.hp:name=DeskJet 930c 4st fl." @Component(name="EditorType", immediate=true) public class EditorComp { @Requires(filter="(&(location=*)(org.device.print.type=inkjet))") private List<PrintService> printServices; @Requires() private LogService logService; // business methods … } © 2001-2010, D. Donsez @Component(name="PrinterType", immediate=true) @Provides() public class PrinterComp implements PrintService, ConfigMBean { @ServiceProperty(name="location") private String position; // méthodes de PrintService … } OSGi 104 iPOJO Handlers ■ Provide ■ Service Requirement ◆ Method injection and Field injection ◆ Nullable object, Default implementation ■ Temporal Service Requirement ■ Lifecycle callback ■ Config ■ JMX ■ Event ■ Extender Pattern ■ Whiteboard Pattern ■ Custom ones … © 2001-2010, D. Donsez OSGi 105 iPOJO Architecture handler ■ Disable architecture ◆ <component classname="fr.imag.adele.escoffier.hello.impl.HelloServiceImpl" architecture="false"> ■ Command ◆ arch => displays instances name & state (equivalent to arch \instances) ◆ arch -instance $instance_name => displays complete information about the instance $instance_name ◆ arch -factories => display the list of available factories ◆ arch -factory $factory_name => display complete information about the factory $factory_name ◆ arch -handlers => list available handlers © 2001-2010, D. Donsez OSGi 106 iPOJO Configuration Handler ■ Add a CM ManagedService <iPOJO> <Component className="system.impl.DiskServiceImpl"> <Provides> Updates are <Property name="quota" field="m_quota"/> propagated to the </Provides> Service Registration <Properties propagation="true" updated="afterReconfiguration" /> <Property name="quota" field="m_quota"/> Invoke the method <Property name="threshold" method="updateThresholdArray"/> on update <Property name="disk.name" type="java.lang.String"/> Static property </Properties> </Component> <instance component="sysrtem.impl.DiskServiceImpl" name="DiskService"> Instance level <property name="quota" value="100"/> <property name="threshold" value="\{10, 20, 30}"/> <property name="disk.name" value="D"/> <property name="managed.service.pid" value="com.acme.system.disk.D"/> service.pid </instance> </iPOJO> class DiskServiceImpl implement DiskService { int m_quota; public void updateThresholdArray(int[] a) { … } public void afterReconfiguration(Dictionary config) { ... } } © 2001-2010, D. Donsez 107 OSGi iPOJO JMX Handler ■ XML <ipojo xmlns:jmx="org.apache.felix.ipojo.handlers.jmx"> ... <jmx:config> <jmx:property name="quota" field="m_quota" rights="r" notification="true"/> <jmx:method name="setQuota"/> </jmx:config> </ipojo> ■ @nnotations (not JSR 255 JMX annotations) @Component @Config(domain="my-domain", usesMOSGi=false) public class Disk { @Property(name="quota", notification=true, rights="r", description="Disk quota") private String m_quota; @Method(description="set the quota") // Method published in the MBean public void setQuota(int q) { if(q>0) m_quota = q; } } © 2001-2010, D. Donsez OSGi 108 iPOJO Event Handler Example ■ Publication @org.apache.felix.ipojo.handlers.event.Publisher(name="p3", synchronous=true, topics="bar") org.apache.felix.ipojo.handlers.event.publisher.Publisher publisher3; public void doSomething() { Dictionary d = new Properties(); // Fill out the even publisher3.send(d); // Send event } ■ Subscription @Subscriber(name="s1", data_key="data") public void receive1(Object o) { // Nothing } @Subscriber(name="s2", topics="foo,bar", filter="(data=DIDIER*)") public void receive2(Event e) { // Nothing } @Subscriber(name="s3", topics="foo", data_key="data", data_type="java.lang.String") public void receive3(String s) { // Nothing } © 2001-2010, D. Donsez OSGi 109 iPOJO Composite ■ Architecture Description Language for ◆ Required Service Specifications ❖ Instantiated and Imported ◆ Provided Service Specifications ❖ Exported and Implemented Instance of service Implementation ◆ Component Types Provided Service Instance of Component Type C B A D B Delegation Scheme from Clément Escoffier’ thesis defense © 2001-2010, D. Donsez E OSGi Imported Service 110 110 iPOJO Composite Example <composite name=“Editor1"> <subservice action="instantiate“ specification=“…Editor“/> <subservice action="instantiate“ specification=“… Plugin“ aggregate="true" /> <subservice action="import“ specification=“…Printer“ optional="true”/> </composite> Plug-in Plug-in Plug-in Printer Editor from Clément Escoffier’ thesis defense 111 © 2001-2010, D. Donsez OSGi 111 iPOJO implementation ■ Main features ◆ Bytecode manipulation (ASM) ◆ Extensible through Handlers ❖ Handlers are iPOJO instances ❖ Natively support dynamism ◆ Heavy use of threads and synchronization constructions ◆ on top of OSGi R4.0 (Felix, Equinox, KF) and various JVM 1.4, 1.5+ Container Handler Handler Content Handler Handler Handler Content Handler Handler from Clément Escoffier’ thesis defense 112 © 2001-2010, D. Donsez OSGi 112 Spring Dynamic Modules (ex Spring OSGi) ■ Rappel : Framework POJO (bean) pour les applications server-side ◆ Pour les déçus des EJB2 ❖ xml ou @nnotation 1.5 ■ Spring Framework + Beans ◆ Conditionnés en bundles ◆ Livrés sur OSGi ◆ + binding à la Declarative Services (~metadata SCR) ■ Remark ◆ Focus JavaEE … © 2001-2010, D. Donsez OSGi 113 SCA OSGi ■ Sujet chaud pour l’EEG ■ Service Component Architecture (SCA) ◆ Hierarchical component model for SO applications ❖ Java, C#, BPEL, JavaScript, Python … ■ SCA/OSGi ◆ SCA Container packaged as OSGi bundles then deployed in a OSGi framework http://www.osoa.org/download/attachments/250/Power_Combination_SCA_Spring_OSGi.pdf?version=3 © 2001-2010, D. Donsez OSGi 114 SCA/OSGi Remote Invocation http://www.osoa.org/download/attachments/250/Power_Combination_SCA_Spring_OSGi.pdf?version=3 © 2001-2010, D. Donsez OSGi 115 SCA OSGi http://www.osoa.org/download/attachments/250/Power_Combination_SCA_Spring_OSGi.pdf?version=3 © 2001-2010, D. Donsez OSGi 116 SOCA Benchmark : from Clément Escoffier’ thesis defense © 2001-2010, D. Donsez 117 OSGi 117 EasyBeans/OSGi ■ EasyBeans www.easybeans.org ◆ Containeur EJB3.0 – JSR 220 (annotations 5.0) + JSR 181 ❖ JSR 220 ~= « EJB for the dummies » ❖ JSR 181 = Web Services Metadata for the Java™ Platform ■ Motivations ◆ Exécuter des POJOs annotés JSR-220 sur OSGi ◆ Injecter le BundleContext dans les Enterprise Beans (@OSGiRessource) ❖ Enregistrement de services / Utilisation de services ■ Fonctionnement ◆ Conditionner les classes des POJOs dans un bundle ❖ l’ejbjar est emballé dans le bundle mais plus de JSR88 ! ◆ L’activateur crée un CL qui analyse et injecte les .class annotés et utilise le service du Runtime d’EasyBeans ◆ Le Runtime d’EasyBeans enregistre les EB Homes et fait l’intermédiaire avec les services techniques requis ◆ http://wiki.easybeans.org/xwiki/bin/view/Main/OSGi © 2001-2010, D. Donsez OSGi 118 Service fourni EasyBeans/OSGi Architecture Service requis Package exporté Package importé Dynamically bound according EJB apps requirements JMS JMS Service Service EJB EJB App App o.e.*MBean jx.jms JTS JTS Service Service EZB Activ. EasyBeans EasyBeans Runtime Runtime EBJ3.0 POJO EBJ3.0 POJO EBJ3.0 POJO o.ow.easybeans.server.Embedded jx.transaction Server Web Web Service Service EJB EJB App App * org.easybeans.* EZB Activ. org.osgi.service.obr.RepositoryAdmin Enables on-demand technical services installation © 2001-2010, D. Donsez J181 POJO EBJ3.0 POJO EBJ3.0 POJO EJB3.0 Apps are packaged and deployed as standard bundles OBR OBR OSGi (not EJB JAR, not JSR 88) 119 EasyBeans/OSGi Architecture (état 30/08/2006) Service fourni Service requis Package exporté Package importé © 2001-2010, D. Donsez OSGi 120 Guice-OSGi http://wiki.ops4j.org/confluence/display/ops4j/Guice-OSGi ■ Google Guice : IoD injector based on Java 5 annotations ■ Guice-OSGi injects required and provided services in a Java class Example ■ ◆ ◆ ◆ ◆ @Inject @OSGiService MyService unaryProxy; @Inject @OSGiService Iterable<MyService> multipleProxy; @Inject @OSGiService("(code=simple)") /* custom LDAP filter */ MyService filtered; @Inject @OSGiService(interfaces = {MyService.class}) /* custom interface list */ MyOSGiListener listener; ◆ @Inject @OSGiServiceRegistration MyService registeredService; ◆ @Inject @OSGiServiceRegistration("lang=en,location=uk") /* custom service properties */ MyService configuredService; ◆ @Inject @OSGiServiceRegistration(interfaces = {MyBaseService.class}) /* custom interface list */ MyService customizedService; ❖ Registered OSGi services can be controlled using the static methods in the GuiceOSGi utility class (ie. enable/disable/modify). ◆ @Inject BundleContext bc; ❖ Inject the bundle context © 2001-2010, D. Donsez OSGi 121 ServiceFactory ■ Motivation ◆ Retourne une instance par bundle client ❖ différentes instances pour un même service ❖ Attention : ~~~ patron de conception de la fabrique (Gamma) ◆ Nécessaire à un singleton manipulant son ServiceRegistration ■ Utilisation ◆ Gestion de sessions multiples ❖ Multi-fenêtrages ❖ plusieurs shells ◆ Contrôle d’accès (le bundle réclamant le service est connu) ❖ Contrôle des permissions ◆ Suivi des références « cachées » ❖ Patron « Whiteboard » (chapitre suivante) © 2001-2010, D. Donsez OSGi 125 ServiceFactory ■ Interface à implémenter interface ServiceFactory { Object getService(Bundle bundle, ServiceRegistration registration) public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) } © 2001-2010, D. Donsez OSGi 126 Sécurité ■ Basé sur les permissions du JDK1.2 ◆ Le SecurityManager vérifie les permissions de chaque bundle ❖ Exemple : FilePermission, DialPermission, … ◆ Accès aux fichiers ❖ ■ Ceux du cache et aux ressources du bundle 3 permissions propres à OSGi ◆ AdminPermission ❖ Autorise l’accès aux fonctions d’administration du framework. ◆ ServicePermission ❖ Contrôle l’enregistrement et la récupération de services ◆ PackagePermission ❖ ■ Contrôle l’import et l’export de packages org.osgi.service.PermissionAdmin ◆ Service de gestion des permissions des bundles ■ Conditional Permission Admin (R4) © 2001-2010, D. Donsez OSGi 127 OSGi Guide de Bonnes pratiques Modularité ■ Séparer les classes « published » (ie contrat) des classes « propriétaires » dans des paquetages des différents ◆ Seul les classes « published » doivent être exportées ■ Conditionner les contrats et les implémentations dans des bundles séparés ◆ Les contrats ne varient peu et sont partagés par plusieurs bundles ■ Import-Package plutôt que Require-Bundle (R4) ◆ substitutabilité avec d’autres fournisseurs de packages ■ Limitez l’usage des fragments ■ Evitez l’usage de DynamicImport-Package ◆ Sauf cas particulier (livraison dynamique de plugin, …) ■ Définissez le ExecutionEnvironnement ◆ Et utilisez le pour la compilation !!! © 2001-2010, D. Donsez OSGi 129 Conditionnement ■ JAR enfouis ◆ Ne déconditionnez pas les JAR ❖ Utilisez le Bundle-ClassPath ◆ Conservation des signatures, manifestes, … ◆ Possibilité de les patcher ! ❖ Bundle-ClassPath: patch.jar,original.jar © 2001-2010, D. Donsez OSGi 130 Service (i) ■ Rappel 1. Code de fourniture << d’un service Code d’usage d’un service 2. 1 service = 1 entrée dans le registre ◆ Courtage sur plusieurs milliers/millions de services 3. Traitement séquentiel (et synchrone) des listeners ■ Quid pour plusieurs milliers de listeners par framework © 2001-2010, D. Donsez OSGi 131 Service (ii) ■ 2 patrons disponibles ■ Listener 3 Service Listeners 0-1 Bundle Listener 1 Service Factory Http Http Service Service WebSite WebSite WebSite WebSite WebSite WebSite Exemple: LogService, HttpService, … 1 Service Listener ■ Whiteboard Shell Shell Command Command Command Command Command Command Exemple: EventAdmin, IOConnector, Felix’ Shell, … ■ Preferez (le plus possible) le patron Whiteboard (sauf si (2)) ◆ Listeners Considered Harmful: The “Whiteboard” Pattern ◆ http://www.osgi.org/documents/osgi_technology/whiteboard.pdf © 2001-2010, D. Donsez OSGi 132 Le patron par extension (Extender) ■ Limite l’usage des services (listeners) ■ Principe ◆ 1 BundleListener scrute les bundles contenant des metadatas particulières (WEB-INF/web.xml, OSGI-INF/component.xml, plugin.xml …) ◆ et instancie des objets depuis le ClassLoader du bundle scruté lors du démarrage, dépose les objets crées ■ Exemples ◆ SCR, iPOJO, Spring DM, Eclipse Extension Point Registry… © 2001-2010, D. Donsez OSGi 133 Service (iii) ■ Granularité ■ Objet << Composant << Service << Bundle © 2001-2010, D. Donsez OSGi 134 Membres statiques ■ AIE ! ■ Evitez les dans les classes « published » ◆ Difficile de tracker le cycle de vie du bundle qui les fournit ■ Il est fréquent de voir class MyActivator implements BundleActivator { public static BundleContext bundleContext; … } © 2001-2010, D. Donsez OSGi 135 Chargeurs de classe (ClassLoaders) ■ Usage ◆ ◆ ◆ ◆ Dynamic bytecode injection Dynamic aspect-weaving Dynamic annotation processing … ■ Principe ◆ Le chargeur de classe doit avoir pour parent le chargeur du bundle ❖ ie MyActivator.class.getClassLoader() ◆ Le chargeur peut demander la récupération des ressources (.class) au bundle r4 ❖ Enumeration e = bundle.findEntries("/", "*.class", true); ■ Exemples ◆ ProActive, Julia (FROGi), EasyBeans, … © 2001-2010, D. Donsez OSGi 136 Cycle de vie ■ Activation ◆ 1 sec par bundle 1 minute pour 60 bundles 5 minutes pour 300 bundles … ◆ Evitez des activateurs qui durent longtemps ■ Solutions ◆ Rendre la main au canevas le plus vite ❖ Utilisez l’eventing ❖ Ou une thread en arrière plan ◆ Préférez l’activation paresseuse (R4.1 lazy activation) ❖ lazyness is goodness © 2001-2010, D. Donsez OSGi 137 Cycle de vie ■ Vous développez le start() ■ Pensez aussi au stop() ◆ Proscrire System.exit() ❖ Votre bundle n’est pas le seul sur la JVM ❖ il y en a d’autres qui bossent ◆ Libérez les ressources ❖ Fermez les fichiers, sockets, connections, … ◆ Arrêtez les threads démarrés ❖ Mieux encore rendez les au service de pool de threads ◆ Nullifiez les références vers les servants ❖ Garbage collection (objets et classes) © 2001-2010, D. Donsez OSGi 138 What are Stale References? “a reference to a Java object that belongs to the class loader of a bundle that is stopped or is associated with a service object that is unregistered” OSGi R4 Section 5.4 © 2001-2010, D. Donsez OSGi 139 An example of Stale Reference Pathology? (i) initial > start 2 Servant ready (v1.0) registered servant class Bv1.0 class loader Bundle #2 v1.0 ACTIVE framework © 2001-2010, D. Donsez OSGi 140 An example of Stale Reference Pathology? (i) initial sayHello() > start 2 Servant ready (v1.0) > start 3 1- Hello World ! (v1.0) 2- Hello World ! (v1.0) registered servant service consumer sayHello() class Bv1.0 class loader Bundle #2 v1.0 ACTIVE class loader Bundle #3 ACTIVE framework © 2001-2010, D. Donsez OSGi 141 An example of Stale Reference Pathology? (ii) After stop 2 stale references > start 2 Servant ready (v1.0) > start 3 1- Hello World ! (v1.0) 2- Hello World ! (v1.0) > stop 2 Servant bye bye (v1.0) service consumer unregistered servant class Bv1.0 class loader Bundle #2 v1.0 RESOLVED (ie stopped) class loader Bundle #3 ACTIVE framework © 2001-2010, D. Donsez OSGi 142 An example of Stale Reference Pathology? (iii) After update 2 & start 2 continue to serve !!! > start 2 Servant ready (v1.0) > start 3 1- Hello World ! (v1.0) 2- Hello World ! (v1.0) > stop 2 Servant bye bye (v1.0) 3- Hello World ! (v1.0) 4- Hello World ! (v1.0) > update 2 > start 2 Servant ready (v1.1) 5- Hello World ! (v1.0) 6- Hello World ! (v1.0) sayHello() unregistered servant service consumer registered servant sayHello() class Bv1.0 class Bv1.1 class loader Bundle #2 v1.0 « Zombie » class loader Bundle #3 ACTIVE class loader Bundle #2 v1.1 ACTIVE framework © 2001-2010, D. Donsez OSGi 143 Bad Consequences ■ Memory leaks ◆ Retention of the classloader of a stopped or uninstalled bundle ◆ Retention of all java.lang.Class loaded by that bundle ■ Utilization of invalid services Inconsistencies! ◆ Service is unregistered but still used (wrong!) ◆ Its context is most likely inconsistent ❖ e.g. closed connections, old date ◆ Possible exceptions upon service calls ❖ good because we can see the problem ◆ Silent propagation of incorrect results (worst case!) ❖ E.g. Returning old cached-data © 2001-2010, D. Donsez OSGi 144 Other « stale » pathologies ■ “Forwarded references” ◆ From one bundle to another ■ “Stale” threads ◆ bundle has stopped but created threads has not ■ Unregistered MBeans, RemoteObjects, … ■ Unreleased resources ◆ sockets, file descriptors, locks, … ■ Stale ExtensionPoints ◆ Eclipse sugests to restart after updating ! ;-( © 2001-2010, D. Donsez OSGi 145 How to ensure « stale reference free » applications? ■ 2 cases of OSGi™ applications ■ From-scratch OSGi™ development ■ Bundlization of Legacy codes ◆ Really frequent ◆ Module with or without Services/Extension Points ■ Good OSGi™ programming practices ◆ Who trusts their developers ? ■ Component Models ◆ Necessary but not enough ■ Stale references may be there but we can’t see them… ■ We need Diagnosis victim bundles x guilty bundles © 2001-2010, D. Donsez OSGi 146 The ServiceCoroner tool ■ A diagnostics tool for detecting stale references in OSGi™ applications ■ “Inspector” of services death ■ Runtime diagnosis ■ Points out victim bundles/services and possible suspects *The coroner is a legal examiner that investigates the causes of unnatural deaths in English speaking countries. Not all coroners have forensic pathology knowledge, but for illustration purposes we have named our tool as ServiceCoroner. © 2001-2010, D. Donsez OSGi 147 Stale References are not a myth ! Experiment results (May 2008) SIP Comm. JOnAS [1] I OSGi-based software II Version III OSGi Impl. Newton Sling (JavaEE server) (multiprotocol VoIP and Chat UA) (SCA container) (Content Repository) 5.0.1 Alpha 3 1.2.3 2.0 incubator snapshot Felix 1.0 Felix 1.0 Equinox 3.3.0 Felix 1.0 20 iPOJO 0 IV Bundles using Component Models 6 Service Binder 18 Declarative Services V Lines of Code Over 1 500 000 Aprox. 120 000 Aprox. 85 000 Over 125 000 VI Total Bundles 86 53 90 41 VII Initial No. of Service Refs. 82 30 142 105 VIII No. of Bundles w/ Stale Svcs. 4 17 25 2 IX No. of Stale Services Found 7 19 58 3 X No. of Stale Threads 2 4 0 0 XI Stale Services Ratio (IX/VII) 8.5 % 63 % 40.8% 2.8% Actually the whole Newton implementation is an SCA constructed on top of OSGi, but its bundles did not use an OSGi component model like the other analyzed applications did. © 2001-2010, D. Donsez OSGi 148 Plus ■ A lire ◆ BJ Hargrave & Peter Kriens, « OSGi Best Practices! », Session TS 1419, 2007 JavaOne Conference et OSGi Users Community Event 2007 ❖ http://www2.osgi.org/wiki/uploads/Conference/OSGiBestPractices.pdf ◆ « Extensions vs Services: Digging Deeper », OSGi Users Community Event 2007 ❖ http://www2.osgi.org/wiki/uploads/Conference/NeilBartlett.pdf © 2001-2010, D. Donsez OSGi 149 Autres ■ Les outils arrivent … ◆ PDE, Management (Console) ■ Les recettes arrivent … ◆ Migration vers OSGi en 2 étapes ❖ Modularisation ▲ Ecueil : ClassLoader, Thread context switch, Ressources, JNDI, static, default packages (includes for rsc.), … ❖ (D)SOA ou Extension Points ◆ Exemple personnel : ❖ Tomcat (dans JOnAS), JacORB, … © 2001-2010, D. Donsez OSGi 150 OSGi Les services standard Vehicle Home & Industrial Automation Mobile Les services OSGi standard R3 R2 R1 • • • • Framework HTTP Log Device Access 2000 © 2001-2010, D. Donsez • • • • • • • Package Admin Configuration Admin Permission Admin User Admin Preferences Meta Type Service Tracker • • • • • • • • • • • UPnP Initial Provisioning Name Space Jioi Start Level IO Connector Wire Admin XML Parser Measurement and State Position Execution Env R4 • • • • • • • • • • • Application Model Signed Bundles Declarative Services Power Management Device Management Security Policies Diagnostic/Monitoring Framework Layering Initial Provisioning UPnP Deployment D’après Peter Kriens 2001 2003 OSGi 2005 152 SystemBundle Under Construction En Construction ◆ Représente le framework. Son identifiant est toujours 0. ◆ Son cycle de vie correspond au démarrage et à l’arrêt du Framework © 2001-2010, D. Donsez OSGi 153 r1 HttpService org.service.http.HttpService (i) ■ Service permettant à d’autres bundles de publier des servlets et ressources par HTTP ◆ Important : Web-based management ■ Implémentations ◆ embarquent un serveur HTTP compact (Jetty,…) ◆ Authentification et Autorisation (BasicSchema, SSL) ◆ Servlets Web-Services : XML-RPC, kSOAP, SOAP/HTTP, RESTFul, … ■ Extra ◆ <jspc> pour éviter d’embarquer un compilateur de JSP ◆ Convertisseurs WAR to Bundles ◆ Canevas Web (Cocoon, Wicket, DysoWeb …) © 2001-2010, D. Donsez OSGi 159 org.service.http.HttpService (ii) Usage HTTP org.osgi.service.http.HttpService MonSiteWeb MonSiteWeb servlet Http Http Service Service /moi/livreor /moi/* http context javax.servlet © 2001-2010, D. Donsez OSGi Servlet Servlet 160 org.service.http.HttpService (iii) Exemple d’activateur d’un site Web HttpService https= ...; String WEBROOT = "/webroot"; // embedded ressources in BUNDLE-CLASSPATH jarfiles String WEBROOT_ALIAS = "/moi"; String SERVLET_ALIAS = WEBROOT_ALIAS +"/livreor"; Servlet servlet=new LivreOrServlet(param1,param2); https.registerServlet(SERVLET_ALIAS, servlet, null, servlet); HttpContext docsContext = new HttpContext() { public String getMimeType(String name) { return (name.endWith("htm"))?"text/html":null; } public boolean handleSecurity(HttpServletRequest req,HttpServletResponse resp) { return true; } public URL getResource(String name) { URL u = this.getClass().getResource(name); System.out.println(this.getClass().getName()); return u; } https.registerResources(WEBROOT_ALIAS, WEBROOT, docsContext ); © 2001-2010, D. Donsez OSGi 161 Exemple d’architecture pour un shell GET https://gw123.acme.com/admin Apache Apache CXF CXF DOSGi DOSGi Script Script Runner Runner gw123 Web Console 3 UserAdmin 4 ShellService Http Http Service Service port tcp 443 Web Web Console Console POST https://gw123.acme.com/ws/script Shell Shell MBean MBean Impl Impl Serial Serial Console Console SSHD SSHD Console Console © 2001-2010, D. Donsez Shell Shell Service Service Web Command Command Command Command OBR OBR Shell Shell Command Command start stop start stop 5 SerialConsole start stop 6 HttpService start stop 7 SoapService start stop 8 HttpAdmin start stop 9 OBRCommand start stop 10 SSHDConsole start stop Console Server POST /ws/script HTTP/1.1 ... <SOAP-ENV:Envelope xmlns:SOAP-ENV=...> <SOAP-ENV:Body> <s:ExecuteScript xmlns:s=...> <s:script SOAP-ENC:arrayType="s:cmd[3]"> <cmd>obr install "SensorLoggerWebSvc"</cmd> <cmd>install http://bundleserver/dep.jar</cmd> <cmd>install http://bundleserver/main.jar</cmd> </s:script> </s:ExecuteScript> </SOAP-ENV:Body> </SOAP-ENV:Envelope> OSGi framework OSGi 162 org.osgi.service.http Limitations ■ Motivations ◆ org.osgi.service.http is limited to javax.servlet v2.1, portlet JSR 168 ❖ No filter chain, no listener, basic JSP, no TagLib, … ◆ Hidden org.osgi.service.http stuff to JEE developers ◆ Keep compatibility with standard webapps (.war) ■ Propositions ◆ Cocoon ◆ Wicket ◆ DysoWeb ❖ Dynamic sub-webapps ❖ Embed Felix in a War : subwebapps are deployed by Felix FW ❖ Tested with Tomcat ◆ HttpRegistry ❖ Server-side equinox http://www.eclipse.org/equinox/server/ ❖ Embed Equinox in a War ❖ Use extender model ❖ Convertor GWT (Google Web Toolkit) module 2 OSGi bundles ❖ Tested with Tomcat and Jetty © 2001-2010, D. Donsez OSGi 163 Un autre exemple: l’architecture de Cocoon 3.0 avec l'aimable autorisation de Sylvain Wallez http://cocoon.zones.apache.org/daisy/cocoon3/g2/1151.html Architecture automatisée avec le SCR Whiteboard pattern © 2001-2010, D. Donsez OSGi 164 Un autre exemple: Wicket OSGi ■ Wicket ◆ « Wicket is a Java web application framework that takes simplicity, separation of concerns and ease of development to a whole new level. Wicket pages can be mocked up, previewed and later revised using standard WYSIWYG HTML design tools. Dynamic content processing and form handling is all handled in Java code using a first-class component model backed by POJO data beans that can easily be persisted using your favourite technology » from JavaGeek.org ■ Wicket sur OSGi ◆ TODO ◆ Utilise le SCR ◆ http://www.wicket-wiki.org.uk/wiki/index.php/OSGi © 2001-2010, D. Donsez OSGi 165 LogService ■ Motivation ◆ journaliser des traces/événements ◆ se mettre en l’écoute de ces traces/événements ◆ Rappel: java.util.logging.Logger est statique ■ Architecture AA CC oos.log.LogService Log Log Service Service Impl Impl Console LogListener Impl oos.log.LogReaderService BB DD Mail LogListener Impl © 2001-2010, D. Donsez OSGi 166 Exemple de LogListener public class ConsoleLogListenerActivator implements BundleActivator { LogReaderService logReaderService; LogListener loglistener; public void start(BundleContext cxt) { logReaderService= ...; loglistener=new ConsoleLogListenerImpl(); logReaderService.addLogListener(loglistener); } public void stop(BundleContext cxt) { logReaderService.removeLogListener(loglistener); }} class ConsoleLogListenerImpl implements org.osgi.service.log.LogListener { public final void logged(LogEntry entry) { System.out.println("Level:"+entry.getLevel()); if( entry.getBundle() != null) { System.out.println("bundle : "+ entry.getBundle().getBundleId()+" "); } System.out.println(entry.getMessage()); }} © 2001-2010, D. Donsez OSGi 167 Device Manager ■ Motivations ◆ Faire apparaître les drivers des périphériques matériels comme des Services OGSi ◆ Charger les drivers grâce aux bundles ◆ Mise à jour des drivers ◆ Un driver fournit plusieurs services plus ou moins raffinés ◆ Plug-and-Play ❖ Le branchement d’un périphérique provoque l’enregistrement d’un service. ❖ Le retrait du périphérique provoque le désenregistrement du service ■ Plusieurs éléments ◆ DeviceService ◆ Driver ◆ DriverLocator ◆ DeviceManager © 2001-2010, D. Donsez OSGi 168 Device Manager ■ Moteur de raffinement des devices ◆ A l’arrivée d’un nouveau service Device, le Device Manager cherche à enregistrer d’autres Device de plus haut niveau. ■ Services utilisés ◆ DriverLocator ◆ Driver © 2001-2010, D. Donsez OSGi 169 Device Manager ■ Device ◆ Gestion d’un périphérique Port Série Port USB Port Parallèle Récepteur GPS Modem Hayes AT Lecteur Carte à Puce Lecteur Amovible Imprimante Récepteur GPS NMEA Modem GPS/SMS Carte à Puce Lecteur ZIP Imprimante HP Modem de Porte Monnaie marque Erikia Electronique © 2001-2010, D. Donsez OSGi 170 Device Manager ■ DriverLocator ◆ Permet d’associer un Device à Driver ❖ Device Id ❖ Id URL ◆ Utiliser par le DeviceManager ◆ Exemple d’implementation ❖ Filtre LDAP Id URL ■ Driver ◆ Vérifie si l’on peut installer le Device associé à ce Driver ◆ Retourne une valeur d’évaluation de l’association ❖ Non possible si =0 ◆ Implémente une méthode d’attachement. © 2001-2010, D. Donsez OSGi 171 Event Admin Service (i) r4 ■ Offre un modèle de communication événementiel entre les bundles. ■ Objet Event = topic + propriétés. ■ Médiateur de Publication-souscription d’événement ◆ ◆ ◆ ◆ L'éditeur poste un événement au service EventAdmin L’EventAdmin le diffuse en parallèle à tous les souscripteurs du topic. Chaque souscripteur enregistre un service EventHandler. L'éditeur peut être synchronisé (ou non) sur la terminaison des exécutions // de tous les services EventHandler concernés. ■ Remarque ◆ Evénements spéciaux liées ❖ au cycles de vie des Services, bundles et framework ❖ au LogService, UPnP Base Driver, … ◆ Le service EventAdmin gère une liste noire des EventHandler défectieux ou consommant trop de CPU. © 2001-2010, D. Donsez OSGi 172 Event Admin Service (ii) Publisher Publisher topic="bar" topic="bar" Subscriber Subscriber event.topics event.topics== {"foo"} {"foo"} E6 Publisher Publisher topic="tic" topic="tic" org.osgi.service .event.EventHandler org.osgi.service .log.LogListener Subscriber Subscriber E4 E1 E2 Publisher Publisher topic="tac" topic="tac" org.osgi.service .event.EventAdmin Event Event Admin Admin Impl Impl E4 E2 E1 E4 E3 E1 event.topics event.topics== {"tic","tac"} {"tic","tac"} Subscriber Subscriber event.topics event.topics== {"tic","tic/tac/*"} {"tic","tic/tac/*"} E5 E3 Publisher Publisher topic= topic= "tic/tac/toe" "tic/tac/toe" © 2001-2010, D. Donsez Subscriber Subscriber event.topics event.topics== event.filter= (bundle.symbolicName=com.acme.*) OSGi {"org.osgi.framework.*"} {"org.osgi.framework.*"} 173 Bridging Event Admin Service and MOM PubSub inter-gateways PubSub OSGi gateway J2EE Server Publisher Publisher topic="bar" topic="bar" Subscriber Subscriber org.osgi.service .log.LogListener E6 event.topics event.topics== E7 {"foo"} {"foo"} org.osgi.service .event.EventHandler Publisher Publisher topic="tic" topic="tic" E4 E1 E2 Publisher Publisher topic="tac" topic="tac" org.osgi.service .event.EventAdmin Event Event Admin Admin Impl Impl E4 E2 E1 event.topics event.topics== {"tic","tac"} {"tic","tac"} E4 E3 E7 E6 EA-MOM EA-MOM Bridge Bridge E3 Subscriber Subscriber E1 E5 Subscriber Subscriber event.topics event.topics== {"tic","tic/tac/*"} {"tic","tic/tac/*"} TopicSubscriber Subscriber Subscriber TopicPublisher Publisher Publisher topic= topic= "tic/tac/toe" "tic/tac/toe" event.topics event.topics== M7 M6 {"org.osgi.framework.*"} {"org.osgi.framework.*"} MOM XMPP …) © 2001-2010, D. Donsez (JMS, JXTA, Ivy, Siena, ECF,WS-Eventing, OSGi event.filter= 174 (bundle.symbolicName=com.acme.*) Wire Admin Service R3 Producer P ■ Motivation Consumer Wire Object C ◆ Patron (design pattern) de services producteurs-consommateurs de données ❖ Données : mesure physique, position, état (discret), … ■ Domaine d’application ◆ Services basés Capteurs ◆ Machine-to-Machine (M2M) ■ WireAdmin ◆ Médiateur reliant 0..N producteurs à 0..M consommateurs ❖ Administrable globalement ▲ WireAdmin, WireAdminListener ◆ Contrôle de la « comptabilité » et adaptation de types de données échangées au travers du Wire ❖ Flavors © 2001-2010, D. Donsez OSGi 175 R3 © 2001-2010, D. Donsez Wire Admin Service Patron Producer-Wire-Consumer OSGi D’après la release3 d’OSGi176 Wire Admin Service Exemple d’application M2M (i) ■ Consultation de mesures via le Web ■ position GPS + température HTTP org.osgi.service.http.HttpService Producer Producer List List Http Http Service Service javax.servlet Servlet Servlet org.osgi.service.wireadmin.Producer OneWire OneWire Producer Producer org.osgi.util.measurement Service fourni COM2 javax.comm Service requis GPS GPS Position Position Producer Producer Package exporté Package importé © 2001-2010, D. Donsez org.osgi.util.measurement org.osgi.util.position OSGi COM1 javax. javax. comm comm GPS 177 Wire Admin Service Exemple d’application ■ Aide à la navigation GPS GPS Receiver Receiver Inertial Inertial Sensor Sensor flavors=Position Position Position Correlator Correlator flavors=DiffPos flavors=Position, DiffPos flavors= Position flavors= Position C C PP C C PP PP POI POI Locator Locator WireAdmin WireAdmin Binder Binder WADL WireAdmin P C Wire flavors=Position P C P Wire C Wire flavors=DiffPos flavors=Position Wire Wire Admin Admin Service Service Impl Impl © 2001-2010, D. Donsez OSGi 178 UPnP Driver Service R3 ■ UPnP (Universal Plug and Play) ◆ Protocoles de découverte d’équipements (SOHO) et d’utilisation leur services ❖ Basé sur SOAP/HTTP (TCP, UDP, UDP Multicast) ◆ Alternative à JINI ◆ Largement répandu et soutenu par les équipementiers SOHO ■ Motivations du service UPnP Driver Service ◆ Spécifie comment des bundles OSGi doivent être développés pour interoperer avec ◆ des equipéments UPnP (devices) ◆ des points de contrôle UPnP (control points) ◆ en respectant la spécification UPnP Device Architecture © 2001-2010, D. Donsez OSGi 186 UPnP Driver Service Les interfaces représentant les équipements UPnP UPnPEventListener UPnPDevice UPnPIcon icons mimetype, w,h,d * * parent 0..1 child * UPnPService 1..n 0..n UPnPStateVariable UPnPAction type sendEvent invoke 1 Parameter direction : in, out * © 2001-2010, D. Donsez OSGi 187 Exemple: un device Téléviseur et son point de contrôle ■ 3 services ◆ urn:schemas-upnp-org:service:SwitchPower:1 ◆ urn:schemas-adele-imag-fr:service:ChannelSelector:1 ◆ urn:schemas-adele-imag-fr:service:VolumeSelector:1 © 2001-2010, D. Donsez OSGi 188 UPnP Device Driver : Mise en œuvre Collocalisé UI My My UPnP UPnP Control Control Point Point UI upnp.UPnPDevice My My UPnP UPnP Device Device upnp.UPnPEvtListner Passerelle OSGi © 2001-2010, D. Donsez OSGi 190 UPnP Device Driver : Mise en œuvre Point de contrôle UI My My UPnP UPnP Control Control Point Point LAN UPnP UPnP Base Base Driver Driver upnp.UPnPDevice HTML/HTTP SOAP/HTTP upnp.UPnPEvtListner SSDP/IPMultiCast 239.255.255.250:1900 UPnP Device (TV monitor) Passerelle OSGi © 2001-2010, D. Donsez OSGi 191 UPnP Device Driver : Mise en œuvre Equipement LAN upnp.UPnPDevice My My UPnP UPnP Device Device UPnP.export= upnp.UPnPEvtListner HTML/HTTP UPnP UPnP Base Base Driver Driver SOAP/HTTPc SSDP/IPMultiCast UPnP Control Point 239.255.255.250:1900 Passerelle OSGi © 2001-2010, D. Donsez OSGi 192 UPnP Device Driver : Mise en œuvre Passerelle micro-monde LAN upnp.UPnPDevice UPnP UPnP X10 X10 Bridge Bridge UPnP.export= upnp.UPnPEvtListner HTML/HTTP UPnP UPnP Base Base Driver Driver SOAP/HTTPc SSDP/IPMultiCast UPnP Control Point 239.255.255.250:1900 Passerelle OSGi © 2001-2010, D. Donsez OSGi 193 Under Construction En Construction R3 Execution Environment Specification ■ Définit l’environnement d’exécution (java) requis pour que le bundle puisse s’exécuter ◆ Il doit être inclut dans la liste des environnements du framework d’accueil pour passer dans l’état RESOLVED ◆ Propriété org.osgi.framework.executionenvironment ■ Entête de Manifest Bundle-RequiredExecutionEnvironment: \ CDC-1.0/Foundation-1.0, OSGi/Minimum-1.0 © 2001-2010, D. Donsez OSGi 194 Under Construction En Construction 114 Deployment Admin Specification © 2001-2010, D. Donsez OSGi 195 Under Construction En Construction 115 Auto Configuration Specification © 2001-2010, D. Donsez OSGi 196 Under Construction En Construction 116 Application Admin Service Specification ■ org.osgi.service.application © 2001-2010, D. Donsez OSGi 197 Under Construction En Construction 117 DMT Admin Service Specification ■ org.osgi.service.dmt © 2001-2010, D. Donsez OSGi 198 119 Monitor Admin Service Specification org.osgi.service.monitor ■ Under Construction En Construction Motivation ◆ Découvrir des variables d'état et publier/écouter leurs modifications ◆ ❖ mémoire disponible, niveau d'énergie de la batterie, nombre de SMS envoyés … S’appuie sur l’Event Admin © 2001-2010, D. Donsez OSGi 199 Under Construction En Construction 119 Monitor Admin Service Specification ■ Notification sur changement © 2001-2010, D. Donsez OSGi 200 Under Construction En Construction 119 Monitor Admin Service Specification ■ Time and Event monitoring job © 2001-2010, D. Donsez OSGi 201 End-to-End Management ■ Motivations ◆ Gestion d’un (très grand) parc de plateformes OSGi ◆ Et des équipements qui y sont attachés ■ Besoins ◆ Déploiement « transactionnel » ◆ Politiques de déploiement ◆ … ■ E2E Remote Management EG ◆ Expert group a l’Alliance ■ Produits ◆ Prosyst Remote Manager ◆ IBM Expeditor Framework ◆ … © 2001-2010, D. Donsez OSGi 202 Enterprise-side OSGi ■ Eric Newcomer « OSGi could not be a YAJEEC » ■ Deal with legacy technologies ◆ CORBA, MQ, CICS, Tuxedo, JEE, .NET ■ Combining with the next technologies ◆ Spring-OSGi, SCA-OSGi © 2001-2010, D. Donsez OSGi 203 Distributed OSGi ■ Motivations ◆ Expose/Provides local services to remote consumers ◆ Consumes remote services ■ Proposition ◆ ◆ ◆ ◆ Extended Service Binder R-OSGi SCA/OSGi RFC 119 ❖ http://www.osgi.org/download/osgi-4.2-early-draft3.pdf). ■ RFC 119 ◆ http://cxf.apache.org/distributed-osgi.html ❖ provides the Reference Implementation of the Distribution Software (DSW) component of the Distributed OSGi Specification ❖ using Web Services, leveraging SOAP over HTTP and exposing the Web Service over a WSDL contract. © 2001-2010, D. Donsez OSGi 204 OSGi and JMX ■ MOSGi ■ RFC 139 © 2001-2010, D. Donsez OSGi 205 Conclusion intermédiaire + Gestion des dépendances de package + Déploiement dynamique de bundles + Environnement varié: embarqué, station de travail, serveur. + Fonctionnalité de base pour le déploiement de composants - Programmation complexe des connexions entre services à la charge du développeur - Centralisé mais pas mal de travaux sur la distribution © 2001-2010, D. Donsez OSGi 206 & © 2001-2010, D. Donsez OSGi 207 What is OSGi Alliance ? ■ Consortium founded in March 1999 ■ Objective ◆ Create open specifications for delivering administrated services through a network ■ Define ◆ A common platform (framework) ❖ Services deployment ❖ Services execution and administration ◆ A set of based services: ❖ Log Service, Http Service… ◆ A device access specification ◆ A deployment unit, a bundle © 2001-2010, D. Donsez OSGi 209 OSGi Main Concepts ■ Framework: ◆ Bundles execution environment ❖ Oscar (Objectweb) / Felix (Apache), Knopperfish, Equinox, SMF, ProSyst, Siemens VDO, … ◆ Event notification ■ Bundles: ◆ Services diffusion and deployment unit ■ Services: ◆ Java Object implementing a well define contract © 2001-2010, D. Donsez OSGi 210 Middleware and Application Packaging ■ Modularize the middleware/application ◆ ◆ ◆ ◆ Distribute the different middleware services Better component visibility Need of a deployment container Partial update without restart all ■ Implementation ◆ Based on Jarfile and Manifest entries ◆ Explicit Package dependencies and Versioning (range) ■ Ready for probably next generation standard ◆ JSR 277, Java Module System ◆ Overtake JNLP(JSR-56), J2EE EAR, OSGi R3 bundle ◆ Java Platform 7.0 (2010), Jigsaw © 2001-2010, D. Donsez OSGi 211 The OSGi « stack » avec l'aimable autorisation de Peter Kriens .jar, .jxe, .dex Bundle Bundle Bundle JVM CDC & Server RT-JVM, MVM Dalvik, … .dll, .so Native JNI Windows, Linux, Solaris, RTOS … iPhone & Android services Framework Java VM (J2ME/CDC, J2SE) Operating System Driver Driver Driver Driver Hardware © 2001-2010, D. Donsez OSGi 213 OSGi Acteurs, concurrences, tendances et perspectives L’ OSGi™ Alliance ■ actuellement 44+ membres ■ de plusieurs domaines industriels Service Providers Auto Makers Device Manufacturers ISV IT Providers Developers Solution Providers Others ■ sur les 4 segments AUTO © 2001-2010, D. Donsez OFFICE OSGi HOME MOBILE 215 L’OSGi™ Alliance = Alpine Electronics Europe Gmbh , Aplix Corporation , Belgacom , BMW Group , Cablevision Systems , Computer Associates , Deutsche Telekom AG , Echelon Corporation , Electricité de France (EDF) , Ericsson Mobile Platforms AB , Esmertec , Espial Group, Inc. , ETRI Electronics and Telecommunications Research Institute , France Telecom , Gatespace Telematics AB , Gemplus , Harman/Becker Automotive Systems GmbH , IBM Corporation , Industrial Technology Research Institute , Insignia Solutions , Intel Corporation , KDDI R&D Laboratories, Inc. , KT Corporation , Mitsubishi Electric Corporation , Motorola, Inc. , NEC Corporation , Nokia Corporation , NTT , Oracle Corporation , Panasonic Technologies, Inc. , Philips Consumer Electronics , ProSyst Software GmbH , Robert Bosch GmbH , Samsung Electronics Co., Ltd. , SavaJe Technologies, Inc. , Sharp Corporation , Siemens AG , Sun Microsystems, Inc. , Telcordia Technologies, Inc. , Telefonica I+D , TeliaSonera , Toshiba Corporation , Vodafone Group Services Limited © 2001-2010, D. Donsez OSGi 216 Les acteurs incontournables ■ IBM ◆ OSGi est au cœur de la stratégie d’IBM ◆ placé sur la partie « edge » du système IT ❖ poste de travail (RCP) ❖ serveur enfoui ◆ Remarque: ❖ Eclipse 3.0 (donc WebSphere Studio) est désormais développé au dessus d’OSGi (Equinox) ■ Nokia ◆ Pousse pour « Java (MIDLet) dans toutes les poches » (2002) « Java Server dans toutes les poches » (2005) © 2001-2010, D. Donsez OSGi 217 Produits ■ SUN Java Embedded Server (JES) l’étincelle ■ Echelon LonWorks Bundle Deployment Kit ■ Ericsson - Residential e-services ■ MakeWave (ex Gatespace AB) ■ IBM SMF ■ Insignia ■ Nano Computer System ■ ProSyst Software mBedded Server ■ Wind River ■ Siemens VDO TLA (R3) ■ … © 2001-2010, D. Donsez OSGi 218 La communauté open-source ■ Plusieurs implémentations et communautés ◆ ObjectWeb Oscar r3 ◆ Knopflerfish ◆ Eclipse Equinox (donation IBM SMF) ◆ Apache Felix (suite d’ ObjectWeb Oscar) r4 ◆ JBoss microContainer (http://labs.jboss.com/jbossmc) ■ Abaissement des barrières de l'OSGi pour le développement open-source. ◆ Dépôts de bundles (org.osgi.service.obr) © 2001-2010, D. Donsez OSGi 219 Oscar/Felix ■ Console texte sur Nokia 770 (JamVM) © 2001-2010, D. Donsez Merci à Corentin Baron OSGi 220 Apache Felix on Google Android ■ Google Android / Dalvik VM ◆ JVM and JRE for the Google phone ❖ Support Java 5 features (annotations, …) ❖ Uses a proprietary class format (.dex instead of .class/.jar) ■ Felix running on Android ◆ See ❖ © 2001-2010, D. Donsez http://felix.apache.org/site/apache-felix-and-google-android.html OSGi 221 Knopflerfish OSGi ■ La console GUI © 2001-2010, D. Donsez OSGi 222 Knopflerfish OSGi ■ La console GUI sur le iPod Touch/iPhone 2.1 (JamVM) D’après http://knopflerfish.blogspot.com/2008/09/knopflerfish-osgi-on-ipod-touchiphone.html © 2001-2010, D. Donsez OSGi 223 Eclipse/OSGi ■ embarqué dans Eclipse/IDE et Eclipse/RCP ◆ Pour le conditionnement et le déploiement des Plugins ◆ Reconditionné par Prosyst ■ La console texte ◆ java -jar %ECLIPSE_HOME%\plugins\org.eclipse.osgi_3.1.0.jar -console © 2001-2010, D. Donsez OSGi 224 Concierge http://concierge.sourceforge.net/ ■ OSGi R3 implementation optimized for constrainted mobile devices (iMote2, BUG (http://buglabs.net/products), NSLU, WRT54, …) http://www.spectrum.ieee.org/video?id=223 ■ Jan S. Rellermeyer, Gustavo Alonso: Concierge: A Service Platform for ResourceConstrained Devices. In: Proceedings of the 2007 ACM EuroSys Conference, Lisbon, Portugal, 2007. © 2001-2010, D. Donsez OSGi 225 OSGi™ TCK ■ Suite de tests pour vérifier la compatibilité ◆ Du framework (Core) ◆ Des services standards (Compendium) ◆ Les tests concernent les fonctionnalités obligatoires et optionnelles (fragments, …) ■ Remarque ◆ Seulement accessible aux membres de l’OSGi Alliance ◆ En principe accessible à quelques plateformes open sources (Felix, Equinox, …) © 2001-2010, D. Donsez OSGi 226 JSR 277 : Java™ Module System ■ defines a distribution format and a repository for collections of Java code and related resources. ■ also defines the discovery, loading, and integrity mechanisms at runtime. © 2001-2010, D. Donsez OSGi 227 JSR 294 : Improved Modularity Support in the Java™ Programming Language ■ Notion of super-package ◆ indroduce in Java ◆ in order to define class visibility outside the deployment unit (JSR277) ■ Example superpackage A { member package P; member superpackage B; } superpackage B member A { member superpackage C; export superpackage C; } superpackage C member B { member package Q; export Q.X; } © 2001-2010, D. Donsez A type P.Y can access type Q.X because Q.X is exported from superpackage C, and C is exported from B, so Q.X is available in A OSGi 228 Jigsaw ■ JDK7 is big ◆ http://blogs.sun.com/mr/entry/jigsaw © 2001-2010, D. Donsez OSGi 229 HK2 « Hundred Kilobytes Kernel » https://hk2.dev.java.net ■ Small kernel (than OSGi™) to build modular softwares ◆ consist of two technologies : ■ Modules subsystem ◆ offer a better level of isolation between parts of the application. ◆ path to the implementation of modules (JSR 277 & 294) in Java SE 7. ■ Component Model ◆ define components which can also be seen as Services. ❖ These components can be automatically and dynamically discovered by the runtime and can use innovative technologies such as Inversion of Control or injection of dependencies as well as automatic resolution of dependencies. ❖ Annotations (org.jvnet.hk2.annotations) ▲ @Contract, @Service, @Inject, @Extract, @Factory, @Scoped, @ContractProvided, … ■ Remark: foundation for the GlassFish V3 application server ◆ April 2008 : GlassFish v3 on Apache Felix !!! © 2001-2010, D. Donsez OSGi 230 JavaCard 3.0 Connected Edition Shareable Interface Object-based Services (SIO) ■ Service-oriented communications between on-card applications ■ Limitations ◆ ◆ ◆ ◆ Service classes must inherit of the interface javacard.framework.Shareable Service lookup is only based on service URI (e.g. sio:///transit/pos/ticketbook) javacardx.facilities.ServiceRegistry lookup methods return Shareable objects No event (javacardx.facilities.StandardEvent) for SIO registration/unregistration © 2001-2010, D. Donsez OSGi 231 La concurrence : embedded-Linux ■ Plate-forme ◆ Multi-application ◆ Multi-fournisseurs (users) ◆ Éventuellement Temps Réel ■ Solutions ◆ Chargement / Déchargement dynamique de .so ◆ Processus (comme sandbox) ■ Avantages ◆ Très très répandu … ■ Inconvénient ◆ Coûts des échanges (IPC,socket) entre les « services » © 2001-2010, D. Donsez OSGi 232 La concurrence : JUNGO OpenRG http://www.jungo.com/openrg/index.html ■ TODO ■ “OpenRG™ is a complete and integrated gateway software platform for developing network devices in the digital home and small office including triple play residential gateways, home/SOHO routers, home gateways, wireless access points, cable/DSL routers and voice gateways.” © 2001-2010, D. Donsez OSGi 233 La concurrence : MicroSoft .NET ■ Passerelles industrielles (SCADA) OPC http://en.wikipedia.org/wiki/OPC_Foundation ◆ COM/DCOM : drivers field bus, domaine SCADA ■ .NET ◆ Alternative à Java (et à machine virtuelle) ◆ Mais des approches similaires ❖ bytecode, JIT, chargeur de classes, … ◆ et différentes ❖ Multi-langage, cache de compilation, domaine d’application, … ◆ Compact.NET alternative à J2ME/CDC ◆ Très récente annonce de .NET Micro Framework (CLR sur bare metal) ■ Le problème de .NET (1 et 2) ◆ Le déchargement d’une classe requière l’arrêt du domaine d’application (AppDomain). ◆ Donc pas de mise à jour partielle d’une application ■ Des compromis (non gratuits) restent possibles ◆ [Escoffier06] ◆ SOF - An OSGI-like modularization framework for C++ ❖ http://sof.tiddlyspot.com/ http://www.codeproject.com/KB/library/SOF_.aspx © 2001-2010, D. Donsez OSGi 234 La concurrence : MicroSoft .NET ■ Managed AddIn Framework (MAF, aka System.AddIn) ◆ Based on Application Domains ◆ ◆ ◆ ◆ Discovery – Find addins at runtime Activation – Load an addin at runtime Versioning - Backwards/forward compatibility Isolation – Load addins into separate AppDomains/Processes so they cannot crash they whole app, among other reasons ◆ Lifetime Management – MAF will handle addin lifetimes and memory/AppDomain management ◆ Sandboxing – Load addins with specific permission sets like “Internet” ◆ Unloading – Unload an addin without worrying about tedious AppDomain management ■ Other: Managed Extensibility Framework (MEF) in .NET 4 © 2001-2010, D. Donsez OSGi 235 La tendance ■ OSGi couvre désormais un spectre étendu de domaine ◆ Passerelle résidentiel ◆ Passerelle véhiculaire ◆ Passerelle industrielle ◆ Téléphonie mobile ◆ Application sur poste de travail (Eclipse RCP) ◆ Enterprise Side OSGi ❖ Serveur IT (J2EE, …), Web Framework, … ❖ JOnAS 5, Geronimo, ApacheDS, JAMES, WebSphere, JBoss 5 … ❖ ECP ◆ Enterprise Expert Group à l’OSGi Alliance ❖ Workshop sur OSGi + J2EE (11/09/2006, San José) ◆ End-to-End Remote Management Expert Group à l’OSGi Alliance © 2001-2010, D. Donsez OSGi 236 708 million+ mobile Java devices installed base 635+ mobile Java device models on the market 32 mobile device vendors using Java 140+ operators with deployed Java services 45,000+ mobile Java applications on the market © 2001-2010, D. Donsez OSGi Jon Bostrom, Nokia at OWC05 237 Les perspectives ■ Open-source ◆ Match entre Eclipse et Apache ? ■ JSR 277 Java™ Module System ◆ ~ couvert par OSGi R4 Module Layer ◆ Prévu pour Java Platform 7.0 ■ JSR 291 Dynamic Component Support for Java™ SE ◆ ~ couvert par OSGi R4 SCR ◆ Prévu pour Java Platform 7.0 ■ JSR 313 Java Platform, Enterprise Edition 6 ◆ Extensibilité du serveur JEE ■ Quel nouveau rôle pour OSGi™ ? ◆ Définition de Services Standards ■ Quand même « une crainte pour les « penseurs » de l’Alliance » ◆ Faire venir les développeurs de logiciels embarqués à J2ME et à OSGi … © 2001-2010, D. Donsez OSGi 238 Pour terminer Un peu de publicité ■ OSGi™ Users’ Group France ◆ Association d’utilisateurs de la technologie OSGi™ ❖ Développeurs, consultants, enseignants, chercheurs, … ❖ de FT R&D, EDF R&D, Schneider Electric, Trialog, Siemens VDO, Gemplus, Alcatel, Bull, Thomson, Scalagent … ❖ et de l’INRIA, CNRS, … ◆ 7 réunions depuis Décembre 2004 ◆ Ecole d’été : 6-7 Septembre 2007 Ile de Bender (près de Vannes) ◆ Prochaine réunion le 29/1/2008 à Paris ■ En savoir plus ◆ http://france.osgiusers.org © 2001-2010, D. Donsez OSGi 239 Conclusion finale + Très fort potentiel Ne passez pas à coté © 2001-2010, D. Donsez OSGi 240 & © 2001-2010, D. Donsez OSGi 241 Bibliographie & Webographie ■ Spécification ◆ OSGi Alliance, « OSGi service gateway specification », http://www.osgi.org ■ Ouvrage ◆ Kirk Chen, Li Gong, « Programming Open Service Gateways with Java Embedded Server Technology », Pub. Addison Wesley, August 2001 ISBN#: 0201711028. 480 pages (un peu ancien) ◆ Niel Barlett, « OSGi in Practice », http://neilbartlett.name/blog/osgibook/ ◆ Richard S. Hall, Karl Pauls, and Stuart McCulloch, « OSGi in Action », May 2009, 375 pages, ISBN: 1933988916, http://code.google.com/p/osgi-in-action & http://manning.com/hall ■ Articles ◆ Li Gong, « A Software Architecture for Open Service Gateways », IEEE Internet Computing, January/February 2001 (Vol. 5, No. 1), pp. 64-70 ◆ Dave Marples, Peter Kriens, The Open Services Gateway Initiative, an Introductory Overview, IEEE Communications Magazine, December 2001 ◆ puis plein d’autres ■ Blogs ◆ Peter Kriens, l’évangeliste OSGi, http://www.osgi.org/blog/ ◆ Java modularity, JSR 277, JSR 291, JSR 294, OSGi, open source, and software design, http://underlap.blogspot.com/ © 2001-2010, D. Donsez OSGi 242 Bibliographie & Webographie ■ Framework open source ◆ Oscar, Felix, Equinox, Knopperfish ■ Index de bundles ◆ http://bundles.osgi.org/browse.php ■ Exhibitions ◆ http://www.osgiworldcongress.com/ ■ Complément de cours ◆ Donsez, Hall, Cervantes http://wwwadele.imag.fr/users/Didier.Donsez/cours/osgi.pdf ◆ Frénot http://citi.insa-lyon.fr/~sfrenot/cours/OSGi/ ◆ INTech http://rev.inrialpes.fr/intech/Registration?op=511&meeting=27 ■ Plus ◆ http://france.osgiusers.org/Main/Documentation © 2001-2010, D. Donsez OSGi 243 Travaux pratiques ■ Sujet ◆ http://www-adele.imag.fr/users/Didier.Donsez/cours/exemplesosgi/tutorialosgi.htm ■ Programme ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ Installation d’Apache Felix Premières commandes via différentes consoles Déploiement de bundles Développement d’un servant Développement d’un composant SCR ❖ Utilisant Event Admin Service ❖ Utilisant Wire Admin Service ❖ Utilisant Http Service Service Démonstration de l’UPnP Base Driver Démonstration d’administration de passerelle sur NSLU2 Démonstration d’administration de passerelle avec JMX Mini-projet (1 bundle utilisant d’autres) http://www.nslu2-linux.org © 2001-2010, D. Donsez OSGi 244 Mini-Projet : http.script ■ Servlet exécutant une séquence de commandes auprès du service org.apache.felix.shell.ShellService ■ Réutiliser le code de ◆ http.webadmin ◆ shell.scriptcmd ■ Utiliser le SCR pour la gestion des liaisons et du cycle de vie © 2001-2010, D. Donsez OSGi 245 OSGi Les Outils Les outils ■ Eclipse PDE ■ Maven Bundle Plugin ■ Bindex ■ BND ■ Console OSGi ■ Unit testing © 2001-2010, D. Donsez OSGi 247 Eclipse PDE ■ Since Eclipse 3, Plugins are OSGi bundles ■ TODO © 2001-2010, D. Donsez OSGi 248 BND (http://www.aqute.biz/Code/Bnd) ■ Tool to create and diagnose OSGi R4 bundles ◆ Available as Command line tool, Eclipse plugin, Ant Task, Maven Plugin ■ Key functions ◆ ◆ ◆ ◆ ◆ Compute the classpath, import, export of a bundle Show the manifest and JAR contents of a bundle Wrap a JAR so that it becomes a bundle Create a Bundle from a specification and a class path Verify the validity of the manifest entries ■ Examples ◆ wrap an existing jar ❖ java -jar bnd.jar wrap -output myOSGi.jar myPlain.jar ◆ More TODO © 2001-2010, D. Donsez OSGi 249 BND – Directives ■ Export-Package LIST of PATTERN ◆ ■ Include-Resource LIST of iclause ◆ ■ The Bundle-ManifestVersion is always set to 2, there is no way to override this. Bundle-Version VERSION ◆ ■ If the Bundle-Name is not set, it will default to the Bundle-SymbolicName. BundleManifestVersion 2 ◆ ■ The default is the name of the main bnd file, or if the main bnd file is called bnd.bnd, it will be the name of the directory of the bnd file. An interesting variable is ${project} that will be set to this default name. Bundle-Name ◆ ■ ■ Works as private package but will only include the packages when they are imported. When this header is used, bnd will recursively add packages that match the patterns until there are no more additions. BundleSymbolicName ◆ ■ lists the packages that are required by the contained packages. See Import Package. Conditional-Package LIST of PATTERN experimental ◆ ■ ■ lists the packages that the bundle should contain but not export. See Private Package. Import-Package LIST of PATTERN ◆ ■ makes it possible to include arbitrary resources; it contains a list of resource paths. See Include Resource. Private-Package LIST of PATTERN ◆ ■ lists the packages that the bundle should export, and thus contain. The version of the bundle. If no such header is provided, a version of 0 will be set. Service-Component LIST of component © 2001-2010, D. Donsez OSGi 250 BND - Directives ■ Operator @ d’expansion de jarfiles <Include-Resource> @hexdump.jar, @libdbus-java.jar, @unix-*.jar, lib/libunix-java.so </Include-Resource> <Bundle-NativeCode> libunix-java.so;osname=Linux;processor=x86 </Bundle-NativeCode> © 2001-2010, D. Donsez OSGi 251 BND - Directives ■ Macro findpath ◆ ${findpath;regexp[;replacement]} ◆ traverse the files in the built jar file and check each path with the regular expression. If the regexp matches it puts it on a comma separated list. This allows us to construct the Bundle-Classpath from the lib directory in the built jar. <Include-Resource>lib=lib</Include-Resource> <Bundle-Classpath>${findpath;lib/.*.jar}</Bundle-Classpath> <Bundle-NativeCode> libunix-java.so;osname=Linux;processor=x86 </Bundle-NativeCode> © 2001-2010, D. Donsez OSGi 252 Maven ■ Tool for building and managing Java projects ◆ Standard organization of the project directory ◆ Repositories (local and remote, public and private) of artifacts ◆ Project description in pom.xml (Project Object Model) ◆ Set of predefined commands ❖ mvn clean ❖ mvn install ❖ mvn eclipse:eclipse ■ Extensibles by plugins ◆ MOJO (in Java, Groovy, …) ◆ For OSGi, Maven Bundle Plugin (Apache Felix) © 2001-2010, D. Donsez OSGi 253 Maven Bundle Plugin (Apache Felix) http://felix.apache.org/site/maven-bundle-plugin-bnd.html ■ Maven plugin to package a Java (Maven) project as a OSGi R4 bundle ◆ Use BND tools (and directives) to calculate import/export packages ■ Example of pom.xml … <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Import-Package>*</Import-Package> <Private-Package>${pom.artifactId}.*</Private-Package> <Export-Package>${pom.artifactId};version=1.0.0</Export-Package> <Bundle-Activator>${pom.artifactId}.impl.Activator</Bundle-Activator> <Export-Service>org.osgi.service.event.EventHandler</Export-Service> <Import-Service>org.osgi.service.log.LogService</Import-Service> … © 2001-2010, D. Donsez OSGi 254 Maven Archetypes for OSGi projects ■ Maven archetype ◆ Parameterized template to start quickly a Maven-enabled project ■ Archetypes for OSGi bundle projects ◆ General purpose ❖ http://gforge.inria.fr/scm/?group_id=526 ◆ SpringOSGi ❖ spring-osgi-bundle-archetype ◆ … © 2001-2010, D. Donsez OSGi 255 Pax Construct http://wiki.ops4j.org/confluence/display/ops4j/Pax+Construct ■ Templates for kick-start bundle development ◆ Project / bundles / common techniques ■ Maven pax plugin ◆ Manages project builds file ❖ mvn clean install pax:provision ◆ + interactive scripts (Windows only) ■ Example pax-create-project -p com.example.client -n simple-test-client -a kxml2 -v 0.1.0-SNAPSHOT -Dunpack=true © 2001-2010, D. Donsez OSGi 256 Simple Pax use case a web based OSGi application ◆ pax-create-project -g simple.project -a osgi-web-app ◆ cd osgi-web-app ◆ ◆ ◆ ◆ ◆ ◆ pax-wrap-jar -g javax.servlet -a servlet-api -v 2.5 pax-import-bundle -g org.ops4j.pax.logging -a api -v 0.9.4 pax-import-bundle -g org.ops4j.pax.logging -a jcl -v 0.9.4 pax-import-bundle -g org.ops4j.pax.logging -a slf4j -v 0.9.4 pax-import-bundle -g org.ungoverned.osgi.bundle -a http -v 1.1.2 pax-create-bundle -p my.osgi.code.myBundle -n myBundle ◆ mvn install pax:provision © 2001-2010, D. Donsez OSGi 257 Pax Runner http://wiki.ops4j.org/confluence/display/ops4j/Pax+Runner ■ Provides URL handlers (mvn,wrap,classpath) to deploy bundles or simple jarfiles from a Maven 2 repository ■ Provides scripts to setup initial provisioning config files for Felix, Equinox and KF © 2001-2010, D. Donsez OSGi 259 BIndex (http://www2.osgi.org/Repository/BIndex) ■ Tool to create RFC-0112 Bundle Repository indexes from bundles manifests. ◆ Available as Command line tool and Ant Task ◆ Maven plugin : org.apache.felix:maven-obr-plugin ❖ Incremental index update ■ Options java -jar bindex.jar [-help] [-r repository.xml (.zip ext is ok)] [-l file:license.html ] [-t http://w.com/servl?s=%s&v=%v %s=symbolic-name %v=version %p=relative path %f=name] [-q (quiet) ] <jar file>* ■ the RFC 112 ◆ http://www2.osgi.org/div/rfc-0112_BundleRepository.pdf © 2001-2010, D. Donsez OSGi 260 ■ bushel http://code.google.com/p/bushel/ ◆ Bushel is a simple utility to convert ad-hoc OSGi bundles into a local Ivy repository. © 2001-2010, D. Donsez OSGi 261 Unit testing ■ Motivation ◆ ◆ ◆ ◆ Unit testing of individual bundles (w w/o services) Before packaging and deployment Running of a OSGi platform (without rebooting) Integration to builders (Maven Surefire) and continuous integration ■ Architectures ◆ Whiteboard pattern ◆ Extended pattern ■ Example ◆ JUnit4OSGi ❖ http://felix.apache.org/site/apache-felix-ipojo-junit4osgi.html ◆ Pax Exam ❖ http://wiki.ops4j.org/display/paxexam/Pax+Exam © 2001-2010, D. Donsez OSGi 262 OSGi and AOT compilation ■ Motivations ◆ “Accelerate”and protect OSGi applications ❖ On-the-fly code injection is harder ❖ Retrocompilation is harder ◆ Reduce the Runtime size ■ Tools ◆ Excelsior JET ❖ Testing with RCP bundles © 2001-2010, D. Donsez OSGi 263 OSGi graphical consoles ■ Felix mOSGi console ■ KF ■ Jasmine OSGi console ■ Prosyst mConsole ■ … © 2001-2010, D. Donsez OSGi 264 Jasmine OSGi Console (http://jasmine.objectweb.org/doc/console/jasmine.htm) ■ Eclipse RCP & Plugin to manage OSGi platforms ◆ connection to the platform with JMX, WS, UPnP, … ◆ OBR plugin © 2001-2010, D. Donsez OSGi 265 Apache Felix mOSGi console ■ TODO © 2001-2010, D. Donsez OSGi 266 Prosyst mConsole © 2001-2010, D. Donsez OSGi 267 Bonus Track JSR 294: Improved Modularity Support in the Java™Programming Language ■ D’aprés Rick Hall ■ Rational ◆ Java programming language needs better support for hierarchical, modular organization ❖ Primarily to support information hiding ❖ Java packages inadequate for this purpose ❖ Only two levels of visibility: internal to the package or public to everyone ■ Module “Files” super package com.foo.moduleA { // Exported classes/interfaces export com.foo.moduleA.api.*; export com.foo.moduleA.ifc.InterfaceC; // Imported modules import org.bar.moduleD; // Module membership com.foo.moduleA.api; com.foo.moduleA.ifc; org.apache.stuff; } © 2001-2010, D. Donsez OSGi 269 Application à développer Ecole d’été OSGi 2007 Application 1 Use ServiceTracker type=hotdog HotDog HotDog Seller Seller Customer Customer type=popcorn PopCorn PopCorn Seller Seller e.b.BuyService © 2001-2010, D. Donsez OSGi 271 Application 2 change property value type=hotdog stock=100 HotDog HotDog Seller Seller Customer Customer type=popcorn stock=10000 PopCorn PopCorn Seller Seller e.b.BuyService © 2001-2010, D. Donsez OSGi 272 Application 3 Use SCR type=hotdog stock=100 o.a.f.s .Command Customer Customer HotDog HotDog Seller Seller Buy Buy Command Command PopCorn PopCorn Seller Seller type=popcorn stock=10000 e.b.BuyService © 2001-2010, D. Donsez OSGi 273 Application 4 Use iPOJO type=bread stock=100 type=hotdog stock=100 HotDog HotDog Seller Seller Customer Customer o.a.f.s .Command Bread Bread Provider Provider Saussage Saussage Povider type=saussage Povider stock=50 Buy Buy Command Command type=popcorn stock=10000 PopCorn PopCorn Seller Seller e.b.BuyService © 2001-2010, D. Donsez OSGi 274 Application 5 Service Listener Consumer Consumer Servlet Servlet o.o.s.h .HttpService type=bread stock=100 type=hotdog stock=100 HotDog HotDog Seller Seller Customer Customer o.a.f.s .Command Bread Bread Provider Provider Saussage Saussage Povider type=saussage Povider stock=50 Buy Buy Command Command type=popcorn stock=10000 PopCorn PopCorn Seller Seller e.b.BuyService © 2001-2010, D. Donsez OSGi 275 RFC 138 Multiple Frameworks In One JVM ■ Motivations ■ Limitations ◆ Multiplex singleton factories (URLHandler, XML, …) ◆ Multiplex System properties ◆ Security Manager for Conditional Permission Admin ■ Solutions ◆ Several FW (several vendors) hosted in the same JVM © 2001-2010, D. Donsez OSGi 276 RFC 138 Multiple Frameworks In One JVM ■ Configurations Peer embedded frameworks © 2001-2010, D. Donsez Nested frameworks OSGi Parent-Chlid frameworks 277 RFC 138 ■ Surrogate Bundle ◆ Represents the parent in the child FW ■ Composite Bundle ◆ Represents a child in a parent FW © 2001-2010, D. Donsez OSGi 278 © 2001-2010, D. Donsez OSGi 279 ■ Lifecycle © 2001-2010, D. Donsez OSGi 280 Import / Export ■ Package import ■ Package export © 2001-2010, D. Donsez OSGi 281 Import / Export ■ Parent Service import ◆ Filtering CompositeServiceFilter-Import ■ Child Service export (to the parent) ◆ Filtering CompositeServiceFilter-Export ■ service.id (globally unique) © 2001-2010, D. Donsez OSGi 282 RFC 139 Distributed OSGi © 2001-2010, D. Donsez OSGi 283 © 2001-2010, D. Donsez OSGi 284