PDA

View Full Version : Intercept getter method


kantorn
Nov 25th, 2004, 06: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 {

public Object invoke(MethodInvocation invocation) throws Throwable{
String out = (String) invocation.getMethod().invoke(invocation.getThis() , new Object[0]);
//Do injection of <br> tags
return invocation.proceed();
// How to return the <br>-injected string???
}
}

Thanks!

Rod Johnson
Nov 25th, 2004, 06: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, 06: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, 08: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 {

public static Logger log = Logger.getLogger(AddHtmlBreakInterceptor.class.get Name());

public Object invoke(MethodInvocation invocation) throws Throwable{
log.debug("AddHtmlBreakInterceptor invoked");
String out = (String) invocation.proceed();
log.debug("Instring = " + out);
String returnthis = replaceWithBrakes( out );
log.debug("Instring massaged = " + returnthis);
return returnthis;
}

public String replaceWithBrakes(String instr){
if( instr == null || instr.length() < 1 ){
return "";
}
StringBuffer sb = new StringBuffer(instr);
int cursor = 0;
for(int i = 0; i < sb.length() && cursor != -1;i = cursor){
cursor = sb.indexOf("\n", cursor);
sb.insert(cursor, "<br/>");
}
return sb.toString();
}
}

I don't know what I'm doing wrong here...

Thanks!

kantorn
Nov 26th, 2004, 12:38 PM
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, 03: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, 06: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, 06:49 AM
This particular problem was successfully solved with the HTML

<PRE></PRE>

tag.