经过大量的谷歌搜索和调试后,我设法生成了以下代码。它适用于一个简单的应用程序。当我假期回来时,我会将它与主应用程序集成,然后我会更新这个问题。
package org.glalejos
import org.springframework.context.ApplicationContext
import org.springframework.webflow.context.ExternalContext
import org.springframework.webflow.context.ExternalContextHolder
import org.springframework.webflow.context.servlet.ServletExternalContext
import org.springframework.webflow.execution.FlowExecution
import org.springframework.webflow.execution.FlowExecutionKey
import org.springframework.webflow.execution.repository.FlowExecutionRepository
class LoggingFilters {
def grailsApplication
String getFlowStateName(def grailsApplication, def servletContext, def request, def response) {
String stateName
if (grailsApplication && servletContext && request && request.queryString && response) {
try {
String strKey = null
String[] keys = request.queryString.split("&")
keys.each{ if (it.startsWith("execution=")) strKey = it.substring(10)}
if (strKey != null) {
ApplicationContext ctx = grailsApplication.mainContext
FlowExecutionRepository fer = ctx.getBean("flowExecutionRepository")
FlowExecutionKey fek = fer.parseFlowExecutionKey(strKey)
ExternalContext previousContext = ExternalContextHolder.getExternalContext()
try {
// You have to set an external context before invoking "fer.getFlowExecution()" or it'll throw a NPE
ExternalContextHolder.setExternalContext(new ServletExternalContext(servletContext, request, response));
FlowExecution fe = fer.getFlowExecution(fek)
stateName = fe.getActiveSession().getState().getId()
} finally {
ExternalContextHolder.setExternalContext(previousContext);
}
} else {
stateName = null
}
} catch(Exception e) {
stateName = null
}
} else {
stateName = null
}
return stateName
}
def filters = {
logData(controller:"*", action:"*") {
before = {
println("Incoming request. Current flow state name is: ${getFlowStateName(grailsApplication, servletContext, request, response)}")
}
after = {
println("Dispatched request. Current flow state name is: ${getFlowStateName(grailsApplication, servletContext, request, response)}")
}
}
}
}
EDIT:上面的代码可以确定给定时间点当前流状态的名称,但它不会将日志框架的 Mapped Diagnostic Context 更新为流执行进化。为此,有必要实现一个org.springframework.webflow.execution.FlowExecutionListener并将其注册到conf/spring/resources.groovy:
beans = {
myLoggingFlowExecutionListener(org.example.MyLoggingFlowExecutionListener)
}
您必须在 executionListenerLoader bean 中注册此侦听器 bean 和 hibernateConversationListener bean,但是,出于某种原因,Spring DSL 在这种情况下不起作用(请参阅下面的 EDIT2)。所以这里是resources.xml,您可以将其放在与resources.groovy 相同的文件夹中,以便正确声明您的资源:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="executionListenerLoader" class="org.springframework.webflow.execution.factory.StaticFlowExecutionListenerLoader">
<constructor-arg>
<list>
<ref bean="hibernateConversationListener" />
<ref bean="myLoggingFlowExecutionListener" />
</list>
</constructor-arg>
</bean>
</beans>
每个FlowExecutionListener 方法都接收到大量可用于记录目的的上下文信息(为了清楚起见,我省略了此类的实现)。
EDIT2:未能将hibernateConversationListener bean 添加到executionListenerLoader 会导致在流的生命周期中操作域对象时出现Hibernate 异常。但是,Spring DSL 在这种特定情况下不起作用,因此我必须使用 XML 格式声明所需的 bean。见http://grails.1312388.n4.nabble.com/Registering-custom-flow-execution-listener-td2279764.html。我已将上面的代码更新为最终的工作版本。