460 likes | 484 Views
Event-based Aspect-Oriented Programming (EAOP) Technion Based on a paper of: R´emi Douence and Mario Sudholt Presented By: Omer Ganot. Agenda. Motivation. The EAOP Model. The EAOP Tool. EAOP by an example. Comparison with other AOP languages. Motivation.
E N D
Event-based Aspect-Oriented Programming (EAOP) Technion Based on a paper of: R´emi Douence and Mario Sudholt Presented By:Omer Ganot
Agenda Motivation. The EAOP Model. The EAOP Tool. EAOP by an example. Comparison with other AOP languages.
Motivation AspectJ has introduced the notion of a “pointcut”. An aspect modifies the base program, and a notion of “advice” defining the modifications themselves. However, AspectJ constitutes only one point within the space of aspect definition languages. In this presentation, we will examine a simple, yet powerful approach, which extends the possibilities concealed in AOP.
Motivation (Cont.) • EAOP is based on monitoring of execution events. • This model extends previous approaches (Like AspectJ) by: • Enabling the systematic treatment of relationships between pointcuts. • Supporting operators for aspect composition. • Allowing the application of aspects to other aspects. • Allowing dynamic instantiation of aspects and events. • Latest EAOP article/tool updated in December 2002.http://www.emn.fr/x-info/eaop
A glimpse on events Events are objects which represent execution points of Java programs, like method call or constructorcall events. (Similar to “Join points” in AspectJ). Events describe the nature of execution points, but also their dynamic contexts. While an event is running, it can dynamically create another event or pass control to one.Thus, this gives us the dynamic power of creating a chain of events.
A companion example Login Event Main menu Event New user registration Event • Consider an ordinary web service (such as email) which needs logging in before use. • The service runs free of charge for the first 3 years. (…) • What happens if we decide to: • Charge a service fee by credit card? • And/or request a survey for each new registration and periodically? • We can use aspects in order to change events instead of changing each class separately.
The Model Login Event Main menu Event Credit card Aspect New user registration Event Survey Aspect Monitor • Aspects are weaved with events, emitted during execution of the base program. • The weaving is determined according to a sequence of events, their stack content etc. • Aspect weaving is realized using an execution monitor, which enables event sequences to be detected. • In order to keep the model simple and intuitive, from now on we will consider a sequential model for synchronous events.
The EAOP Tool • The research team has implemented a tool for EAOP, which realizes the model for Java. • This tool is composed of five parts: A preprocessor, a unique execution monitor and three libraries- for the definition of events, aspects, and composition of aspects. • No change to the JVM.
The EAOP Tool - The preprocessor • The preprocessor instruments the Java source code of the base program in order to generate events and call the entry method trace of the execution monitor. • If applied, it also instruments the source code of aspects, if one wants to define aspects of aspects (Explained later). • The instrumentation is based on the classic technique of method wrapping:
The EAOP Tool - The preprocessor (Example) • // original code • classBar{ • inti; • intfoo(intl){ • returni+l; • } • } • // instrumented code • classBar{ • inti; • intfoo(intl){ • Evente=newMethodCall(this,"foo",l); • Monitor.monitor.trace(e); • if(!e.skip) • e.res=foo_original(e.arg("l")); • Monitor.monitor.trace(newMethodReturn(e)); • returne.res; • } • intfoo_original(intl){ • returni+l; • } • } The transformation then: Introduces a method foo whose body creates a method call event Calls the monitor Calls the method foo_original Creates a method return event Calls the monitor Returns to the calee First, a method foo to be wrapped is renamed to foo_original
The EAOP Tool - Events • Events are objects which represent execution points of Java programs. • The current version of the EAOP tool supports four kinds of events: method call and return events as well as constructor call and return events. • These four kinds were sufficient for the research team experiments, but new kinds are to be added as needed (for instance, events representing field accesses or entry into an else-branch).
The EAOP Tool - Events (Cont.) • Events describe the nature of execution points but also their dynamic contexts. • For example, a method call event contains the receiver, method name, argument values, depth of the execution stack and the identity of the code currently being executed. • While an event is running, it can create or pass control to another event.Thus, this gives us the dynamic power of creating a chain of events. Login Event Main menu Event New user registration Event
The EAOP Tool - Events (Cont.) • The event also contains a boolean skip which is used to indicate that a wrapper must not call the original method. • For what purpose? • This enables an aspect to “replace” a method by another one. In this case, a field of the event object, (to be set by the aspect) defines the return value.
The EAOP Tool - Aspects • An aspect can be seen as an event transformer. • In fact, an aspect is a Java program which takes an event as a parameter, performs some computation (which may modify the event), and waits for the next event to perform on. • For instance, a security aspect may encrypt the arguments contained in the method call event it receives. • void encryptArgs(Evente){ • for (int i=0 ; i<e.argNum ; i++) • e.arg(i)=encrypt(e.arg(i)); • } Take an event And encrypt each of his arguments
The EAOP Tool - Aspects (Cont.) • The wrappers of the base program are responsible to extract the potentially modified values of the event arguments before calling the original method. • classBar{ • inti; • intfoo(intl){ • Evente=newMethodCall(this,"foo",l); • Monitor.monitor.trace(e); • if(!e.skip){ • if (SecurityAspect.encryptionPolicy) • e.res=foo_original(decrypt(e.arg("l"))); • else • e.res=foo_original(e.arg("l")); • Monitor.monitor.trace(newMethodReturn(e)); • returne.res; • } • intfoo_original(intl){ • returni+l; • } • } What for? Other aspects may react to this event, and we don’t want them to know the arguments. This can also be a remote method call of an ATM on an unsecured communication line. A new method call event is created The security aspect may encrypt the argument in this stage The argument is decrypted as needed, and the original method is called The return value of a call may be filtered by an aspect similarly
The EAOP Tool - Aspects (Cont.) • An aspect is defined by subclassing the abstract class Aspect and defining the method definition. • This method does not impose constraints on the structure of the code and uses the method nextEvent in order to obtain the following event. • This last method is blocking and waits for the monitor “waking up” the aspect with the current event. • This is implemented using Java’s threads and a library of co-routines ensuring that, at each moment, only the base program or the monitor or an aspect is active.
The EAOP Tool - The execution monitor Aspect 1 Aspect 2 Monitor • The monitor observes events emitted during execution of the base program. • It propagates the event corresponding to the current execution point to all aspects. • The architecture is sequential in two aspects: Aspects... Aspects... Aspects... • First, when the base program generates an event and calls the monitor, the base program suspends its execution. Once every aspect had the possibility to react to the current event, the monitor yields control to the base program which resumes its execution. Base program Base program An event
The EAOP Tool - The execution monitor (Cont.) Monitor • Second, the monitor propagates the current event to an aspect and waits for an aspect to finish its treatment before propagating this event to another aspect. Login Event Main menu Event New user registration Event Credit card Aspect Survey Aspect • Hence, we eliminate any possibility of concurrency between the monitor and aspects as well as among aspects. • Otherwise, the semantics of event-based systems becomes very complex.
The EAOP Tool - Aspect Composition • If events would be propagated sequentially to all aspects, the monitor would be equivalent to an iterator over an array of aspects. • Aspect composition provides greater expressiveness.
The EAOP Tool - Aspect Composition (Cont.) Op1 Aspect1 Op2 Aspect2 Aspect3 • It is possible to restrict the propagation of events to certain aspects and this decision is dynamic: • The monitor manages a binary tree in which aspects are leaves and interior nodes are aspect composition operators (which propagate events). • The monitor attempts to propagate events to every aspect by performing a DFS of the aspect tree.
The EAOP Tool - Aspect Composition (Cont.) • The tool currently proposes four binary operators for aspects compositions: • Seq- Propagates the current event (coming from the parent) to its left child and then to its right one. • Any- Propagates the event to its two children in an arbitrary order. Seq Credit card Aspect Survey Aspect
The EAOP Tool - Aspect Composition (Cont.) Fst Student survey Aspect General survey Aspect • Fst-Propagates the event to its left child, and then, if and only if the left child did not detect a crosscut, the event is forwarded to its right child. • This means the aspect doesn't want other aspects to treat this crosscut concern. • Every aspect and every composition operator maintains abooleanfieldisCrosscuttingin order to propagate this information. Thebooleanis managed explicitly by the programmer in all aspects. New user registration Event Student survey Aspect General survey Aspect isCrosscutting
The EAOP Tool - Aspect Composition (Cont.) • Cond-Propagates the event to its left child, and then, if and only if the left child did detect a crosscut, the event is forwarded to its right child. • These operators enable the elimination of conflicts caused by aspects interacting at the same crosscuts.
The EAOP Tool - Aspect Composition (Example) Instrumented base program Monitor (1) evt1 Seq (2) evt1 (3) evt1 Aspect 1 Aspect 2 (4) control • Consider, for instance, the aspect tree shown above, which is composed of two aspects. • During execution of the base program, an eventevt1 is generated (step 1), then, the monitor propagates this event to Aspect1 (step 2) and finally to Aspect2 (step 3). • When Aspect2 blocks on a call to nextEvent, control switches back to the base program (step 4).
The EAOP Tool - Aspects of aspects • The EAOP tool allows the application of aspects to other aspects. • On the implementation level, the main problem in this case consists in managing recursive calls to the execution monitor. • When an aspect is woken up by the monitor with the current event, it executes its code up to the next (blocking) call tonextEvent. • The execution of this code may emit events which are transmitted to the monitor.
The EAOP Tool - Aspects of aspects (Example) • Reconsider the differences between the previous example and the one shown above: Aspect1 has been instrumented. • Therefore, during its execution, an event evt2 is generated and passed to the monitor (step 3), which propagates it to the aspects ready to consume it, here only Aspect2 (step 4). • When Aspect2 blocks on nextEvent, control goes back to Aspect1, which resumes its execution as described previously.
The EAOP Tool - Aspects of aspects (Cont.) Base program Base program An event Statistics Aspect Statistics Aspect • This does not mean that an aspect cannot be applied to itself but this requires the creation of different instances. • For example, an instance of a statistics aspect may profile another instance of this statistics aspect, which, in turn, profiles the base program.
The EAOP Tool - Aspects of aspects (Cont.) • Although the monitor sequentializes application of aspects and guarantees that exactly one thread (base program, monitor or aspect) is active at each point of time, the base program may be composed of multiple threads. • How is it dealt? • For this case, the methodtrace of the monitor is declared to besynchronized. • Finally, note that the monitor is re-entrant in order to cope with aspects of aspects.
The EAOP Tool - Dynamic instantiation of aspects and events • Aspects and events may be: • Instantiated. • Composed. • Destroyed. • at runtime. • Examples in the following.
Interim Summary • EAOP is based on monitoring of execution events. • This model includes: • The preprocessor. • The execution monitor. • Events. • Aspects. • Aspect composition. • Application of aspects to other aspects. • Dynamic instantiation of aspects and events.
EAOP by an example We consider an e-commerce example consisting of a toy e-shop application whose functionality is to be extended by different kinds of discounts. Note that we see aspects as general structuring mechanisms which can be useful to extend a legacy application with new functionality.
Example - Aspects • Four aspects are presented in this example • AspectBingo-An aspect defining a lottery-style discount intended to improve customer fidelity: each 1000th customer pays only half of its purchase. • AspectDiscount- An aspect for regular discounts (10%, say) which are applied each time the accumulated value of purchases of a given customer exceeds a certain threshold (100 euro, say). • AspectProfiling- Allowing the shop manager to count the number of discounts granted. • AspectCheckout- An abstract aspect factoring out common functionality of the different discount aspects (Bingo & Discount).
Example – Expressive crosscuts In order to illustrate expressive crosscuts, we will have a look at an excerpt of the code of the method definition of the aspect defining regular discounts (classDiscount):
Example – Expressive crosscuts The methodnextShiptakes the client referencecustas a parameter and waits for a call to methodOrder.ship()of the base application for the clientcust Order.ship() while(true){ Ordero=nextShip(cust); accPurchases+=o.total(); System.out.println("aspect discount: accumulated purchases of "+cust+" is "+accPurchases); if(accPurchases>Discount.discountThreshold){ Evente2=nextCheckout(cust); System.out.print("aspect discount: "); floatdiscount=computeDiscount(Discount.discountRate,cust); Settmp=newHashSet(); tmp.add(newProduct("discount",-discount)); cust.shoppingCart.add(tmp); accPurchases-=Discount.discountThreshold; } } So, when an order of cust is shipped, The aspect accumulates his purchases When the accumulated purchases exceeda threshold, Order.buy() Another event is created, as the discount is given only when the customer checks out- when the method Order.buy() is called Discount is computed and “added” to the customer’s cart So, a discount is applied by this aspect only if the patternship();...;buy()corresponding to a sequence of two base method calls is matched, in a context where the accumulated purchase value exceeds the threshold
Example – Expressive crosscuts (Cont.) This last point illustrates how to express crosscuts representing relations between events using the EAOP approach. An aspect definition may wait for sequences of events, extract contextual information from these events and use this information to perform an action.
Seq Profiling Fst Bingo Discount Example – Explicit Aspect Composition Recalling the aspects Profiling, Bingo & Discount mentioned previously, how do you think the monitor’s binary tree should look? For an example of aspect composition, have a look at the initialization of the monitor:Monitor.monitor.aspects= newRoot(newSeq(newProfiling(), newFst(newBingo(3,25),newDiscount())));
Example – Explicit Aspect Composition (Cont.) This initialization defines a sequential composition of two aspects (Profilingand another composition ofBingo and Discount) such that discounts are profiled. If both discount aspects interact - at most one discount is applied. (Reminder)- The composition operatorFsttries to apply its first argument and only applies the second if the first has not been applied. This term demonstrates that aspect composition can be controlled programmatically. Monitor.monitor.aspects= newRoot(newSeq(newProfiling(), newFst(newBingo(3,25),newDiscount())));
Example – Aspects of aspects • Profiling of discounts is an example of an aspect which is applied to other aspects. Have a look at the definition of the aspectProfiling, an excerpt of which is shown here: while(true){ Evente=nextComputeDiscount(); this.isCrosscutting=true; n++; System.out.println("aspect profile: " +"number of discounts and bingos = "+n); } • In the loop, events of calls to methodcomputeDiscountare counted, a method which is defined in the abstract aspectCheckoutand inherited by theBingoandDiscountaspects. Bingo Discount Profiling
Example – Dynamic instantiation of aspects and events While we have already seen examples of dynamic event creation during runtime, and aspect creation at program start time, theDiscountaspect provides a more interesting example of dynamic instantiation of aspects. Let's have another look at an excerpt of this aspect:
Example – Dynamic instantiation of aspects In order to express that discounts are applied on a per-customer basis, we use dynamic instantiation of aspects: We wait for the next customer creation, publicvoiddefinition(){ Customercust=newCustomer(); insert(newDiscount()); while(true){ Ordero=nextShip(cust); accPurchases+=o.total(); System.out.println("aspect discount: accumulated purchases of " +cust+" is "+accPurchases); if(accPurchases>Discount.discountThreshold){ Evente2=nextCheckout(cust); System.out.print("aspect discount: "); floatdiscount=computeDiscount(Discount.discountRate,cust); Settmp=newHashSet(); tmp.add(newProduct("discount",-discount)); cust.shoppingCart.add(tmp); accPurchases-=Discount.discountThreshold; } } } Dynamically create a new instance of thediscountaspect And insert it into the tree representing the collection of aspects associated to the monitor We have already seen event instantiation This new instance is then ready to manage the new customer
Example – Possible Output Welcome to www.javazon.com ... shipping p2 to c2 aspect discount: accumulated purchases of c1 is 40.0 shipping p1 to c1 aspect profile: number of discounts and bingos = 2 apply discount of 10% to c2 aspect profile: number of discounts and bingos = 3 apply discount of 25% (i.e.5.0) to c1 ... • This demonstrates: • The different discounts crosscut the execution of the base program. • The profiling aspect applies to the discounting aspects. • The profiling action is performed before discounting.
Comparison with other AOP languages AspectJ Uses a preprocessor rather than an execution monitor, which is probably more efficient but also less flexible. Comparison with EAOP features: Systematic treatment of relationships between pointcuts:The programmer has to manually code dependencies between the involved pointcuts. Supporting operators for aspect composition:Very limited (Essentially allowing to order several aspects). Application of aspects to other aspects:Not supported. Dynamic instantiation of aspects:Not supported.
Comparison with other AOP languages JAC Comparison with EAOP features: Systematic treatment of relationships between pointcuts:The programmer has to manually code dependencies between the involved pointcuts. Supporting operators for aspect composition:A wrapper is the composition unit when composing several aspects together. JAC provides a composition aspect that can be congured by the programmer to activate some composition rules. Application of aspects to other aspects:Supported. Dynamic instantiation of aspects:An aspect can instantiate a new wrapper or destroy an unused one on demand. This allows JAC to perform dynamic weaving.
Comparison with other AOP languages CaesarJ Comparison with EAOP features: Systematic treatment of relationships between pointcuts:Aspect-related interfaces that are “aware” of each other and can collaborate. Supporting operators for aspect composition:Supported. Application of aspects to other aspects:Not supported. Dynamic instantiation of aspects:It is possible to decide dynamically which aspect implementation to deploy.
References R´emi Douence and Mario Sudholt, “A model and a tool for Event-based Aspect-Oriented Programming (EAOP)”.http://www.emn.fr/x-info/sudholt/papers/tr-11-2002.pdf “EAOP by Example”http://www.emn.fr/x-info/eaop/doc/eaop-by-example.html