PDA

View Full Version : Intercept getter method



kantorn
Nov 25th, 2004, 05:07 AM
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:


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!

Rod Johnson
Nov 25th, 2004, 05:32 AM
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

kantorn
Nov 25th, 2004, 05:46 AM
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?

kantorn
Nov 25th, 2004, 07:40 AM
I don't seem to be able to 'fire' the advice

Here's a part of my setup


<bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakIntercep tor"/>

<bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>

<!-- Interceptor to inject <br>-tags into output -->
<bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointc utAdvisor">
<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:


public class AddHtmlBreakInterceptor implements MethodInterceptor &#123;

public static Logger log = Logger.getLogger&#40;AddHtmlBreakInterceptor.class.get Name&#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!

kantorn
Nov 26th, 2004, 11:38 AM
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?

Rod Johnson
Nov 28th, 2004, 02:28 PM
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.

kantorn
Nov 30th, 2004, 05:53 AM
OK.

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


<bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakIntercep tor"/>

<bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>

<!-- Interceptor to inject <br>-tags into output -->
<bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointc utAdvisor">
<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>

kantorn
Jan 16th, 2005, 05:49 AM
This particular problem was successfully solved with the HTML


<PRE></PRE>

tag.