Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Passing a parameter created in an advice to the pointcut implementation

Why not just use a ThreadLocal?

-Archie

On Fri, Feb 20, 2015 at 12:53 PM, Holger King <HolgerKing@xxxxxxx> wrote:
Dear mailing list participants,

I just moved a cross-cutting-concern (the creation and closing of a non-JDBC-connection) from a web service to a Spring AOP AspectJ implementation. This aspect is being called each time any method of the web service interface is requested.

The problem: the pointcut (= web service implementation) needs the connection object being created in the advice! But as the method signatures are defined by the web service contract (the WSDL document has been converted to Java classes via "wsdl2java" based on the contract first approach) they cannot be adapted to take over the new connection parameter.

It's clear that the other way around where the AspectJ advice gets the parameter used in the method call of the pointcut is possible.

Even changing existing parameter values and passing the same argument list (having the same types) to the implementation where the pointcut matches does work (using "&& args()" in the pointcut in combination with joinpoint.proceed(jointpoint.getArgs())). But as I do not have a collection argument there I cannout put the connection in :-(

That's why the problem exists here as it's not possible to change parameter values. Instead, a complete new variable owning the connection created in the advice needs to be accessible in the object the pointcut matches.

Below you find the example code that addresses the problem in a concrete way:

Here comes my Spring AspectJ annotated aspect:

/* AOP aspect running on all methods being called on objects of type "IdmAdaptor" */
package com.xxxxx.fs.idm.ws.aop;

@Aspect
public class FSConnectionAspect {
    /* match all methods located in classes implementing interface "IdmAdaptor" and limited to Spring bean "fsIdmAdaptor" */
    @Pointcut("execution(within(com.xxxxx.fs.idm.ws.IdmAdaptor+) and bean(fsIdmAdaptor)")
    public void operations() {
    }

    @Around("operations")
    public Object handleConnection(ProceedingJoinPoint joinpoint) {
        //do connection creation
        Connection conn = ConnectionManager.getConnection(<host>, <port>, <mode>,<user>, <password>);
            try {
                conn.connect();

                //how to pass the connection object to the pointcut implementation?
                Object result = joinpoint.proceed();
            } catch (IOException e) {
            } catch (AuthenticationException e) {
            } catch(MaximumNumberOfSessionsExceededException e) {
            } finally {
                if (conn.isConnected()) {
                    try {
                        conn.close();
                    } catch (IOException e) {
                    }
                }
            }
        } catch (Throwable e) {
        }
        return retVal;
    }
}

Here comes my Spring bean implementing the "IdmAdaptor" interface where the pointcut - defined in the aspect - works with. It has to use the connection object administrated within the aspect. The question: how to do that without changing the method signature?

package com.xxxxx.fs.idm.ws.impl;

public class FSIdmAdaptor implements IdmAdaptor {
    @Override
    public boolean enableAccount(EnableAccountTypeparameters)
        throws IdmFaultMessage {
        boolean retVal = false;
        ...

        //here the connection object is used created and managed in the Spring AspectJ aspect
        AdminService adminService=conn.getService(AdminService.class)

        ...
        return retVal;
    }
    ...  //other web service operation methods using the connection "conn" object
}

Do you have any idea how to realize that? Or is it a complete wrong approach to do that using Java aspects - although connection handling might be a typical cross-cutting-concern to realize there.

Your feedback is highly appreciated and welcome :-)

Kind regards
Holger
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users



--
Archie L. Cobbs

Back to the top