building modular software with osgi - ulf fildebrandt

53
Building Modular Software with OSGi Ulf Fildebrandt

Upload: mfrancis

Post on 05-Dec-2014

1.678 views

Category:

Technology


2 download

DESCRIPTION

OSGi Community Event 2013 (http://www.osgi.org/CommunityEvent2013/Schedule) ABSTRACT

TRANSCRIPT

Page 1: Building modular software with OSGi - Ulf Fildebrandt

Building Modular Software withOSGi

Ulf Fildebrandt

Page 2: Building modular software with OSGi - Ulf Fildebrandt

Some words before

This presentation should…

• …give some insights in big software products

• …and how the structure of these products ismanaged

Page 3: Building modular software with OSGi - Ulf Fildebrandt

About the presenter

• Ulf Fildebrandt• Studied computer science• Works for SAP since 1998• Starting with C++ and COM• Development in Java for Java EE server and Eclipse

(even Eclipse 2.x)• Product architect of SAP Netweaver Composition

Environment (Java stack)• Program architect of SAP Netweaver 7.30• Program architect of Integration on Java on demand

stack

Page 4: Building modular software with OSGi - Ulf Fildebrandt

Agenda

Problem statement

Goals

Implementation of Modularity

Measurement ofModularity

Page 5: Building modular software with OSGi - Ulf Fildebrandt

PROBLEM STATEMENT

Findbugs Analysis by Structure 101

SAP Netweaver Structure

„In every phenomenon the beginning remainsalways the most notable moment.“

Thomas Carlyle

Page 6: Building modular software with OSGi - Ulf Fildebrandt

Findbugs Analysis

• Famous analysis done by Ian Sutton

• Can be found at http://structure101.com/blog/2008/11/software-erosion-findbugs/

• Used Structure 101 (see also JAX 2012 innovation award for Restructure 101)

Page 7: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 0.7.2

Page 8: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 0.8.6

Page 9: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 0.8.7

Page 10: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 1.0.0

Page 11: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 1.3.0

Page 12: Building modular software with OSGi - Ulf Fildebrandt

Findbugs 1.3.5

Page 13: Building modular software with OSGi - Ulf Fildebrandt

Server Analysis

• SAP Java server’s own analysis tool

• It shows the structure of the current Netweaver server

• In contrast to Structure 101 it does not only show Java dependencies (packages) but also:

– Usage Types

– Software Components

– Packages

Page 14: Building modular software with OSGi - Ulf Fildebrandt

Structure of Complex System – IOverview of product

http://pwdfm295.wdf.sap.corp:9090/makeresults/ce.lc.reporting/NW730EXT_SP_COR/gen/dbg/java/packaged/full/_doc_pkg/content/index.html

Page 15: Building modular software with OSGi - Ulf Fildebrandt

Structure of a Complex System – IIBasic Scenario

Page 16: Building modular software with OSGi - Ulf Fildebrandt

Structure of a Complex System – IIIDetailed dependencies

Page 17: Building modular software with OSGi - Ulf Fildebrandt

GOALS

Substitutability

Extensibility

„...it is almost always incorrect to begin thedecomposition of a system into modules on thebasis of a flowchart. We propose instead thatone begins with a list of difficult designdecisions or design decisions which are likely to change. Each module is then designed to hidesuch a decision from the others.“

David Parnas

Page 18: Building modular software with OSGi - Ulf Fildebrandt

Software Development GoalsModules

A module is a self-contained component of a system, often interchangeable, whichhas a well-defined interface to the other components.

http://en.wiktionary.org/wiki/module

Manage complexity of softwaresystems by „divide and conquer“

(seehttp://firstround.com/article/The-one-cost-engineers-and-product-managers-dont-consider#)

Page 19: Building modular software with OSGi - Ulf Fildebrandt

Software Development GoalsPrinciples

Extensibility Substitutability

A module is a self-contained component of a system, often interchangeable, whichhas a well-defined interface to the other components.

http://en.wiktionary.org/wiki/module

Page 20: Building modular software with OSGi - Ulf Fildebrandt

Software Development GoalsOne Product

One system can evolve over time and modules can be replaced without impacting the other modules.

Page 21: Building modular software with OSGi - Ulf Fildebrandt

Software Development GoalsMultiple Products

Multiple systems can be assembled in different products and do not have to be implemented again and again.

Product 1 Product 2

Common modules

Page 22: Building modular software with OSGi - Ulf Fildebrandt

Demo

Page 23: Building modular software with OSGi - Ulf Fildebrandt

MODULARITY BY INTENTIONProcess in development

„Programming without an overall architectureor design in mind is like exploring a cave withonly a flashlight: You don't know where you'vebeen, you don't know where you're going, and you don't know quite where you are.“

Danny Thorpe

Page 24: Building modular software with OSGi - Ulf Fildebrandt

Process to define modular software

Select modular runtime

Definition ofmodules

Decoupling•SOLID•Design Pattern

Active definitionof system

Measurement ofmetrics

Page 25: Building modular software with OSGi - Ulf Fildebrandt

BASIC MODULARITY WITH OSGI

Packages

Services

Lifecycle

Select modular runtime

Definition of modules

Decoupling•SOLID•Design Pattern

Activedefinition of

system

Measurementof metrics

Page 26: Building modular software with OSGi - Ulf Fildebrandt

Package Dependencies of a Bundle

(internal packages)Bundle

ExportedPackage

ExportedPackage

Bundle(internal packages)

ImportedPackage

• Bundle exports onlydefined packages

• Packages can beimported from otherarchives (bundles)

• All other packages areonly visible inside thebundle

ImportedPackage

Page 27: Building modular software with OSGi - Ulf Fildebrandt

Lifecycle of a Bundle

installed

resolved

uninstalled

starting

active

stopping

start

stop

install

resolve

uninstall

uninstall

Page 28: Building modular software with OSGi - Ulf Fildebrandt

How to achieve modularity...

• OSGi is a framework on top of Java

• Complements Java very good

• ...but not suffient, because complex systemrequire higher-level structure

OSGi subsystem specification

Page 29: Building modular software with OSGi - Ulf Fildebrandt

Subsystems

http://coderthoughts.blogspot.de/2013/04/osgi-subsystems.html

• Subsystems bundle manyOSGi bundles

• Defined in OSGi R5 Enterprise Specification

• Reference implementation in Apache Aries (http://apache.aries.org)

Page 30: Building modular software with OSGi - Ulf Fildebrandt

PRINCIPLES AND PATTERNS- DESIGN PATTERN (GOF)

Facade

Factory

Select modular runtime

Definition of modules

Decoupling•SOLID•Design Pattern

Activedefinition of

system

Measurementof metrics

Page 31: Building modular software with OSGi - Ulf Fildebrandt

Factory – I

• A factory externalizes thecreation of objects

• Creation and usage of objects is decoupled

• Pattern definition works forcoding

With factory:IDataAggregator aggregator =

DataAggregatorFactory.getInstance();

Without factory:DataAggregator da = new

DataAggregator();

A factory is an object for creating other objects. It is an abstraction of a constructor.

http://en.wikipedia.org/wiki/Factory_(software_concept)

Page 32: Building modular software with OSGi - Ulf Fildebrandt

Factory – II

Apply on component level

• Externalization of creation of objects is a general pattern

How can OSGi help:

• Service registry provides an abstraction between providerand consumer of a service (object instance)

• Creation of instances of a service can be handled byframeworks:– Declarative services

– Blueprint

Page 33: Building modular software with OSGi - Ulf Fildebrandt

Facade – IA facade is an object that provides a simplified interface to a larger body of code, such as a class library.

http://en.wikipedia.org/wiki/Facade_pattern

Facade:public interface IDataAggregator {

public List<IDataItem> get();

}

Implementation of facade:final class IdentityDataAggregator extends DataAggregator {

@Override

public ArrayList<IDataItem> get() {

List<IDataItem> itemList = new ArrayList<IDataItem>();

...

return itemList;

}

}

Page 34: Building modular software with OSGi - Ulf Fildebrandt

Facade – II

Apply on component level

• Access to a component as a general pattern

How can OSGi help:

1. Services are Java interfaces1. Service implementations are accessed via interface = facade

2. Exported packages are the only external visible entities of a bundle1. Not exported packages are not accessible

2. Clear definition of a facade of the bundle

Page 35: Building modular software with OSGi - Ulf Fildebrandt

PRINCIPLES AND PATTERNS- SOLID

Dependency Injection (SOLID)

Liskov (SOLID)

Select modular runtime

Definition of modules

Decoupling•SOLID•Design Pattern

Activedefinition of

system

Measurementof metrics

Page 36: Building modular software with OSGi - Ulf Fildebrandt

Dependency Injection – IDependency injection involves at least three elements:

• a dependent consumer,• a declaration of a component's dependencies, defined as interface

contracts,• an injector (sometimes referred to as a provider or container) that creates

instances of classes that implement a given dependency interface on request.

The dependent object describes what software component it depends on to do itswork. The injector decides what concrete classes satisfy the requirements of thedependent object, and provides them to the dependent.

http://en.wikipedia.org/wiki/Dependency_injection

Page 37: Building modular software with OSGi - Ulf Fildebrandt

Dependency Injection – II

pro

per

tym

eth

od

Class 1 Class 2

met

ho

d

pro

per

tym

eth

od

met

ho

d

pro

per

tym

eth

od

met

ho

d

pro

per

tym

eth

od

met

ho

d

Page 38: Building modular software with OSGi - Ulf Fildebrandt

Dependency Injection – III

Apply on component level

• Reverse the dependency is a general pattern

How can OSGi help:

• Service registry provides an abstraction between providerand consumer of a service (object instance)

• Injecting dependencies can be handled by additional frameworks– Declarative services

– Blueprint

Page 39: Building modular software with OSGi - Ulf Fildebrandt

LiskovLet q(x) be a property provable about objects x of type T. Then q(y) should beprovable for objects y of type S where S is a subtype of T.

http://en.wikipedia.org/wiki/Liskov_substitution_principle

Facade = T:public interface IDataAggregator {

public List<IDataItem> get();

}

Implementation of facade = S:final class IdentityDataAggregator extends DataAggregator {

@Override

public ArrayList<IDataItem> get() {

List<IDataItem> itemList = new ArrayList<IDataItem>();

...

return itemList;

}

}

Page 40: Building modular software with OSGi - Ulf Fildebrandt

SYSTEM ARCHITECTUREArchitecture Layer

Select modular runtime

Definition of modules

Decoupling•SOLID•Design Pattern

Activedefinition of

system

Measurementof metrics

Page 41: Building modular software with OSGi - Ulf Fildebrandt

Architecture Layer – I

System

Platform

System Layer

Sub system …

Module

Package

• A platform (in terms of runtime framework) hosts a system and is a versioned artifact

• A system consists of a set of sub-systems and is a versioned artifact running on a platform

• A system layer groups sub-systems providing structure and direction in terms of dependencies and is a layer which has no associated versioned artifact

• A system layer incarnation is the bill of materials or assembled sub-systems of a system layer relevant for a specific use case

• A sub-system consists of a set of modules and is a versioned artifact

• A module contains packages and is a versioned artifact

• A package contains components

– A component is a File

– A file is of type resource or source

Page 42: Building modular software with OSGi - Ulf Fildebrandt

Architecture Layer (as is) – II

System Layer

Package analysis of web application

Uses Structure 101 (see also JAX 2012 innovation award for Restructure 101)

Module = Subsystem = Bundle

Pac

kage

Page 43: Building modular software with OSGi - Ulf Fildebrandt

Architecture Layer (to be) – III

Data Source Interface

Data Aggregator Interface

Data Provider

Aggregator

UIData Display

(Servlet)

Data Source 1 Data Source 2

DataAggregator 1

DataAggregator 2

Data AggregatorInterface

Data SourceInterface

Page 44: Building modular software with OSGi - Ulf Fildebrandt

Architecture Layer – IV ConQAT (to-be diagram)

• ConQAT (https://www.conqat.org)

• Compared to Structure 101: extensible because of open source

– Used to implement modularity metrics

• Easy integration in build process (Maven) automatic check of to-be and as-is

Page 47: Building modular software with OSGi - Ulf Fildebrandt

MEASUREMENT OF MODULAR SOFTWARE

ConQAT structure check

Metrics

Select modular runtime

Definition of modules

Decoupling•SOLID•Design Pattern

Activedefinition of

system

Measurementof metrics

Page 49: Building modular software with OSGi - Ulf Fildebrandt

Modularity Metrics – I

Coupling: determines the coupling of this component to other components and is computed based on instability (Robert C. Martin).

– I = Ce / (Ca+Ce) 1..0 (Ce (efferent) = outgoing, Ca (afferent) = ingoing)

– Consequence: the more components THIS component depends upon, the more instable it is (0= stable, 1=instable)

(Internal Packages) Bundle

ExportedPackage

Ca = usages of exported packages

Ce = imported classes in packages

ExportedPackage

ImportedPackage

ImportedPackage

Page 50: Building modular software with OSGi - Ulf Fildebrandt

Modularity Metrics – II

Relational Cohesion: a component should consist of cohesive elements, otherwise, it should be splitted. Average number of internal relationships per type.

– rc = Td / T (Td.. type relationships that are internal to this component; T.. number of types within the component)As classes inside an assembly should be strongly related, the cohesion should be high. On the other hand, too high values may indicate over-coupling. A good range for RelationalCohesion is 1.5 to 4.0. Assemblies where RelationalCohesion < 1.5 or RelationalCohesion > 4.0 might be problematic. Therefore rcI is the normalized RC, having value 1 for 1.5 to 4, decreasing to 0 outside this range based on gaussian bell curve.

Page 51: Building modular software with OSGi - Ulf Fildebrandt

Modularity Metrics – III

Encapsulation: Components should encapsulate knowledge and offer a slim interface.

– ep = pt / T (pt = private Types, T = all types) pt = types in internal packages, T = types in internal packages +

types in exported packages

The Overall Modularity Score is now defined as:– M = ((1-I) + rcI + ep) / 3.0

(Internal Packages) Bundle

ExportedPackage

ExportedPackage

ImportedPackage

ImportedPackage

Page 52: Building modular software with OSGi - Ulf Fildebrandt

SUMMARY

Page 53: Building modular software with OSGi - Ulf Fildebrandt

Key take-aways

• Big software systems have to be structured, but more high-level than packages– Concrete implementation: OSGi sub systems

• Use iterative process to structure software during the wholelifecycle of the product

• Implementation best practices support modularity– Facade

– Factory

– Dependency Injection

• Modularity can be measured by metrics