Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
AspectWerkz
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>Authors: <a class="confluence-link confluence-userlink" data-username="avasseur" href="/display/~avasseur" data-linked-resource-id="2613" data-linked-resource-type="userinfo" username="avasseur" data-linked-resource-default-alias="Alexandre Vasseur" data-base-url="http://docs.codehaus.org">Alexandre Vasseur</a></p> <h3>Introduction</h3> <p>This tutorial tries to walk you through the idea of Aspects deployment we have introduced in <strong>AspectWerkz</strong>.<br /> This tutorial is using <strong>AspectWerkz 2.0</strong>.<br /> You will learn how to integrate aspects and aspects libraries as regular components in your J2EE environment through the help of <strong>META-INF/aop.xml</strong> and <strong>WEB-INF/aop.xml</strong> files.</p> <p>It won't give a lot of detail on what is AOP, how the aspects are implemented using AspectWerkz, etc...<br /> If you want to read more about that first, you can read and practice the</p> <ul class="alternate"> <li><a href="http://docs.codehaus.org/display/AW/Hello+World">HelloWorld tutorial</a></li> <li><a href="http://docs.codehaus.org/display/AW/Hijacking+Hello+World">HelloWorldHijacked tutorial</a></li> </ul> <p>In this tutorial, we will use <strong>Tomcat 5</strong> samples applications since everyone can download it easily. Some more articles are on their way to explain integration on <strong>BEA WebLogic Server</strong> and <strong>Apache Geronimo</strong>, where the same concepts apply.</p> <h3>Installation</h3> <p>I assume you have a Java 1.4 correctly installed.</p> <p><em>Download</em> the <strong>2.0 AspectWerkz release</strong> (or latest RC) and <em>unzip</em> it into a relevant location. I will use C:\aw\aspectwerkz\<br /> This tutorial is based on the 2.0 version of AspectWerkz.</p> <p>The latest distribution can be found <a href="http://aspectwerkz.codehaus.org/releases.html">here</a>.</p> <p>After installation you need to set the <code>ASPECTWERKZ_HOME</code> <em>environment variable</em> to point to the installation directory.<br /> This is because quite a few of the scripts use this to find the required libraries and those scripts aims at making this tutorial<br /> simpler to understand by focussing on the topic to cover.</p> <p>Below is some samples (for Windows - adapt for Unix/Linux and do an <code>export ASPECTWERKZ_HOME</code>)<br /> You should check that your <code>JAVA_HOME</code> is properly configured as well.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> set ASPECTWERKZ_HOME=C:\aw\aspectwerkz echo %ASPECTWERKZ_HOME% echo %JAVA_HOME% </pre></td></tr></table> <p>Then you need to <em>install</em> <a href="http://jakarta.apache.org/tomcat/">Tomcat 5</a>, since we will deploy our aspects in Tomcat samples applications.<br /> The distribution 5.0.25 can be found <a href="http://apache.crihan.fr/dist/jakarta/tomcat-5/v5.0.25/bin/">here</a> (choose <em>jakarta-tomcat-5.0.25.zip</em>). <em>Download</em> and <em>unzip</em> it to somewhere relevant.<br /> I will use C:\aw\jakarta-tomcat-5.0.25\</p> <p>After installation, it is wise to <em>check the Tomcat installation</em> by launching</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> C:\aw\jakarta-tomcat-5.0.25\bin\startup.bat (or use .sh on Unix/Linux) </pre></td></tr></table> <p>and open your browser to (this link should work) <a href="http://localhost:8080/">http://localhost:8080/</a> to access the main Tomcat page.</p> <p>In the next parts of the tutorial, we will go thru some Tomcat servlet samples <a href="http://localhost:8080/servlets-examples/">http://localhost:8080/servlets-examples/</a> and jsp examples <a href="http://localhost:8080/jsp-examples/">http://localhost:8080/jsp-examples/</a>.</p> <p><em>Shutdown</em> Tomcat by closing the MSDos window or doing some <code>CTRL-C</code> twice.</p> <p>You can check the <a href="http://docs.codehaus.org/display/AW/How+to+integrate+Aspects+in+J2EE+apps+with+aop.xml?showAttachments=true#attachments">attachments</a> if you don't want to code or build the single class as explained in the following sections. I advise you to practice some instead of downloading them.</p> <h3>The reusable Aspect</h3> <p>Since Tomcat provides several sample applications, we have to provide a test Aspect that we can use and reuse to understand the different aspect deployment schemes.<br /> To make things easy to understand, we will not go into the details.<br /> Just notice that it is yet another tracing aspect, and that the aspect method named "trace" will be our around advice.</p> <p>This aspect will print some information about himself, like the hashcode of the ClassLoader that created it (I do not use toString() since Tomcat is too verbose, but you can give it a try).</p> <p>We will pack this Aspect in different ways in the next parts of the tutorial, but we can already compile it as a regular Java class.<br /> I will write this Aspect in C:\aw\tomcat, in the package <code>demoAOP</code></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> mkdir C:\aw\tomcat\demoAOP cd C:\aw\tomcat\demoAOP </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package demoAOP; import org.codehaus.aspectwerkz.joinpoint.JoinPoint; /** Our Aspect is just a Java class */ public class DemoAspect { private int indent = -1; /** a method for pretty formatting */ private void log(String message) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.print("DemoAspect[" + this.getClass().getClassLoader().hashCode() + "] "); System.out.println(message); } /** An around advice is just a method with this JoinPoint parameter */ public Object trace(JoinPoint jp) throws Throwable { indent++; log("--> " + jp.toString()); Object result = jp.proceed(); // will call the next advice or target method, field access, constructor etc log("<--"); indent--; return result; } } </pre></td></tr></table> <p>This is a standard Java class, and can be <em>compiled</em> with <code>javac ... demoAOP\HelloWorld.java</code> to produce the regular ".class" file in the "target" directory. We need to link to the aspectwerkz jar due to this JoinPoint class that is used in the source code of the DemoAspect, so we should write<br /> <code>javac -classpath %ASPECTWERKZ_HOME%\lib\aspectwerkz-2.0.jar demoAOP\DemoAspect.java</code>, but usage of the environment variable makes sense here to set the <code>CLASSPATH</code>:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> cd C:\aw\tomcat %ASPECTWERKZ_HOME%\bin\setEnv javac demoAOP\DemoAspect.java </pre></td></tr></table> <p>From there we have several options if we are using online mode (class load time weaving), which is standardized in Java 1.5 with <a href="http://www.jcp.org/en/jsr/detail?id=163">JSR-163</a> but that AspectWerkz support even for Java 1.3:</p> <ul> <li>copy the aspect <code>.class</code> file in the <code>WEB-INF/classes</code> directory and write a <code>WEB-INF/aop.xml</code> AOP XML descriptor to define the aspect pointcut within the web application scope</li> <li>package the aspect <code>.class</code> file in a jar file, and add a <code>META-INF/aop.xml</code> in this jar file. Copy this jar in the <code>WEB-INF/lib</code> directory.</li> <li>instead of deploying our aspect within a specific web application, we could copy the jar at the system classpath level. The interesting thing to understand is that such a system wide <code>META-INF/aop.xml</code> AOP XML descriptor will affect all web applications due to java ClassLoader hierachies.</li> <li>and of course we could have both: some aspects at the application level with a <code>WEB-INF/aop.xml</code> and some aspects at the system wide level with a <code>META-INF/aop.xml</code></li> </ul> <p>When using offline mode (post compilation of your classes before deployment), the same results can be achieved, though it can be sometimes a bit tricky to remember to weave the application both for its own aspects and for the system level aspects.<br /> We will not go through offline mode in this tutorial (refer to HelloWorld tutorial).</p> <p>To keep things simple, we will always use the following pointcut that will allow to pick out all public methods execution:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> execution(public * *..*.*(..)) </pre></td></tr></table> <p>We carrfully exclude the package name of the aspect thus:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> execution(public * *..*.*(..)) && !within(demoAOP..*) </pre></td></tr></table> <p>As a consequence, the following AOP XML descriptor will be used.<br /> <em>Write</em> this file in C:\aw\tomcat\META-INF\aop.xml for now.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" "http://aspectwerkz.codehaus.org/dtd/aspectwerkz.dtd"> <aspectwerkz> <system id="webapp"> <aspect class="demoAOP.DemoAspect"> <pointcut name="allPublic" expression="execution(public * *..*.*(..)) AND !within(demoAOP..*)"/> <advice name="trace" type="around" bind-to="allPublic"/> </aspect> </system> </aspectwerkz> </pre></td></tr></table> <p>Note that we could have simplified this XML by defining our <code>DemoAspect</code> with annotations (see HelloWorld tutorial for this).</p> <p>Some of you will have already understand that when the aspect is <strong>deployed within the web application</strong>, its weaving scope will not be the same: the<br /> pointcut will pick out public method from classes <em>really loaded</em> by the web application (e.g. not <code>javax.servlet.http.HttpServlet</code> class).</p> <h3>Enabling online mode in Tomcat</h3> <p>Since we will use online mode, we need to alter some of the Tomcat startup scripts. If we were using BEA JRockit or Java 1.5, it would be a matter of adding a single JVM option.</p> <p>Since I will not make any assumption on your JVM environment, I will skip 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 command line tool in <code>ASPECTWERKZ_HOME/bin/aspectwerkz</code> (<a href="http://aspectwerkz.codehaus.org/startup_and_runtime_issues.html#AspectWerkz_command_line_tool">read doc</a>).</p> <p><strong>Note: for Tomcat 5.0.27 and after, please refer to the comments at the end of the article</strong></p> <p><em>Open</em> the Tomcat startup script <code>bin/catalina.bat</code> (C:\aw\jakarta-tomcat-5.0.25\bin\catalina.bat).<br /> The main idea is to set <code>ASPECTWERKZ_HOME</code> in this script and replace <code>JAVA_HOME/bin/java</code> with <code>ASPECTWERKZ_HOME/bin/aspectwerkz</code>.</p> <p><em>Locate</em> at the end of the script:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ... set _EXECJAVA=%JAVA_HOME%\bin\java rem Execute Java with the applicable properties if not "%JPDA%" == "" goto doJpda ... </pre></td></tr></table> <p>and <em>alter</em> it as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ... @REM ** Begin of AspectWerkz configuration ** @REM ** Adapt ASPECTWERKZ_HOME ** set ASPECTWERKZ_HOME=C:\aw\aspectwerkz set _EXECJAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat @REM ** we can add some more AspectWerkz option this way: set JAVA_OPTS=%JAVA_OPTS% -Daspectwerkz.transform.details=false ... @REM ** End of AspectWerkz configuration ** rem Execute Java with the applicable properties if not "%JPDA%" == "" goto doJpda ... </pre></td></tr></table> <p>For a complete understanding of the different options and other modes, refer to the <a href="http://aspectwerkz.codehaus.org/online.html">AspectWerkz weaving schemes documentation</a>.<br /> 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).</p> <p><strong><em>Do not startup</em> Tomcat for now.</strong></p> <h3>Aspect deployed in the application with a WEB-INF/aop.xml</h3> <p>This section explains the main step required and consequences of packaging an aop.xml file in your deployed application (WEB-INF/aop.xml).<br /> The big picture is as follow:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Application server / container | | Web application | WEB-INF/ | |___ aop.xml (web application AOP deployment descriptor) | |___ classes/ (regular packaging for our Aspect class) </pre></td></tr></table> <p>In this section we decide to package our aspect as a regular class of the web application, and we define the pointcut and activate the aspect<br /> through the help of the WEB-INF/aop.xml file.<br /> As a consequence, only the classes of the web application will be eventually weaved (if the pointcut match), even if the pointcut is supposed to match everything (all public method execution as explained in the previous section). In fact, the deployment mechanism tied to the web application deployment add an unbreakable isolation, and only public method execution <strong>of classes loaded belonging to the web application</strong> will be targetted by our <code>DemoAspect</code>.</p> <p>Lets apply our Aspect to the Tomcat jsp-examples web application:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Open the C:\aw\jakarta-tomcat-5.0.25\webapps\jsp-examples\WEB-INF folder of the jsp-examples web application. Copy the C:\aw\tomcat\demoAOP folder (compiled Aspect) in the WEB-INF\classes subfolder just as regular web app classes. You should end up in having a C:\aw\jakarta-tomcat-5.0.25\webapps\jsp-examples\WEB-INF\classes\demoAOP\DemoAspect.class file. </pre></td></tr></table> <p>Lets add the AOP XML descriptor to the web app:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Copy the C:\aw\tomcat\META-INF\aop.xm file to the WEB-INF directory of the web app </pre></td></tr></table> <p>To sum up we have only</p> <ul> <li>added the aspect class to the web app as regular web app packaging dictates to do</li> <li>added a WEB-INF/aop.xml file to the web app</li> </ul> <p><em>Lets start Tomcat</em> with the <code>bin/startup.bat</code> script (which will delegate to our modified <code>catalina.bat</code> script).<br /> On the <em>stdout</em> window some AspectWerkz message should appear, with some verbose blocks of logs about aop.xml deployment.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ... ****************************************************************** * ClassLoader = org.apache.catalina.loader.WebappClassLoader@7088278 * SystemID = webapp * Aspect total count = 1 * file:/C:/aw/tomcat/java/jakarta-tomcat-5.0.25/webapps/jsp-examples/WEB-INF/aop.xml ****************************************************************** ... </pre></td></tr></table> <p>And immediately we should see our DemoAspect at work, with some information about the web app initialization.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> DemoAspect[7088278] --> METHOD_EXECUTION:org.apache.jsp.jsp2.el.basic_002darithmetic_jsp._jspService DemoAspect[7088278] --> METHOD_EXECUTION:listeners.SessionListener.sessionCreated DemoAspect[7088278] <-- DemoAspect[7088278] <-- DemoAspect[7088278] --> METHOD_EXECUTION:org.apache.jsp.jsp2.simpletag.hello_jsp._jspService DemoAspect[7088278] --> METHOD_EXECUTION:jsp2.examples.simpletag.HelloWorldSimpleTag.doTag </pre></td></tr></table> <p>You will notice this <em>7088278</em> hashcode here, which indeed match the web app classloader hashcode as logged at deployment time.</p> <p>By browsing the <a href="http://localhost:8080/jsp-examples/">jsp-samples</a> web application, we learn more about public method execution <strong>of the web application</strong>.<br /> Off course, no information about Tomcat itself since the aspects was deployed <strong>within</strong> the web application.</p> <p>This section is interesting, and I hope you have already some new ideas to deploy your aspects in your J2EE application with AspectWerkz.<br /> But what happens if you would like to provide some ready to use out of the box aspects, completely obliouvness to the user ?</p> <p><em>Shutdown</em> Tomcat for the next section.</p> <h3>Aspect deployed in the application with an Aspect jar file</h3> <p>This section explains the main step required to package a <em>"drop n weave"</em> aspect jar file that contains a META-INF/aop.xml file, and consequence of adding this jar file in your deployed application (WEB-INF/lib).<br /> The big picture is as follow:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Application server / container | | Web application | WEB-INF/ | lib/ | |_ aspect.jar (drop n weave aspect jar file) | |_ META-INF/aop.xml | |_ regular packaging for our Aspect class </pre></td></tr></table> <p>In this section we will achieve the <strong>same result</strong> as in previous section, but in a slightly different way.<br /> We will package our Aspect in a jar file, and we will add the <code>aop.xml</code> file in the jar file itself in the special <code>META-INF</code> folder, so that our jar file is completely self defined.<br /> The jar file is then added to the web application as a regular jar file.<br /> As a consequence of web application 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 happen: the aspect will target classes laoded by the web application.</p> <p>Lets prepare the jar file (regular java / jar packaging rules applies)</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> cd C:\aw\tomcat jar -cf ..\aspect.jar * </pre></td></tr></table> <p>You will end up in having a C:\aw\aspect.jar file ready to use.</p> <p>Lets add this <em>drop n weave</em> jar file to the Tomcat <em>servlet samples</em> web application.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Open the C:\aw\jakarta-tomcat-5.0.25\webapps\servlet-examples\WEB-INF folder Create the lib subdirectory (to have a WEB-INF/lib) Copy the C:\aw\tomcat\aspect.jar file to it </pre></td></tr></table> <p><em>Startup Tomcat</em>.</p> <p>Note: if you still have the <strong>jsp</strong> sample application with its aspect, you will see some messages as well as detailled in previous section.</p> <p>The following should appear on stdout: some messages about the deployment of our aspect.jar file and its <code>META-INF/aop.xml</code> AOP XML deployment descriptor and right after that, our DemoAspect at work.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ****************************************************************** * ClassLoader = org.apache.catalina.loader.WebappClassLoader@22041176 * SystemID = webapp * Aspect total count = 1 * jar:file:/D:/java/jakarta-tomcat-5.0.25/webapps/servlets-examples/WEB-INF/lib/aspect.jar!/META-INF/aop.xml ****************************************************************** DemoAspect[22041176] --> METHOD_EXECUTION:listeners.ContextListener.contextInitialized DemoAspect[22041176] <-- DemoAspect[22041176] --> METHOD_EXECUTION:listeners.SessionListener.contextInitialized DemoAspect[22041176] <-- DemoAspect[22041176] --> METHOD_EXECUTION:filters.ExampleFilter.init DemoAspect[22041176] <-- DemoAspect[22041176] --> METHOD_EXECUTION:compressionFilters.CompressionFilter.init DemoAspect[22041176] <-- </pre></td></tr></table> <p>Something interesting: if you kept the jsp sample application aspect, you 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 see each other and the <em>naming space</em> is guaranteed by the class loader and by the AspectWerkz AOP container.</p> <p><em>Browse</em> some in the <a href="http://localhost:8080/servlets-examples/">servlet sample application</a> and see the DemoAspect at work.</p> <p>We will learn something more about that in the next part...<br /> Have you heard that "Aspects are everywhere" (c) The AspectWerkz team ?<br /> Go on reading !</p> <p><em>Shutdown Tomcat</em></p> <h3>Aspect jar file at the system classpath</h3> <p>This section explains the main step required to package <strong>several META-INF/aop.xml</strong> files and understand what happens then.<br /> The big picture is as follow:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Application server / container | |-- aspect.jar with system id in META-INF/aop.xml = "system" | Web application | WEB-INF/ | lib/ | |_ aspect.jar (drop n weave aspect jar file) | |_ META-INF/aop.xml with system id = "webapp" | |_ regular packaging for our Aspect class </pre></td></tr></table> <p>In this section we keep the packaging of the previous section, but we add a new aspect.jar file at the Tomcat wide level (system classpath) to check if we can have there a more wider pointcut. Remember the pointcut is supposed to match all public method, but that AOP container behavior enforce isolation based on the regular class loader hierarchy.</p> <p>There is an important things to do.<br /> AspectWerkz AOP container requires sysem id as defined in the <em>aop.xml</em> file to be unique <strong>within a class loader hierarchy</strong>. That means:</p> <ol> <li>we can not just copy the previous aspect.jar at the system classpath level without changing its id</li> <li>we could just copy the previous aspect.jar to some other webapplication, since then it will be in a parallel classloader hierarchy due to regular J2EE classloading schemes</li> </ol> <p>Lets change the <code>META-INF/aop.xml</code> then and may be lets reduce some the scope of the pointcut so that we don't have too much data:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Open the C:\aw\tomcat\META-INF\aop.xml file Change the <system id="..."> value to "system" instead of "webapp" (use the name you want) Change the pointcut so that it is a bit narrowed to match all public method in whatever direct subclass of whatever class/interface of javax.servlet.http.* </pre></td></tr></table> <p>The C:\aw\tomcat\META-INF\aop.xml file will look like</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" "http://aspectwerkz.codehaus.org/dtd/aspectwerkz.dtd"> <aspectwerkz> <system id="system"> <aspect class="demoAOP.DemoAspect"> <pointcut name="nonPrivate" expression="execution(!private * org.apache.catalina.servlets..*.*(..)"/> <advice name="trace" type="around" bind-to="nonPrivate"/> </aspect> </system> </aspectwerkz> </pre></td></tr></table> <p>Lets package a new <em>weave n drop</em> aspect jar file:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> cd C:\aw\tomcat jar -cf ..\aspect-system.jar * </pre></td></tr></table> <p>You will end up in having a C:\aw\aspect-system.jar file ready to use.</p> <p><em>Copy</em> this file to C:\aw\jakarta-tomcat-5.0.25\common\lib which contains some regular jar for Tomcat.</p> <p><em>Startup Tomcat</em>.</p> <p>Now we have both DemoAspect of each system at work:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> DemoAspect[13859719] --> METHOD_EXECUTION:filters.ExampleFilter.doFilter DemoAspect[13859719] --> METHOD_EXECUTION:HelloWorldExample.doGet DemoAspect[13859719] <-- DemoAspect[13859719] --> METHOD_EXECUTION:filters.ExampleFilter.toString DemoAspect[13859719] <-- DemoAspect[13859719] <-- DemoAspect[16094127] --> METHOD_EXECUTION:org.apache.catalina.servlets.DefaultServlet.doGet DemoAspect[16094127] --> METHOD_EXECUTION:org.apache.catalina.servlets.DefaultServlet.serveResource DemoAspect[16094127] --> METHOD_EXECUTION:org.apache.catalina.servlets.DefaultServlet.getRelativePath DemoAspect[16094127] --> METHOD_EXECUTION:org.apache.catalina.servlets.DefaultServlet.doGet </pre></td></tr></table> <h3>Conclusion</h3> <p>We have: </p> <ol> <li>learned how to package and deploy our aspect along our web application</li> <li>understood the consequence of doing so</li> <li>understood a way to do it with a self contained jar file dropped in the web application libs</li> <li>understood the difference with an Aspect jar that would belong to the system classpath</li> <li>practice class load time integration using the command line tool</li> </ol> <p>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 you 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.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced