Message-ID: <231627546.22096.1406844215516.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_22095_1077752820.1406844215515" ------=_Part_22095_1077752820.1406844215515 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Authors: Alexandre Vasseur=20
This tutorial tries to walk you through the idea of Aspects deployment w=
e have introduced in AspectWerkz.
This tutorial is u= sing AspectWerkz 2.0.
You will learn how to integrat= e aspects and aspects libraries as regular components in your J2EE environm= ent through the help of META-INF/aop.xml and WEB-I= NF/aop.xml files.
It won't give a lot of detail on what is AOP, how the aspects are implem=
ented using AspectWerkz, etc...
If you want to read more about that f= irst, you can read and practice the
In this tutorial, we will use Tomcat 5 samples applicat=
ions since everyone can download it easily. Some more articles are on their=
way to explain integration on BEA WebLogic Server and
I assume you have a Java 1.4 correctly installed.=20
Download the 2.0 AspectWerkz release (or lates=
t RC) and unzip it into a relevant location. I will use C:\aw\aspe=
This tutorial is based on the 2.0 version of AspectWerkz.
The latest distribution can be found here.= p>=20
After installation you need to set the
ASPECTWERKZ_HOME environment variable to point to the installation directory.
Th= is is because quite a few of the scripts use this to find the required libr= aries and those scripts aims at making this tutorial
simpler to under= stand by focussing on the topic to cover.
Below is some samples (for Windows - adapt for Unix/Linux and do an
You should check that your
VA_HOME is properly configured as well.
Then you need to install Tomcat 5, since we wil=
l deploy our aspects in Tomcat samples applications.
The distribution= 5.0.25 can be found here (choose = jakarta-tomcat-5.0.25.zip). Download and unzip i= t to somewhere relevant.
I will use C:\aw\jakarta-tomcat-5.0.25\
After installation, it is wise to check the Tomcat installation= by launching=20 =20
and open your browser to (this link should work) http://localhost:8080/= to access the main Tomcat page.=20
Shutdown Tomcat by closing the MSDos window or doing some
You can check the attachments if you don't want to code or build th= e single class as explained in the following sections. I advise you to prac= tice some instead of downloading them.=20
Since Tomcat provides several sample applications, we have to provide a =
test Aspect that we can use and reuse to understand the different aspect de=
To make things easy to understand, we will not go i= nto the details.
Just notice that it is yet another tracing aspect, a= nd that the aspect method named "trace" will be our around advice= .
This aspect will print some information about himself, like the hashcode= of the ClassLoader that created it (I do not use toString() since Tomcat i= s too verbose, but you can give it a try).=20
We will pack this Aspect in different ways in the next parts of the tuto=
rial, but we can already compile it as a regular Java class.
I will w= rite this Aspect in C:\aw\tomcat, in the package
This is a standard Java class, and can be compiled with
avac ... demoAOP\HelloWorld.java to produce the regular ".class=
" file in the "target" directory. We need to link to the asp=
ectwerkz jar due to this JoinPoint class that is used in the source code of=
the DemoAspect, so we should write
javac -classpath %ASPECTWER=
KZ_HOME%\lib\aspectwerkz-2.0.jar demoAOP\DemoAspect.java, but usage =
of the environment variable makes sense here to set the
From there we have several options if we are using online mode (class lo= ad time weaving), which is standardized in Java 1.5 with JSR-163 but that AspectWerkz support even for Java 1.3:=20
.classfile in the
WEB-INF/classes= code> directory and write a
WEB-INF/aop.xmlAOP XML descriptor= to define the aspect pointcut within the web application scope
.classfile in a jar file, and add a
META-INF/aop.xmlAOP XML = descriptor will affect all web applications due to java ClassLoader hierach= ies.
WEB-INF/aop.xmland some aspects at the system wide le= vel with a
When using offline mode (post compilation of your classes before deploym=
ent), the same results can be achieved, though it can be sometimes a bit tr=
icky to remember to weave the application both for its own aspects and for =
the system level aspects.
We will not go through offline mode in this= tutorial (refer to HelloWorld tutorial).
To keep things simple, we will always use the following pointcut that wi= ll allow to pick out all public methods execution:=20 =20
We carrfully exclude the package name of the aspect thus:=20 =20
As a consequence, the following AOP XML descriptor will be used.
<= em>Write this file in C:\aw\tomcat\META-INF\aop.xml for now.
Note that we could have simplified this XML by defining our
pect with annotations (see HelloWorld tutorial for this).
Some of you will have already understand that when the aspect is deployed within the web application, its weaving scope will not b=
e the same: the
pointcut will pick out public method from classes really loaded by the web application (e.g. not
Since we will use online mode, we need to alter some of the Tomcat start= up scripts. If we were using BEA JRockit or Java 1.5, it would be a matter = of adding a single JVM option.=20
Since I will not make any assumption on your JVM environment, I will ski=
p the interesting details in this part and we will use a non-optimized but =
out-of-the-box solution that ships with the AspectWerkz distribution: the c=
ommand line tool in
ASPECTWERKZ_HOME/bin/aspectwerkz (read doc=
Note: for Tomcat 5.0.27 and after, please refer to the comments = at the end of the article=20
Open the Tomcat startup script
The main idea is to set=
ASPECTWERKZ_HOME in this script and replace
Locate at the end of the script:=20 =20
and alter it as follows:=20 =20
For a complete understanding of the different options and other modes, r=
efer to the AspectWerkz weaving schemes documentati=
Note that as a consequence of using the command line tool for= simplicity here, we will run the Tomcat with -Xdebug JVM mode (with Java 1= .4).
Do not startup Tomcat for now.=20
This section explains the main step required and consequences of packagi=
ng an aop.xml file in your deployed application (WEB-INF/aop.xml).
Th= e big picture is as follow:
In this section we decide to package our aspect as a regular class of th=
e web application, and we define the pointcut and activate the aspect
= through the help of the WEB-INF/aop.xml file.
As a consequence, only= the classes of the web application will be eventually weaved (if the point= cut match), even if the pointcut is supposed to match everything (all publi= c method execution as explained in the previous section). In fact, the depl= oyment mechanism tied to the web application deployment add an unbreakable = isolation, and only public method execution of classes loaded belon= ging to the web application will be targetted by our
Lets apply our Aspect to the Tomcat jsp-examples web application:=20 =20
Lets add the AOP XML descriptor to the web app:=20 =20
To sum up we have only=20
Lets start Tomcat with the
bin/startup.bat script =
(which will delegate to our modified
On the stdout window some AspectWerkz message should appear, wi= th some verbose blocks of logs about aop.xml deployment.
And immediately we should see our DemoAspect at work, with some informat= ion about the web app initialization.=20 =20
You will notice this 7088278 hashcode here, which indeed match = the web app classloader hashcode as logged at deployment time.=20
By browsing the jsp-samples web application, we learn =
more about public method execution of the web application.=
Off course, no information about Tomcat itself since the aspects was= deployed within the web application.
This section is interesting, and I hope you have already some new ideas =
to deploy your aspects in your J2EE application with AspectWerkz.
But= what happens if you would like to provide some ready to use out of the box= aspects, completely obliouvness to the user ?
Shutdown Tomcat for the next section.=20
This section explains the main step required to package a "drop=
n weave" aspect jar file that contains a META-INF/aop.xml file, =
and consequence of adding this jar file in your deployed application (WEB-I=
The big picture is as follow:
In this section we will achieve the same result as in p=
revious section, but in a slightly different way.
We will package our= Aspect in a jar file, and we will add the
aop.xml file in the=
jar file itself in the special
META-INF folder, so that our j=
ar file is completely self defined.
The jar file is then added to the= web application as a regular jar file.
As a consequence of web appli= cation deployment, the aop.xml file from the aspect jar file will still be = deployed by the web application class loader, and the same behavior will ha= ppen: the aspect will target classes laoded by the web application.
Lets prepare the jar file (regular java / jar packaging rules applies)= p>=20=20
You will end up in having a C:\aw\aspect.jar file ready to use.=20
Lets add this drop n weave jar file to the Tomcat servlet s= amples web application.=20 =20
Note: if you still have the jsp sample application with= its aspect, you will see some messages as well as detailled in previous se= ction.=20
The following should appear on stdout: some messages about the deploymen=
t of our aspect.jar file and its
META-INF/aop.xml AOP XML depl=
oyment descriptor and right after that, our DemoAspect at work.
Something interesting: if you kept the jsp sample application aspect, yo= u realize that you deployed the same aspect class and the same aop.xml file= twice. This is made possible due to class loader hierarchies. They don't s= ee each other and the naming space is guaranteed by the class load= er and by the AspectWerkz AOP container.=20
Browse some in the servlet sample applicatio= n and see the DemoAspect at work.=20
We will learn something more about that in the next part...
Have y= ou heard that "Aspects are everywhere" (c) The AspectWerkz team ?=
Go on reading !
This section explains the main step required to package several =
META-INF/aop.xml files and understand what happens then.
The= big picture is as follow:
In this section we keep the packaging of the previous section, but we ad= d a new aspect.jar file at the Tomcat wide level (system classpath) to chec= k if we can have there a more wider pointcut. Remember the pointcut is supp= osed to match all public method, but that AOP container behavior enforce is= olation based on the regular class loader hierarchy.=20
There is an important things to do.
AspectWerkz AOP container requ= ires sysem id as defined in the aop.xml file to be unique = within a class loader hierarchy. That means:
Lets change the
META-INF/aop.xml then and may be lets reduc=
e some the scope of the pointcut so that we don't have too much data:
The C:\aw\tomcat\META-INF\aop.xml file will look like=20 =20
Lets package a new weave n drop aspect jar file:=20 =20
You will end up in having a C:\aw\aspect-system.jar file ready to use.= p>=20
Copy this file to C:\aw\jakarta-tomcat-5.0.25\common\lib which = contains some regular jar for Tomcat.=20
Now we have both DemoAspect of each system at work:=20 =20
To go on in practicing, you can check that the AOP container will refuse= deploying the same system id within the same class loader hierarchy, or yo= u can find some interesting pointcut tied to Tomcat and Servlet 2.3 API, or= try to change the Aspect to have it a bit more usefull.