Download presentation
Presentation is loading. Please wait.
1
1 Event-based Aspect-Oriented Programming (EAOP) Technion Based on a paper of: R´emi Douence and Mario Sudholt Based on slides by: Omer Ganot
2
2 Agenda Motivation. The EAOP Model. The EAOP Tool. EAOP by an example. Comparison with other AOP languages.
3
3 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
4
4 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 in a fully general way. Allowing dynamic instantiation of aspects and events. Motivation (Cont.)
5
5 Events are objects which represent execution points of Java programs, like method call or constructor call 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 glimpse on events
6
6 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. A companion example Login Event New user registration Event Main menu Event
7
7 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 Model Login Event New user registration Event Main menu Event Credit card Aspect Survey Aspect Monitor
8
8 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
9
9 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
10
10 // original code class Bar { int i; int foo(int l){ return i + l; } // instrumented code class Bar { int i; int foo(int l) { Event e = new MethodCall(this, "foo", l); Monitor.monitor.trace(e); if (!e.skip) e.res = foo_original(e.arg("l")); Monitor.monitor.trace(new MethodReturn(e)); return e.res; } int foo_original(int l) { return i + l; } The EAOP Tool - The preprocessor (Example) First, a method foo to be wrapped is renamed to foo_original 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 callee
11
11 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
12
12 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. The EAOP Tool - Events (Cont.) Login Event New user registration Event Main menu Event
13
13 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 - Events (Cont.)
14
14 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(Event e) { for (int i=0 ; i<e.argNum ; i++) e.arg(i)=encrypt(e.arg(i)); } The EAOP Tool - Aspects And encrypt each of his arguments Take an event
15
15 The wrappers of the base program are responsible to extract the potentially modified values of the event arguments before calling the original method. class Bar { int i; int foo(int l) { Event e = new MethodCall(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(new MethodReturn(e)); return e.res; } int foo_original(int l) { return i + l; } The EAOP Tool - Aspects (Cont.) The security aspect may encrypt the argument in this stage A new method call event is created 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 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.
16
16 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 - Aspects (Cont.)
17
17 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: The EAOP Tool - The execution monitor Base program An event Aspect 1Aspect 2 Monitor Base program 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.
18
18 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. The EAOP Tool - The execution monitor (Cont.) 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. Login Event New user registration Event Main menu Event Credit card AspectSurvey Aspect Monitor
19
19 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
20
20 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.) Op1 Aspect1 Op2 Aspect2Aspect3
21
21 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. The EAOP Tool - Aspect Composition (Cont.) Seq Credit card Aspect Survey Aspect
22
22 Fst- Propagates the event to its left child, and then, if and only if the left child did not detect a crosscut (=a joinpoint), 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 a boolean field isCrosscutting in order to propagate this information. The boolean is managed explicitly by the programmer in all aspects. The EAOP Tool - Aspect Composition (Cont.) Fst Student survey Aspect General survey Aspect New user registration Event Student survey AspectGeneral survey Aspect isCrosscuttin g
23
23 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 (joinpoints). The EAOP Tool - Aspect Composition (Cont.)
24
24 Consider, for instance, the aspect tree shown above, which is composed of two aspects. During execution of the base program, an event evt1 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). Instrumented base program Seq Aspect 1Aspect 2 (1) evt1 (2) evt1(3) evt1 Monitor (4) control The EAOP Tool - Aspect Composition (Example)
25
25 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 to nextEvent. The execution of this code may emit events which are transmitted to the monitor. The EAOP Tool - Aspects of aspects
26
26 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 (Example)
27
27 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.) Base program An event Statistics Aspect Base program Statistics Aspect
28
28 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 with? For this case, the method trace of the monitor is declared to be synchronized. Finally, note that the monitor is re-entrant in order to cope with aspects of aspects. The EAOP Tool - Aspects of aspects (Cont.)
29
29 Aspects and events may be: Instantiated. Composed. Destroyed. at runtime. Examples in the following. The EAOP Tool - Dynamic instantiation of aspects and events
30
30 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. Interim Summary
31
31 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.
32
32 Example - Aspects Four aspects are presented in this example 1.Aspect Bingo - An aspect defining a lottery-style discount intended to improve customer fidelity: each 1000th customer pays only half of its purchase. 2.Aspect Discount - 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). 3.Aspect Profiling - Allowing the shop manager to count the number of discounts granted. 4.Aspect Checkout - An abstract aspect factoring out common functionality of the different discount aspects ( Bingo & Discount ).
33
33 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 (class Discount ): Example – Expressive crosscuts
34
34 The method nextShip takes the client reference cust as a parameter and waits for a call to method Order.ship() of the base application for the client cust while (true) { Order o = nextShip(cust); accPurchases += o.total(); System.out.println("aspect discount: accumulated purchases of " + cust + " is " + accPurchases); if (accPurchases > Discount.discountThreshold) { Event e2 = nextCheckout(cust); System.out.print("aspect discount: "); float discount = computeDiscount(Discount.discountRate, cust); Set tmp = new HashSet(); tmp.add(new Product("discount", -discount)); cust.shoppingCart.add(tmp); accPurchases -= Discount.discountThreshold; } Example – Expressive crosscuts So, when an order of cust is shipped, The aspect accumulates his purchases When the accumulated purchases exceed a threshold, Another event is created, as the discount is given only when the customer checks out- when the method Order.buy() is called So, a discount is applied by this aspect only if the pattern ship() ;...; buy() corresponding to a sequence of two base method calls is matched, in a context where the accumulated purchase value exceeds the threshold Order.ship() Order.buy() Discount is computed and “added” to the customer’s cart
35
35 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.
36
36 Example – Explicit Aspect Composition Recalling the aspects Profiling, Bingo & Discount mentioned previously, how do you think the monitor’s binary tree should look? Seq Profiling Fst BingoDiscount For an example of aspect composition, have a look at the initialization of the monitor: Monitor.monitor.aspects = new Root(new Seq(new Profiling(), new Fst(new Bingo(3,25),new Discount())));
37
37 Example – Explicit Aspect Composition (Cont.) This initialization defines a sequential composition of two aspects ( Profiling and another composition of Bingo a nd Discount ) such that discounts are profiled. If both discount aspects interact - at most one discount is applied. (Reminder)- The composition operator Fst tries 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 = new Root(new Seq(new Profiling(), new Fst(new Bingo(3,25),new Discount())));
38
38 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 aspect Profiling, an excerpt of which is shown here: while (true) { Event e = nextComputeDiscount(); this.isCrosscutting = true; n++; System.out.println("aspect profile: " + "number of discounts and bingos = " + n); } In the loop, events of calls to method computeDiscount are counted, a method which is defined in the abstract aspect Checkout and inherited by the Bingo and Discount aspects. Profiling BingoDiscount
39
39 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, the Discount aspect provides a more interesting example of dynamic instantiation of aspects. Let's have another look at an excerpt of this aspect:
40
40 Example – Dynamic instantiation of aspects public void definition() { Customer cust = newCustomer(); insert(new Discount()); while (true) { Order o = nextShip(cust); accPurchases += o.total(); System.out.println("aspect discount: accumulated purchases of " + cust + " is " + accPurchases); if (accPurchases > Discount.discountThreshold) { Event e2 = nextCheckout(cust); System.out.print("aspect discount: "); float discount = computeDiscount(Discount.discountRate,cust); Set tmp = new HashSet(); tmp.add(new Product("discount", -discount)); cust.shoppingCart.add(tmp); accPurchases -= Discount.discountThreshold; } 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, Dynamically create a new instance of the discount aspect And insert it into the tree representing the collection of aspects associated to the monitor This new instance is then ready to manage the new customer We have already seen event instantiation
41
41 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.
42
42 Comparison with AspectJ Uses a preprocessor rather than an execution monitor, which is 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 in as much generality or flexibility. Dynamic instantiation of aspects: Not supported.
43
43 The End
44
44 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 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 http://www.emn.fr/x-info/eaop/doc/eaop-by-example.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.