AnsweredAssumed Answered

Method interception

Question asked by ajv on Jan 13, 2012
Latest reply on Dec 4, 2013 by kaynezhang
Hi all,

I needed to intercept method calls "createSite" and I wired and Spring aspect to do it:


   <!– Advice –>
   <bean id="myService" class="MyServiceClass"/>

   <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames">
         <value>SiteService</value>
      </property>
      <property name="interceptorNames">
         <list>
            <value>createSiteAspect</value>
         </list>
      </property>
   </bean>

   <bean name="createSitePointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
      <property name="mappedName" value="createSite" />
   </bean>

   <bean name="createSiteAspect" class="org.springframework.aop.support.DefaultPointcutAdvisor">
      <property name="advice" ref="myService" />
      <property name="pointcut" ref="createSitePointcut" />
   </bean>

This works fine: When the user creates a site, my service is called.
However, when I enable Alfresco audit (audit.enabled=true + audit.alfresco-access.enabled=true)….an exception is thrown:


2012-01-13 16:54:44,323  ERROR [extensions.webscripts.AbstractRuntime] [http-8080-3] Exception from executeScript - redirecting to status template error: 00130002 Wrapped Exception (with status template): 00130004 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js': Bean named 'SiteService' must be of type [$Proxy85], but was actually of type [$Proxy87]
org.springframework.extensions.webscripts.WebScriptException: 00130002 Wrapped Exception (with status template): 00130004 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js': Bean named 'SiteService' must be of type [$Proxy85], but was actually of type [$Proxy87]
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:884)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:400)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:388)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:462)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:500)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:316)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:352)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
   at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
   at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
   at java.lang.Thread.run(Thread.java:619)
Caused by: org.alfresco.scripts.ScriptException: 00130004 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js': Bean named 'SiteService' must be of type [$Proxy85], but was actually of type [$Proxy87]
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:195)
   at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:282)
   at org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
   at org.springframework.extensions.webscripts.AbstractWebScript.executeScript(AbstractWebScript.java:1107)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:86)
   … 25 more
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'SiteService' must be of type [$Proxy85], but was actually of type [$Proxy87]
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:398)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:388)
   at org.alfresco.repo.audit.BeanIdentifierImpl.getBeanNameImpl(BeanIdentifierImpl.java:105)
   at org.alfresco.repo.audit.BeanIdentifierImpl.getName(BeanIdentifierImpl.java:82)
   at org.alfresco.repo.audit.BeanIdentifierImpl.getBeanName(BeanIdentifierImpl.java:69)
   at org.alfresco.repo.audit.AuditMethodInterceptor.proceed(AuditMethodInterceptor.java:188)
   at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:151)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy85.createSite(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at test.MyServiceClass.invoke(SiteServiceDcsAspect.java:79)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy87.createSite(Unknown Source)
   at org.alfresco.repo.site.script.ScriptSiteService.createSite(ScriptSiteService.java:108)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:155)
   at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:243)
   at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:86)
   at org.mozilla.javascript.gen.c5._c1(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js:86)
   at org.mozilla.javascript.gen.c5.call(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js)
   at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:108)
   at org.mozilla.javascript.gen.c5._c0(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js:97)
   at org.mozilla.javascript.gen.c5.call(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js)
   at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
   at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
   at org.mozilla.javascript.gen.c5.call(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js)
   at org.mozilla.javascript.gen.c5.exec(file:/E:/Alfresco/c40a/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.js)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:483)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:191)
   … 29 more
   
My class it's like this:
   

public class MyServiceClass implements MethodInterceptor {
   public Object invoke(MethodInvocation invocation) throws Throwable {
      …
      Object returnValue = invocation.proceed();
      …
   }
}
   


How I could combine interception method and Alfresco audit??   
Thanks in advance!

Outcomes