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.
How to Make Your Code
“OSGi-Friendly”...
Without Depending on
OSGi!
Neil Bartlett – Paremus Ltd
Target Audience
Java Library Author?
Want your library to be
widely used?
...by OSGi users and
non-OSGi users?
Ubuntu Bug #1
https://launchpad.net/ubuntu/+bug/1
OSGi Bug #1
Some Java developers are
still not using OSGi!
(yet)
Some Java developers are
creating libraries that violate
modular principals.
• Jigsaw will break lots of libraries too.
• Awesome!!!
• Developers finally stop blaming OSGi for
the failure of their own...
Class Loading
Classpath
Class Identity
=
Name + Loader
Class Identity
=
Name + Module
Module A
Module C
Module D
org.example.Foo
org.example.Foo
org.example.Foo
Module B
org.example.Foo
Library:
“Give me class
org.example.Foo”
OSGi:
“Duh, which one??”
Developer:
“OSGi was mean to
me, waaah!!”
ClassLoader (Ab)use
• Poor-man’s extension framework
• e.g. Log4J Appenders
• Mapping data to objects
• e.g. Hibernate
• L...
Class.forName
(String);
:-(
Class.forName
(String, boolean,
ClassLoader);
:-|
ClassLoader.
loadClass(String);
:-)
Thread.getContextClassLoa
der().loadClass(
String);
!@&$!!!£@%($@!!!
I really need to load
classes by name...
• Always allow clients to specify ClassLoader
• Can you take a java.lang.Class instead?
• Use your own ClassLoader and/or ...
• Classes can be used as a cheap factory.
• Example: JAX-RS resource registration.
Laziness/Lifecycle
• Classes suck as factories!
• Real factories can:
• Reuse from a pool
• Create with context
• etc
• Even Eclipse (pre 4.0...
ServiceLoader
Okay idea... shame
about the
implementation
Provide Services via
META-INF/services
Consume Services via
ServiceLoader.load
Module A
Module B
Module C
Module D
Module E
Scanned
IGNORED
Classpath Scanning
java.util
∴
Boot Class Loader
∴
Neither overridable
nor extensible
• OSGi R5 Enterprise: Service Loader
Mediator Spec.
• Bytecode munging of ServiceLoader.load()
invocations, at install tim...
• No lifecycle... what if service provider goes
away?
• Mediator forced to refresh the consumer
(new wiring, new class loa...
Suggestions
• Avoid calling ServiceLoader directly.
• Dependency Injection is your friend.
• Consider PojoSR / OSGi Connec...
Dynamics
Who wants to live
forever??
Lifetime of JVM
Lifetime of Library
Traditional Java App
OSGi App
Lifetime of JVM
Lifetime of Library
• Clean up those threads, open streams,
sockets, windows...
• Careful with Observable Pattern (memory
leaks).
• Die nicely...
Configuration
Whence to load your
Configuration?
Config File in
$HOME/.myjunk
• Application config now in 20 different
places!
• ... and in 20 different formats including
properties, XML, JSON...
• Wha...
System Properties
• Also known as GLOBALVARIABLES
• Not modular!
• Yeah I really want to type War & Peace on
the command line...
Suggestions
• Libraries need to fit into the application’s
config system...
• ... but they don’t know what that will be!
• S...
Recommendations
1. If you need an
Object, ask for an
Object, not a Class.
2. If you must control
the lifecycle, define a
factory interface.
(java.lang.Class is a crappy Factory!)
3. If you really need a
Class, let me pass a
java.lang.Class.
(a String is NOT enough!)
4. If you REALLY need a
class name, let me pass
you the ClassLoader.
(I know better than you do where to find
that class!)
5. Die nicely.
6. Support
programmatic
configuration.
(only use “well-known” config locations,
system props etc as a last resort)
Conclusion
KISS
(Keep It Simple, Stupid!)
YANIC
(You Are Not In Charge)
Upcoming SlideShare
Loading in …5
×

How to Make Your Code OSGi Friendly Without Depending on OSGi - Neil Bartlett

493 views

Published on

OSGi Community Event 2012

Published in: Technology
  • Be the first to comment

How to Make Your Code OSGi Friendly Without Depending on OSGi - Neil Bartlett

  1. 1. How to Make Your Code “OSGi-Friendly”... Without Depending on OSGi! Neil Bartlett – Paremus Ltd
  2. 2. Target Audience
  3. 3. Java Library Author?
  4. 4. Want your library to be widely used?
  5. 5. ...by OSGi users and non-OSGi users?
  6. 6. Ubuntu Bug #1 https://launchpad.net/ubuntu/+bug/1
  7. 7. OSGi Bug #1 Some Java developers are still not using OSGi! (yet)
  8. 8. Some Java developers are creating libraries that violate modular principals.
  9. 9. • Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty code?
  10. 10. Class Loading
  11. 11. Classpath
  12. 12. Class Identity = Name + Loader
  13. 13. Class Identity = Name + Module
  14. 14. Module A Module C Module D org.example.Foo org.example.Foo org.example.Foo Module B org.example.Foo
  15. 15. Library: “Give me class org.example.Foo”
  16. 16. OSGi: “Duh, which one??”
  17. 17. Developer: “OSGi was mean to me, waaah!!”
  18. 18. ClassLoader (Ab)use • Poor-man’s extension framework • e.g. Log4J Appenders • Mapping data to objects • e.g. Hibernate • Lazy initialization, lifecycle control • e.g. JAX-RS
  19. 19. Class.forName (String); :-(
  20. 20. Class.forName (String, boolean, ClassLoader); :-|
  21. 21. ClassLoader. loadClass(String); :-)
  22. 22. Thread.getContextClassLoa der().loadClass( String); !@&$!!!£@%($@!!!
  23. 23. I really need to load classes by name...
  24. 24. • Always allow clients to specify ClassLoader • Can you take a java.lang.Class instead? • Use your own ClassLoader and/or TCCL as last resort. • Document what you did!
  25. 25. • Classes can be used as a cheap factory. • Example: JAX-RS resource registration. Laziness/Lifecycle
  26. 26. • Classes suck as factories! • Real factories can: • Reuse from a pool • Create with context • etc • Even Eclipse (pre 4.0) got this wrong.
  27. 27. ServiceLoader
  28. 28. Okay idea... shame about the implementation
  29. 29. Provide Services via META-INF/services
  30. 30. Consume Services via ServiceLoader.load
  31. 31. Module A Module B Module C Module D Module E Scanned IGNORED Classpath Scanning
  32. 32. java.util ∴ Boot Class Loader ∴ Neither overridable nor extensible
  33. 33. • OSGi R5 Enterprise: Service Loader Mediator Spec. • Bytecode munging of ServiceLoader.load() invocations, at install time. • You do what you gotta do...
  34. 34. • No lifecycle... what if service provider goes away? • Mediator forced to refresh the consumer (new wiring, new class loader) • Hope the consumer doesn’t create any long-lived threads...
  35. 35. Suggestions • Avoid calling ServiceLoader directly. • Dependency Injection is your friend. • Consider PojoSR / OSGi Connect. • OSGi service registry without the module layer.
  36. 36. Dynamics
  37. 37. Who wants to live forever??
  38. 38. Lifetime of JVM Lifetime of Library Traditional Java App
  39. 39. OSGi App Lifetime of JVM Lifetime of Library
  40. 40. • Clean up those threads, open streams, sockets, windows... • Careful with Observable Pattern (memory leaks). • Die nicely when asked to.
  41. 41. Configuration
  42. 42. Whence to load your Configuration?
  43. 43. Config File in $HOME/.myjunk
  44. 44. • Application config now in 20 different places! • ... and in 20 different formats including properties, XML, JSON... • What if config needs to be dynamically changed? • What if there’s no filesystem??
  45. 45. System Properties
  46. 46. • Also known as GLOBALVARIABLES • Not modular! • Yeah I really want to type War & Peace on the command line...
  47. 47. Suggestions • Libraries need to fit into the application’s config system... • ... but they don’t know what that will be! • Support programmatic configuration.
  48. 48. Recommendations
  49. 49. 1. If you need an Object, ask for an Object, not a Class.
  50. 50. 2. If you must control the lifecycle, define a factory interface. (java.lang.Class is a crappy Factory!)
  51. 51. 3. If you really need a Class, let me pass a java.lang.Class. (a String is NOT enough!)
  52. 52. 4. If you REALLY need a class name, let me pass you the ClassLoader. (I know better than you do where to find that class!)
  53. 53. 5. Die nicely.
  54. 54. 6. Support programmatic configuration. (only use “well-known” config locations, system props etc as a last resort)
  55. 55. Conclusion
  56. 56. KISS (Keep It Simple, Stupid!)
  57. 57. YANIC (You Are Not In Charge)

×