View
10
Download
0
Category
Preview:
Citation preview
Softwaretechnik
Prof. Dr. Rainer Koschke
Fachbereich Mathematik und InformatikArbeitsgruppe Softwaretechnik
Universitat Bremen
Wintersemester 2011/12
Uberblick I
OSGI
OSGI:
OSGI
OSGIArchitekturManifestLebenszyklus von BundlesExistierende Container-ImplementierungenDemo mit Eclipse
Einfaches Bundle anlegenOSGI-Konsole und LebenszyklusKooperierende BundlesService-Orientierung mit Bundles
3 / 17
OSGI:
Beispiel eines Komponenten-Rahmewerks: OSGI
Open Services Gateway Initiative (OSGI)
Kompontenmodell fur Java
Komponente = Bundle = JAR + Manifest
unterstutzt entfernte Installation, Start, Stopp, Aktualisierungund Deinstallation von Bundles
Service-Register ermoglicht den Bundles, Dienstehinzuzufugen, zu entfernen und anzupassen
Verbreitung: Eclipse, Mobiltelefone, . . .
4 / 17
OSGI: Architektur
Architektur
5 / 17
Bundles Bundles are normal jar components with extra manifestheaders.
Services The services layer connects bundles in a dynamic way byoffering a publish-find-bind model for plain old JavaInterfaces (POJI) or Plain Old Java Objects POJO.
Services Registry The API for management services (ServiceRegistration,ServiceTracker and ServiceReference).
Life-Cycle The API for life cycle management for (install, start, stop,update, and uninstall) bundles.
Modules The layer that defines encapsulation and declaration ofdependencies (how a bundle can import and export code).
Security The layer that handles the security aspects by limitingbundle functionality to pre-defined capabilities.
Bildquelle: Faisal Akeel, Bill Streckfus
OSGI: Architektur
Architektur
6 / 17
Bildquelle: Michael Grammling, Bill Streckfus (Creative CommonsAttribution ShareAlike 3.0 License)
OSGI: Manifest
Bundle-Manifest
Bundle−Name : He l l o WorldBundle−SymbolicName : org . w i k i p e d i a . h e l l o w o r l dBundle−De s c r i p t i o n : A He l l o World bund leBundle−Man i f e s tVe r s i o n : 2Bundle−Ve r s i on : 1 . 0 . 0Bundle−Ac t i v a t o r : o rg . w i k i p e d i a . A c t i v a t o rExport−Package : org . w i k i p e d i a . h e l l o w o r l d ; v e r s i o n=” 1 . 0 . 0 ”Import−Package : org . o s g i . f ramework ; v e r s i o n=” 1 . 3 . 0 ”
7 / 17
Bundle-Name Defines a human-readable name for this bundle, Simplyassigns a short name to the bundle.
Bundle-SymbolicName The only required header, this entry specifies aunique identifier for a bundle, based on the reverse domainname convention (used also by the java packages).
Bundle-Description A description of the bundle’s functionality.
Bundle-ManifestVersion This little known header indicates the OSGispecification to use for reading this bundle.
Bundle-Version Designates a version number to the bundle.
Bundle-Activator Indicates the class name to be invoked once a bundle isactivated.
Export-Package Expresses what Java packages contained in a bundle willbe made available to the outside world.
Import-Package Indicates what Java packages will be required from theoutside world, in order to fulfill the dependencies neededin a bundle.
OSGI: Lebenszyklus von Bundles
Lebenszyklus eines Bundles
8 / 17
INSTALLED The bundle has been successfully installed.
RESOLVED All Java classes that the bundle needs are available. Thisstate indicates that the bundle is either ready to bestarted or has stopped.
STARTING The bundle is being started, the BundleActivator.startmethod will be called, and this method has not yetreturned. When the bundle has an activation policy, thebundle will remain in the STARTING state until thebundle is activated according to its activation policy.
ACTIVE The bundle has been successfully activated and is running;its Bundle Activator start method has been called andreturned.
STOPPING The bundle is being stopped. The BundleActivator.stopmethod has been called but the stop method has not yetreturned.
UNINSTALLED The bundle has been uninstalled. It cannot move intoanother state.
Bildquelle: Faisal Akeel
OSGI: Existierende Container-Implementierungen
Open-Source-OSGi-Container
Equinox
Knopflerfish
Apache Felix
9 / 17
Equinox is the reference implementation for the framework portion of theOSGi Service Platform Release 4. It is the modular Java runtime at theheart of the Eclipse IDE, and implements all of the mandatory and mostof the optional features of the OSGi R4 specification.
Knopflerfish is an open source implementation of the OSGi R3 and OSGiR4 specifications. Knopflerfish 2 implements all the mandatory featuresand some of the optional features defined in the R4 specification.
Apache Felix is the open source OSGi container from the ApacheSoftware Foundation. At the time of writing this container is not fullycompliant with the OSGI R4 specification.
Quelle: Wikipedia
OSGI: Demo mit Eclipse
Eclipse-Demo: Einfaches Bundle anlegen
1 Neues Plug-in-Projekt “File → New → Project”
2 Selektiere “Plug-in Project” und “Next”.3 Eingabe:
Project Name: com.javaworld.sample.HelloWorldTarget Platform: OSGi framework → Equinox
4 Weiter mit “Next”.
5 Selektiere Voreinstellungen im Plug-in-Context-Dialog und“Next”
6 Templates-Dialog “Hello OSGi Bundle” und “Finish”
7 Manifest anschauen.
8 Quellcode von Activator anschauen
10 / 17
OSGI: Demo mit Eclipse
Eclipse-Demo: Einfaches Bundle starten
1 Framework testweise starten: Sektion “Testing”, Link “Launchthe framework”
2 Status des Bundles anschauen: “ss Hello” in OSGI-Konsoleeingeben.
3 Bundle anhalten: “stop <nummer>”
4 Bundle starten: “start <nummer>”
5 Quelltext von Activator verandern: Ausgabe verandern.
6 Bundle aktivieren: “update <nummer>”
11 / 17
OSGI: Demo mit Eclipse
Eclipse-Demo: Export1 Neues Plug-in-Projekt com.javaworld.sample.HelloService
anlegen (wie oben)2 Im Projekt neues Interface HelloService im Package
com.javaworld.sample.service:
package com . j a v awo r l d . sample . s e r v i c e ;p u b l i c i n t e r f a c e H e l l o S e r v i c e {
p u b l i c S t r i n g s a yHe l l o ( ) ;}
3 Neue Klasse HelloServiceImpl im Packagecom.javaworld.sample.service.impl :
package com . j a v awo r l d . sample . s e r v i c e . imp l ;impor t com . j a v awo r l d . sample . s e r v i c e . H e l l o S e r v i c e ;
p u b l i c c l a s s H e l l o S e r v i c e Imp l implements H e l l o S e r v i c e {@Over r idep u b l i c S t r i n g s a yHe l l o ( ) {
r e t u r n ” at your s e r v i c e ” ;}
}
4 In MANIFEST.MF im Reiter Runtime als Exported Packagescom.javaworld.sample.service hinzufugen
12 / 17
OSGI: Demo mit Eclipse
Eclipse-Demo: Import
1 In MANIFEST.MF von Plug-In HelloWorld im Reiter RequiredPlug-ins com.javaworld.sample.HelloService hinzufugen
2 Editiere com.javaworld.sample.helloworld.Activator.java:HelloService-Interface ist sichtbar, aber nichtHelloServiceImpl-Klasse:
p u b l i c vo i d c a l l S e r v i c e ( H e l l o S e r v i c e s e r v i c e ) {System . out . p r i n t l n ( ”He l loWor ld : ” + s e r v i c e . s a yHe l l o ( ) ) ;
}
13 / 17
OSGI: Demo mit Eclipse
Eclipse-Demo: Service-ProviderIm Plug-in-Projekt com.javaworld.sample.HelloService die KlasseActivator wie folgt abandern:
package com . j a v awo r l d . sample . h e l l o s e r v i c e ;impor t org . o s g i . f ramework . Bund l eAc t i v a t o r ;
p u b l i c c l a s s A c t i v a t o r implements Bund l eAc t i v a t o r {
S e r v i c e R e g i s t r a t i o n h e l l o S e r v i c e R e g i s t r a t i o n ;p u b l i c vo i d s t a r t ( Bund leContext c on t e x t ) throws Excep t i on {
System . out . p r i n t l n ( ” S e r v i c e : He l l o World ! ! ” ) ;H e l l o S e r v i c e h e l l o S e r v i c e = new He l l o S e r v i c e Imp l ( ) ;h e l l o S e r v i c e R e g i s t r a t i o n= con t e x t . r e g i s t e r S e r v i c e ( H e l l o S e r v i c e . c l a s s . getName ( ) ,
h e l l o S e r v i c e , n u l l ) ;}p u b l i c vo i d s top ( Bund leContext c on t e x t ) throws Excep t i on {
System . out . p r i n t l n ( ” S e r v i c e : Goodbye World ! ! ” ) ;h e l l o S e r v i c e R e g i s t r a t i o n . u n r e g i s t e r ( ) ;
}}
14 / 17
In OSGi, a source bundle registers a POJO (you don’t have to implementany interfaces or extend from any superclass) with the OSGi container asa service under one or more interfaces. The target bundle can then askthe OSGi container for services registered under a particular interface.Once the service is found, the target bundle binds with it and can startcalling its methods.
Quelle: http://www.javaworld.com/javaworld/jw-03-2008/
jw-03-osgi1.html?page=4
OSGI: Demo mit Eclipse
Eclipse-Demo: Service-ConsumerIm Plug-in-Projekt com.javaworld.sample.HelloWord die KlasseActivator wie folgt abandern:
package com . j a v awo r l d . sample . h e l l o w o r l d ;
impor t org . o s g i . f ramework . Bund l eAc t i v a t o r ;impor t org . o s g i . f ramework . Bund leContext ;impor t org . o s g i . f ramework . S e r v i c eR e f e r e n c e ;
impor t com . j a v awo r l d . sample . s e r v i c e . H e l l o S e r v i c e ;
p u b l i c c l a s s A c t i v a t o r implements Bund l eAc t i v a t o r {
S e r v i c eR e f e r e n c e h e l l o S e r v i c e R e f e r e n c e ;p u b l i c vo i d s t a r t ( Bund leContext c on t e x t ) throws Excep t i on {
System . out . p r i n t l n ( ”Consumer : He l l o World ! ! ” ) ;h e l l o S e r v i c e R e f e r e n c e= con t e x t . g e t S e r v i c eR e f e r e n c e ( H e l l o S e r v i c e . c l a s s . getName ( ) ) ;H e l l o S e r v i c e h e l l o S e r v i c e= ( H e l l o S e r v i c e ) c on t e x t . g e t S e r v i c e ( h e l l o S e r v i c e R e f e r e n c e ) ;System . out . p r i n t l n ( h e l l o S e r v i c e . s a yHe l l o ( ) ) ;
}p u b l i c vo i d s top ( Bund leContext c on t e x t ) throws Excep t i on {
System . out . p r i n t l n ( ”Consumer : Goodbye World ! ! ” ) ;c on t e x t . u n g e t S e r v i c e ( h e l l o S e r v i c e R e f e r e n c e ) ;
}}
15 / 17
OSGI: Demo mit Eclipse
Eclipse-Demo: Start von Service-Provider/Consumer
1 Im Manifest von com.javaworld.sample.HelloService “Launchthe framework” auswahlen.
2 “ss Hello”
3 “update <nummer>”, wobei nummer die Id desConsumer-Plug-in HelloWorld ist
16 / 17
OSGI: Demo mit Eclipse
1 OSGI : OSGI Alliance. – URLhttp://www.osgi.org/Main/HomePage
17 / 17
Recommended