Understanding Framework Architecture using Eclipse

45
Eclipse as a framework of frameworks (Please view in presentation mode for desired flow of information) Anshu Jain IBM Research - India [email protected] anshunjain {gmail, twitter, googlesites)

description

Talk on Framework architectures given at SAP Labs India for Eclipse Day India 2011 - Code attached Here: https://sites.google.com/site/anshunjain/eclipse-presentations

Transcript of Understanding Framework Architecture using Eclipse

Page 1: Understanding Framework Architecture using Eclipse

Eclipse as a framework of frameworks (Please view in presentation mode for desired flow of information)

Anshu Jain

IBM Research - India

[email protected]

anshunjain

{gmail, twitter, googlesites)

Page 2: Understanding Framework Architecture using Eclipse

Introductions

Cheating ‘Basic’ Exams => Programming is for Clerks => Java is so easy => Eclipse is easier => Programming is for lazy bums like me

– First Job: Right here – IBM Research – The best research lab

http://sites.google.com/site/anshunjain

Your background

Page 3: Understanding Framework Architecture using Eclipse

“In theory, there is no difference between theory and practice. But, in practice, there is”

Jan La Van De Snepscheut

Page 4: Understanding Framework Architecture using Eclipse

Motivation

Move over hello world, shopping cart, ATM example From consumers to providers From provider to engine From servlets to servlets containers From eclipse plug-ins to eclipse platform From EJBs to EJB container From android apps to android platform

Also driving …back to OO..!! …and a bit of agility..!!

Nothing u don’t already know ..!!

Page 5: Understanding Framework Architecture using Eclipse

What are we doing today

Building a VERY simple calculator

Page 6: Understanding Framework Architecture using Eclipse

V1 - Monolithic Class

Page 7: Understanding Framework Architecture using Eclipse

addFunctions

private void addFunctions(Composite functionComposite) {

Button addButton = new Button(functionComposite, SWT.NONE);addButton.setText("+");addButton.addSelectionListener(new ButtonSelectionListener("+"));addButton.setLayoutData(data);

Button subtractButton = new Button(functionComposite, SWT.NONE);subtractButton.addSelectionListener(new ButtonSelectionListener("-"));subtractButton.setLayoutData(data);subtractButton.setText("-");

Button multiplyButton = new Button(functionComposite, SWT.NONE);multiplyButton.addSelectionListener(new ButtonSelectionListener("*"));multiplyButton.setLayoutData(data);multiplyButton.setText("*");

Button divideButton = new Button(functionComposite, SWT.NONE);divideButton.addSelectionListener(new ButtonSelectionListener("/"));divideButton.setLayoutData(data);divideButton.setText("/");

}

+-*/

Page 8: Understanding Framework Architecture using Eclipse

executeOperation

private void executeOperation() {// perform necessary operation and move on

int result = 0;inputNumberTwo = Integer.parseInt(text.getText());if (currentOperation.equals("+")) {

result = add(inputNumberOne, inputNumberTwo);}if (currentOperation.equals("-")) {

result = subtract(inputNumberOne, inputNumberTwo);

}if (currentOperation.equals("*")) {

result = multiply(inputNumberOne, inputNumberTwo);

}if (currentOperation.equals("/")) {

result = divide(inputNumberOne, inputNumberTwo);}text.setText("" + result);

}

Page 9: Understanding Framework Architecture using Eclipse

The only constant - Change

1. Add new features – New functions2. Fix existing features – Divide by Zero

Page 10: Understanding Framework Architecture using Eclipse

The only constant – Change..!!

private void addFunctions(Composite functionComposite) {……Button xpoweryButton = new Button(functionComposite, SWT.NONE);xpoweryButton.addSelectionListener(new ButtonSelectionListener("+"));xpoweryButton.setLayoutData(data);xpoweryButton.setText("x^y");

Button modButton = new Button(functionComposite, SWT.NONE);addButton.addSelectionListener(new ButtonSelectionListener("Mod"));modButton.setLayoutData(data);modButton.setText("Mod");

private int divide(int a, int b) {if (b == 0) {text.setText("DivideBy0 Error!");return -1;} else {if (a % b == 0) {text.setText("Not absolute");return a % b;}return a / b;}}

Page 11: Understanding Framework Architecture using Eclipse

The only constant – Change..!!

New feature needs change in existing code

New features can break existing code

Fixes can break existing code

Page 12: Understanding Framework Architecture using Eclipse

Changes cannot be avoided

But they can be isolated

Breakdown code into logical components

Separation of concern

Page 13: Understanding Framework Architecture using Eclipse

V2 Refactoring – Separation of concern

Page 14: Understanding Framework Architecture using Eclipse

V2 Refactoring – Separation of Concern

Encapsulation

Page 15: Understanding Framework Architecture using Eclipse

Indirection vs. Separation

Life is tougher:– Now new features needs changes in multple classes

Indirection is not separation– Still writing a lot of redundant code

Separation has to be logical– Based on components (think objects..)– Based on complete functionality or features

Page 16: Understanding Framework Architecture using Eclipse

We still need to reference each function in calculator

Code of createButton is common

V3 – Thought ful Refactoring

Page 17: Understanding Framework Architecture using Eclipse

V4 - Thoughtful Inheritance

Page 18: Understanding Framework Architecture using Eclipse

The only constant - Change

New requirement– Dynamic location of new functions

Ask questions:– Where should I look for it– How do I know which new function has come– How do I tell what is the name of that function– How would I execute the function without knowing

what it does

Page 19: Understanding Framework Architecture using Eclipse

How to enable dynamic functions

Where should I look for it– In some predefined scope – folder, directory etc.

How do I know which new function has come– They should announce themselves to us

How do I tell what is the name of that function– They should announce the name

How would I execute the function without knowing what it does

– There has to be a common language

Page 20: Understanding Framework Architecture using Eclipse

Where to look for

Any folder in my working directory becomes a function

Page 21: Understanding Framework Architecture using Eclipse

Where to look for

Nah..!! Not any folder..!!

Page 22: Understanding Framework Architecture using Eclipse

V5 - Identification

Page 23: Understanding Framework Architecture using Eclipse

V5 - Invocation and Interaction

The common language Interface

Page 24: Understanding Framework Architecture using Eclipse

V5 – Dynamic Runtime

- Dynamic loading of this jar (Classloading)- Dynamic instantiation of this class(Reflection)

Page 25: Understanding Framework Architecture using Eclipse

V6 - Dynamic Discovery and Loading

public void load(Calculator_Original calculator, String installDirectory) throws InstantiationException,IllegalAccessException {try {

String[] nameOfFunctions = loadFunctionsFromDirectory(installDirectory);

IFunction[] functions = new IFunction[nameOfFunctions.length];

MyCustomClassLoader loader = new MyCustomClassLoader(installDirectory);for (IFunction function : functions) {

Class functionClass = loader.loadClass("");function = (IFunction)

functionClass.newInstance();function.setCalculator(calculator);

}} catch (ClassNotFoundException e) {// Handle this}}

Page 26: Understanding Framework Architecture using Eclipse

V7 – providing a service

Avoid scratch implementations Provide common functionalities in Base classes Provide any other housekeeping functions useful

for all– BaseFunction– Math Library.!!

Page 27: Understanding Framework Architecture using Eclipse

As if life wasn’t tough

Discover folders and file Parse files for info Write your own Classloader Learn reflection

For what???

Building a VERY simple calculator

But we already had one…!! Long ago..!

Building a calculator framework

So we can make as complex calculators as we want, very simply... without worrying about all housekeeping

Page 28: Understanding Framework Architecture using Eclipse

Revisiting our framework

Separation of Concern Componentization

Abstraction/Interface

Registration and Discovery

Dynamic Loading

Page 29: Understanding Framework Architecture using Eclipse

What is a framework

Framework– a basic reusable infrastructure or skeleton, on which various components integrate to provide a solution

Page 30: Understanding Framework Architecture using Eclipse

What does a framework do

Provide a component model Registration

– components tell they want to plug in

Discovery– framework finds out who wants to plug in

Abstraction/Interface– common protocol for interaction– you don’t call me, I will call you

Services– required by most of the components, exposed by framework

Page 31: Understanding Framework Architecture using Eclipse

Building another pluggable framework

A dynamic signal processing tool – Matlab A dynamic java development tool – jBuilder A dynamic paint application – Gimp Your own custom framework – ??

– The wheel re-invented

Page 32: Understanding Framework Architecture using Eclipse

What does a framework do

Provide a component model– jars?, plugins?, bundles, activex components etc..

Registration Discovery– File management, parsing, registry management

Abstraction/Interface– most modern programming languages

Services– file access services, jobs, scheduling, threading, user

interfaces, etc etc..

Page 33: Understanding Framework Architecture using Eclipse

Eclipse does all the above for u

Eclipse is a “framework of frameworks” Allows building a calculator framework

– Without you having to parse inputs– Without you managing registries– Without you worrying about dynamic classloading– Without you working about housekeeping

Eclipse allows you to create sockets, not just plug – in to sockets..!!

Must Read: Notes to Eclipse Plugin Architecture:– http://www.eclipse.org/articles/Article-Plug-in-

architecture/plugin_architecture.html

Page 34: Understanding Framework Architecture using Eclipse

Eclipse – framework for component model

Component based OSGI Model– Dependencies– Lifecycle management– Loading

Your framework need not worry about– Identification– Scoping– Loading jars– Checking class paths

Page 35: Understanding Framework Architecture using Eclipse

Component Model - Identification

- add.cff- add.jar

- Component Identifier- Component runtime info

Page 36: Understanding Framework Architecture using Eclipse

Component Model - Identification

BUNDLES- Manifest.MF- <bundlename>.jar

- Bundle ID- Runtime Info: Classpath- blah..blah..

Page 37: Understanding Framework Architecture using Eclipse

Component Model – OSGI Bundles

The API’s become almost intuitive

Page 38: Understanding Framework Architecture using Eclipse

Component Model – Other APIs

Framework for observing other components and services

Resource and class loading

Dynamic management of dependencies

Hooks to component lifecycle

Service oriented component management

Page 39: Understanding Framework Architecture using Eclipse

Framework for discovery

Eclipse ‘plugins’ folder Eclipse plug-in registry infrastructure

– Allows you to create ‘sockets’ declaratively– Matches ‘plugs’ that can fit in your ‘socket’

automatically– Stores all extension information

Your framework need not worry about– File/Folder parsing– XML Schema creation– Managing data structures to store the extensions

Page 40: Understanding Framework Architecture using Eclipse

Framework for discovery

Who wants to let others extend them? Who wants to extend?

Who is extending me?

Configuration provided by the extenders

Properties of extenders

I am lazy to even load the runtime

Dynamic management of extenders

Page 41: Understanding Framework Architecture using Eclipse

Eclipse – framework for abstraction/interface

Naturally based on java Declaration of desired interfaces in plugin.xml Your framework need not worry about

– Verifying interface implementations

Page 42: Understanding Framework Architecture using Eclipse

Eclipse – services for frameworks

UI Toolkit MVC Architecture Component Management API’s XML/Help.. …on and on..

Your framework need not worry about– Just about any service..– Some plug-in would provide that service

Page 43: Understanding Framework Architecture using Eclipse

Eclipse – framework of frameworks

Your application need not worry about– Anything – Except for the core functionality your framework

provides…like calculation

Page 44: Understanding Framework Architecture using Eclipse

Closing thoughts

Code, Code, Code Design patterns Understand reflection Understand how your containers, servers,

providers work Think objects, eat objects, sleep objects…. https://sites.google.com/site/anshunjain/eclipse-presentations

Page 45: Understanding Framework Architecture using Eclipse

Thank you