45
1 Aspect Oriented Software Development Seminar Winter 2005 Presented by: Gilad Broner JAC JAC Java Aspect Components Java Aspect Components

1 Aspect Oriented Software Development Seminar Winter 2005 Presented by: Gilad Broner JAC Java Aspect Components

Embed Size (px)

Citation preview

1

Aspect Oriented Software Development Seminar

Winter 2005

Presented by: Gilad Broner

JACJACJava Aspect Java Aspect ComponentsComponents

2

Introduction Programming Model:

Programming Level Configuration Level

Distributed Application Example Two AOP Approaches: AspectJ Vs. JAC Implementation & Performance JAC as an application server; Comparison with

J2EE Conclusion

Contents:

3

Introduction JAC IS –

a framework supporting distributed application development.

Global Modification of distributed objects to handle cross-cutting concerns requires programming efforts.So, JAC’s goal is to provide the concepts for:

Distributed Dynamic Reconfigurable

programming.

4

Introduction JAC is based on ‘containers’ – a host for

software entities.In JAC’s case - a host for both business objects and non-functional aspect components.

Regarding distributed concerns, these containers are remotely accessible using RMI or CORBA.

JAC IS NOT –(yet another) new language for aspectual programming.

5

The JAC Programming Model

6

The JAC Programming Model

1. Programming level –Create new aspects, pointcuts, wrappers and advice code to handle crosscutting concerns.

2. Configuration level –Customize existing aspects to work with, and meet your application’s needs.

JAC provides 2 levels of aspect-oriented programming:

7

The Programming Level:

The programming level is basically the same as AspectJ –Provides a mean to create aspects to weave into the application: Define pointcuts Define advice code to wrap base object’s

code

Lets view a simple example…

8

Example - Lets say we have the following code for the

class Account:

public class Account {double balance=0;public void withdraw(double amount) {balance-=amount;}public void deposit(double amount) {balance+-=amount;}

} We would like to impose constraints on the

arguments and alert if needed.i.e. we wouldn’t like to allow withdrawing a negative sum…

9

Example - This would be the solution in AspectJ:

pointcut withdrawOperations(Account account, float amount) :call(public void Acount.withdraw(float)) && target(account) &&

args(amount);

before(Account account, float amount) throws AccountException: withdrawOperations( account, amount ){

if ( 0 > amount ) {client.send(“Cannot withdraw negative amount”);throw new AccountException();

}}

10

Example - And this is the solution using JAC:

public class TestAC extends AspectComponent {public TestAC(){

pointcut(“Account", “withdraw.*", AccountWrapper.class,

“check",false);}public class AccountWrapper extends Wrapper {

public void check(Interaction i ) {if ( i.args[0].doubleValue() < 0 )throw new Exception (“Negative amount is

forbidden");proceed(i);

}}

}

11

The Programming Level:

As you can tell, the code for writing the aspectwas pure java:

AspectComponent, Wrapper and Interaction are just regular classes (which should be imported).

Pointcut() is a method for defining a pointcut. Proceed() calls the base object’s (“wrapee”) code. Benefits: No need for source code! Dynamic – can define pointcuts at runtime! Applying OOP principals such as inheritance

on aspects!

12

Pointcut Semantics

Pointcut must be specified through 3 sub-expressions (object, class, method) and a wrapping method.

Pointcut expressions are based on regular expressions but can include specific keywords

Regular expressions and keywords can be composed with logical operators: || (logical or) && (logical and) ! (logical not)

13

Pointcut type

Keyword/expressionMatching entity

ClassALLAll the classes

<name>+All the children classes of name

<name>-All the parent classes of name

ObjectALLAll the instances

<rootName>path exprAny object in relation with rootName through the given path

MethodALLAll the methods

STATICSAll the static methods

CONSTRUCTORSAll the constructors

SETTER/GETTER(name)Setter/getter method for field name

COLSETTERS/COLGETTERSAll setters/getters for collections

REFSETTERS/REFGETTERSAll setters/getters for references

FIELDSETTERS/FIELDGETTERSAll setters/getters for primitive fields

MODIFIERS/ACCESSORSAll methods that change/read the state

Pointcut Semantics

14

Dynamic Wrappers An object that defines behaviors that

extend the behavior of a regular object. We say that the base object is wrapped.

There are 3 kinds of methods a dynamic wrapper can define:

1. Wrapping methods –applied before and/or after the wrapee’s method (same as ‘arround’ in AspectJ).

2. Role methods –can extend base object’s interface (similar to the ‘introduce’ statement in AspectJ).

3. Exception Handlers –Handles raised exceptions in the object the wrapper is applied to.

15

Dynamic Wrapperspublic class MyWrapper extends Wrapper {

public MyWrapper(AspectComponent ac) {super(ac);}public Object aWrappingMethod(Interaction interaction) {

Object ret;[...] // before codeproceed(interaction);[...] // after codereturn ret;

}

public Object aRoleMethod(Wrappee wrappee,.../*params*/) {

[...]

}

public void anExceptionHandler(Interaction interaction, AnException e) {

[...]

}

}

16

Dynamic Wrappers Local state –

A wrapper can wrap a unique object.In this case its state is local to the wrappee.

wrapper1 wrapper2 wrapper3

17

Dynamic Wrappers Shared State –

The same wrapper can also wrap several objects. In this case its state is shared by the wrapees For example, when the pointcuts are

mutually exclusive.wrapper

18

The Configuration Level: Separation of concerns and writing the aspects

to handle them is not always trivial.Definitely not in distributed systems.

OOP has taught us that reusing components should be encouraged

JAC has provided us with a range of ready to use, pre-developed aspects, and a systematic way to configure them according to our needs Reusable aspects !

19

The Configuration Level: These are some of the ready made aspect JAC

provides(distributed systems oriented):

Persistence Authentication Session Transactions Deployment Load-balancing Broadcasting Data consistency Synchronization Remote access Integrity User GUI (SWING and Web)

20

The Configuration Level: Configuring an aspect with JAC consists of

defining a sequence of calls to the public methods of the AC. Hence, this is the responsability of the AC developer to define such public methods, and by that, define what can be configured in an AC.

A configuration file is simply a text file with the “.acc” extension.

Syntax: Parameters are separated by spaces.<configuration_method> [<value> ...];

Common configurations can be shared between applications using the ‘include’ directive:include "org/objectweb/jac/aspect/gui/gui.acc"

21

The Configuration Level: Example – GUI aspect component: We have a ready GUI aspect component and

we want to customize it to our needs.i.e. set the title name, the geometry and position of different panes, their content and so on.

To do that, we should check the API and use the configuration methods with the appropriate parameters.

setTitle myGUI "Invoices";setSubPanesGeometry myGUI 2 HORIZONTAL {false,false};setPaneContent myGUI 0 Object {"default","invoices#0"};

22

Distributed Application Examplepublic class RingElement {

public RingElement next;

public setNext (RingElement next) {

this.next = next;

}

public void roundTrip(int steps) {

if (step>0) next.roundTrip(steps-1);

}

}

23

Distributed Application Examplepublic class Ring {

public static void main(String args[]) {RingElement re0 = new RingElement();RingElement re1 = new RingElement();RingElement re2 = new RingElement();re0.setNext(re1);re1.setNext(re2);re2.setNext(re0);re2.roundTrip(9);

}}

24

Distributed Application Example Now, one can develop an AC to deploy these

3 objects in JAC containers, or, better yet, use the existing DeploymentAC aspect provided with JAC.

Let’s also associate a configuration file that: Remotely install (AC method deploy) the 3

instances: re0, re1 & re2 on containers bound to 3 different RMI names.

Create a client stub for each of the ring elements;A stub delegates method calls to remote instances.

25

Distributed Application ExampleThis would be the configuration file (ring.acc):

deploy “re0” “rmi://host0/s0”

createAsyncStubFor “re0” “rmi://host0/s0” “rmi://host1/s1”

deploy “re1” “rmi://host1/s1”

createAsyncStubFor “re1” “rmi://host1/s1” “rmi://host2/s2”

deploy “re0” “rmi://host2/s2”

createAsyncStubFor “re0” “rmi://host2/s2” “rmi://host0/s0”

26

Distributed Application Example

ringElement0

ringElement2

ringElement1

CentralizedVersion

Deployment Aspect

DistributedVersion

Stub

Stub

Stub

S0

S2S1

27

Distributed Application Example We see that we did not have to write any

special code Remote communication details were

hidden from the ring elements objects. Let’s extend our example and add tracing.

Tracing is hard in distributed environments…Or is it?

28

Distributed Application Examplepublic class TracingAC extends AspectComponent {

Trace trace = new Trace();TracingAC() {

pointcut(“RingElement”, “!roundTrip(int):void”, TracingAC.TracingWrapper,

“tokenPassed”);pointcut(“RingElement”, “?roundTrip(int):void”,

TracingAC.TracingWrapper, “tokenArrived”);}class TracingWrapper extends Wrapper {

public Object tokenPassed (Interaction i) {trace.trace(“Token passed by “+I.wrapee);return proceed();

}public Object tokenArrived (Interaction i) {

trace.trace(“Token arrived in “+I.wrapee);return proceed();

} } }

29

Distributed Application Example

We note that nothing of the tracing aspect code mentions distribution!

What does this mean? The code works both for centralized and

distributed versions. A true separation of concerns – the tracing

concern from the distributed concern. Pointcut semantics are inherently distributed. This reinforced expressiveness allows the

modularizing of extensions that crosscut distributed systems.

30

Two AOP approches:JAC & AspectJ

AspectJJAC

Java language extensionAOP framework, application

server

New grammar to express aspects

Aspects written in pure Java

Working on source code.Each modification needs

compiling

Working on bytecode allowing to add, remove or modify aspects

dynamically

Does not handle distributionAutomatically distributes aspects

on distant servers

Only allows aspect developement

Allows aspect developement and aspect configuration

IDE support for JBuilder, Forte, and Emacs

UML IDE supporting aspects

No pre-developed configurable aspects

Set of pre-developed configurable aspects

31

Implementation of JAC JAC is entirely written in Java.

The aspect weaving is performed at class load time using the bytecode engineering library BECL, and Java’s Reflection mechanism.

This gives us the ability to weave application whose source-code is not available.

The idea is to use a customized classloader that reads the class file and modifies (translates) the stream contents before actually defining and registering the new class within the JVM.

2 translators were used: Javassist 2.0 and BECL Javassist is fast and easy to use, however, it is

restricted. BECL is more complex and slower, but more powerful

and the bytecode produced is better.

JAC Kernel (system objects)

Application repository

AC manager

Composition aspect

JAC container

JAC loader (uses BCEL)R.T.T.I

Business classes

Aspect

Aspect configuration

Aspect Component

Business object

Java loader

tags

generates metamodel

new

calls

dispatchesWrappers

creates

Pointcuts

creates

calls

ordersApplication descriptor

(.jac) reads

JAC.prop

reads

loads

Wrapable classes

translates

JAC’s dynamic aspectsinstallation process

33

Performance Measurements

Type of Calls #of callsTotal Time

(ms)Time per

CallOverhead

Regular object calls

6,000,00055~9.16ns

Reflective calls60,00047~0.78s(A )x 85

JAC object calls60,00061~1.00 s(A )x 111

(0 wrapper)(B )x 1.29

JAC (1 wrapper)60,00085~1.41 s(C + )41%

JAC (2 wrappers)60,000110~1.83 s(C + )83%

JAC (3 wrappers)60,000130~2.16 s(C + )116%

34

JAC as an alternative to Application Servers An application server is:

An Integrated Development Environment A container for business classes providing

technical services Complex Expensive Used in 45% of new projects

33 main servers, 23 in Java (Websphere, Weblogic…)

35

J2EE Application Server Some concerns are well-modularized

(persistence, transaction) EJBs (Entity Java Beans) have to be

implemented in a special fashion and sometimes need to explicitly call the technical services of the container

These technical services are harcoded, they can’t be modified => once you’ve reached the limits of the container, extra concerns must be implemented by hand

Practically, concerns still crosscut.

36

JAC as an Application Server An AOP framework providing

An UML IDE supporting AOP Ready to use configurable aspects A container for pure business classes and

technical aspects A kernel weaving aspects and business logic

at runtime An administration interface A tutorial, development guide, programming

guide, and examples set

37

JAC’s UML IDE

38

J2EE application servers: concerns are crosscutting

JAC: no more concerns crosscutting

39

Aspects are woven at runtime

40

JAC Vs J2EE App. ServersJ2EE application serverJAC application server

Container for EJBsContainer for pure business classes and technical aspects

Hardcoded technical services, can’t be modified

Pre-developed technical aspects, can be modified

EJBs have to be configured to call for technical services: crosscut of

concerns

Each technical aspect configured in a single file and weaved at runtime:

good modularization

Heavy, always including all hardcoded technical services

Light, each technical aspect is only added if useful

Requires many development skillsEasy to learn and use

Separation of concerns only when previously developped concerns

Newly identified concerns can be modularized by implementing new

aspects

ExpensiveFree under LGPL

JAC benefits from AOP advantages: traceability, better productivity, code reuse, code quality, easy evolution of applications

41

Conclusions Pros:

JAC aims to be applied to distributed objects. i.e. a given crosscut can change object on distributed hosts.

JAC uses a container mechanism that hosts both business object and aspect components, accessible through RMI or CORBA.

JAC provides a set of pre-developed aspects and a mechanism for configurating existing aspects.

JAC provides means to rapid development.

42

Conclusions Cons:

Very high run-time overhead!! Currently(?) only supports Java Pointcuts are only defined on methods Still not commercially used

43

In The Future?… Overcoming the reflection overhead

needed by dynamic (un)weaving of aspects.

Better support for aspect configuration – several configuration languages with well-defined grammar.

Eclipse Plug-in J2EE integration

44

Resources: Renaud Pawlak, Lionel Seinturier, Laurence Duchien,

Laurent Martelli, Fabrice Legond-Aubry and Gerard Florin:Aspect Oriented Software Development with Java Aspect Components (Chapter 16).

Renaud Pawlak, Laurence Duchien, Gerard Florin, Fabrice Legond-Aubry, Lionel Seinturier, Laurent Martelli:JAC: an Aspect-Based Distributed Dynamic Framework(December 5, 2002)

JAC website - http://jac.objectweb.org About J2EE – http://java.sun.com/J2EE

45

The End – Thank You!