490 likes | 857 Views
JAsCo: an Aspect-Oriented Approach tailored for Components. Presented by: Guy Korland. Overview. Introduction Component Based Software Development (CBSD) Why yet another AOP language? The JAsCo Approach The JAsCo language Aspect Language Connector Language The JAsCo Technology
E N D
JAsCo: an Aspect-Oriented Approach tailored for Components Presented by: Guy Korland 236800 - Seminar in Software Engineering
Overview • Introduction • Component Based Software Development (CBSD) • Why yet another AOP language? • The JAsCo Approach • The JAsCo language • Aspect Language • Connector Language • The JAsCo Technology • JAsCo Component Model • JAsCo Jutta and HotSwap • Prolog - JAsCoAP 236800 - Seminar in Software Engineering
Component Based Software Development (CBSD) • What is Component Based Software Development (CBSD)? • Selecting appropriate off-the-shelf components and then assemble them with a well-defined software architecture. • “Buy, don't build” philosophy by Fred Brooks (1987). • CBSD has three major functions: • Developing software from prefabricated, reusable parts. • The ability to use those parts in other applications. • Easily maintaining and customizing those parts to produce new functions and features. • Remark: Interact through: Properties,Events & Public methods. 236800 - Seminar in Software Engineering
Why yet another AOP language? • Current AOP approaches are not very well suited to be used in a Component-Based context. • Requirements: • General purpose. • Component interaction. • Reusable aspects independent of concrete components. • Expressive Combinations between aspects/components. • No invasive changes. • Aspects first-class at run-time. • Dynamic reconfiguration. 236800 - Seminar in Software Engineering
Why yet another AOP language? • None of the approaches are entirely suitable to be used in CBSD. 236800 - Seminar in Software Engineering
The JAsCo AOP approach • New general purpose AOP language (extension of Javatm) • Reusable, independently specified Aspect Beans. • Explicit Connectors. • Backward compatible at the technological level. From JavaBeantm 236800 - Seminar in Software Engineering
The JAsCo AOP approach: Overview ASPECT BEAN Q COMP X • method1() • method2() • method3(int) • fires event1 • methody() • fires eventx HookA HookA(m1,m2) CONNECTOR • before • after HookA.m1 X.method2 COMP Y HookA.m2 Y.event2 HookB.m3 Y.method1 HookB.m3 Y.method1 • method1(str) • method2() • fires event1 • fires event2 HookB HookB(m3) • replace 236800 - Seminar in Software Engineering
Java Behavior shared amongst all hooks One or more hooks which implement the crosscutting behavior itself The JAsCo Language: Example Aspect Bean class AccessManager { private PermissionDb p_db = new PermissionDb(); private User currentuser = null; void login(User user, String pass) {//login code} void logout() { //logout code } void addAccessManagerListener(AML listener) {//add observer listener} void removeAccesManagerListener(AML listener) {//remove observer listener } hook AccessControl { … } hook hook2 { … } } 236800 - Seminar in Software Engineering
When? Constructor Conditional method The JAsCo Language: Example Aspect Bean class AccessManager { … hook AccessControl { AccessControl(amethod (..args)) { execute(amethod); } isApplicable() { return !p_db.isRoot(currentuser); } replace() { if(p_db.check(currentuser, calledobject)) return amethod(args); else throw new AccessException(); } } } } What? Behavior methods 236800 - Seminar in Software Engineering
JAsCo Aspect Beans:Hook constructor • Describes a set of join points. • Independent of a concrete context. • Abstract Method Parameters: • Minimal one required per hook constructor. • Scope extends the entire hook. • Example: 1…n abstract method parameters: specify the abstract context AccessControl(amethod(..args)) { execute(amethod); } Hook constructor body: when to cut the normal execution of the application 236800 - Seminar in Software Engineering
JAsCo Aspect Beans: Hook constructor • Several hook constructor body constructs are available: • execute: Concrete method is executed. • cflow: In the control flow of the concrete method. (Delimiter) • withincode: Direct caller of the executing method. (Delimiter) • Can be described using logical operators: &&, ||, ! • Examples: execute(method1); execute(method1)&& cflow(method2); execute(method1)&& ! cflow(method2); execute(method1)&& (!(cflow(method2)|| withincode(method3))); 236800 - Seminar in Software Engineering
The behavior of the hook is performed only when the currentuser is not root. JAsCo Aspect Beans: Conditional method • Conditional method: • Optional. • Conditions which can not be incorporated into the constructor. • Example: isApplicable() { return !p_db.isRoot( currentuser); } 236800 - Seminar in Software Engineering
JAsCo Aspect Beans: Hook behavior methods • Hook behavior methods: • Whenever the “when”-part of the hook evaluates to true. • Three kinds of behavior methods are available: • Beforeand after:Executed before or after the encountered method join-point. (Have no return value) • Replace: Wrap the execution of method joinpoint. (Has a return value if wrapped method has a return value) • After throwing: Executed if an exception is thrown. (always rethrows the exception) 236800 - Seminar in Software Engineering
Call the wrapped method Can have multiple after throwing advices Even throw exceptions JAsCo Aspect Beans: Hook behavior methods Access Aspect Bean shared behavior • Example: replace() { if(p_db.check(currentuser, calledobject)) return amethod(args); else throw new AccessException(); } after throwing (Exception ex) { System.out.println(“exception occurred: “+ex.getMessage()); } 236800 - Seminar in Software Engineering
Aspect Bean Hook: The Total Package hook AccessControl { AccessControl(amethod (..args)) { execute(amethod); } isApplicable() { return !p_db.isRoot(currentuser); } replace() { if(p_db.check(currentuser,calledobject)) return amethod(args); else throw new AccessException(); } } When the concretemethod bound to amethod is executed and the current user is not root. Check if the user has the correct permissions and execute the amethodif so. 236800 - Seminar in Software Engineering
Aspect Bean Language: Advanced concepts - calledobject • The “calledobject” keyword: • Refers to the object instance on which the method was executed. • Allows to implement behavior which is dependent on the context. • Example: Refers to the object instance on which amethod was called. replace() { if(p_db.check(currentuser, calledobject)) return amethod(args); else throw new AccessException(); } 236800 - Seminar in Software Engineering
Aspect Bean Language: Advanced concepts - abstract • Abstract methods: • Abstractmethods used to refer to behavior which is not implemented. (& inheritance) • Example: hook AccessControl { … replace() { if(p_db.check(currentuser) return method(args); else noAccessPolicy(currentuser); } abstract void noAccessPolicy(User user); } Implementation is provided in the connector. (anonymous) 236800 - Seminar in Software Engineering
Aspect Bean Language: Advanced concepts - global • Accessing shared aspect bean behavior • To access the Java behavior, the global keyword is employed. (Omitted in the previous slides for clarity) • Example: Required for accessing shared Java aspect bean behavior. replace() { if(global.p_db.check(global.currentuser, calledobject)) return amethod(args); else throw new AccessException(); } 236800 - Seminar in Software Engineering
Aspect Bean Language: Advanced concepts - Reflection • Reflection on abstract method parameters: • Allows to introspect the concrete methods. • Provided reflection API: (Most important ones) • getName() : Get the name of the concrete method • getClassName() : Get the name of the concrete class • getCalledObject() : Retrieve the called object • invokeJAsCoMethod() : Execute method with original arguments • invoke(Object obj, Object[] args) : Execute method with user-defined arguments. • ... 236800 - Seminar in Software Engineering
Aspect Bean Language: Advanced concepts - Reflection • Reflection on abstract method parameters: • Example: hook SimpleLogging { SimpleLogging(calledmethod(..args)) { execute(calledmethod); } before() { System.out.println(“Executing”+ calledmethod.getName() + " IN "+ calledmethod.getClassName()); } } Allows to trace the name and class of the concrete method bound to calledmethod. 236800 - Seminar in Software Engineering
Only when startmethod has been invoked, all method executions are logged. Delimited to a specific transitions Aspect Bean Language: Stateful Aspects • Allows specifying regular hook triggering conditions. • Advices can be attached to any transition, or all of them (global). • Example hook StatefulLogging { StatefulLogging(startmethod(..a1), logmethod(..a2), stopm(..a3)) { start>p1; p1: execute(startmethod) > p3||p2; p2: execute(logmethod) > p3||p2; p3: execute(stopm) > p1; } isApplicable p2(){ return !p_db.isRoot(currentUser); } beforep2() { //logging } } 236800 - Seminar in Software Engineering
JAsCo Connectors • JAsCo Connectors: • Specifies “where” the aspect bean hooks should become active. • Mappingsof abstract method parameters upon concrete method signatures. Maps abstract method parameters upon concrete method signatures 236800 - Seminar in Software Engineering
JAsCo Connector Language • JAsCo Connector: • Explicitly instantiating aspect bean hooks with a concrete component context. • Allows to describe the collaboration between several instantiated aspect bean hooks. • Example: One ore more hook instantiations (Share same aspect bean instance) static connector PrintAccessControl { Bean.Hook1 hook1 = new Bean.Hook1(* Printer.*(*)); Bean.Hook2 hook2 = new Bean.Hook2(* Printer.*(*)); hook2.replace(); hook1.replace(); addCombinationStrategy(new ExStrategy(hook1,hook2)); } Specify precedence Specify collaboration 236800 - Seminar in Software Engineering
JAsCo Connectors: Hook instantiations • Deploys an new instance aspect bean hook upon a concretecomponent. • Example: Concrete component context AccessManager.AccessControl control = new AccessManager.AccessControl( void Printer.Print(File) ); Both methods are mapped. AccessManager.AccessControl control = new AccessManager.AccessControl( {void Printer.Print(File), File Scanner.Scan()}); AccessManager.AccessControl control = new AccessManager.AccessControl( oneventPrinter.jobFinished(PrintEvent)); Executed whenever the jobFinished event is triggered. 236800 - Seminar in Software Engineering
JAsCo Connectors: Hook instantiations • Hook instantiations and abstract methods: • All abstract methods specified within a hook must be implemented in the connector. (Syntax is similar to anonymous classes in Java) • Example: static connector PrintAccessControl { AccessManager.AccessControl control = new AccessManager.AccessControl(void Printer.Print(File)) { void noAccessPolicy(User user) { System.out.println(“No access for:“+ user.toString()); } } } In this particular case, noAccessPolicyprints some warning to the standard output. 236800 - Seminar in Software Engineering
JAsCo Connectors: Hook instantiations • Automatically create multiple hook instance: • 5 instantiation constructs: • perall: New hook instance each time the hook is triggered. • perobject : New hook instance for each calledobject. • perclass: Similar to perobject, only for each class. • permethod: Similar to perobject, only for each method. • percflow : A new hook instance for each controlflow through a joint point where the hook is triggered. 236800 - Seminar in Software Engineering
JAsCo Connectors: Hook instantiations • Automatically create multiple hook instance: • Example: static connector PrintAccessControl { perobject AccessManager.AccessControl control = new AccessManager.AccessControl(void Printer.Print(File)) { void noAccessPolicy(User user) { System.out.println(“No access for:“+ user.toString()); } } } For each instance of class Printer, a new hook instance will be instantiated. 236800 - Seminar in Software Engineering
JAsCo Connectors: Precedence Strategies • Executing hook behavior methods: • Specify explicitly which hook behavior methodsto execute. • default() : Executes all behavior methods. • Example: static connector PrintAccessControl { AccessManager.AccessControl control = new AccessManager.AccessControl(void Printer.Print(File)); control.default(); } 236800 - Seminar in Software Engineering
JAsCo Connectors: Precedence Strategies • Precendence Strategies: • Specify the precedence between hooks. • Example: static connector PrintAccessControl { Control control = … Logger logger = … Locker locker = … logger.before(); lock.before(); control.replace(); lock.after(); logger.after(); } Logging at the outside Locking at the inside 236800 - Seminar in Software Engineering
JAsCo Connectors: Precedence Strategies • Chaining replace behavior methods: • Multiple replace upon the same joinpoint execution is chained. (Similar to around-mechanism in AspectJ) • Example: static connector test { Hook1 hook1 = … Hook2 hook2 = … Hook2 hook3 = … hook1.replace(); hook2.replace(); hook3.replace(); } Chained up 236800 - Seminar in Software Engineering
JAsCo Connectors: Combination Strategies • Combination Strategies: • Consider: If the behavior of aspect A is performed, the behavior of aspect B cannot be executed. • Solved using combination strategy: acts as a filter. • Added to a connector, making use of the addCombinationStrategy. interface CombinationStrategy { public HookList validateCombinations(Hooklist); } Each combination strategy must implement the CombinationStrategy interface 236800 - Seminar in Software Engineering
JAsCo Connectors: Combination Strategies • Combination strategies: • Example: class ExcludeCombinationStrategy implements CombinationStrategy { private Object hookA, hookB; ExcludeCombinationStrategy(Object a,Object b) { hookA = a; hookB = b; } HookList validateCombinations(Hooklist hlist) { if (hlist.contains(hookA)) { hlist.remove(hookB); return hlist; } } If the behavior ofHook Ais applicable,Hook Bis removed from the list of applicable hooks. 236800 - Seminar in Software Engineering
JAsCo Connectors: Combination Strategies • Combination strategies: • Example (cont): static connector test { Hook1 hook1 = … Hook2 hook2 = … hook1.replace(); hook2.replace(); addCombinationStrategy( new ExcludeCombinationStrategy(hook1,hook2)); } Explicitly add combination strategy The replace behavior ofhook2is only performed when the behavior ofhook1is not applicable. 236800 - Seminar in Software Engineering
JAsCo Connector Language: Advanced concepts • Static connector: • All hooks are automatically triggered. • When omitted, specific calledobjectsneed to be added manually. • Connector reflection: • The connector API allows to query a connector for various properties • addInstance(Object): Add a specific called object (see static connector) • getHooks(): Return the list of hooks • setEnabled(boolean) : Allows to enable or disable a connector at run-time. • isEnabled(): Is the connector enabled? • … 236800 - Seminar in Software Engineering
JAsCo Language: Conclusion • General-purpose AOP language. • Extension of Java. • Aspects beans are described independent of a concrete component context. (Promotes encapsulation and reusability) • Aspects beans are explicitly initialized using connectors. • Collaborations between aspects can be specified making use of precedence and combination strategies. 236800 - Seminar in Software Engineering
The JAsCo Technology • Requirements: • Weaving should not depend on source-code, as components are blackbox. • No invasive changes, as it should be possible to easily extract the “weaved” aspect afterwards. • Aspects should remain first class entities. • Dynamicallyenable or disable aspects. • New backward compatible component model is introduced! 236800 - Seminar in Software Engineering
JAsCo Component Model • JAsCo Bean component model: • Backward compatible: JAsCo Beans can still be used as regular Java Beans. • Existing Java Beans can automatically be transformed into JAsCo Bean. • Allows dynamic application and removal of aspects. • Each public method of a component is provided with a trap that refers to the JAsCo run-time infrastructure. 236800 - Seminar in Software Engineering
JAsCo Component Model: Overview 236800 - Seminar in Software Engineering
JAsCo Component Model • JAsCo beans and JAsCo aspect beans are totally independent. • The connector registry manages the set of connectors • Whenever a trap is reached, the connector registry is queriedfor obtaining the set of applicable hooks. • Flexible system: connector registry is the only link between JAsCo beans and JAsCo aspect beans. • Easy to add and remove Aspect Beans at run-time, as the connector registry is used as central access-point. 236800 - Seminar in Software Engineering
JAsCo Bean Runtime execution • Imagine method1 of JAsCo Bean Ais executed. • Trap is encountered. • Retrieve the set of applicable hooks from the connector registry. • Initialize each hook with the context of method1. • Filter the set of applicable hooks making use of the applicable combination strategies. • Order the set of applicable hooks using the precedence strategies – Execute befores. • Order the set of applicable hooks using the precedence strategies – Build up thereplace chain and execute. • Order the set of applicable hooks using the precedence strategies – Execute afters. • Return 236800 - Seminar in Software Engineering
JAsCo Component Model: Runtime Performance • Flexible, dynamic system causes rather big run-time overhead. • For each encountered trap, the lookup logic is performed. • The same logic is executed all over again, Even if the trap was already encountered. • For some traps, no hooks are applicable, but the connector registry is still queried. • Solution: • JAsCo Jutta: Improves the JAsCo aspect interpreter. • JAsCo HotSwap: Improves the JAsCo interception mechanism. 236800 - Seminar in Software Engineering
JAsCo Jutta System • JAsCo Jutta System: • Generates a highly optimized code fragment in byte code whenever a particular joinpointis encountered for the first time. • Whenever the joinpoint is encountered again, this codefragment is executed, and no hook lookup mechanism is performed. • Example code fragment (Java counterpart): public void executeJoinpoint(Joinpoint p) { hook0._Jasco_initialize(p); hook1._Jasco_initialize(p); hook2._Jasco_initialize(p); hook1.before(); hook2.before(); hook0.replace(); } 236800 - Seminar in Software Engineering
JAsCo HotSwap system • JAsCo HotSwap system. • Idea:Only trap those methods where aspects are applied upon. Methods on which no aspects are applied do not cause an additional run-time overhead. • Technically enabled making use of Java HotSwap • When a new hook is enabled, all applicable methods are automatically trapped. • Their corresponding class is hotswapped for a new class where the applicable methods are trapped. • When the hook is removed, the class is hotswapped with a new version where the corresponding traps are removed. 236800 - Seminar in Software Engineering
JAsCo Technology: Conclusion • New backward compatible component model. • Flexibly: allows adding and removing aspects at run-time. • Improved run-time performance: Jutta & HotSwap. 236800 - Seminar in Software Engineering
Prolog - JAsCoAP Introduce a new kind of connector, called “traversal connector”. 236800 - Seminar in Software Engineering
JAsCoAP: an aspect bean class DataPersistence { hook Backup { int i = 0; Backup(triggeringmethod(..args)) { execute(triggeringmethod); } isApplicable() { //returns true if calledobject is changed since last visit } before() { ObjectOutputStream writer = … writer.writeObject(getDataMethod(calledobject)); } public abstract Object getDataMethod(Object context); } } 236800 - Seminar in Software Engineering
JAsCoAP: traversal Connectors traversalconnector BackupTraversal("from system.Root to *") { DataPersistenceAspectBean.Backup hook = new DataPersistenceAspectBean.Backup(visiting DataStore) { public void getDataMethod(Object visitedobject) { DataStore store = (DataStore) visitedobject; return store.getData(); } }; hook.before(); } Result: Backup traversal is instantiated on visiting the abstract traversal “from system.Root to *” Only objects of type DataStore are effectively visited 236800 - Seminar in Software Engineering
JAsCoAP: invoking a traversal • Difference with AOP, AP is explicitly invoked, AOP implicitly! public void backup(system.Root mySystemRoot) { Connector myBackup = BackupTraversal.getConnector(); myBackup.traverse(mySystemRoot); } 236800 - Seminar in Software Engineering
References • Suvee, D., Vanderperren, W. and Jonckers, V. JAsCo: an Aspect-Oriented approach tailored for Component Based Software Development. In Proceedings of international conference on aspect-oriented software development (AOSD), Boston, USA, pp 21-29, ISBN 1-58113-660-9, ACM Press, march 2003. [download paper] • Vanderperren, W. and Suvee, D. JAsCoAP: Adaptive Programming for Component-Based Software Engineering. In Proceedings to Third International AOSD Workshop on Aspects, Components, and Patterns for Infrastructure Software (ACP4IS), Lancaster, UK, March 2004. • Vanderperren, W., Suvee, D., Verheecke, B. and Cibran, M. JAsCo&WSML: AOP for Component-Based Software Engineering applied to a Web Services Management Layer. Formal Research Demo at AOSD 2004, Lancaster, UK, March 2004. 236800 - Seminar in Software Engineering