Author: bpoussin Date: 2011-08-24 17:30:32 +0200 (Wed, 24 Aug 2011) New Revision: 2183 Url: http://nuiton.org/repositories/revision/nuiton-utils/2183 Log: Evolution #1253: Switch from aspectwerkz to aspectj Modified: trunk/nuiton-profiling/pom.xml trunk/nuiton-profiling/src/main/java/org/nuiton/profiling/NuitonTrace.java trunk/nuiton-profiling/src/test/java/org/nuiton/profiling/NuitonTraceTest.java trunk/nuiton-profiling/src/test/resources/META-INF/aop.xml Modified: trunk/nuiton-profiling/pom.xml =================================================================== --- trunk/nuiton-profiling/pom.xml 2011-08-24 13:13:11 UTC (rev 2182) +++ trunk/nuiton-profiling/pom.xml 2011-08-24 15:30:32 UTC (rev 2183) @@ -50,8 +50,25 @@ <artifactId>commons-lang</artifactId> </dependency> + + <!-- aspectJ --> + <dependency> + <groupId>org.aspectj</groupId> + <artifactId>aspectjrt</artifactId> + <version>${aspectj.version}</version> + </dependency> + + <dependency> + <groupId>org.aspectj</groupId> + <artifactId>aspectjweaver</artifactId> + <version>${aspectj.version}</version> + <scope>runtime</scope> + </dependency> + + <!-- aspectwerkz use asm* dependencies which are not on central... --> <!-- Event if we relocated them to a org.nuiton.thirdparty groupId --> +<!-- <dependency> <groupId>aspectwerkz</groupId> <artifactId>aspectwerkz-core</artifactId> @@ -75,13 +92,13 @@ <artifactId>aspectwerkz-jdk5</artifactId> <scope>compile</scope> </dependency> - +--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> - + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> @@ -107,9 +124,11 @@ <!-- extra files to include in release --> <redmine.releaseFiles>${redmine.libReleaseFiles}</redmine.releaseFiles> + <aspectj.version>1.6.11</aspectj.version> <!-- Configure aop --> - <maven.surefire.debug>-javaagent:target/lib/aspectwerkz-jdk5-2.0.jar</maven.surefire.debug> + <maven.surefire.debug>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-1.6.11.jar</maven.surefire.debug> + <!--maven.surefire.debug>-javaagent:target/lib/aspectwerkz-jdk5-2.0.jar</maven.surefire.debug--> <!--<maven.surefire.debug>-javaagent:target/lib/aspectwerkz-jdk5-2.0.jar -Daspectwerkz.definition.file=src/test/resources/aop.xml</maven.surefire.debug>--> </properties> @@ -141,6 +160,7 @@ </plugin> </plugins> </build> + <profiles> <profile> <id>reporting</id> Modified: trunk/nuiton-profiling/src/main/java/org/nuiton/profiling/NuitonTrace.java =================================================================== --- trunk/nuiton-profiling/src/main/java/org/nuiton/profiling/NuitonTrace.java 2011-08-24 13:13:11 UTC (rev 2182) +++ trunk/nuiton-profiling/src/main/java/org/nuiton/profiling/NuitonTrace.java 2011-08-24 15:30:32 UTC (rev 2183) @@ -22,14 +22,6 @@ package org.nuiton.profiling; import org.apache.commons.lang.time.DurationFormatUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codehaus.aspectwerkz.annotation.After; -import org.codehaus.aspectwerkz.annotation.AfterThrowing; -import org.codehaus.aspectwerkz.annotation.Aspect; -import org.codehaus.aspectwerkz.annotation.Before; -import org.codehaus.aspectwerkz.joinpoint.JoinPoint; -import org.codehaus.aspectwerkz.joinpoint.MethodSignature; import java.io.PrintStream; import java.lang.reflect.Method; @@ -38,8 +30,18 @@ import java.util.List; import java.util.Map; import java.util.Stack; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; /** + * TODO poussin 20110824 check documentation: this documentation is for aspectwerkz, there are little + * change for aspectj. see example file aop.xml + * * Permet de tracer les appels aux methodes. * <p/> * Pour l'utiliser il faut définir un fichier XML qui intercepte les methodes @@ -96,16 +98,20 @@ * <p/> * Voir la classe de test {@code org.nuiton.profiling.NuitonTraceTest}. * + * @see http://aspectwerkz.codehaus.org/definition_issues.html + * * @author bpoussin <poussin@codelutin.com> * @author tchemit <chemit@codelutin.com> */ -@Aspect("perJVM") -public class NuitonTrace { +@Aspect +public abstract class NuitonTrace { static private final List<NuitonTrace> instances = new ArrayList<NuitonTrace>(); + // poussin 20110824: ne pas mettre de logger, car sinon, ca pose des problemes + // avec l'AOP. NoClassDefFound :( /** to use log facility, just put in your code: log.info("..."); */ - static private Log log = LogFactory.getLog(NuitonTrace.class); +// static private Log log = LogFactory.getLog(NuitonTrace.class); /** nombre d'appel */ final static public int STAT_CALL = 0; @@ -154,7 +160,11 @@ return result; } - @Before("executeMethod") + // abstract pointcut: no expression is defined + @Pointcut + abstract void executeMethod(); + + @Before("executeMethod()") public void traceBeforeExecute(JoinPoint jp) { // ajout dans le stack long current = System.nanoTime(); @@ -164,20 +174,20 @@ } - @AfterThrowing("executeMethod") + @AfterThrowing("executeMethod()") public void traceAfterThrowingExecute(JoinPoint jp) { // si une exeption est leve, il faut faire la meme chose traceAfterExecute(jp); } - @After("executeMethod") + @After("executeMethod()") public void traceAfterExecute(JoinPoint jp) { Method method = ((MethodSignature) jp.getSignature()).getMethod(); long current = System.nanoTime(); if (callStack.isEmpty()) { - log.warn("Empty stack in afterExecute for method " + method.getName()); +// log.warn("Empty stack in afterExecute for method " + method.getName()); } else { long[] stackItem = callStack.pop(); long timeSpent = current - stackItem[STACK_TIME_START]; Modified: trunk/nuiton-profiling/src/test/java/org/nuiton/profiling/NuitonTraceTest.java =================================================================== --- trunk/nuiton-profiling/src/test/java/org/nuiton/profiling/NuitonTraceTest.java 2011-08-24 13:13:11 UTC (rev 2182) +++ trunk/nuiton-profiling/src/test/java/org/nuiton/profiling/NuitonTraceTest.java 2011-08-24 15:30:32 UTC (rev 2183) @@ -38,11 +38,9 @@ */ public class NuitonTraceTest { - /** Logger. */ - private static final Log log = LogFactory.getLog(NuitonTraceTest.class); - @Test public void testAOP() { + Log log = LogFactory.getLog(NuitonTraceTest.class); if (log.isDebugEnabled()) { log.debug("DEBUG"); @@ -72,4 +70,13 @@ } } + // pour pouvoir tester la meme chose avec un simple java -javaagent:... + // par exemple: + // java -javaagent:target/lib/aspectjweaver-1.6.11.jar -classpath target/classes:target/lib/aspectjrt-1.6.11.jar:target/lib/aspectjweaver-1.6.11.jar:target/lib/commons-lang-2.6.jar:target/lib/commons-logging-1.1.1.jar:target/lib/junit-4.8.2.jar:target/lib/log4j-1.2.16.jar:target/test-classes org.nuiton.profiling.NuitonTraceTest + + public static void main(String... args) { + NuitonTraceTest t = new NuitonTraceTest(); + t.testAOP(); + } + } Modified: trunk/nuiton-profiling/src/test/resources/META-INF/aop.xml =================================================================== --- trunk/nuiton-profiling/src/test/resources/META-INF/aop.xml 2011-08-24 13:13:11 UTC (rev 2182) +++ trunk/nuiton-profiling/src/test/resources/META-INF/aop.xml 2011-08-24 15:30:32 UTC (rev 2183) @@ -1,45 +1,20 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%L - Nuiton Utils :: Nuiton Profiling - - $Id$ - $HeadURL$ - %% - Copyright (C) 2004 - 2011 CodeLutin - %% - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation, either version 3 of the - License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Lesser Public License for more details. - - You should have received a copy of the GNU General Lesser Public - License along with this program. If not, see - <http://www.gnu.org/licenses/lgpl-3.0.html>. - #L% - --> +<!DOCTYPE aspectj PUBLIC + "-//AspectJ//DTD//EN" + "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> -<!DOCTYPE aspectwerkz PUBLIC - "-//AspectWerkz//DTD//EN" - "http://aspectwerkz.codehaus.org/dtd/aspectwerkz2.dtd"> -<aspectwerkz> - <system id="webapp"> - <aspect class="org.nuiton.profiling.NuitonTrace"> - <pointcut name="executeMethod"> - <![CDATA[ execution(public * org.apache..*.*(..)) && !within(org.apache.log4j..*) && !within(org.apache.commons.lang..*)]]> - <!--<![CDATA[ execution(public * *..*.*(..))]]>--> - </pointcut> - <!--<pointcut name="executeMethod" expression="execution(public * org.apache..*(..))"/>--> - <!--<pointcut name="executeMethod"--> - <!--expression="execution(public * org.apache..*(..))"/>--> - <!--expression="execution(public * org.apache.commons..*(..)) && !within(org.apache.log4j..*) && !within(org.nuiton.profiling..*)"/>--> - </aspect> - <!--</package>--> - - </system> -</aspectwerkz> \ No newline at end of file +<aspectj> + <aspects> + <!-- creation d'un aspect pour definir les methodes a intercepter --> + <concrete-aspect name="org.nuiton.profiling.NuitonTraceTestAspect" + extends="org.nuiton.profiling.NuitonTrace"> + <pointcut name="executeMethod" expression="execution(public * org.apache..*.*(..)) AND !within(org.apache.log4j..*) AND !within(org.apache.maven..*) AND !within(org.apache.commons.lang..*)"/> + </concrete-aspect> + </aspects> + <!-- quelques options pour voir ce qu'il se passe (non obligatoire) --> + <weaver options="-verbose -showWeaveInfo "> + <!-- quelque restriction sur les classes a prendre en compte, pour avec un exemple --> + <include within="org.apache.commons.logging.*" /> + <!-- la config pour que aspectj ecrive sur le disque les fichiers java qu'il genere --> + <!-- <dump within="*"/> --> + </weaver> +</aspectj>