osgi community event 2010 - dependencies, dependencies, dependencies

Download OSGi Community Event 2010 - Dependencies, dependencies, dependencies

Post on 20-Aug-2015

1.037 views

Category:

Technology

2 download

Embed Size (px)

TRANSCRIPT

  1. 1. !"#$%&'&()"* Marcel Offermans Dependencies, dependencies, dependencies 1
  2. 2. Marcel Offermans Fellow and Software Architect at Luminis Technologies marcel.offermans@luminis.nl Member and Committer at Apache Software Foundation marrs@apache.org 2
  3. 3. Agenda Introduction Basic Concepts Dependencies Design Patterns Custom Dependencies Add-ons Wrap-up 3
  4. 4. Agenda Introduction Basic Concepts Dependencies Design Patterns Custom Dependencies Add-ons Wrap-up Software distribution: copy both zip archives from the memory stick 3
  5. 5. Introduction 4
  6. 6. Framework !"#"$%&'()"*$+",-&./ 012&$3'"4/56'7 1/,)'&(8 +69)*/ :&3/$,8,*/ 1/'#&,/ ;).9*/ ;).9*/ ;).9*/ 5
  7. 7. Service Dependencies framework: ServiceListener notication whenever there is a change in the service registry but: only changes that occur while you are listening utility: ServiceTracker solves the listening issue adds ability to customize services 6
  8. 8. Problem using the framework supplied tooling, you are dealing with dependencies at a very low level a lot of boiler plate code is needed to implement real world scenarios there are very few design patterns that deal with composing an application based on services 7
  9. 9. Declaring your dependencies Service Binder Dependency Manager Declarative Services iPOJO Blueprint ...many more 8
  10. 10. Dependency Manager Subproject of Apache Felix Nearing a 3.0 release Some interesting new features ...Im not unbiased, being its author 9
  11. 11. Basic Concepts 10
  12. 12. Basic Concepts Component: class that implements certain behaviour (a POJO) Dependency: something our component needs or wants (ie. a service) Service: OSGi service interface(s) under which our component is registered 11
  13. 13. Declarative API Declarative XML Using a Java API has many advantages: less error prone because of syntax and compile time checking refactoring support, completion very readable through uid API everything in one place, no magic 12
  14. 14. Example code Projects Download: http://www.xs4all.nl/~mfo/projects.zip Uses Eclipse + BndTools Homepage: http://njbartlett.name/bndtools.html Update Site: http://bndtools-updates.s3.amazonaws.com Based on a snapshot release of the upcoming Dependency Manager 3.0 During the presentation, we will switch between slides and Eclipse to show running examples 13
  15. 15. Example code Projects Download: http://www.xs4all.nl/~mfo/projects.zip Uses Eclipse + BndTools Homepage: http://njbartlett.name/bndtools.html Update Site: http://bndtools-updates.s3.amazonaws.com Based on a snapshot release of the upcoming Dependency Manager 3.0 During the presentation, we will switch between slides and Eclipse to show running examples On the memory stick: a) an Eclipse update site for BndTools b) a set of Eclipse projects 13
  16. 16. Using the Dependency Manager public class Activator extends DependencyActivatorBase { public void init(BundleContext context, DependencyManager manager) throws Exception { manager.add(createComponent() .setImplementation(new HelloWorld()) ); } public void destroy(BundleContext context, DependencyManager manager) throws Exception { } } Import-Package = org.apache.felix.dm;version="[3.0,4)" 14
  17. 17. Basic Use Cases Declare a component Declare it lazily HelloWorld helloWorld = new HelloWorld(); manager.add(createComponent() .setImplementation(helloWorld) ); manager.add(createComponent() .setImplementation(HelloWorld.class) ); 15
  18. 18. Component Life Cycle methods of the component setCallbacks(init, start, ...) setCallbacks(inst, init, start, ...) to invoke the methods on inst ComponentStateListener if you want to listen from the outside public static class HelloWorldLifeCycle { private void init() { System.out.println("init"); } private void start() { System.out.println("start"); } private void stop() { System.out.println("stop"); } private void destroy() { System.out.println("destroy"); } } 16
  19. 19. Declaring as a service setInterface(...) allows you to declare multiple services allows you to specify service properties manager.add(createComponent() .setInterface(LogService.class.getName(), new Properties() {{ put(Constants.SERVICE_RANKING, 20); }}) .setImplementation(MyLogService.class) 17
  20. 20. Declaring Dependencies Adding a dependency Injects dependency uses null object pattern injects other OSGi instances setCallbacks(...) setCallbacks(inst, ...) manager.add(createComponent() .setImplementation(HelloWorldLogger.class) .add(createServiceDependency() .setService(LogService.class) ) ); 18
  21. 21. Dependencies 19
  22. 22. Dependencies Different types: Service Dependencies Conguration Dependencies Bundle Dependencies Resource Dependencies 20
  23. 23. Conguration Dependencies Based on Conguration Admin designed for required dependencies service.pid to identify the conguration allows you to only accept *valid* congurations update() throws CongurationException 21
  24. 24. Bundle Dependencies Depend on a bundle: in certain states with certain manifest entries Bundle instance can be injected 22
  25. 25. Resource Dependencies Resources are modeled as URLs Are provided by a repository another bundle an Eclipse workspace some external source Filter on host, port, protocol, path and URL 23
  26. 26. Design Patterns 24
  27. 27. Design Patterns Moving up a level in abstraction OSGi is too low level to expose to everybody, but it is a great platform to build on Patterns provide a common language and describe solutions in context 25
  28. 28. Overview of Design Patterns Whiteboard Pattern Null Object Pattern Singleton Service Aspect Service Adapter Service Resource Adapter Service 26
  29. 29. Whiteboard Pattern dont call us... well call you 27
  30. 30. Null Object Pattern An object that implements a certain interface, can be safely invoked and does nothing 28
  31. 31. Singleton Service Publishes a component as a service Ties its life cycle to that of its dependencies get() getVersions() get(version) store(Document) Document Repository Singleton Storage requires 29
  32. 32. Aspect Service Works at the service level intercepts certain services can be chained based on rankings completely dynamic get() getVersions() get(version) store(Document) Repository Cache Aspect get() getVersions() get(version) store(Document) Repository intercepts 30
  33. 33. Adapter Service Works on existing services Adapts them, publishes a different service for each existing one getCacheHits() setSize() setTTL() ush() Repository Cache Manageable Adapter get() getVersions() get(version) store(Document) Repository Cache adapts 31
  34. 34. Resource Adapter Service Adapts resources (instead of services) Allows you to expose the behavior of certain resources instead of their inner guts play() pause() stop() Audio Track Resource Adapter MP3 File adapts 32
  35. 35. Custom Dependencies 33
  36. 36. Custom Dependencies Dependency Manager is extensible with custom dependencies: depend on time of day depend on some custom instance / condition (start level, app state) 34
  37. 37. Add-ons 35
  38. 38. Add-ons Shell (Felix, Equinox, Gogo) Annotation based (Java 5 required) Legacy support (2.x API adapter) 36
  39. 39. Wrap-up 37
  40. 40. Wrap-up Points to take away: do not expose every developer to the OSGi API build higher level abstractions, use design patterns consider the dependency manager, it is very exible 38
  41. 41. More about OSGi ApacheCon 2010 North America November 1-5, Atlanta OSGi tutorial full day OSGi track Masterclass on OSGi October 12-15, Girona Neil Bartlett and Peter Kriens 39