create *real* modular java applications - a brief introduction -

52
OSGi Create *real* modular Java applications - a brief introduction - By Jeffrey Groneberg @inkvine [email protected]

Upload: jeffrey-groneberg

Post on 10-May-2015

777 views

Category:

Technology


4 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Create *real* modular Java applications - a brief introduction -

OSGi Create *real* modular

Java applications - a brief introduction -

By Jeffrey Groneberg @inkvine

[email protected]

Page 2: Create *real* modular Java applications - a brief introduction -

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?

Page 3: Create *real* modular Java applications - a brief introduction -

About

me

Page 4: Create *real* modular Java applications - a brief introduction -

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: [email protected]

Page 5: Create *real* modular Java applications - a brief introduction -

What is

OSGi?

Page 6: Create *real* modular Java applications - a brief introduction -

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)

Page 7: Create *real* modular Java applications - a brief introduction -

Car example

ABS-

System

(v1)

Brakes

(Front)

Brakes

(BAck)

ABS-

System

(v2)

Fallback

Uses

Uses

Page 8: Create *real* modular Java applications - a brief introduction -
Page 9: Create *real* modular Java applications - a brief introduction -

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)

Page 10: Create *real* modular Java applications - a brief introduction -

Different Implementations

•  A spec needs to be implemented. Therefore different implementations are available on the market

Name Open Source Description Equinox 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

Page 11: Create *real* modular Java applications - a brief introduction -

Classloader

And Problems

with twins

Page 12: Create *real* modular Java applications - a brief introduction -

The Java Classloader approach (1/2)

Application

Classloader

Extension

classloader

Bootstrap

classloader

Parent

Parent

Delegates to parent

Delegates to parent

Page 13: Create *real* modular Java applications - a brief introduction -

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!

Page 14: Create *real* modular Java applications - a brief introduction -

JAVA is stupid to identify twins

EF A

B

D

C

Load order

Jar 1 Jar 3 Jar 2

Merged

B

A DF

Classes: -  Different shape means different class -  Same shape means similar class

(but different implementations)

First loaded – first seated!!

Page 15: Create *real* modular Java applications - a brief introduction -

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

Page 16: Create *real* modular Java applications - a brief introduction -

OSGi CLASSLOADER (2/2)

Component

classloader

OSGi component

Component

classloader

OSGI component

Classloader isolation

Class resolution Class loading Class loading

Classloader chaining (if allowed)

Class not found

Page 17: Create *real* modular Java applications - a brief introduction -

It’S all

about bundles

Page 18: Create *real* modular Java applications - a brief introduction -

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

Page 19: Create *real* modular Java applications - a brief introduction -

EXAMPLE MANIFEST.MF

Bundle-Name: Toast Emergency Bundle-SymbolicName: org.equinoxosgi.toast.client.emergency Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: J2SE-1.4 Bundle-Activator: org.equinoxosgi.toast.client.emergency.Activator Import-Package: org.equinoxosgi.toast.dev.airbag, org.equinoxosgi.toast.dev.gps, org.osgi.framework;

Page 20: Create *real* modular Java applications - a brief introduction -

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 implementation

Export-Package: org.equinoxosgi.toast.dev.airbag

Page 21: Create *real* modular Java applications - a brief introduction -

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;

Page 22: Create *real* modular Java applications - a brief introduction -

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

Page 23: Create *real* modular Java applications - a brief introduction -

OSGi

Architecture

& Layers

Page 24: Create *real* modular Java applications - a brief introduction -

Layers (1/2)

Services

Modules

Service Registry

Lifecycle

Security

JVM

Operating System

Bundles

Page 25: Create *real* modular Java applications - a brief introduction -

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

Page 26: Create *real* modular Java applications - a brief introduction -

S e r v i c e s ,

s e r v i c e s ,

s e r v i c e s

Micro SOA

Page 27: Create *real* modular Java applications - a brief introduction -

What is a service?

“A service is a normal Java object that is registered under one or more Java interfaces with the service registry. Bundles can register services, search for them, or receive notifications when their registration state

changes.”

Page 28: Create *real* modular Java applications - a brief introduction -

SOA

Page 29: Create *real* modular Java applications - a brief introduction -

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)

Page 30: Create *real* modular Java applications - a brief introduction -

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)

Page 31: Create *real* modular Java applications - a brief introduction -

Doing it the hard way �

the service implementation

Page 32: Create *real* modular Java applications - a brief introduction -

Doing it the hard way �

The consumer

Page 33: Create *real* modular Java applications - a brief introduction -

DOING it the hard way � THE Service Tracker (1/2)

Page 34: Create *real* modular Java applications - a brief introduction -

DOING it the hard way � THE Service Tracker (2/2)

Page 35: Create *real* modular Java applications - a brief introduction -

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

Page 36: Create *real* modular Java applications - a brief introduction -

Building

components

witH OSGI

Page 37: Create *real* modular Java applications - a brief introduction -

CHOOSE WISELY

OSGi Alliance with

SCR

SpringSource with

SpringDM

“YOU SHALL NOT PASS! UAAH!!” and btw:

“One does not simple build an iOS framework”

Page 38: Create *real* modular Java applications - a brief introduction -

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

Page 39: Create *real* modular Java applications - a brief introduction -

What is an OSGI SERVICE component?

Services

Bundle

Component

Component instance

Component declaration

Bundle

Component

Component instance

Component declaration

Consumes Provides

Page 40: Create *real* modular Java applications - a brief introduction -

SCR by example � THE provider

•  A component implementing a given API (interface) and registering itself as service

Bundle structure Public API

component.xml

Page 41: Create *real* modular Java applications - a brief introduction -

SCR BY Example � THE CONSUMER

•  A component consuming a service by a Public API (interface)

The consumer

component.xml

Page 42: Create *real* modular Java applications - a brief introduction -

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

Page 43: Create *real* modular Java applications - a brief introduction -

Spring DM

Bundle with Spring config

Services

Spring DM Extender

Spring container

beans2.xml

beans1.xml Bean

Bean

Bean

Searches for

Configures and creates

Consumes Provides

uses

creates

Page 44: Create *real* modular Java applications - a brief introduction -

Spring DM Example � The Provider

Bundle structure Public API (interface)

Implementation

Beans declaration file

OSGi service declaration

Page 45: Create *real* modular Java applications - a brief introduction -

SPRING DM EXAMPLE � THE CONSUMER

Consumer

Beans declaration

OSGi services declaration

Page 46: Create *real* modular Java applications - a brief introduction -

Tools

Page 47: Create *real* modular Java applications - a brief introduction -

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? !

Page 48: Create *real* modular Java applications - a brief introduction -

How to

learn OSGi?

Page 49: Create *real* modular Java applications - a brief introduction -

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“

Page 50: Create *real* modular Java applications - a brief introduction -

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 !

Page 51: Create *real* modular Java applications - a brief introduction -

Thanks!

ANY QUESTIONS?

Page 52: Create *real* modular Java applications - a brief introduction -

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.