OSGiCreate *real* modular   Java applications- a brief introduction -     By Jeffrey Groneberg                @inkvine    ...
Table of Contents•    About me•    What is OSGi?•    Class Loader – Problems with “Twins”•    It’s all about bundles•    O...
About me
About me•  Working at SAP AG as a developer for mobile healthcare solutions   (Android & iOS)•  MSc Computer Science•  Jav...
What is OSGi?
What is it!?•  Actually OSGi is not a framework•  Open Service Gateway initiative•  OSGi is a specification (http://www.os...
Car example             Uses      ABS-Brakes                      System(Front)                        (v1)           Fall...
Ever used ECLIPSE?•  Eclipse is completely build on OSGi•  Eclipse is running within an OSGi container•  Every time you in...
Different Implementations •  A spec needs to be implemented. Therefore different implementations    are available on the m...
ClassloaderAnd Problems with twins
The Java Classloader approach (1/2)                         Bootstrap                        classloader  Delegates to par...
The Java Classloader approach (2/2)•  Bootstrap classloader:   all the java core libs that are located in $JAVA_HOME•  Ext...
JA A is stupid to identify twins              V                                                   Load order  A           ...
OSGi CLASSLOADER (1/2)•  “Non hierarchical” classloader•  Every component in OSGi is a bundle (JAR-file that contains   me...
OSGi CLASSLOADER (2/2)                                            Class not found                  Component              ...
It’S allabout bundles
AN osgi Bundle•  Physically it’s a JAR-File that contains:   Implementations & metadata•  Metadata (the MANIFEST.mf-File w...
EXAMPLE MANIFEST.MFBundle-Name: Toast EmergencyBundle-SymbolicName:org.equinoxosgi.toast.client.emergencyBundle-Version: 1...
Visibility•  Information Hiding    –  Hide classes used internally and expose classes for reusability    –  Encapsulate fu...
Dependencies•  Declare dependencies (What is needed for the bundle to be executed)•  Versions can be added easily (“I am r...
LIFE CYCLE AND STATES•  Every bundle has to be installed within the container•  It passes different states:    –  Validati...
OSGiArchitecture  & Layers
Layers (1/2)                                Services                            Service                            Registr...
Layers (2/2)•  Security    –  Adopts the Java Security Standard (optional)•  JVM    –  Different JREs can be supported the...
Services,  services,  servicesMicro SOA
What is a service?“A service is a normal Java object that is registered under one or moreJava interfaces with the service ...
SOA
OSGi SOA•  Create a bundle with the interface (the public API) of your service•  Export the package containing the interfa...
Two WAYS of service implementations•  Program the whole registration/referencing and state change listeners   by your own ...
Doing it the hard waythe service implementation
Doing it the hard way    The consumer
DOING it the hard way THE Service Tracker (1/2)
DOING it the hard way THE Service Tracker (2/2)
Downsides of the hard way•  Starting time is extended due to a lot of “management” code within the activator     –  Think ...
Buildingcomponents witH OSGI
CHOOSE WISELY     OSGi Alliance         with        SCR      “YOU SHALL NOT PASS! UAAH!!”                 and btw:        ...
THE SCR•    By the OSGi Alliance•    A component consumes and/or provides services•    Using the Service Component Runtime...
What is an OSGI SERVICE component?            Bundle                              Bundle         Component                ...
SCR by example THE provider•  A component implementing a given API (interface) and registering   itself as service        ...
SCR BY Example THE CONSUMER•  A component consuming a service by a Public API (interface)The consumer                     ...
SPRING DM•  From SpringSource•  A lot more than just a component declaration    –  SpringMVC    –  Spring Security    –  A...
Spring DM                    Bundle with Spring config           Bean                                      beans1.xml     ...
Spring DM Example The Provider       Bundle structure                                                               Public...
SPRING DM EXAMPLE THE CONSUMER                Consumer          OSGi services declaration             Beans declaration
Tools
What do i need (not all)?•    Eclipse (all out of the box)•    SpringSource ToolSuite (my favorite Eclipse distribution)• ...
How tolearn OSGi?
I want to OSGI“•    Buy the book „OSGi & Equinox“•    Install SpringSource ToolSuite•    Install the book‘s plugin•    Wor...
Great and what do I get from osgi              when programming non-java“?•    Think OSGi!•    Divide API and implementati...
Thanks!ANY QUESTIONS?
Disclaimer•  All photos/pictures within this slides are provided from flickr.com and   licenced under CC for commercial us...
Create *real* modular Java applications - a brief introduction -
Upcoming SlideShare
Loading in …5
×

Create *real* modular Java applications - a brief introduction -

910 views
867 views

Published on

Just a brief introduction I have given my team to get into the OSGi topic. Guess it is not the highest academic level, but should be enough to understand the fundamentals of OSGi.

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

No Downloads
Views
Total views
910
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
85
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Create *real* modular Java applications - a brief introduction -

  1. 1. OSGiCreate *real* modular Java applications- a brief introduction - By Jeffrey Groneberg @inkvine jeffrey.groneberg@gmail.com
  2. 2. Table of Contents•  About me•  What is OSGi?•  Class Loader – Problems with “Twins”•  It’s all about bundles•  OSGi architecture and layers•  Services, Services, Services – Micro-SOA•  Building components with OSGi –  SCR –  SpringDM•  Tools•  How to learn?
  3. 3. About me
  4. 4. About me•  Working at SAP AG as a developer for mobile healthcare solutions (Android & iOS)•  MSc Computer Science•  Java enthusiast•  API creator ;)•  Reading and interested in everything related to technology•  Twitter: @Inkvine•  Mail: jeffrey.groneberg@gmail.com
  5. 5. What is OSGi?
  6. 6. What is it!?•  Actually OSGi is not a framework•  Open Service Gateway initiative•  OSGi is a specification (http://www.osgi.org/Main/HomePage)•  Originally intended to be used within the Embedded-Device-Section (Cars, Facility, Home-Environment)•  Extremely lose coupled and highly extendable (Communication via services)•  Replace modules during runtime or extend (hard- & software)•  Extremely lightweight (limited resources on embedded devices)
  7. 7. Car example Uses ABS-Brakes System(Front) (v1) Fallback Uses ABS-Brakes System(BAck) (v2)
  8. 8. Ever used ECLIPSE?•  Eclipse is completely build on OSGi•  Eclipse is running within an OSGi container•  Every time you install a plugin via “Update/Install” an OSGi bundle is downloaded and installed•  OSGi is a specification therefore Eclipse uses a special implementation•  Container = Implementation running within the JVM•  OSGi = Java !•  Different implementations available (for different requirements)
  9. 9. Different Implementations •  A spec needs to be implemented. Therefore different implementations are available on the marketName Open Source DescriptionEquinox Yes Most spread OSGi container. Used in Eclipse and so called “reference implementation”Felix Yes Apache implementation, formerly known as “Oscar”Knoplerfish Yes Originally used within embedded systems now ported to “every day” usage.mBedded Server No Commercial implementation with official support and maintainance
  10. 10. ClassloaderAnd Problems with twins
  11. 11. The Java Classloader approach (1/2) Bootstrap classloader Delegates to parent Parent Extension classloader Delegates to parent Parent Application Classloader
  12. 12. The Java Classloader approach (2/2)•  Bootstrap classloader: all the java core libs that are located in $JAVA_HOME•  Extensions classloader: lib/ext directory•  Application classloader: all libs within the started application•  Every classloader asks his parent if he has already loaded the needed class. If he receives a “no” he has to take care of it by himself.•  No twins possible!
  13. 13. JA A is stupid to identify twins V Load order A D F B C E Jar 1 Jar 2 Jar 3Classes: First loaded – first seated!! Merged-  Different shape means different class-  Same shape means similar class (but different implementations) A D F B
  14. 14. OSGi CLASSLOADER (1/2)•  “Non hierarchical” classloader•  Every component in OSGi is a bundle (JAR-file that contains metadata)•  Every bundle has its own classloader (sandbox – no sight to outer world)•  Classloader asks OSGi container for references•  If restrictions (given by the metadata) are fine the class will be provided by another component and its classloader•  --> classloader chaining
  15. 15. OSGi CLASSLOADER (2/2) Class not found Component Component classloader classloader Classloader chaining (if allowed)Class loading Class resolution Class loading OSGi component OSGI component Classloader isolation
  16. 16. It’S allabout bundles
  17. 17. AN osgi Bundle•  Physically it’s a JAR-File that contains: Implementations & metadata•  Metadata (the MANIFEST.mf-File within the JAR) allows fine granularly definitions of (just the most important definitions): –  What packages (classes) are needed that the bundle is able to run –  What packages (classes) are visible (and therefore exposed) to other bundles within the OSGi container –  Which version of packages the bundle exposes –  Which version of packages the bundle needs to import•  Every bundle has to be installed within the OSGi container
  18. 18. EXAMPLE MANIFEST.MFBundle-Name: Toast EmergencyBundle-SymbolicName:org.equinoxosgi.toast.client.emergencyBundle-Version: 1.0.0.qualifierBundle-RequiredExecutionEnvironment: J2SE-1.4Bundle-Activator:org.equinoxosgi.toast.client.emergency.ActivatorImport-Package: org.equinoxosgi.toast.dev.airbag,org.equinoxosgi.toast.dev.gps, org.osgi.framework;
  19. 19. Visibility•  Information Hiding –  Hide classes used internally and expose classes for reusability –  Encapsulate functionalities by interfaces, but hide implementations•  Best practice –  One bundle for the public API that exposes the package containing the interfaces –  One bundle for the implementationExport-Package: org.equinoxosgi.toast.dev.airbag
  20. 20. Dependencies•  Declare dependencies (What is needed for the bundle to be executed)•  Versions can be added easily (“I am running on legacy code”)Import-Package: org.equinoxosgi.toast.dev.airbag,org.equinoxosgi.toast.dev.gps, org.osgi.framework;
  21. 21. LIFE CYCLE AND STATES•  Every bundle has to be installed within the container•  It passes different states: –  Validating dependencies –  Exposing packages –  Checking versions•  States are: –  installed –  resolved –  Uninstalled•  A bundle has callback methods to react to the loss of dependencies –  Programmer has to take care of it
  22. 22. OSGiArchitecture & Layers
  23. 23. Layers (1/2) Services Service Registry SecurityBundles Lifecycle Modules JVM Operating System
  24. 24. Layers (2/2)•  Security –  Adopts the Java Security Standard (optional)•  JVM –  Different JREs can be supported therefore a kind of representation needs to be provided within the OSGi container•  Modules –  All the bundles that are added to OSGi container•  Services Registry –  Functionalities can be provided within the OSGi container as services. The registry takes care of registering and exposing those services and notifies bundles that consumes those•  Services –  OSGi provides (based on the implementation) services out of the box (HTTP e.g.).•  Life Cycle Management
  25. 25. Services, services, servicesMicro SOA
  26. 26. What is a service?“A service is a normal Java object that is registered under one or moreJava interfaces with the service registry. Bundles can register services, search for them, or receive notifications when their registration state changes.”
  27. 27. SOA
  28. 28. OSGi SOA•  Create a bundle with the interface (the public API) of your service•  Export the package containing the interface•  Create a bundle with the implementation of the interface (import the public API package)•  Reference the OSGi Service Registry and deliver your implementation for the given interface•  Reference the Service Registry and ask for an implementation for a given interface•  The implementation is always hidden to the consumer (lose coupling)
  29. 29. Two WAYS of service implementations•  Program the whole registration/referencing and state change listeners by your own –  Think about it: Services can come and go or are never there. What shall your bundle do if this happens? –  A lot of code to write•  Use Inversion of Code and Dependency Injection –  SpringDM or Declarative Services (formerly known as SCR) (will be shown later)
  30. 30. Doing it the hard waythe service implementation
  31. 31. Doing it the hard way The consumer
  32. 32. DOING it the hard way THE Service Tracker (1/2)
  33. 33. DOING it the hard way THE Service Tracker (2/2)
  34. 34. Downsides of the hard way•  Starting time is extended due to a lot of “management” code within the activator –  Think about a lot of bundles where each bundle runs a lot of initializing code –  The application takes a lot of time to start•  Allocations during runtime –  Trackers or other “flags” needs to be initialized to handle services even though the service is never there•  Complexity•  As a programmer I do not want to take of all this stuff: –  Give me a service if it is there –  Take a service if I provide you one –  Declarations > Programming
  35. 35. Buildingcomponents witH OSGI
  36. 36. CHOOSE WISELY OSGi Alliance with SCR “YOU SHALL NOT PASS! UAAH!!” and btw: SpringSource“One does not simple build an iOS framework” with SpringDM
  37. 37. THE SCR•  By the OSGi Alliance•  A component consumes and/or provides services•  Using the Service Component Runtime•  SCR = Declarative Services•  SCR is a bundle that is installed within the OSGi container –  Extender Pattern –  Scans all bundles for metadata containing the component definition (declaration) –  Registers provided services from bundles in the OSGi Service Registry –  Automatically binds services to consumers
  38. 38. What is an OSGI SERVICE component? Bundle Bundle Component Component Component instance Component instance Component declaration Component declaration Provides Consumes Services
  39. 39. SCR by example THE provider•  A component implementing a given API (interface) and registering itself as service Bundle structure Public API component.xml
  40. 40. SCR BY Example THE CONSUMER•  A component consuming a service by a Public API (interface)The consumer component.xml
  41. 41. SPRING DM•  From SpringSource•  A lot more than just a component declaration –  SpringMVC –  Spring Security –  AOP –  Spring WS/RS –  Dependency Injection with Beans (most powerful feature)•  Not „component“, it is called „Bean“•  Using Extender Pattern•  Bundle within the OSGi container
  42. 42. Spring DM Bundle with Spring config Bean beans1.xml Searches for beans2.xml Bean uses Bean Spring container createsProvides Consumes Spring DM Extender Configures and creates Services
  43. 43. Spring DM Example The Provider Bundle structure Public API (interface)Implementation Beans declaration file OSGi service declaration
  44. 44. SPRING DM EXAMPLE THE CONSUMER Consumer OSGi services declaration Beans declaration
  45. 45. Tools
  46. 46. What do i need (not all)?•  Eclipse (all out of the box)•  SpringSource ToolSuite (my favorite Eclipse distribution)•  Maven (bundles with bundles need other bundles, that need bundles)•  SpringSource Enterprise Repository•  Virgo –  OSGi based application server•  Tomcat –  You need an OSGi Servlet Bridge to launch the OSGi Container within a web application•  LeanDI? !
  47. 47. How tolearn OSGi?
  48. 48. I want to OSGI“•  Buy the book „OSGi & Equinox“•  Install SpringSource ToolSuite•  Install the book‘s plugin•  Work through the examples•  Buy „OSGi in Action“ as a great reference•  Buy „SpringDM in Action“ –  Pure hardcore geek food –  If you understand everything you can call yourself „the shiat“
  49. 49. Great and what do I get from osgi when programming non-java“?•  Think OSGi!•  Divide API and implementation•  Hide implementations behind registries and services•  Highly reusable components (technical)•  Seperate domain from the rest of the application•  A LOT of OSGi paradigms are adopted to our EMR Android app and will „travel“ to iOS soon !
  50. 50. Thanks!ANY QUESTIONS?
  51. 51. Disclaimer•  All photos/pictures within this slides are provided from flickr.com and licenced under CC for commercial use.•  The code samples can be found in the books „OSGi & Equinox“ and „SpringDM in Action“•  If you want to use these slides in any lecture do contact me before, pls.

×