Results 1 to 3 of 3

Thread: AOP newbie has a question

  1. #1
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

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

    Rgds
    Rod
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default

    Quote 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

Similar Threads

  1. Newbie question: How to get spring debug logging on?
    By wangjammer5 in forum Container
    Replies: 2
    Last Post: Jul 15th, 2005, 07:11 AM
  2. Replies: 1
    Last Post: Jul 11th, 2005, 03:50 PM
  3. Replies: 3
    Last Post: Jul 8th, 2005, 09:00 AM
  4. Replies: 3
    Last Post: Apr 3rd, 2005, 04:34 PM
  5. Replies: 1
    Last Post: Mar 23rd, 2005, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •