Top Banner
How to Make Your Code “OSGi-Friendly”... Without Depending on OSGi! Neil Bartlett – Paremus Ltd
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

Sep 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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

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

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

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

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

...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

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

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

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

• 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

• 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

• 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

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

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

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

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

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

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

• 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

• 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

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

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

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

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

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

• 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

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

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

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

• 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

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

• 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

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

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

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

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

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

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

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

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

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

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

YANIC(You Are Not In Charge)