我正在处理一个大型的Grails 1.3.7项目,我想从过滤器访问流状态名称以进行日志记录。我一直在谷歌搜索,我能找到的最接近的答案是:Grails WebFlow State Name,但它只能在流程本身内工作。
有没有办法从流程外(过滤器)获取当前会话中正在执行的流的状态名称?
提前致谢,
吉列尔莫
答案 0 :(得分:3)
经过大量的谷歌搜索和调试后,我设法生成以下代码。它适用于简单的应用程序。当我从假期回来时,我会将它与主应用程序集成,然后我会更新这个问题。
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)}")
}
}
}
}
编辑:上面的代码可以确定给定时间点当前流状态的名称,但它不会像流程一样更新日志框架的映射诊断上下文执行发展。为此,有必要实现org.springframework.webflow.execution.FlowExecutionListener
并在conf/spring/resources.groovy
中注册它:
beans = {
myLoggingFlowExecutionListener(org.example.MyLoggingFlowExecutionListener)
}
您必须在hibernateConversationListener
bean中注册此侦听器bean和executionListenerLoader
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。我已将上面的代码更新为最终的,有效的版本。