1 / 38

Aspects in Distributed Systems

[http://research.lumeta.com/ches/map/]. Distributed Systems. Aspects in Distributed Systems. CSC 253 Phil Greenwood p.greenwood@lancaster.ac.uk. Overview of the Session. Problems with OOP Why do we need aspects? Key concepts of Aspect-Oriented Programming (AOP). What is AOP?

beyla
Download Presentation

Aspects in Distributed Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. [http://research.lumeta.com/ches/map/] Distributed Systems Aspects in Distributed Systems CSC 253 Phil Greenwood p.greenwood@lancaster.ac.uk

  2. Overview of the Session • Problems with OOP • Why do we need aspects? • Key concepts of Aspect-Oriented Programming (AOP). • What is AOP? • Programming Elements • Aspects in Distributed Systems • Example aspects • Advanced AOP • Dynamic AOP Phil Greenwood

  3. Good things about OOP • OOP has several nice properties • Modular structure • Reuse • Extensibility • Allows code to be created which is: • Maintainable • Reusable • Understandable Phil Greenwood

  4. Bad things about OOP • Some features cannot be cleanly modularised. • This leads to code relating to a single feature being scattered amongst a number of objects. • This prevents code from being created which is: • Maintainable • Reusable • Understandable Phil Greenwood

  5. Example of good modularity All relevant code located in one object XML Parsing in Apache Phil Greenwood

  6. Example of bad modularity Relevant code is scattered amongst lots of objects Can say that the logging concern crosscuts a number of objects. Logging in Apache Phil Greenwood

  7. Cost of Scattered Code • Redundant Code • The same code repeated in several places • Difficult to reason about • Structure is unclear • Difficult to see the ‘big-picture’ • Changes are difficult • Need to find all relevant segments of code Phil Greenwood

  8. AOP Definition “There are some design decisions that are hard to cleanly capture because they crosscut the system’s basic functionality. We call the issues that these features address aspects…” “Aspect-Oriented Programming is a new programming technique that makes it possible to clearly express programs involving such aspects, including appropriate isolation, composition and reuse of the aspect code.” [Kiczales 1997] Phil Greenwood

  9. Important Note • AOP is NOT a substitute for OOP • Should be used in conjunction with a base-language such as: • Java • C++ • SmallTalk • More supported all the time • The rest of this lecture will focus on Java and AspectJ Phil Greenwood

  10. Fundamental AOP Constructs • Aspect • Top-level of encapsulation • Conceptually similar to a Java class • Pointcut • Used to identify execution points (joinpoint) where code related to the concern should be executed • Advice • Contains the actual code to implement the concern • Conceptually similar to a method • Before, after, around execution points Phil Greenwood

  11. Display * Figure FigureElement moveBy(int, int) makePoint(..)makeLine(..) Point Line 2 getX()getY()setX(int)setY(int) getP1()getP2()setP1(Point)setP2(Point) Display Updating Example DisplayUpdating Phil Greenwood

  12. OO Solution • Problems with this solution • No central encapsulation of the Display Update feature • Evolution is difficult • Changes to all classes necessary class Line { private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; Display.update(this); } void setP2(Point p2) { this.p2 = p2; Display.update(this); } } class Point { privateint x = 0, y = 0; int getX() { return x; } int getY() { return y; } void setX(int x) { this.x = x; Display.update(this); } void setY(int y) { this.y = y; Display.update(this); } } Phil Greenwood

  13. AspectJ Solution • A more elegant solution • All changes located in one place • Evolution is much easier class Line { private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; } void setP2(Point p2) { this.p2 = p2; } } class Point { privateint x = 0, y = 0; int getX() { return x; } int getY() { return y; } void setX(int x) { this.x = x; } void setY(int y) { this.y = y; } } aspect DisplayUpdating { pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int)); after(): move() { Display.update(); } } Phil Greenwood

  14. Pointcuts • Last example introduced the call pointcut • Other pointcut types include: • execution • field operations (get and set) • handler • within • Cflow • Able to generalise a pointcut using wildcard token • Instead of specifying each individual method signature this generalised pattern could be used: call(void *.set*(..)) Phil Greenwood

  15. Advice Types • Display Updating demonstrated after advice • Before advice • Executes immediately before the execution point • The execution point is executed automatically after the advice is executed • Around advice • Can be used to replace the advised execution point • Uses the proceed method to execute the advised execution point Phil Greenwood

  16. The Weaving Process • The process used to combine the base-code and aspect-code is known as the weaver • Modifies the base-code to insert ‘hooks’ to the aspect code • Variety of implementations to achieve this • Source code weaving • Byte-code weaving (off-line and at load time) • Modified JVM • Proxy based Phil Greenwood

  17. AspectJ Mechanism • Uses byte-code modification of the classes affected by aspects. • Weaving can take place at compile, post-compile or load-time. ajc/javac ajc (weaver) Base-classes source Base-classes byte-code ajc Modified byte-code Aspect source Aspect byte-code Phil Greenwood

  18. AOP in Distributed Systems • Used to address both functional and non-functional concerns • Typical distributed systems related crosscutting concerns: • Security • Quality of Service (QoS) • Fault Tolerance • Synchronisation • Transactions • Caching • Logging Phil Greenwood

  19. Security Example • Adding encryption to the Coursework Spec Exercise • Extend the password solution • Demonstrates call pointcut and around advice • Need to access parameter values Phil Greenwood

  20. Encryption Implementation (1) public static void main(String[] args) { try { int uid = (new Integer(args[0])).intValue(); Random ran = new Random(); Client_request req = new Client_request(uid, ran.nextInt()); server = (CW_server_interface)Naming.lookup("rmi://148.88.1.16/CW_server_interface"); Server_response response = server.getSpec(uid, req); File file = new File("spec.doc"); FileOutputStream fos= new FileOutputStream(file); response.write_to(fos); } catch (Exception e) { System.out.println(e.toString()); } } Phil Greenwood

  21. Encryption Implementation (1) public static void main(String[] args) { try { int uid = (new Integer(args[0])).intValue(); Random ran = new Random(); Client_request req = new Client_request(uid, ran.nextInt()); server = (CW_server_interface)Naming.lookup("rmi://148.88.1.16/CW_server_interface"); Server_response response = server.getSpec(uid, req); File file = new File("spec.doc"); FileOutputStream fos= new FileOutputStream(file); response.write_to(fos); } catch (Exception e) { System.out.println(e.toString()); } } Apply encryption behaviour here Phil Greenwood

  22. Encryption Implementation (2) Customised Application Client + Encryption Aspect = Base Classes Aspects Executable Code Phil Greenwood

  23. Encryption Implementation (3) • First need to specify the pointcut pattern where the advice should be applied. • Needs to be a call pointcut as the client does not contain the method which is executed • Parameter values being passed need to be exposed aspect Encryption { pointcut encrypt(int uid, Client_request request): call(Server_response CW_server_interface.getSpec(int,Client_request)) && args(uid,request); … Phil Greenwood

  24. Encryption Implementation (3) • First need to specify the pointcut pattern where the advice should be applied. • Needs to be a call pointcut as the client does not contain the method which is executed • Parameter values being passed need to be exposed aspect Encryption { pointcut encrypt(int uid, Client_request request): call(Server_response CW_server_interface.getSpec(int,Client_request)) && args(uid,request); … Identifies pointcut where the advice should be woven Phil Greenwood

  25. Encryption Implementation (3) • First need to specify the pointcut pattern where the advice should be applied. • Needs to be a call pointcut as the client does not contain the method which is executed • Parameter values being passed need to be exposed aspect Encryption { pointcut encrypt(int uid, Client_request request): call(Server_response CW_server_interface.getSpec(int,Client_request)) && args(uid,request); … Exposes method parameters Phil Greenwood

  26. Encryption Implementation (4) • The advice to implement the encryption needs to: • Load the private key • Initiate the cipher • Encrypt the Client_request object passed • Call the SealedObject version of the getSpec method instead of the password version • Decrypt the response • Pass the decrypted response back to the Client Phil Greenwood

  27. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Phil Greenwood

  28. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Around advice definition Phil Greenwood

  29. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Initialises the cipher Phil Greenwood

  30. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Encrypts the request parameter passed to the getSpec method Phil Greenwood

  31. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Invokes the encryption version of the getSpec method Phil Greenwood

  32. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } Decrypts the returned response so the return value is the correct type Phil Greenwood

  33. Encryption Implementation (5) Server_response around(int uid, Client_request req): encrypt(uid,req){ try { SecretKey key= loadKey(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(req, cipher); SealedObject soRes = Client.server.getSpec(uid, so); cipher.init(Cipher.DECRYPT_MODE, key); Server_response res= (Server_response)soRes.getObject(cipher); return response; } catch (Exception e) { return proceed(uid, request); } } If the encryption fails, the password version is used instead Phil Greenwood

  34. Dynamic AOP • Traditionally, AOP uses static weaving • Normally woven prior to the system’s execution • Cannot modify the aspects deployment at run-time • Recent development is the introduction of Dynamic AOP • Weaver operates at run-time • Allows aspects to be woven/removed at run-time • Very powerful • Adaptive behaviour • Rapid Testing • Improved Availability Phil Greenwood

  35. AOP Limitations • Aspect Identification • How to successfully “aspectise” an application? • Aspect interaction • Potential unforeseen run-time affects • Considered a dark-art in some circles • Dynamic AOP • Performance overhead • Aspect consistency Phil Greenwood

  36. Some other approaches • AspectWerkz • Entirely Java-based Dynamic AOP • Now merged with AspectJ • JAC • Allows remote distribution of aspects • Run-time configuration of aspects • JBoss AOP • Similar to AspectWerkz • Integrated into JBoss middleware platform • Prose • JVM based Phil Greenwood

  37. Outcomes • Understand the problems of OO • Know how and why AOP prevents these problems • Be aware of the basic terminology used in AOP • Understand how AOP can be useful in distributed systems Phil Greenwood

  38. Questions? Phil Greenwood

More Related