Your SlideShare is downloading. ×
0
OSGi
 Embrace                          Change
A Developer's Quickstart



     Carsten Ziegeler
     cziegeler@apache.org
...
About
• Member of the ASF
  – Sling, Felix, Cocoon, Portals, Sanselan,
    Excalibur, Incubator
  – PMC: Felix, Portals, S...
Agenda
1 Motivation
2 And Action...
3 Why OSGi?
4 Apache Felix
5-7 Bundles, Services, Dynamics
8 Famous Final Words



   ...

1 Example Application
   Motivation
               


                        4
Motivation
• Modularity is key
  – Manage growing complexity
  – Support dynamic extensibility
• No solution in standard J...

2 Example Action...
   And Application
           


                  6
Paint Program
• Swing-based paint program
• Interface SimpleShape for drawing
  – Different implementations
  – Each shape...
Shape Abstraction
• Conceptual SimpleShape interface

public interface SimpleShape
{
    /**
     * Method to draw the sha...
Paint Program Mock Up




                        9
High-Level Architecture


Shape     1   1    Drawing    1   *   Default
Tracker             Frame             Shape
      ...
High-Level Architecture
                   Best practice – Try to
                    centralize interaction
          1  ...
High-Level Architecture


Shape     1      1   Drawing    1     *   Default
Tracker               Frame               Shap...
High-Level Architecture


Shape     1     1   Drawing     1   *   Default
Tracker              Frame              Shape
  ...
High-Level Architecture

    Injected “proxied” shape
Shape      1     1
     implementation to hide
                     ...
High-Level Architecture
           Component that draws the
          shape in parent frame; looks
Shape     up shape via ...

    LIVE DEMO
    Example Application

                 


                          16

3 Example Application
   Why OSGi?
               


                        17
Class Path Hell




• Can you spot some potential problems?




                                      18
Class Path Hell




• What libs are used? Versions?
• Which jar is used? Version?
• No difference between private and publ...
Java's Shortcomings
• Simplistic version handling
  – “First” class from class path
  – JAR files assume backwards compati...
Java's Shortcomings
• Split packages by default
  – Class path approach searches until it finds,
    which leads to shadow...
Java Dynamism Limitations
• Low-level support for dynamics
  – Class loaders are complicated to use and
    error prone
• ...
OSGi Technology
• Adds modularity and dynamics
  – Module concept
    • Explicit sharing (importing and exporting)
  – Aut...
OSGi Alliance
• Industry consortium
• OSGi Service Platform specification
  – Framework specification for hosting
    dyna...

4 Example Application
   Apache Felix
             


                   25
Apache Felix
• Top-level project (March 2007)
• OSGi R4 (R4.2) implementation
  – Framework (frequent releases)
  – Servic...
Apache Felix
• Growing community
  – Diverse and healthy!
  – Several code grants and contributions
  – Various (Apache) p...

5 Example Application 1
   OSGi – Part
   Bundles     


                     28
OSGi Architectural Overview



              Bundle




                                                 k
               ...
OSGi Framework Layering
                 L3 – Provides a publish/find/bind service model to
 SERVICE MODEL   decouple bund...
OSGi Framework
• Component-oriented framework
• Module concept: Bundles
  – Separate class loader -> graph
  – Package sha...
OSGi Modularity
• Explicit code boundaries and
  dependencies
  – Package imports and exports
• Multi-version support
  – ...
OSGi Modularity - Example
• Dynamic module deployment and
  dependency resolution




                Provided package

  ...
OSGi Modularity - Example
• Dynamic module deployment and
  dependency resolution




                                  ex...
OSGi Modularity - Example
• Dynamic module deployment and
  dependency resolution




                                  ex...
OSGi Modularity - Example
• Dynamic module deployment and
  dependency resolution




                 automatic package
 ...
Creating a Bundle
• Plain old JAR with additional metadata in
  the manifest
  – Bundle identifier, version, exports, impo...
Maven is Your Friend
• Apache Felix Maven Bundle Plugin
• Creates metadata based on POM
  – Automatically: import packages...
Maven Bundle Plugin Sample
 <artifactId>org.apache.sling.engine</artifactId>
 <packaging>bundle</packaging>
 <version>2.0....
Maven Bundle Plugin Sample
 <artifactId>org.apache.sling.engine</artifactId>
 <packaging>bundle</packaging>
 <version>2.0....
Maven Bundle Plugin Sample
 <artifactId>org.apache.sling.engine</artifactId>
 <packaging>bundle</packaging>
 <version>2.0....
Maven Bundle Plugin Sample
 <artifactId>org.apache.sling.engine</artifactId>
 <packaging>bundle</packaging>
 <version>2.0....
Be Modular!
• Create clean package spaces
  – public vs private
• Provide Bundles
  – Add manifest information
• Think abo...

6 Example Application 2
   OSGi – Part
   Services    


                     44
OSGi Services (1/3)
• Service-oriented architecture
  – Publish/find/bind
  – Possible to use modules without services
   ...
OSGi Services (2/3)
• An OSGi application is...
  – A collection of bundles that interact via
    service interfaces
  – B...
OSGi Services (3/3)
• Dynamic service lookup



                   Provided service


                 Provided package

 ...
OSGi Services (3/3)
• Dynamic service lookup




                                      existing
                          ...
OSGi Services (3/3)
• Dynamic service lookup




                                      existing
        activate          ...
OSGi Services (3/3)
• Dynamic service lookup




                    automatic package
                  dependency resolu...
OSGi Services (3/3)
• Dynamic service lookup




                     manual service
                  dependency resoluti...
OSGi Services Advantages
• Lightweight services
   – Lookup is based on interface name
  – Direct method invocation
• Good...
OSGi Services Advantages
• Dynamic
   – Loose coupling and late binding
• Application's configuration is simply the set of...
OSGi Services Issues
• More sophisticated, but more complicated
  – Requires a different way of thinking
     • Things mig...

7 Example Application 3
   OSGi – Part
   Dynamics    


                     55
Everything is a Bundle
• How to structure bundles?
  – API vs implementation bundle
  – Fine-grained vs coarse-grained
  –...
Third Party Libraries
• Use them as bundles
  – Project delivers already a bundle
     • Apache Commons, Apache Sling etc....
Everything is Dynamic
• Bundles can come and go!
  – Packages
  – Services
• Services can come and go!
• Be prepaired!
  –...
Dynamic Services
• OSGi Declarative Services Specification
  – XML Configuration
    • Contained in bundle
    • Manifest ...
Dynamic Services Configuration
<scr:component enabled="true"
         name="org.apache.sling.event.impl.DistributingEventH...
Dynamic Services Configuration
<scr:component enabled="true"
         name="org.apache.sling.event.impl.DistributingEventH...
Dynamic Services Configuration
<scr:component enabled="true"
         name="org.apache.sling.event.impl.DistributingEventH...
Dynamic Services Configuration
<scr:component enabled="true"
         name="org.apache.sling.event.impl.DistributingEventH...
Declarative Services
• Reads XML configs on bundle start
• Registers services
• Keeps track of dependencies
  – Starts/sto...
Example Service
protected ThreadPool threadPool;

protected void activate(ComponentContext context)
throws Exception {
   ...
Config Admin and Metatype
• OSGi Config Admin
  –   Configuration Manager
  –   Persistence storage
  –   API to retrieve/...
Maven SCR Plugin
• Combines everything (DS, ConfigAdmin,
  Metatype, Maven)
• Annotation-based (even for 1.4)
• Annotate c...
SCR Plugin Sample
/**
 * @scr.component
 * @scr.property name="repository.path"
                  value="/var/eventing/dis...
Alternatives
• Manually through bundle activator
• Apache Felix iPojo
  – Next session in this track!
• Blueprint Containe...
Handling extensibility
• Two basic implementation strategies
  – Service-based approach
  – Extender model




           ...
Service Whiteboard Pattern
• Clients register a service interface
• Service tracker for registered services
• Simple, more...
Externder Model
• Bundles contain manifest entries
  – Like available service classes
• Custom bundle tracker
  – Keeps tr...

8 Example Application
   Famous
   Final Words 


                    73
Apache Felix Karaf
•   OSGi based kernel
•   Includes a preset of bundles
•   File based deployment and configuration
•   ...
Apache Felix Sigil
• OSGi centric development tools
• Property configuration file
• Integration with Ivy and Eclipse
  –  ...
Suggestions for Development
• Think about modularity!
  – Clean package space
• Think about dynamics!
• Consider OSGi
• Ch...
Suggestions for Using OSGi
• Think about dynamics
  – Optional bundles
  – Optional services
  – Handle these cases
• Use ...
Check It Out
• Read the OSGi spec
  – Framework
  – Config Admin, Metatype, Declarative
    Services
  – Deployment Admin,...
OSGi
Embrace   Change

Questions?
Upcoming SlideShare
Loading in...5
×

Embrace Change - Embrace OSGi

2,331

Published on

My OSGi presentation from ApacheCon US 2009, November, in Oakland, CA.

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,331
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
70
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Embrace Change - Embrace OSGi"

  1. 1. OSGi Embrace Change A Developer's Quickstart Carsten Ziegeler cziegeler@apache.org Apache Con US Presentation – November 2009 - Oakland
  2. 2. About • Member of the ASF – Sling, Felix, Cocoon, Portals, Sanselan, Excalibur, Incubator – PMC: Felix, Portals, Sling, Incubator, Excalibur (Chair) • RnD Team at Day Software • Article/Book Author, Technical Reviewer • JSR 286 Spec Group (Portlet API 2.0) 2
  3. 3. Agenda 1 Motivation 2 And Action... 3 Why OSGi? 4 Apache Felix 5-7 Bundles, Services, Dynamics 8 Famous Final Words 3
  4. 4.  1 Example Application Motivation  4
  5. 5. Motivation • Modularity is key – Manage growing complexity – Support dynamic extensibility • No solution in standard Java – OSGi: tried and trusted • Embrace change – Embrace OSGi – Only a few concepts – easy to get started 5
  6. 6.  2 Example Action... And Application  6
  7. 7. Paint Program • Swing-based paint program • Interface SimpleShape for drawing – Different implementations – Each shape has name and icon properties – Available shapes are displayed in tool bar • Select shape and then select location – Shapes can be dragged, but not resized • Support dynamic deployment of shapes 7
  8. 8. Shape Abstraction • Conceptual SimpleShape interface public interface SimpleShape { /** * Method to draw the shape of the service. * @param g2 The graphics object used for * painting. * @param p The position to paint the shape. **/ public void draw(Graphics2D g2, Point p); } 8
  9. 9. Paint Program Mock Up 9
  10. 10. High-Level Architecture Shape 1 1 Drawing 1 * Default Tracker Frame Shape 1 1 1 1 * 1 Shape Simple Component Shape 10
  11. 11. High-Level Architecture Best practice – Try to centralize interaction 1 1 with OSGi API * that 1 so Shape Drawing componentsDefault other Tracker Frame POJOs...only remain Shape 1 Shape Tracker will 1 1 interact with OSGi API. 1 * 1 Shape Simple Component Shape 11
  12. 12. High-Level Architecture Shape 1 1 Drawing 1 * Default Tracker Frame Shape 1 1 1 1 * 1 Main application window – gets Shape Simple dynamically injected Component Shape with available shapes from the Shape Tracker. 12
  13. 13. High-Level Architecture Shape 1 1 Drawing 1 * Default Tracker Frame Shape 1 1 1 1 * 1 Shape Simple Actual shape Component Shape implementation. 13
  14. 14. High-Level Architecture Injected “proxied” shape Shape 1 1 implementation to hide Drawing 1 * Default aspects of dynamism Tracker Frame Shape and provide a default implementation. 1 1 1 1 * 1 Shape Simple Actual shape Component Shape implementation. 14
  15. 15. High-Level Architecture Component that draws the shape in parent frame; looks Shape up shape via Drawing 1 1 1 Drawing Frame * Default Tracker Frame rather than having a direct Shape reference. 1 1 1 1 * 1 Shape Simple Component Shape 15
  16. 16.  LIVE DEMO Example Application  16
  17. 17.  3 Example Application Why OSGi?  17
  18. 18. Class Path Hell • Can you spot some potential problems? 18
  19. 19. Class Path Hell • What libs are used? Versions? • Which jar is used? Version? • No difference between private and public classes 19
  20. 20. Java's Shortcomings • Simplistic version handling – “First” class from class path – JAR files assume backwards compatibility at best • Implicit dependencies – Dependencies are implicit in class path ordering – JAR files add improvements for extensions, but cannot control visibility 20
  21. 21. Java's Shortcomings • Split packages by default – Class path approach searches until it finds, which leads to shadowing or version mixing • Limited scoping mechanisms – No module access modifier – Impossible to declare all private stuff as private • Missing module concept – Classes are too fine grained, packages are too simplistic, class loaders are too low level • No deployment/lifecycle support 21
  22. 22. Java Dynamism Limitations • Low-level support for dynamics – Class loaders are complicated to use and error prone • Support for dynamics is still purely manual – Must be completely managed by the programmer – Leads to many ad hoc, incompatible solutions • Limited deployment support 22
  23. 23. OSGi Technology • Adds modularity and dynamics – Module concept • Explicit sharing (importing and exporting) – Automatic management of code dependencies • Enforces sophisticated consistency rules for class loading – Life-cycle management • Manages dynamic deployment and configuration • Service Registry – Publish/find/bind 23
  24. 24. OSGi Alliance • Industry consortium • OSGi Service Platform specification – Framework specification for hosting dynamically downloadable services – Standard service specifications • Several expert groups define the specifications – Core Platform Expert Group (CPEG) – Mobile Expert Group (MEG) – Vehicle Expert Group (VEG) – Enterprise Expert Group (EEG) 24
  25. 25.  4 Example Application Apache Felix  25
  26. 26. Apache Felix • Top-level project (March 2007) • OSGi R4 (R4.2) implementation – Framework (frequent releases) – Services (continued development) • Log, Package Admin, Event Admin, Configuration Admin, Declarative Services, Meta Type, Deployment Admin (and more) – Moving towards completing R4.2 • Tools – Maven Plugins, Web Console, iPojo – Karaf, Sigil 26
  27. 27. Apache Felix • Growing community – Diverse and healthy! – Several code grants and contributions – Various (Apache) projects use Felix / have expressed interest in Felix and/or OSGi • e.g., ServiceMix, Directory, Sling, Tuscany • Roadmap – Continue toward R4.2 compliance – New Tools (Karaf, Sigil) 27
  28. 28.  5 Example Application 1 OSGi – Part Bundles  28
  29. 29. OSGi Architectural Overview Bundle k or ew am Fr OSGi Java Operating System Driver Driver Driver Hardware 29
  30. 30. OSGi Framework Layering L3 – Provides a publish/find/bind service model to SERVICE MODEL decouple bundles L2 - Manages the life cycle of bundle in a bundle repository without requiring the VM be restarted LIFECYCLE L1 - Creates the concept of modules (aka. bundles) that use classes from each other in a controlled way MODULE according to system and bundle constraints Execution L0 - •OSGi Minimum Execution Environment Environment CDC •CDC/Foundation CDC •JavaSE 30
  31. 31. OSGi Framework • Component-oriented framework • Module concept: Bundles – Separate class loader -> graph – Package sharing and version management – Life-cycle management and notification • Dynamic! – Install, update, and uninstall at runtime • Runs multiple applications and services in a single VM 31
  32. 32. OSGi Modularity • Explicit code boundaries and dependencies – Package imports and exports • Multi-version support – Version ranges for dependencies • Class space is managed by OSGi • Managed life cycle – Dynamic install, update, uninstall 32
  33. 33. OSGi Modularity - Example • Dynamic module deployment and dependency resolution Provided package existing bundle OSGi framework 33
  34. 34. OSGi Modularity - Example • Dynamic module deployment and dependency resolution existing bundle install bundle.jar OSGi framework 34
  35. 35. OSGi Modularity - Example • Dynamic module deployment and dependency resolution existing resolve bundle bundle OSGi framework 35
  36. 36. OSGi Modularity - Example • Dynamic module deployment and dependency resolution automatic package dependency existing resolution bundle OSGi framework 36
  37. 37. Creating a Bundle • Plain old JAR with additional metadata in the manifest – Bundle identifier, version, exports, imports • Tools – Text editor (Manifest) – Eclipse (PDE) – Bundle packaging tools • BND from Peter Kriens • Apache Felix maven-bundle-plugin based on BND 37
  38. 38. Maven is Your Friend • Apache Felix Maven Bundle Plugin • Creates metadata based on POM – Automatically: import packages – Manually: export and private packages • Analyses classes for consistency • Allows to include dependencies • Creates final bundle JAR file 38
  39. 39. Maven Bundle Plugin Sample <artifactId>org.apache.sling.engine</artifactId> <packaging>bundle</packaging> <version>2.0.7-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package> org.apache.sling.engine;version=${pom.version} </Export-Package> <Private-Package> org.apache.sling.engine.impl </Private-Package> <Embed-Dependency> commons-fileupload </Embed-Dependency> </instructions> 39
  40. 40. Maven Bundle Plugin Sample <artifactId>org.apache.sling.engine</artifactId> <packaging>bundle</packaging> <version>2.0.7-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package> org.apache.sling.engine;version=${pom.version} </Export-Package> <Private-Package> org.apache.sling.engine.impl </Private-Package> <Embed-Dependency> commons-fileupload </Embed-Dependency> </instructions> 40
  41. 41. Maven Bundle Plugin Sample <artifactId>org.apache.sling.engine</artifactId> <packaging>bundle</packaging> <version>2.0.7-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package> org.apache.sling.engine;version=${pom.version} </Export-Package> <Private-Package> org.apache.sling.engine.impl </Private-Package> <Embed-Dependency> commons-fileupload </Embed-Dependency> </instructions> 41
  42. 42. Maven Bundle Plugin Sample <artifactId>org.apache.sling.engine</artifactId> <packaging>bundle</packaging> <version>2.0.7-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package> org.apache.sling.engine;version=${pom.version} </Export-Package> <Private-Package> org.apache.sling.engine.impl </Private-Package> <Embed-Dependency> commons-fileupload </Embed-Dependency> </instructions> 42
  43. 43. Be Modular! • Create clean package spaces – public vs private • Provide Bundles – Add manifest information • Think about dependencies – Additional bundle vs include – Optional – Version ranges • Manage releases and versions properly • Benefits even without OSGi 43
  44. 44.  6 Example Application 2 OSGi – Part Services  44
  45. 45. OSGi Services (1/3) • Service-oriented architecture – Publish/find/bind – Possible to use modules without services Service Registry Publish Find Service Description Service Service Provider Interact Requester 45
  46. 46. OSGi Services (2/3) • An OSGi application is... – A collection of bundles that interact via service interfaces – Bundles may be independently developed and deployed – Bundles and their associated services may appear or disappear at any time • Resulting application follows a Service- Oriented Component Model approach 46
  47. 47. OSGi Services (3/3) • Dynamic service lookup Provided service Provided package existing bundle component OSGi framework 47
  48. 48. OSGi Services (3/3) • Dynamic service lookup existing bundle install component bundle.jar OSGi framework 48
  49. 49. OSGi Services (3/3) • Dynamic service lookup existing activate bundle bundle component OSGi framework 49
  50. 50. OSGi Services (3/3) • Dynamic service lookup automatic package dependency resolution existing bundle component OSGi framework 50
  51. 51. OSGi Services (3/3) • Dynamic service lookup manual service dependency resolution existing bundle component OSGi framework 51
  52. 52. OSGi Services Advantages • Lightweight services – Lookup is based on interface name – Direct method invocation • Good design practice – Separates interface from implementation – Enables reuse, substitutability, loose coupling, and late binding 52
  53. 53. OSGi Services Advantages • Dynamic – Loose coupling and late binding • Application's configuration is simply the set of deployed bundles – Deploy only the bundles that you need 53
  54. 54. OSGi Services Issues • More sophisticated, but more complicated – Requires a different way of thinking • Things might appear/disappear at any moment – Must manually resolve and track services • There is help – Service Tracker • Still somewhat of a manual approach – Declarative Services, Blueprint, iPOJO • Sophisticated service-oriented component frameworks • Automated dependency injection and more • More modern, POJO-oriented approaches 54
  55. 55.  7 Example Application 3 OSGi – Part Dynamics  55
  56. 56. Everything is a Bundle • How to structure bundles? – API vs implementation bundle – Fine-grained vs coarse-grained – No “One Size Fits All” • Simple Rules – Stable code vs changing code – Optional parts 56
  57. 57. Third Party Libraries • Use them as bundles – Project delivers already a bundle • Apache Commons, Apache Sling etc. – Use special bundle repositories • Felix Commons, Spring etc. • But check included metadata! – Create your own wrapper • Easy with the Felix maven bundle plugin • Include classes in your bundle – Again: easy with the Felix maven bundle plugin 57
  58. 58. Everything is Dynamic • Bundles can come and go! – Packages – Services • Services can come and go! • Be prepaired! – Application code must handle dynamics! 58
  59. 59. Dynamic Services • OSGi Declarative Services Specification – XML Configuration • Contained in bundle • Manifest entry pointing to config(s) – Publishing services – Consuming services • Policy (static,dynamic), cardinality (0..1, 1..1, 0..n) – Default configuration – Service Lifecycle management • Various Implementations – Apache Felix SCR 59
  60. 60. Dynamic Services Configuration <scr:component enabled="true" name="org.apache.sling.event.impl.DistributingEventHandler"> <implementation class="org.apache.sling.event.impl.DistributingEventHandler"/> <service servicefactory="false"> <provide interface="org.osgi.service.event.EventHandler"/> </service> <property name="repository.path" value="/var/eventing/distribution"/> <property name="cleanup.period" type="Integer" value="15"/> <reference name="threadPool" interface="org.apache.sling.event.ThreadPool" cardinality="1..1" policy="static" bind="bindThreadPool" unbind="unbindThreadPool"/> 60
  61. 61. Dynamic Services Configuration <scr:component enabled="true" name="org.apache.sling.event.impl.DistributingEventHandler"> <implementation class="org.apache.sling.event.impl.DistributingEventHandler"/> <service servicefactory="false"> <provide interface="org.osgi.service.event.EventHandler"/> </service> <property name="repository.path" value="/var/eventing/distribution"/> <property name="cleanup.period" type="Integer" value="15"/> <reference name="threadPool" interface="org.apache.sling.event.ThreadPool" cardinality="1..1" policy="static" bind="bindThreadPool" unbind="unbindThreadPool"/> 61
  62. 62. Dynamic Services Configuration <scr:component enabled="true" name="org.apache.sling.event.impl.DistributingEventHandler"> <implementation class="org.apache.sling.event.impl.DistributingEventHandler"/> <service servicefactory="false"> <provide interface="org.osgi.service.event.EventHandler"/> </service> <property name="repository.path" value="/var/eventing/distribution"/> <property name="cleanup.period" type="Integer" value="15"/> <reference name="threadPool" interface="org.apache.sling.event.ThreadPool" cardinality="1..1" policy="static" bind="bindThreadPool" unbind="unbindThreadPool"/> 62
  63. 63. Dynamic Services Configuration <scr:component enabled="true" name="org.apache.sling.event.impl.DistributingEventHandler"> <implementation class="org.apache.sling.event.impl.DistributingEventHandler"/> <service servicefactory="false"> <provide interface="org.osgi.service.event.EventHandler"/> </service> <property name="repository.path" value="/var/eventing/distribution"/> <property name="cleanup.period" type="Integer" value="15"/> <reference name="threadPool" interface="org.apache.sling.event.ThreadPool" cardinality="1..1" policy="static" bind="bindThreadPool" unbind="unbindThreadPool"/> 63
  64. 64. Declarative Services • Reads XML configs on bundle start • Registers services • Keeps track of dependencies – Starts/stops services • Invokes optional activation and deactivation method – Provides access to configuration • Caution: A service is by default only started if someone else uses it! – Immediate flag forces a service start 64
  65. 65. Example Service protected ThreadPool threadPool; protected void activate(ComponentContext context) throws Exception { @SuppressWarnings("unchecked") final Dictionary<String, Object> props = context.getProperties(); this.cleanupPeriod = (Integer)props.get("cleanup.period"); super.activate(context); } protected void bindThreadPool(ThreadPool p) { this.threadPool = p; } protected void unbindThreadPool(ThreadPool p) { if ( this.threadPool == p ) { this.threadPool = null; } } 65
  66. 66. Config Admin and Metatype • OSGi Config Admin – Configuration Manager – Persistence storage – API to retrieve/update/remove configs – Works with Declarative Services • OSGi Metatype Service – Description of bundle metadata – Description of service configurations • Various Implementations – Apache Felix 66
  67. 67. Maven SCR Plugin • Combines everything (DS, ConfigAdmin, Metatype, Maven) • Annotation-based (even for 1.4) • Annotate components – Properties with default values – Service providers – Services references (policy and cardinality) • Generates DS XML • Generates Metatype config • Generates Java code 67
  68. 68. SCR Plugin Sample /** * @scr.component * @scr.property name="repository.path" value="/var/eventing/distribution" private="true" * @scr.service interface="EventHandler" */ public class DistributingEventHandler implements EventHandler { protected static final int DEFAULT_CLEANUP_PERIOD = 15; /** @scr.property valueRef="DEFAULT_CLEANUP_PERIOD" type="Integer" */ protected static final String PROP_CLEANUP_PERIOD = "cleanup.period"; /** @scr.reference */ protected ThreadPool threadPool; protected void activate(ComponentContext context) throws Exception { final Dictionary<String, Object> props = context.getProperties(); this.cleanupPeriod = (Integer)props.get(PROP_CLEANUP_PERIOD); } 68
  69. 69. Alternatives • Manually through bundle activator • Apache Felix iPojo – Next session in this track! • Blueprint Container Specification – Apache Aries (Incubator) 69
  70. 70. Handling extensibility • Two basic implementation strategies – Service-based approach – Extender model 70
  71. 71. Service Whiteboard Pattern • Clients register a service interface • Service tracker for registered services • Simple, more robust, leverages the OSGi service model • Service whiteboard pattern – It is an Inversion of Control pattern 71
  72. 72. Externder Model • Bundles contain manifest entries – Like available service classes • Custom bundle tracker – Keeps track of bundles – Specifically, STARTED and STOPPED events – Checks bundles manifest data • Creates/removes services 72
  73. 73.  8 Example Application Famous Final Words  73
  74. 74. Apache Felix Karaf • OSGi based kernel • Includes a preset of bundles • File based deployment and configuration • Assemblies • Extensible shell console • Download and go solution 74
  75. 75. Apache Felix Sigil • OSGi centric development tools • Property configuration file • Integration with Ivy and Eclipse – Dependency Analysis – Repository browser – Respects private packages of dependencies – Quick fix support • Roadmap – Maven support – Extension points 75
  76. 76. Suggestions for Development • Think about modularity! – Clean package space • Think about dynamics! • Consider OSGi • Check out the spec and other projects – Attend today's OSGi/Felix track • Minimize dependencies to OSGi – but only if it makes sense 76
  77. 77. Suggestions for Using OSGi • Think about dynamics – Optional bundles – Optional services – Handle these cases • Use your preferred logging library – LogManager takes care • Use available tooling • Be part of the community! 77
  78. 78. Check It Out • Read the OSGi spec – Framework – Config Admin, Metatype, Declarative Services – Deployment Admin, OBR • Download Apache Felix – Try tutorials and samples – Quickstart with Apache Felix Karaf • Download Apache Sling :) • Explore the web – embrace OSGi 78
  79. 79. OSGi Embrace Change Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×