34
Seite 1 CS5233 Components – Models and Engineering - Komponententechnologien - Master of Science (Informatik) Th Letschert Reflections on Containers

CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 1

CS5233Components – Models and Engineering- Komponententechnologien - Master of Science (Informatik)

Th Letschert

Reflections on Containers

Page 2: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 2

Container

Container

– Simple Version of a component system

– The whole and the parts: The whole: Container and deployed Applications The parts (components): the applications

– Dependencies / Interactions The container

controls the life-cycle of the applicationsprovides common services and resources

Applicationsuse resources provided by the container

– Interfaces Applications expose an interface to the container

through which the container may call methods The container passes a “context object”

i.e. a “handle” of itself to the applicationthrough which the application can access the container's resources

Page 3: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 3

Container

Th Letschert

Asymmetric nature of the interface container – component

– Container ~> component : call interface The component is under control of the containerContainer calls component

– Component ~> container: Components can not call the container directlyThey have to get hold of the resources (parts of the container) in some other way

class MyComponent implements Component{ @Override public start () {...} // life-cycle control method ... }

container calls components, components have no direct way to access the container

Page 4: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 4

Container

Th Letschert

Inversion of Control / Dependency Injection

– IoC: Inversion of Control (Hollywood principle) Helper / library-element / framework calls application

– IoC Dependencies

Access a resource that is unknown to its useraccess a resource of someone that calls me. E.g. a component has to access resource of an “IoC-framework / container”

Dependency HandleDependent objects get a handle to access resources of someone they do not know

Typically the handle is passed to a life-cycle method of the component

Handle: Object that implements an interface, may be a factory

Examples OSGi: BundleContext Servlet technology: SevletContext

Dependency InjectionDependent objects get – by “injection” – hold of (framework) resources

Page 5: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 5

Container

Th Letschert

Inversion of Control / Dependency Injection

– Resolving IoC Dependency via HandleExamples

class MyComponent implements Component{ @Override public start (Cxt cxt) { Resource resource; resource = ctx.getResource(); //getter resource = ctx.create(); //factory ctx.setResource(resource); //setter resource = ctx.locate('Resource'); //lookup ... } ... }

different ways to solve a IoC-dependency via handle

Page 6: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 6

Container

Th Letschert

Inversion of Control / Dependency Injection

– Resolving IoC Dependency via injectioninject object into the code that can not access it directly

– Different ways to injectSetter callsReflection

controlled by configuration files and / or annotations

Page 7: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 7

Container

Th Letschert

Different ways to inject

Setter call / example

class MyComponent implements Component{ RessourceInterface resource; ... @Override public setResource (RessourceInterface resource) { this.resource = resource; } ... }

Page 8: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 8

Container

Th Letschert

Different ways to inject

Reflectioncontrolled by configuration files and / or annotations

class MyComponent implements Component{ RessourceInterface xyResource;}

xyResource → someThingSomewhere

class MyComponent implements Component{ @Resource('someThingSomeWhere') RessourceInterface resource;}

+

class MyComponent implements Component{ @Resource() RessourceInterface xyResource;}

xyResource → someThingSomewhere+

source codeconfig. file

config. filesource code

source code

Container / frameworks inspects code and injects dependencies.

Page 9: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 9

Container

Th Letschert

Container – Where are we

– Run-time framework for classes of applications– Where the framework provides solutions / resources for common tasks– Example

Servlet container supports server-side part of Web-Applictions

Parse / build HTPP-Requests / ResponsesSession-ManagementThreadingetc.

components are Web-Applicationswhich have a sub-structure (servlets, HTML-pages, ...)

Homework 1? explain ?

Page 10: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 10

The Next Component System

Th Letschert

Container – Where can we go from here

Page 11: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 11

The Next Component System

Th Letschert

Container – Where can we go from here

Components interact

Page 12: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 12

The Next Component System

Th Letschert

Container – Where can we go from here

Components have a substructure

Page 13: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 13

The Next Component System

Th Letschert

Container – Where can we go from here

Components have a substructureand interact

Page 14: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 14

Run-time replaceable Components

Th Letschert

Useful component scenario I: Replace parts of running Application Container + components = application

with run-time replaceable parts (components)

Application typically has to provide continuous service (server, embedded system)

Container (“program-main”) manages installations and replacement of componentsof an application.

run-time

Page 15: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 15

Run-time replaceable Components

Th Letschert

Useful component scenario II: Plugins Sub-Scenario of I

Container + components = application

with run-time replaceable parts (components)

Components conform to a single interface

Application typically has to provide continuous service (server, embedded system)

Container (“program-main”) manages installations and replacement of plugins (components conforming to a unique interface that do not use the container).

run-time

Page 16: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 16

Wiring Components to applications

Th Letschert

Useful component scenario III: Development Components

– One application consists of several components

– Applications are build form components once (no run-time replacement)at link- / start- / or deployment-time

– container / framework“wires” components

pre run-time

Container (“program-builder / linker”) creates application from generic components bevore starting the application.

Page 17: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 17

Component Systems

Th Letschert

Basic components scenarios

– Container component : applicationwhole : container + all applicationsframework / container = controls life-cycle

– Plugin-Systemcomponent : part of an application conforming to an interface

that provides a service (is called by / does not call the “main program”)whole : application container / framework: organizes installation / replacement of plugins

– Runtime replaceable componentsgeneralization of plugin concept: application consists of “peer” componentsthat may be replaced at run-time

– Post-compile-time / Pre-run-time componentscomponent : generic (reusable) part of an applicationwhole : one applicationframework / container: wires components

Page 18: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 18

Component Systems

Th Letschert

Advanced Components scenarios

Basic Scenarios may be combined in an arbitrary waye.g:

a container may support deployment of applications applications consist of generic components components are wired to form an application components of a running application may be replaced at run-time

Page 19: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 19

Designing a Component System

Th Letschert

Example

Components are– processes / threads that– send and receive Messages through ports– Ports a typed (in message types)

Applications are– sets of components– that are wired– by connections between ports– and executed by starting all processes

Design a development and execution environment !

Page 20: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 20

Designing a Component System

Th Letschert

Life cycle

Components– are to be developed and compiled independently– a compile-time framework has to facilitate separate compilation

Applications– are build by instantiating components– type compatibility of ports has to be checked– communication channels have to be created– all processes have to be started

Page 21: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 21

Designing a Component System

Th Letschert

Life cycle => supporting environments

Components = Processes– are to be developed and compiled independently– a compile-time framework has to facilitate separate compilation

Applications = Process nets– are build by instantiating components– communication channels are created– all processes are started and executed

Development environment

? Run-time environment

Page 22: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 22

Designing a Component System

Th Letschert

Life cycle => supporting environments

Development of processes– needs library support e.g. interface of ports

Building process nets – are build by instantiating components– and creating channels– needs port and channel implementation

Running process nets – create threads– assign them to processes– execute all process– needs threading resources

Development environment

?

Run-time environment

Page 23: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 23

Designing a Component System

Th Letschert

Life cycle => supporting environments

Development of processes– needs developer-library e.g. interface of ports

Building process nets – are build by instantiating components– and creating channels– needs net-library (e.g. port and channel implementation)

Running process nets – create threads– assign them to processes– execute all process– needs run-time library (e.g. threading)

Development environment

Run-time environment

Page 24: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 24

Designing a Component System

Th Letschert

Life cycle => supporting environments

Developer library : used by developers of processes

Net library : used net builders (and their tools)

Run-time : used by the container running the application

processdevelopment

developer-lib.

netdevelopment

net-lib.

netexecution

net-run-time

supported by IDE

supported by net-builder

supported by container

interfaces classes objects

Page 25: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 25

Designing a Component System

Th Letschert

Design space / options

Execution environmentApplications run as stand-alone JVM-Processes

Applications are run by a container

ContainerGeneric without knowledge of Processes / Nets

Specialized on Net-Applications

Wiring of components (processes) to applications (nets) done

by the container by the development environment (a framework)

specified as source code (a net is a class) / “internal DSL” as configuration file (a net is an XML file) / “external DSL”

Pros and Cons ?

Other options ?

Page 26: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 26

Designing a Component System

Th Letschert

Design decision: Container-less solution

Execution environmentApplications are run as stand-alone JVM-Processes

Container no container (for the time being)

Wiring of components (processes) to applications (nets) done

by the development environment: using a “framework”

specified as source code (a net is a class) “internal DSL”

Page 27: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 27

Designing a Component System

Th Letschert

Container-less solution needs:

Developer libraryinterfaces used to compile process definitions

Net libraryused for building nets

contains channel-objects

a net-linker that wires process, net, and channel objects

.java

processdefinition

developer-lib.

net building

net-lib.

net execution

class

process class

net def. class

compile net linkage

runnable jar runnable jar

JVM

Page 28: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 28

Designing a Component System

Th Letschert

Container solution needs:

Developer libraryinterfaces used to compile process definitions

Net libraryused for building nets

Containerused for running nets

.java

processdefinition

developer-lib.

net building

net-lib.

net execution

class

process class

net def. class

compile

net linkage

runnable jarrunnable jar

JVM

net-lib.

JVM

net linkageandrun

When where and who links components to nets?

Page 29: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 29

Designing a Component System

Th Letschert

Example: Produces / Consumer

ComponentsProducer: produces values and sends them to an output port

Consumer: reads values form a set of input ports and consumes them

Netcreate two producers and one consumer and link them to a net

producer

producer

consumer

Page 30: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 30

Designing a Component System

Th Letschert

Example: Produces / Consumer

import network.api.OutPort;import network.api.ThreadedNode;

public class Producer implements ThreadedNode {

private int start;

private OutPort<Integer> out;

@Override public void work() throws InterruptedException { int v = start; while (v < 100) { out.send(v); v = v+1; } out.close(); }}

Producer Component

Start values will be set when a component is instantiated as part of a net.Orwhen the instantiated component is activated.

Port will instantiated when the net-library is used to create net-infrastructure.At compile time: Outport is a class visible to the compiler an on the classpath when loading this class.At deploy/link/? time: Outport is an interface and the implementation has to be provided later.

Lifecycle: – write and compile provide OutPort implementation now? – instantiate provide start value and/or OutPort implementation now? – activate provide start value and/or OutPort implementation now?

Page 31: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 31

Designing a Component System

Th Letschert

Example: Produces / Consumer

Consumer Component

InP will instantiated when the net-library is used to create net-infrastructure.

import network.api.MultiInPort;import network.api.ThreadedNode;

public class Consumer implements ThreadedNode {

private MultiInPort<Integer> inp;

@Override public void work() throws InterruptedException { for(Integer[] values : inp) { System.out.println("<" + values[0] + ", " + values[1] + ">"); } }}

Page 32: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 32

Designing a Component System

Th Letschert

Example: Produces / Consumer

ProducerConsumerNetwork (String initval) {

NODE p1 = Producer(1)

NODE p2 = Producer(Integer.parseInt(initval))

NODE c = Consumer();

Link p1.out -> c.inp(0) p2.out -> c.inp(1)

}

Network

Net definition in imaginary notation

Linguistic alternatives: – Configuration file (external DSL) – Real (Java) code (internal DSL) – Preprocessed real (Java) code code (perhaps instrumented with annotations) is preprocessed – at compile time (JSR269) – at run time (reflection)

Page 33: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 33

Designing a Component System

Th Letschert

Example: Produces / Consumer

import network.api.NODE;import network.api.Network;import network.api.PARAMETER;import network.api.PORT_FACTORY;import network.api.PortFactory;

public class ProducerConsumerNetwork implements Network {

@PARAMETER private String initval; @NODE private Producer p1; @NODE private Producer p2; @NODE private Consumer c; @PORT_FACTORY private PortFactory portFactory;

@Override public void build() { p1 = new Producer(portFactory, 1); p2 = new Producer(portFactory, Integer.parseInt(initval)); c = new Consumer(portFactory, 2); }

@Override public void link() { p1.getOut().asSourceOf(c.getInp().asSink(0)); p2.getOut().asSourceOf(c.getInp().asSink(1)); }}

Example Net definitionas some random (?) mixture of the linguistic options.

as internal DSL with instrumented code that has to be preprocessed at “link time”.

factory:Postpone linkage of port implementation

build / link:internal DSL code

initvalpreprcessed (refelection)

Page 34: CS5233 Components – Models and Engineeringhg51/Veranstaltungen/Komponenten-11/… · Inversion of Control / Dependency Injection – IoC: Inversion of Control (Hollywood principle)

Seite 34