Understanding Framework Architecture using Eclipse


Published on

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

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Board Exercise: What are the standard programs you have written so far ? Give examples…!!
  • Understanding Framework Architecture using Eclipse

    1. 1. Eclipse as a framework of frameworks (Please view in presentation mode for desired flow of information) Anshu Jain IBM Research - India [email_address] anshunjain {gmail, twitter, googlesites)
    2. 2. Introductions <ul><li>Cheating ‘Basic’ Exams => Programming is for Clerks => Java is so easy => Eclipse is easier => Programming is for lazy bums like me  </li></ul><ul><ul><li>First Job: Right here  </li></ul></ul><ul><ul><li>IBM Research – The best research lab </li></ul></ul><ul><li>http://sites.google.com/site/anshunjain </li></ul><ul><li>Your background </li></ul>
    3. 3. <ul><li>“ In theory , there is no difference between theory and practice. But, in practice, there is” </li></ul><ul><ul><ul><li>Jan La Van De Snepscheut </li></ul></ul></ul>
    4. 4. Motivation <ul><li>Move over hello world, shopping cart, ATM example </li></ul><ul><li>From consumers to providers </li></ul><ul><li>From provider to engine </li></ul><ul><li>From servlets to servlets containers </li></ul><ul><li>From eclipse plug-ins to eclipse platform </li></ul><ul><li>From EJBs to EJB container </li></ul><ul><li>From android apps to android platform </li></ul><ul><li>Also driving </li></ul><ul><li>… back to OO..!! </li></ul><ul><li>… and a bit of agility..!! </li></ul><ul><li>Nothing u don’t already know ..!! </li></ul>
    5. 5. What are we doing today Building a VERY simple calculator
    6. 6. V1 - Monolithic Class
    7. 7. addFunctions <ul><li>private void addFunctions(Composite functionComposite) { </li></ul><ul><ul><li>Button addButton = new Button(functionComposite, SWT. NONE ); </li></ul></ul><ul><ul><li>addButton.setText(&quot;+&quot;); </li></ul></ul><ul><ul><li>addButton.addSelectionListener(new ButtonSelectionListener(&quot;+&quot;)); </li></ul></ul><ul><ul><li>addButton.setLayoutData(data); </li></ul></ul><ul><ul><li>Button subtractButton = new Button(functionComposite, SWT. NONE ); </li></ul></ul><ul><ul><li>subtractButton.addSelectionListener(new ButtonSelectionListener(&quot;-&quot;)); </li></ul></ul><ul><ul><li>subtractButton.setLayoutData(data); </li></ul></ul><ul><ul><li>subtractButton.setText(&quot;-&quot;); </li></ul></ul><ul><ul><li>Button multiplyButton = new Button(functionComposite, SWT. NONE ); </li></ul></ul><ul><ul><li>multiplyButton.addSelectionListener(new ButtonSelectionListener(&quot;*&quot;)); </li></ul></ul><ul><ul><li>multiplyButton.setLayoutData(data); </li></ul></ul><ul><ul><li>multiplyButton.setText(&quot;*&quot;); </li></ul></ul><ul><ul><li>Button divideButton = new Button(functionComposite, SWT. NONE ); </li></ul></ul><ul><ul><li>divideButton.addSelectionListener(new ButtonSelectionListener(&quot;/&quot;)); </li></ul></ul><ul><ul><li>divideButton.setLayoutData(data); </li></ul></ul><ul><ul><li>divideButton.setText(&quot;/&quot;); </li></ul></ul><ul><li>} </li></ul>+ - * /
    8. 8. executeOperation <ul><li>private void executeOperation() { </li></ul><ul><li>// perform necessary operation and move on </li></ul><ul><ul><ul><ul><ul><li>int result = 0; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>inputNumberTwo = Integer. parseInt ( text .getText()); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (currentOperation.equals(&quot;+&quot;)) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>result = add(inputNumberOne, inputNumberTwo); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (currentOperation.equals(&quot;-&quot;)) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>result = subtract(inputNumberOne, inputNumberTwo); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (currentOperation.equals(&quot;*&quot;)) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>result = multiply(inputNumberOne, inputNumberTwo); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (currentOperation.equals(&quot;/&quot;)) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>result = divide(inputNumberOne, inputNumberTwo); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>text .setText( &quot;&quot; + result); </li></ul></ul></ul></ul></ul><ul><li>} </li></ul>
    9. 9. The only constant - Change 1. Add new features – New functions 2. Fix existing features – Divide by Zero
    10. 10. The only constant – Change..!! private void addFunctions(Composite functionComposite) { … … Button xpoweryButton = new Button(functionComposite, SWT. NONE ); xpoweryButton.addSelectionListener( new ButtonSelectionListener( &quot;+&quot; )); xpoweryButton.setLayoutData(data); xpoweryButton.setText( &quot;x^y&quot; ); Button modButton = new Button(functionComposite, SWT. NONE ); addButton.addSelectionListener( new ButtonSelectionListener( &quot;Mod&quot; )); modButton.setLayoutData(data); modButton.setText( &quot;Mod&quot; ); private int divide( int a, int b) { if (b == 0) { text .setText( &quot;DivideBy0 Error!&quot; ); return -1; } else { if (a % b == 0) { text .setText( &quot;Not absolute&quot; ); return a % b; } return a / b; } }
    11. 11. The only constant – Change..!! <ul><li>New feature needs change in existing code </li></ul><ul><li>New features can break existing code </li></ul><ul><li>Fixes can break existing code </li></ul>
    12. 12. Changes cannot be avoided <ul><li>But they can be isolated </li></ul><ul><li>Breakdown code into logical components </li></ul><ul><li>Separation of concern </li></ul>
    13. 13. V2 Refactoring – Separation of concern
    14. 14. V2 Refactoring – Separation of Concern Encapsulation
    15. 15. Indirection vs. Separation <ul><li>Life is tougher: </li></ul><ul><ul><li>Now new features needs changes in multple classes </li></ul></ul><ul><li>Indirection is not separation </li></ul><ul><ul><li>Still writing a lot of redundant code </li></ul></ul><ul><li>Separation has to be logical </li></ul><ul><ul><li>Based on components (think objects..) </li></ul></ul><ul><ul><li>Based on complete functionality or features </li></ul></ul>
    16. 16. V3 – Thought ful Refactoring We still need to reference each function in calculator Code of createButton is common
    17. 17. V4 - Thoughtful Inheritance
    18. 18. The only constant - Change <ul><li>New requirement </li></ul><ul><ul><li>Dynamic location of new functions </li></ul></ul><ul><li>Ask questions: </li></ul><ul><ul><li>Where should I look for it </li></ul></ul><ul><ul><li>How do I know which new function has come </li></ul></ul><ul><ul><li>How do I tell what is the name of that function </li></ul></ul><ul><ul><li>How would I execute the function without knowing what it does </li></ul></ul>
    19. 19. How to enable dynamic functions <ul><li>Where should I look for it </li></ul><ul><ul><li>In some predefined scope – folder, directory etc. </li></ul></ul><ul><li>How do I know which new function has come </li></ul><ul><ul><li>They should announce themselves to us </li></ul></ul><ul><li>How do I tell what is the name of that function </li></ul><ul><ul><li>They should announce the name </li></ul></ul><ul><li>How would I execute the function without knowing what it does </li></ul><ul><ul><li>There has to be a common language </li></ul></ul>
    20. 20. Where to look for Any folder in my working directory becomes a function
    21. 21. Where to look for Nah..!! Not any folder..!!
    22. 22. V5 - Identification
    23. 23. V5 - Invocation and Interaction <ul><li>The common language </li></ul><ul><li>Interface </li></ul>
    24. 24. V5 – Dynamic Runtime - Dynamic loading of this jar (Classloading) - Dynamic instantiation of this class(Reflection)
    25. 25. 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( &quot;&quot; ); function = (IFunction) functionClass.newInstance(); function.setCalculator(calculator); } } catch (ClassNotFoundException e) { // Handle this } }
    26. 26. V7 – providing a service <ul><li>Avoid scratch implementations </li></ul><ul><li>Provide common functionalities in Base classes </li></ul><ul><li>Provide any other housekeeping functions useful for all </li></ul><ul><ul><li>BaseFunction </li></ul></ul><ul><ul><li>Math Library.!! </li></ul></ul>
    27. 27. As if life wasn’t tough <ul><li>Discover folders and file </li></ul><ul><li>Parse files for info </li></ul><ul><li>Write your own Classloader  </li></ul><ul><li>Learn reflection </li></ul><ul><li>For what??? </li></ul>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
    28. 28. Revisiting our framework Separation of Concern Componentization Abstraction/Interface Registration and Discovery Dynamic Loading
    29. 29. What is a framework <ul><li>Framework– a basic reusable infrastructure or skeleton, on which various components integrate to provide a solution </li></ul>
    30. 30. What does a framework do <ul><li>Provide a component model </li></ul><ul><li>Registration </li></ul><ul><ul><li>components tell they want to plug in </li></ul></ul><ul><li>Discovery </li></ul><ul><ul><li>framework finds out who wants to plug in </li></ul></ul><ul><li>Abstraction/Interface </li></ul><ul><ul><li>common protocol for interaction </li></ul></ul><ul><ul><li>you don’t call me, I will call you </li></ul></ul><ul><li>Services </li></ul><ul><ul><li>required by most of the components, exposed by framework </li></ul></ul>
    31. 31. Building another pluggable framework <ul><li>A dynamic signal processing tool – Matlab </li></ul><ul><li>A dynamic java development tool – jBuilder </li></ul><ul><li>A dynamic paint application – Gimp </li></ul><ul><li>Your own custom framework – ?? </li></ul><ul><ul><li>The wheel re-invented </li></ul></ul>
    32. 32. What does a framework do <ul><li>Provide a component model </li></ul><ul><ul><li>jars?, plugins?, bundles, activex components etc.. </li></ul></ul><ul><li>Registration Discovery </li></ul><ul><ul><li>File management, parsing, registry management </li></ul></ul><ul><li>Abstraction/Interface </li></ul><ul><ul><li>most modern programming languages </li></ul></ul><ul><li>Services </li></ul><ul><ul><li>file access services, jobs, scheduling, threading, user interfaces, etc etc.. </li></ul></ul>
    33. 33. Eclipse does all the above for u <ul><li>Eclipse is a “framework of frameworks” </li></ul><ul><li>Allows building a calculator framework </li></ul><ul><ul><li>Without you having to parse inputs </li></ul></ul><ul><ul><li>Without you managing registries </li></ul></ul><ul><ul><li>Without you worrying about dynamic classloading </li></ul></ul><ul><ul><li>Without you working about housekeeping </li></ul></ul><ul><li>Eclipse allows you to create sockets, not just plug – in to sockets..!! </li></ul><ul><li>Must Read: Notes to Eclipse Plugin Architecture: </li></ul><ul><ul><li>http://www.eclipse.org/articles/Article-Plug-in-architecture/plugin_architecture.html </li></ul></ul>
    34. 34. Eclipse – framework for component model <ul><li>Component based OSGI Model </li></ul><ul><ul><li>Dependencies </li></ul></ul><ul><ul><li>Lifecycle management </li></ul></ul><ul><ul><li>Loading </li></ul></ul><ul><li>Your framework need not worry about </li></ul><ul><ul><li>Identification </li></ul></ul><ul><ul><li>Scoping </li></ul></ul><ul><ul><li>Loading jars </li></ul></ul><ul><ul><li>Checking class paths </li></ul></ul>
    35. 35. Component Model - Identification - add.cff - add.jar - Component Identifier - Component runtime info
    36. 36. Component Model - Identification BUNDLES - Manifest.MF - <bundlename>.jar - Bundle ID - Runtime Info: Classpath - blah..blah..
    37. 37. Component Model – OSGI Bundles <ul><li>The API’s become almost intuitive </li></ul>
    38. 38. 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
    39. 39. Framework for discovery <ul><li>Eclipse ‘plugins’ folder </li></ul><ul><li>Eclipse plug-in registry infrastructure </li></ul><ul><ul><li>Allows you to create ‘sockets’ declaratively </li></ul></ul><ul><ul><li>Matches ‘plugs’ that can fit in your ‘socket’ automatically </li></ul></ul><ul><ul><li>Stores all extension information </li></ul></ul><ul><li>Your framework need not worry about </li></ul><ul><ul><li>File/Folder parsing </li></ul></ul><ul><ul><li>XML Schema creation </li></ul></ul><ul><ul><li>Managing data structures to store the extensions </li></ul></ul>
    40. 40. 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
    41. 41. Eclipse – framework for abstraction/interface <ul><li>Naturally based on java </li></ul><ul><li>Declaration of desired interfaces in plugin.xml </li></ul><ul><li>Your framework need not worry about </li></ul><ul><ul><li>Verifying interface implementations </li></ul></ul>
    42. 42. Eclipse – services for frameworks <ul><li>UI Toolkit </li></ul><ul><li>MVC Architecture </li></ul><ul><li>Component Management API’s </li></ul><ul><li>XML/Help.. </li></ul><ul><li>… on and on.. </li></ul><ul><li>Your framework need not worry about </li></ul><ul><ul><li>Just about any service.. </li></ul></ul><ul><ul><li>Some plug-in would provide that service </li></ul></ul>
    43. 43. Eclipse – framework of frameworks <ul><li>Your application need not worry about </li></ul><ul><ul><li>Anything  </li></ul></ul><ul><ul><li>Except for the core functionality your framework provides…like calculation </li></ul></ul>
    44. 44. Closing thoughts <ul><li>Code, Code, Code </li></ul><ul><li>Design patterns </li></ul><ul><li>Understand reflection </li></ul><ul><li>Understand how your containers, servers, providers work </li></ul><ul><li>Think objects, eat objects, sleep objects…. </li></ul><ul><li>https://sites.google.com/site/anshunjain/eclipse-presentations </li></ul>
    45. 45. Thank you