Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dependencies, dependencies, dependencies

Managing dependencies in OSGi using the Apache Felix Dependency Manager gives you some powerful tools for expressing and managing all kinds of dependencies between components. This presentation shows you how to use them, and explains what design patterns are available for building applications. It also shows how to extend this system with custom types of dependencies.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

Dependencies, dependencies, dependencies

  1. 1. Dependencies, dependencies, dependencies Marcel Offermans TECHNOLOGIES
  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
  3. 3. Agenda • Introduction • Basic Concepts • Dependencies • Design Patterns • Custom Dependencies • Add-ons • Wrap-up Software distribution: copy both zip archives from the memory stick
  4. 4. Introduction
  5. 5. Framework Service Life$cycle Module Security OSGi$framework Bundle Bundle Bundle Java$Virtual$Machine
  6. 6. Service Dependencies • framework: ServiceListener – notification 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
  7. 7. 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
  8. 8. Declaring your dependencies • Service Binder • Dependency Manager • Declarative Services • iPOJO • Blueprint • ...many more
  9. 9. Dependency Manager • Subproject of Apache Felix • Nearing a 3.0 release • Some interesting new features ...I’m not unbiased, being it’s author
  10. 10. Basic Concepts
  11. 11. 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
  12. 12. 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 fluid API – everything in one place, no magic
  13. 13. On the memory stick: ! a) an Eclipse update site for BndTools b) a set of Eclipse projects 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
  14. 14. Using the Dependency Manager Import-Package = org.apache.felix.dm;version="[3.0,4)" 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 { } }
  15. 15. Basic Use Cases • Declare a component HelloWorld helloWorld = new HelloWorld(); manager.add(createComponent() ! ! ! • Declare it lazily .setImplementation(helloWorld) ); manager.add(createComponent() .setImplementation(HelloWorld.class) );
  16. 16. Component Life Cycle •methods of the component public static class HelloWorldLifeCycle { ! ! ! • setCallbacks(“init”, “start”, ...) setCallbacks(inst, “init”, “start”, ...) 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"); } } – to invoke the methods on ‘inst’ • ComponentStateListener – if you want to listen from the outside
  17. 17. 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)
  18. 18. Declaring Dependencies • Adding a dependency manager.add(createComponent() ! ! ! • Injects dependency .setImplementation(HelloWorldLogger.class) .add(createServiceDependency() .setService(LogService.class) ) ); – uses null object pattern – injects other “OSGi” instances • setCallbacks(...) setCallbacks(inst, ...)
  19. 19. Dependencies
  20. 20. Dependencies • Different types: – Service Dependencies – Configuration Dependencies – Bundle Dependencies – Resource Dependencies
  21. 21. Configuration Dependencies • Based on Configuration Admin – designed for required dependencies – service.pid to identify the configuration – allows you to only accept *valid* configurations •update() throws ConfigurationException
  22. 22. Bundle Dependencies • Depend on a bundle: – in certain states – with certain manifest entries • Bundle instance can be injected
  23. 23. 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
  24. 24. Design Patterns
  25. 25. 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
  26. 26. Overview of Design Patterns •Whiteboard Pattern • Null Object Pattern • “Singleton” Service • Aspect Service • Adapter Service • Resource Adapter Service
  27. 27. Whiteboard Pattern “don’t call us... we’ll call you”
  28. 28. Null Object Pattern • An object that implements a certain interface, can be safely invoked and does nothing
  29. 29. “Singleton” Service • Publishes a component as a service • Ties its life cycle to that of its dependencies Document Repository get() getVersions() get(version) store(Document) Singleton Storage requires
  30. 30. Aspect Service •Works at the service level • “intercepts” certain services • can be chained based on rankings • completely dynamic Repository Cache get() getVersions() get(version) store(Document) Aspect Repository get() getVersions() get(version) store(Document) intercepts
  31. 31. Adapter Service •Works on existing services • Adapts them, publishes a different service for each existing one Repository Cache Manageable getCacheHits() setSize() setTTL() flush() Adapter Repository Cache get() getVersions() get(version) store(Document) adapts
  32. 32. Resource Adapter Service • Adapts resources (instead of services) • Allows you to expose the behavior of certain resources instead of their “inner guts” Audio Track play() pause() stop() Resource Adapter MP3 File adapts
  33. 33. Custom Dependencies
  34. 34. Custom Dependencies • Dependency Manager is extensible with custom dependencies: – depend on time of day – depend on some custom instance / condition (start level, app state)
  35. 35. Add-ons
  36. 36. Add-ons • Shell (Felix, Equinox, Gogo) • Annotation based (Java 5 required) • Legacy support (2.x API adapter)
  37. 37. Wrap-up
  38. 38. 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 flexible
  39. 39. 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

    Be the first to comment

    Login to see the comments

  • pbrada

    Oct. 1, 2014

Managing dependencies in OSGi using the Apache Felix Dependency Manager gives you some powerful tools for expressing and managing all kinds of dependencies between components. This presentation shows you how to use them, and explains what design patterns are available for building applications. It also shows how to extend this system with custom types of dependencies.

Views

Total views

916

On Slideshare

0

From embeds

0

Number of embeds

3

Actions

Downloads

8

Shares

0

Comments

0

Likes

1

×