Upload
anshunjain
View
936
Download
2
Embed Size (px)
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
Citation preview
Eclipse as a framework of frameworks (Please view in presentation mode for desired flow of information)
Anshu Jain
IBM Research - India
anshunjain
{gmail, twitter, googlesites)
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
“In theory, there is no difference between theory and practice. But, in practice, there is”
Jan La Van De Snepscheut
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 ..!!
What are we doing today
Building a VERY simple calculator
V1 - Monolithic Class
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("/");
}
+-*/
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);
}
The only constant - Change
1. Add new features – New functions2. Fix existing features – Divide by Zero
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;}}
The only constant – Change..!!
New feature needs change in existing code
New features can break existing code
Fixes can break existing code
Changes cannot be avoided
But they can be isolated
Breakdown code into logical components
Separation of concern
V2 Refactoring – Separation of concern
V2 Refactoring – Separation of Concern
Encapsulation
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
We still need to reference each function in calculator
Code of createButton is common
V3 – Thought ful Refactoring
V4 - Thoughtful Inheritance
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
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
Where to look for
Any folder in my working directory becomes a function
Where to look for
Nah..!! Not any folder..!!
V5 - Identification
V5 - Invocation and Interaction
The common language Interface
V5 – Dynamic Runtime
- Dynamic loading of this jar (Classloading)- Dynamic instantiation of this class(Reflection)
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}}
V7 – providing a service
Avoid scratch implementations Provide common functionalities in Base classes Provide any other housekeeping functions useful
for all– BaseFunction– Math Library.!!
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
Revisiting our framework
Separation of Concern Componentization
Abstraction/Interface
Registration and Discovery
Dynamic Loading
What is a framework
Framework– a basic reusable infrastructure or skeleton, on which various components integrate to provide a solution
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
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
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..
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
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
Component Model - Identification
- add.cff- add.jar
- Component Identifier- Component runtime info
Component Model - Identification
BUNDLES- Manifest.MF- <bundlename>.jar
- Bundle ID- Runtime Info: Classpath- blah..blah..
Component Model – OSGI Bundles
The API’s become almost intuitive
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
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
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
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
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
Eclipse – framework of frameworks
Your application need not worry about– Anything – Except for the core functionality your framework
provides…like calculation
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
Thank you