Dynamic Guice Applications

2,352 views
2,236 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
2,352
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dynamic Guice Applications

  1. 1. Dynamic Applications
  2. 2. Why ? ● Standard: reference implementation for JSR 330 ● Flexible: can map all sorts of metadata to bindings ● Type-safe: detailed messages when things go wrong ● Modular: multiple extensions available, OSGi-ready
  3. 3. ● Fluent Java binding API ● Records generic information lost during erasure ● Can be driven by Java / annotations / XML / ...etc...
  4. 4. JSR 330 ● New standard for Java dependency injection // Constructor injection // Field injection // Setter injection
  5. 5. Extending JSR 330 ● JSR 330 tells us how to mark dependencies ● and qualify them (just like with Plexus hints) ● But it does not say how to mark components
  6. 6. Identifying JSR 330 components ● Wrap the Class-Path up as a ClassSpace and scan it ● Look for classes with qualifiers such as @Named ● Empty @Named means "use class name" instead ● Binding type found by analysing class hierarchy
  7. 7. What is a dynamic application? ● OSGi lets us dynamically add / remove bundles ● ... without needing to restart the application ● This may add / remove qualified components ● ... but Guice bindings are static
  8. 8. Dynamic application vs. static bindings ● How can we resolve this mismatch? ● Need support for dynamic component collections!
  9. 9. Injector Graph
  10. 10. Bean Locator
  11. 11. Dynamic Collections ● Backed by dynamic Iterable of qualified beans ● Iterables updated as injectors come and go ● Copy-on-iteration avoids synchronization overhead ● Weak-ref detects when Iterable not used anymore
  12. 12. Qualified Dependencies ● Qualified Map ● Qualified Instance
  13. 13. Hinted Dependencies ● Hinted Map ● Hinted List
  14. 14. Dynamic Updates ● Passive collections are not enough ● Sometimes need active notifications ● Should we force use of an explicit API? ● ... or use reflection to scan for methods?
  15. 15. Mediator Pattern Bean Locator add bean remove bean Mediator POJO
  16. 16. Mediator Benefits ● Watching application bean can remain a POJO ● Mediators detected just like any other component ● Created on-demand as bean instances created ● Removed when the watching bean is GC'd
  17. 17. Bootstrapping ● Just wrap your Class-Path as a ClassSpace ● ... install the QualifiedScannerModule ● and kick-start Guice injection as usual
  18. 18. Time for a Demonstration!
  19. 19. What about Peaberry? ● Majority of plugin bindings tied to its lifecycle ● Can simply create one injector per plugin ● For plugin services that are not tied to the lifecycle we need something more dynamic! ● Proxies backed by the OSGi Service Registry
  20. 20. Links JSR 330 http://code.google.com/p/atinject/ Guice http://code.google.com/p/google-guice/ Peaberry http://code.google.com/p/peaberry/ Spice-Inject http://svn.sonatype.org/spice/trunk/spice-inject OSGi http://www.osgi.org/About/HowOSGi Blog Updates http://www.sonatype.com/people/author/mcculls/
  21. 21. Questions?
  22. 22. Transfo rm ing th e w ay so ftw are is m ad e .

×