Announcement Announcement Module
Collapse
No announcement yet.
AOP newbie has a question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP newbie has a question

    Hi,

    I've been using Spring from quite some time now but this is the first time I want to use the Spring AOP framework. I want to create an interceptor that intercepts a xwork Action to implement paging of a collection through AOP.

    I've created an interface Pageable that has to be implemented by every Action:

    Code:
    public interface Pageable {
    
    	public List getData();
    	
    	public String getKey();
    }
    The key is used to store the data in the session context so that the action does not have to be executed again.

    I've created another interface PagedAction:

    Code:
    public interface PagedAction {
    
    	public int getPage();
    	
    	public void setPage(int page);
    	
    	public int getRecordsPerPage();
    	
    	public void setRecordsPerPage(int records);
    	
    	public boolean hasPreviousPage();
    	
    	public boolean hasNextPage();
    	
    	public List getPagedData();
    	
    	public void setRefresh(boolean refresh);
    	
    	public boolean getRefresh();
    }
    Next, I've created an interceptor that implements PagedAction (experienced AOP users understand the problem at this point). The interceptor is conceived more or less as a decorator and handles all paging related work.

    The problem I face is that only the methods of the target action are available in the proxy object and not the methods implemented by the interceptor. Currently I do not have an interface for my xwork actions, but the only workaround I see would be to create an interface per action, create a proxy with said interface plus the PagedAction interface and add logic to the invoke method of the interceptor to delegate method calls belonging to the PagedAction interface to the methods implemented in the interceptor (are you still with me?).

    To make things a bit clearer, this is the interceptor:

    Code:
    public class PagedActionInterceptor implements MethodInterceptor, PagedAction {
    
    	// instance variables here.
    
    	public Object invoke(MethodInvocation invocation) throws Throwable {
    
    		// logic here, invocation.proceed() is called at some point and the
    		// return value is returned.
    	}
    	
    	public List getPagedData() {
    		return this.data;
    	}
    	public int getPage() {
    		return this.page;
    	}
    	public int getRecordsPerPage() {
    		return this.records;
    	}
    	public boolean hasNextPage() {
    		return this.hasNextPage;
    	}
    	public boolean hasPreviousPage() {
    		return this.hasPreviousPage;
    	}
    	public void setPage(int page) {
    		this.page = page;
    	}
    	public void setRecordsPerPage(int records) {
    		this.records = records;
    	}
    	public boolean getRefresh() {
    		return this.refresh;
    	}
    	public void setRefresh(boolean refresh) {
    		this.refresh = refresh;
    	}
    }
    Before I go ahead with my workaround, I ask for your help because I have a feeling Spring AOP offers a way to both intercept and decorate.

    Kind regards

    tentacle

  • #2
    Have you looked at introduction, and the DelegatingIntroductionInterceptor? I think this may do what you want.

    Rgds
    Rod

    Comment


    • #3
      Originally posted by Rod Johnson
      Have you looked at introduction, and the DelegatingIntroductionInterceptor? I think this may do what you want.
      Thanks for your feedback Rod. The DelegatingIntroductionInterceptor indeed does the trick. The Spring documentation is really good.

      Kind regards

      tentacle

      Comment

      Working...
      X