how to make your code “osgi-friendly”...•jigsaw will break lots of libraries too. •...
TRANSCRIPT
![Page 1: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/1.jpg)
How to Make Your Code “OSGi-Friendly”...
Without Depending on OSGi!
Neil Bartlett – Paremus Ltd
![Page 2: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/2.jpg)
Target Audience
![Page 3: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/3.jpg)
Java Library Author?
![Page 4: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/4.jpg)
Want your library to be widely used?
![Page 5: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/5.jpg)
...by OSGi users and non-OSGi users?
![Page 6: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/6.jpg)
Ubuntu Bug #1
https://launchpad.net/ubuntu/+bug/1
![Page 7: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/7.jpg)
OSGi Bug #1
Some Java developers are still not using OSGi!
(yet)
![Page 8: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/8.jpg)
Some Java developers are creating libraries that violate
modular principals.
![Page 9: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/9.jpg)
• Jigsaw will break lots of libraries too.
• Awesome!!!
• Developers finally stop blaming OSGi for the failure of their own shitty code?
![Page 10: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/10.jpg)
Class Loading
![Page 11: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/11.jpg)
Classpath
![Page 12: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/12.jpg)
Class Identity=
Name + Loader
![Page 13: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/13.jpg)
Class Identity=
Name + Module
![Page 14: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/14.jpg)
Module A
Module C
Module D
org.example.Foo
org.example.Foo
org.example.Foo
Module B
org.example.Foo
![Page 15: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/15.jpg)
Library:
“Give me class org.example.Foo”
![Page 16: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/16.jpg)
OSGi:
“Duh, which one??”
![Page 17: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/17.jpg)
Developer:
“OSGi was mean to me, waaah!!”
![Page 18: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/18.jpg)
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
![Page 19: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/19.jpg)
Class.forName(String);
:-(
![Page 20: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/20.jpg)
Class.forName(String, boolean,
ClassLoader);
:-|
![Page 21: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/21.jpg)
ClassLoader.loadClass(String);
:-)
![Page 22: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/22.jpg)
Thread.getContextClassLoader().loadClass(
String);
!@&$!!!£@%($@!!!
![Page 23: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/23.jpg)
I really need to load classes by name...
![Page 24: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/24.jpg)
• 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!
![Page 25: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/25.jpg)
• Classes can be used as a cheap factory.
• Example: JAX-RS resource registration.
Laziness/Lifecycle
![Page 26: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/26.jpg)
• Classes suck as factories!
• Real factories can:
• Reuse from a pool
• Create with context
• etc
• Even Eclipse (pre 4.0) got this wrong.
![Page 27: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/27.jpg)
ServiceLoader
![Page 28: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/28.jpg)
Okay idea... shame about the
implementation
![Page 29: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/29.jpg)
Provide Services viaMETA-INF/services
![Page 30: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/30.jpg)
Consume Services viaServiceLoader.load
![Page 31: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/31.jpg)
Module A
Module B
Module C
Module D
Module E
Scanned
IGNORED
Classpath Scanning
![Page 32: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/32.jpg)
java.util∴
Boot Class Loader∴
Neither overridable nor extensible
![Page 33: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/33.jpg)
• OSGi R5 Enterprise: Service Loader Mediator Spec.
• Bytecode munging of ServiceLoader.load() invocations, at install time.
• You do what you gotta do...
![Page 34: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/34.jpg)
• 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...
![Page 35: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/35.jpg)
Suggestions
• Avoid calling ServiceLoader directly.
• Dependency Injection is your friend.
• Consider PojoSR / OSGi Connect.
• OSGi service registry without the module layer.
![Page 36: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/36.jpg)
Dynamics
![Page 37: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/37.jpg)
Who wants to live forever??
![Page 38: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/38.jpg)
Lifetime of JVM
Lifetime of Library
Traditional Java App
![Page 39: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/39.jpg)
OSGi App
Lifetime of JVM
Lifetime of Library
![Page 40: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/40.jpg)
• Clean up those threads, open streams, sockets, windows...
• Careful with Observable Pattern (memory leaks).
• Die nicely when asked to.
![Page 41: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/41.jpg)
Configuration
![Page 42: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/42.jpg)
Whence to load your Configuration?
![Page 43: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/43.jpg)
Config File in $HOME/.myjunk
![Page 44: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/44.jpg)
• 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??
![Page 45: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/45.jpg)
System Properties
![Page 46: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/46.jpg)
• Also known as GLOBAL VARIABLES
• Not modular!
• Yeah I really want to type War & Peace on the command line...
![Page 47: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/47.jpg)
Suggestions
• Libraries need to fit into the application’s config system...
• ... but they don’t know what that will be!
• Support programmatic configuration.
![Page 48: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/48.jpg)
Recommendations
![Page 49: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/49.jpg)
1. If you need an Object, ask for an
Object, not a Class.
![Page 50: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/50.jpg)
2. If you must control the lifecycle, define a
factory interface.(java.lang.Class is a crappy Factory!)
![Page 51: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/51.jpg)
3. If you really need a Class, let me pass a java.lang.Class.
(a String is NOT enough!)
![Page 52: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/52.jpg)
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!)
![Page 53: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/53.jpg)
5. Die nicely.
![Page 54: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/54.jpg)
6. Support programmatic configuration.
(only use “well-known” config locations, system props etc as a last resort)
![Page 55: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/55.jpg)
Conclusion
![Page 56: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/56.jpg)
KISS(Keep It Simple, Stupid!)
![Page 57: How to Make Your Code “OSGi-Friendly”...•Jigsaw will break lots of libraries too. • Awesome!!! • Developers finally stop blaming OSGi for the failure of their own shitty](https://reader036.vdocuments.net/reader036/viewer/2022071114/5feb81f4fe20e6272c16ddfb/html5/thumbnails/57.jpg)
YANIC(You Are Not In Charge)