Search code examples
liferayportletaspectjjsr286

how to configure AspectJ with JSR 286 Generic Portlet ??? any link


we are developing portal application using JSR-286 portlet inside liferay tomcat.We are creating our portlet extending Generic Portlet.Now I want to apply logs on doView() and doModify() methods using AspectJ. I tried with Spring AspectJ. But Spring AspectJ works only on spring managed beans.

any luck


Solution

  • I resolved above problem.

    here are some solution to get it done.

    Create an Aspect

               @Aspect
         public class TestAspect {
    
     TestAspect (){
    
        System.out.println("TestAspect Initialized");
    }
    @Pointcut( "execution(public void doView*(javax.portlet.RenderRequest,javax.portlet.RenderResponse) throws java.io.IOException, javax.portlet.PortletException)" )
    
     private void testDoView() {
        }
           @Around("testDoView()")
          public void logAround(ProceedingJoinPoint joinPoint)
          {
    
        System.out.println("AROUND ADVICE START");
    
    try{
        joinPoint.proceed(joinPoint.getArgs());
            }catch(PortletException portletException){
        System.out.println("[AROUND ADVICE] :"+portletExceptionio.getMessgae());
    
    }catch(IOException ioException){
        System.out.println("[AROUND ADVICE] :"+ioException.getMessgae());
    }
    catch(Throwable throwable){
        System.out.println("[AROUND ADVICE] :"+throwable.getMessage();
    }
    
    System.out.println("AROUND ADVICE EXIT");
    
        }
    
          }
    

    some *.jar files are AspectJ binaries:

    aspectjrt.jar - necessary in runtime for correct aspects processing;

    aspectjtools.jar - contains implementation of aspectj compiler;

    aspectjweaver.jar - bridge between aspectj logic and java instrumentation;

    1.In command line enviroment (with Ant-build.xml)

     **There are three ways to inject instructions implied by AspectJ aspects:**
    
    
    <project name="aspectj-example" xmlns:aspectj="antlib:org.aspectj">
    
    <property name="src.dir" value="src/main/java"/>
    <property name="resource.dir" value="src/main/resources"/>
    <property name="target.dir" value="target"/>
    <property name="classes.dir" value="${target.dir}/classes"/>
    <taskdef uri="antlib:org.aspectj"
            resource="org/aspectj/antlib.xml"
            classpath="${resource.dir}/aspectjtools.jar"/>
     <path id="aspectj.libs">
        <fileset dir="${resource.dir}"/>
    </path>
    <target name="clean">
        <delete dir="${target.dir}"/>
        <mkdir dir="${target.dir}"/>
        <mkdir dir="${classes.dir}"/>
    </target>
    
    way 1: compile-time weaving
    
     <target name="compile-time" depends="clean">
        <aspectj:iajc source="1.5" srcdir="${src.dir}" classpathref="aspectj.libs" destDir="${classes.dir}"/>
        <java classname="com.aspectj.TestTarget" fork="true">
            <classpath>
                <path refid="aspectj.libs"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </java>
    </target>
    

    way 2: post-compile weaving

    <target name="post-compile" depends="clean">
    
        <echo message="Compiling..."/>
        <javac debug="true" srcdir="${src.dir}" classpathref="aspectj.libs" destdir="${classes.dir}"/>
    
        <echo message="Weaving..."/>
        <aspectj:iajc classpathref="aspectj.libs" inpath="${classes.dir}" aspectpath="${src.dir}" outJar="${classes.dir}/test.jar"/>
    
    
    </target>
    

    way 3: load-time weaving

         <target name="load-time" depends="clean">
    
         <echo message="Compiling..."/>
         <javac debug="true" srcdir="${src.dir}" classpathref="aspectj.libs"               destdir="${classes.dir}"/>
         </target>
         </project>
    

    2.In Ecilpse (IDE environment)

          select your project,than select configure->convert to AspectJ Project.