OSGi Best and Worst Practices (pdf) - Martin Lippert
Transcript of OSGi Best and Worst Practices (pdf) - Martin Lippert
![Page 1: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/1.jpg)
OSGi Best and Worst Practices
Martin Lippert
![Page 2: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/2.jpg)
Context
• Client apps using: Swing, Hibernate, JDO, JDBC, JNI, SOAP, a lot of Apache
stuff, JUnit, FIT, Spring DM, Jetty, CICS-Adaptor, …• Server apps using:
JDO, Hibernate, SOAP, REST, Tomcat, Spring DM, CICS-Adaptor, HTTP, a lot of custom libs, Memcached
• Eclipse platforms and frameworks including: Equinox, IDE, RCP, p2 and various RT projects
• Educating and mentoring people in the real world
![Page 3: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/3.jpg)
Don’t program OSGi
![Page 4: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/4.jpg)
Program your application
• Use POJO• Keep your business logic clean• Programming practices to make gluing easy• Dependency injection to allow composition• Separation of concerns
• Benefits Delay packaging decisions Increased deployment flexibility
![Page 5: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/5.jpg)
Solutions composed of POJOs
• Bundle POJOs as needed• Glue together using
Use Declarative Services iPOJO BluePrint Services GuicePeaberry …
• Use insulating layers to keep OSGi out of your code
![Page 6: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/6.jpg)
Structure matters
![Page 7: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/7.jpg)
Dependencies
Managing dependencies within large systemsis one of the most critical success factors for healthy object-oriented business applications
![Page 8: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/8.jpg)
What kind of dependencies?
• Dependencies between: Individual classes and interfaces Packages Subsystems/Modules
• Dependencies of what kind? Uses Inherits Implements
![Page 9: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/9.jpg)
Don’t shoot the messenger
OSGi
Module Factory
Around here our policy is to shot the messenger!
![Page 10: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/10.jpg)
“Low coupling, high cohesion”Not just a nice idea
OSGi makes you thinkabout dependencies
It does not create them!
![Page 11: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/11.jpg)
Observations when using OSGi
• Design flaws and structural problems often have a limited scope Problems remain within single bundles No wide-spreading flaws
![Page 12: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/12.jpg)
Take just what you need
![Page 13: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/13.jpg)
Import-Package vs. Require-Bundle
• Require-Bundle Imports all packages of the bundle, including re-exported
bundle packages• Import-Package
Import just the package you need
![Page 14: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/14.jpg)
What does it mean?
• Require-Bundle Defines a dependency on the producer Broad scope of visibility
• Import-Package Defines a dependency on what you need Doesn't matter where it comes from!
![Page 15: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/15.jpg)
When to use what?
• Prefer using Import-Package Lighter coupling between bundles Less visibilities Eases refactoring
• Require-Bundle only when necessary: Higher coupling between bundles Use only for very specific situations:
split packages
![Page 16: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/16.jpg)
Version management
• Version number management is essential• Depending on a random version is pointless• Failing to manage version numbers undermines
consumers
• Import-Package package version management• Require-Bundle bundle version management
![Page 17: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/17.jpg)
Keep Things Private
![Page 18: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/18.jpg)
API
• API is a contract with between producer and consumer Prerequisites Function Consequences Durability
• Key to effective modularity
![Page 19: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/19.jpg)
Bundle API
• What should you export from a bundle?• The easy way:
Blindly export everything
• That is a really bad idea: No contract was defined Consumers have no guidance Broad visibility High coupling between components
![Page 20: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/20.jpg)
Producers: Think about your APIs
• Export only what consumers need Less is more Think about the API of a component API design is not easy
• Don’t export anything until there is a good reason for it Its cheap to change non-API code Its expensive to change API code
![Page 21: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/21.jpg)
Consumers: Think about what you’re doing
• Stay in bounds• If you can’t do something, perhaps
Use a different component Use the component differently Work with the producer to cover your use-case
![Page 22: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/22.jpg)
Informed Consent
![Page 23: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/23.jpg)
Composing
![Page 24: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/24.jpg)
Structuring Bundles
Just having bundles is not enough
You still need an architectural viewYou still need additional structures
![Page 25: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/25.jpg)
Your Bundles shouldn't end up like this
Go! Get some structure!
![Page 26: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/26.jpg)
Guidelines
• Bundle rules in the small Separate UI and core Separate client and server and common Separate service implementations and interfaces Isolate backend connectors
• Bundle rules in the mid-size Access to resources via services only Access to backend systems via services only Technology-free domain model
![Page 27: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/27.jpg)
Guidelines
• Bundle rules in the large Separate between domain features Separate between applications / deliverables Separate between platform and app-specific bundles
• Don’t be afraid of having a large number of bundles Mylyn Working Sets Platforms
![Page 28: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/28.jpg)
Dynamics
![Page 29: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/29.jpg)
Dynamics are hard
Its hard to build a really dynamic system,you need to change your mindset
Think about dependenciesThink about services
Think about everything as of being dynamic
![Page 30: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/30.jpg)
Dynamics are hard
It’s even harder to turn a static systeminto a dynamic one
![Page 31: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/31.jpg)
Integration
![Page 32: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/32.jpg)
Integration is easy
Integrating an OSGi system into anexisting environment is easy
OSGi runtimes are easy to start and to embedClear separation between inside and outside world
![Page 33: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/33.jpg)
Experiences
• Integrate existing rich client app into proprietary client container Ugly boot-classpath additions like XML parser stuff Self-implemented extension model using classloaders in a
strange way Used a large number of libs that where not necessarily
compatible with the existing rich client app
• Integration went smoothly just launch your OSGi framework and you are (mostly)
done
![Page 34: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/34.jpg)
Integration can be hard
• Using existing libraries can be hard Sometimes they do strange classloader stuff Start to love ClassNotFoundException, it will be your best
friend for some time
• The Context-Classloader hell Some libs are using context-classloader OSGi has no meaning for context-classloader Arbitrary problems
![Page 35: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/35.jpg)
Experiences
• We got every (!) library we wanted to use to work within our OSGi environment Rich-client on top of Equinox Server-app on Equinox Server-app embedded into Tomcat and Jetty using Servlet-
Bridge
• But it can cause some headaches at the beginning
![Page 36: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/36.jpg)
Conclusions
![Page 37: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/37.jpg)
Looking back
• Large OO systems grow over years• Its easy and fast to add/change features
• OSGi is a major reason…• But why?
![Page 38: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/38.jpg)
OSGi leds us to…
• Thinking about structure all the time Avoids mistakes early (before the ugly beast grows) Less and defined dependencies No broken windows
• Good separation of concerns• Dependency injection & pluggable architecture
Easy to add features without changing existing parts• Many small frameworks
Better than few overall ones
![Page 39: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/39.jpg)
Conclusions
Never without OSGi
You will love itYou will hate it
![Page 40: OSGi Best and Worst Practices (pdf) - Martin Lippert](https://reader030.fdocuments.in/reader030/viewer/2022032816/620614098c2f7b173004654b/html5/thumbnails/40.jpg)
In the end its your best friend