Results 1 to 8 of 8

Thread: Intercept getter method

  1. #1
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default Intercept getter method

    Newbie at AOP

    Task: A string stored in a MySQL DB contains newlines, which however isn't diplayed in the webpage by <c:out value="">-tag.

    I was thinking on intercepting the getXXX-methods of a POJO in order to inject <br>-tags instead of newlines. For different reasons, I don' want to store HTML in datastore.

    How do I do this?
    First problem is to get the return value of the getXXX-method, second is where I'm supposed to return it. 'invocation.proceed()' isn't optional, is it?

    My code so far:

    Code:
    public class AddHtmlBreakInterceptor implements MethodInterceptor &#123;
    	
    	public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable&#123;
    		String out = &#40;String&#41; invocation.getMethod&#40;&#41;.invoke&#40;invocation.getThis&#40;&#41;, new Object&#91;0&#93;&#41;;
    		//Do injection of <br> tags
    		return invocation.proceed&#40;&#41;;
    		// How to return the <br>-injected string???
    	&#125; 		
    &#125;
    Thanks!
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    You can use code like this:
    String fromDb = (String) invocation.proceed();
    String massagedString = doMySubstitution(fromDb);

    return massagedString;
    You are responsible for calling proceed() in interception around advice; it's up to you if you want to massage the return value.

    You can use a Pointcut to target this interceptor to only the appropriate getters.

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

  3. #3
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    Thanks for the quick and clarifying response!!

    About Pointcut: is it possible to target multiple getXXX-methods with only one pointcut, or do one need one Pointcut per method?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  4. #4
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default Can't get advice to 'fire'

    I don't seem to be able to 'fire' the advice

    Here's a part of my setup

    Code:
    <bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakInterceptor"/>
    	
    	<bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>
    
    	<!-- Interceptor to inject <br>-tags into output -->
    	<bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice"><ref local="breakInjector"/></property>
    		<property name="patterns">
    			<list>
    				<value>^ks.rah.avik2.domain.Event.getEventDescr.*</value>
    				<value>^ks.rah.avik2.domain.Event.getWhyComment.*</value>
    				<value>^ks.rah.avik2.domain.Event.getConseqComment.*</value>
    				<value>^ks.rah.avik2.domain.Event.getPreventComment.*</value>
    				<value>^ks.rah.avik2.domain.Event.getAuxComment.*</value>
    				<value>^ks.rah.avik2.domain.support.TextEntry.getText.*</value>
    			</list>
    		</property>
    	</bean>
    	
    	
    	<bean id="breakInjectorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="target"><ref local="breakInjectorTarget"/></property>
    		<property name="interceptorNames">
    			<list>
    				<value>breakInjector</value>
    			</list>		
    		</property>
    	</bean>
    The interceptor:

    Code:
    public class AddHtmlBreakInterceptor implements MethodInterceptor &#123;
    	
    	public static Logger log = Logger.getLogger&#40;AddHtmlBreakInterceptor.class.getName&#40;&#41;&#41;;
    	
    	public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable&#123;
    		log.debug&#40;"AddHtmlBreakInterceptor invoked"&#41;;
    		String out =  &#40;String&#41; invocation.proceed&#40;&#41;;
    		log.debug&#40;"Instring = " + out&#41;;
    		String returnthis = replaceWithBrakes&#40; out &#41;;
    		log.debug&#40;"Instring massaged = " + returnthis&#41;;
    		return  returnthis;
    	&#125;
    	
    	public String replaceWithBrakes&#40;String instr&#41;&#123;
    		if&#40; instr == null || instr.length&#40;&#41; < 1 &#41;&#123;
    			return "";	
    		&#125;
    		StringBuffer sb = new StringBuffer&#40;instr&#41;;
    		int cursor = 0;
    		for&#40;int i = 0; i < sb.length&#40;&#41; && cursor != -1;i = cursor&#41;&#123;
    			cursor = sb.indexOf&#40;"\n", cursor&#41;;
    			sb.insert&#40;cursor, "<br/>"&#41;;
    		&#125;
    		return sb.toString&#40;&#41;;
    	&#125; 			
    &#125;
    I don't know what I'm doing wrong here...

    Thanks!
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  5. #5
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    I don't know if it's relevant, but the Event object is a POJO persisted to a datastore. I want the advice to fire when I display certain get-methods in the web layer, but perhaps this is not an applicable use of Spring AOP?

    Any suggestions?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    About Pointcut: is it possible to target multiple getXXX-methods with only one pointcut, or do one need one Pointcut per method?
    A pointcut will typically define a set of methods.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  7. #7
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    OK.

    Does this look alright?
    I cannot get it to fire... no log trace... nothing

    Code:
    <bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakInterceptor"/>
       
       <bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>
    
       <!-- Interceptor to inject <br>-tags into output -->
       <bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
          <property name="advice"><ref local="breakInjector"/></property>
          <property name="patterns">
             <list>
                <value>^ks.rah.avik2.domain.Event.getEventDescr.*</value>
                <value>^ks.rah.avik2.domain.Event.getWhyComment.*</value>
                <value>^ks.rah.avik2.domain.Event.getConseqComment.*</value>
                <value>^ks.rah.avik2.domain.Event.getPreventComment.*</value>
                <value>^ks.rah.avik2.domain.Event.getAuxComment.*</value>
                <value>^ks.rah.avik2.domain.support.TextEntry.getText.*</value>
             </list>
          </property>
       </bean>
       
       
       <bean id="breakInjectorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
          <property name="target"><ref local="breakInjectorTarget"/></property>
          <property name="interceptorNames">
             <list>
                <value>breakInjector</value>
             </list>      
          </property>
       </bean>
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  8. #8
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default Resolved

    This particular problem was successfully solved with the HTML

    Code:
    <PRE></PRE>
    tag.
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

Similar Threads

  1. PerformanceMonitorInceptor
    By chenrici in forum AOP
    Replies: 15
    Last Post: May 18th, 2006, 04:28 PM
  2. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  3. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  4. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  5. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 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
  •