Skip to content

Commit

Permalink
GRAILS-10548 - Add support for the old BindEventListener listeners to…
Browse files Browse the repository at this point in the history
… the new data binder
  • Loading branch information
Jeff Scott Brown committed Oct 30, 2013
1 parent b0f59d3 commit 10af68c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Expand Up @@ -17,6 +17,7 @@ package org.codehaus.groovy.grails.plugins.databinding

import grails.util.GrailsUtil

import org.codehaus.groovy.grails.web.binding.BindEventListenerAdapter
import org.codehaus.groovy.grails.web.binding.DataBindingUtils
import org.codehaus.groovy.grails.web.binding.GrailsWebDataBinder
import org.codehaus.groovy.grails.web.binding.bindingsource.DataBindingSourceRegistry
Expand Down Expand Up @@ -85,5 +86,9 @@ class DataBindingGrailsPlugin {
jsonDataBindingSourceCreator(JsonDataBindingSourceCreator)
halJsonDataBindingSourceCreator(HalJsonDataBindingSourceCreator)
halXmlDataBindingSourceCreator(HalXmlDataBindingSourceCreator)

if(Boolean.TRUE.equals(databindingConfig?.enableSpringEventAdapter)) {
grailsBindEventListenerAdapter(BindEventListenerAdapter)
}
}
}
@@ -0,0 +1,83 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.web.binding

import groovy.transform.CompileStatic

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.grails.databinding.events.DataBindingListenerAdapter
import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.annotation.Autowired

/**
* An adapter which supports notifying BindEventListener instances
* of binding events generated by the Grails 2.3 data binder.
*
* Note that the BindEventListener interface defines a doBind method
* which accepts some parameters which make sense in the Spring data
* binder but do not make sense in the Grails 2.3 data binder. This
* adapter invokes the doBind method and passes null for the
* MutablePropertyValues and TypeConverter arguments. The only real
* value passed to the doBind method is the object being bound to,
* which is often all that is needed. For more flexible binding event
* notification applications should convert their BindEventListener
* listeners to DataBindingListener listeners.
*
* @author Jeff Brown
* @since 2.3.2
* @see org.grails.databinding.events.DataBindingListener
* @see org.grails.databinding.events.DataBindingListenerAdapter
* @see org.codehaus.groovy.grails.web.binding.BindEventListener
*
*/
@CompileStatic
class BindEventListenerAdapter extends DataBindingListenerAdapter implements InitializingBean {

private static Log LOG = LogFactory.getLog(BindEventListenerAdapter)

@Autowired(required=false)
List<BindEventListener> listeners

@Override
Boolean beforeBinding(Object obj, Object errors) {
if(listeners) {
for(BindEventListener listener : listeners) {
try {
listener.doBind(obj, null, null)
} catch (Exception e) {
if(LOG.isErrorEnabled()) {
LOG.error "An error occurred notifying the ${listener.getClass().getName()} listener", e
}
}
}
}
true
}

@Override
public void afterPropertiesSet() throws Exception {
if(listeners) {
if(LOG.isDebugEnabled()) {
LOG.debug "${listeners.size()} BindEventListener beans are configured in the BindEventListenerAdapter bean."
}
} else {
if(LOG.isWarnEnabled()) {
LOG.warn 'No BindEventListener beans are configured in the application context. There is no need to have grails.databinding.enableSpringEventAdapter set to true in Config.groovy.'
}
}
}
}

0 comments on commit 10af68c

Please sign in to comment.