Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GRAILS-10548 - Add support for the old BindEventListener listeners to…
… the new data binder
- Loading branch information
Jeff Scott Brown
committed
Oct 30, 2013
1 parent
b0f59d3
commit 10af68c
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...eb/src/main/groovy/org/codehaus/groovy/grails/web/binding/BindEventListenerAdapter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.' | ||
} | ||
} | ||
} | ||
} |