Skip to content

Commit f279f3e

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Conflicts: build.gradle build.properties grails-test-suite-uber/src/test/groovy/grails/util/GrailsUtilTests.java
2 parents d9d0607 + 9093d93 commit f279f3e

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ext {
2424
commonsCollectionsVersion = "3.2.1"
2525
commonsIOVersion = "2.1"
2626
commonsLangVersion = "2.6"
27-
datastoreVersion = "1.1.4.BUILD-SNAPSHOT"
27+
datastoreVersion = "1.1.4.RELEASE"
2828
gantVersion = "1.9.6"
2929
gdocEngineVersion = "1.0.1"
3030
groovyVersion = "2.0.7"

grails-bootstrap/src/main/groovy/org/codehaus/groovy/grails/resolve/GrailsCoreDependencies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public Object doCall() {
172172
};
173173
registerDependencies(dependencyManager, compileTimeDependenciesMethod, commonsExcludingLoggingAndXmlApis, "commons-logging", "xml-apis", "commons-digester");
174174

175-
String datastoreMappingVersion = "1.1.4.BUILD-SNAPSHOT";
175+
String datastoreMappingVersion = "1.1.4.RELEASE";
176176
ModuleRevisionId[] compileDependencies = {
177177
ModuleRevisionId.newInstance("aopalliance", "aopalliance", "1.0"),
178178
ModuleRevisionId.newInstance("com.googlecode.concurrentlinkedhashmap", "concurrentlinkedhashmap-lru", "1.3.1"),

grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/EventTriggeringInterceptor.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@
2525

2626
import org.codehaus.groovy.grails.commons.AnnotationDomainClassArtefactHandler;
2727
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler;
28+
import org.codehaus.groovy.grails.commons.GrailsApplication;
29+
import org.codehaus.groovy.grails.commons.GrailsDomainClass;
30+
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty;
31+
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder;
2832
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
33+
import org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping;
2934
import org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventListener;
3035
import org.codehaus.groovy.grails.orm.hibernate.support.SoftKey;
3136
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
3237
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent;
3338
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener;
3439
import org.grails.datastore.mapping.engine.event.ValidationEvent;
3540
import org.hibernate.HibernateException;
41+
import org.hibernate.SessionFactory;
3642
import org.hibernate.event.PostDeleteEvent;
3743
import org.hibernate.event.PostInsertEvent;
3844
import org.hibernate.event.PostLoadEvent;
@@ -42,6 +48,9 @@
4248
import org.hibernate.event.PreLoadEvent;
4349
import org.hibernate.event.PreUpdateEvent;
4450
import org.hibernate.event.SaveOrUpdateEvent;
51+
import org.slf4j.Logger;
52+
import org.slf4j.LoggerFactory;
53+
import org.springframework.context.ApplicationContext;
4554
import org.springframework.context.ApplicationEvent;
4655

4756
/**
@@ -61,6 +70,8 @@ public class EventTriggeringInterceptor extends AbstractPersistenceEventListener
6170
private boolean failOnError;
6271
private List<?> failOnErrorPackages = Collections.emptyList();
6372

73+
protected Logger log = LoggerFactory.getLogger(getClass());
74+
6475
public EventTriggeringInterceptor(HibernateDatastore datastore, ConfigObject co) {
6576
super(datastore);
6677

@@ -211,10 +222,9 @@ private ClosureEventListener findEventListener(Object entity) {
211222
synchronized(clazz) {
212223
eventListener = eventListeners.get(key);
213224
if (eventListener == null) {
214-
shouldTrigger = (entity != null &&
215-
(GroovySystem.getMetaClassRegistry().getMetaClass(entity.getClass()) != null) &&
216-
(DomainClassArtefactHandler.isDomainClass(clazz) ||
217-
AnnotationDomainClassArtefactHandler.isJPADomainClass(clazz)));
225+
shouldTrigger = (GroovySystem.getMetaClassRegistry().getMetaClass(entity.getClass()) != null &&
226+
(DomainClassArtefactHandler.isDomainClass(clazz) || AnnotationDomainClassArtefactHandler.isJPADomainClass(clazz)) &&
227+
isDefinedByCurrentDataStore(entity));
218228
if (shouldTrigger) {
219229
eventListener = new ClosureEventListener(clazz, failOnError, failOnErrorPackages);
220230
ClosureEventListener previous = eventListeners.putIfAbsent(key, eventListener);
@@ -229,6 +239,49 @@ private ClosureEventListener findEventListener(Object entity) {
229239
return eventListener;
230240
}
231241

242+
protected boolean isDefinedByCurrentDataStore(Object entity) {
243+
SessionFactory currentDataStoreSessionFactory = ((HibernateDatastore) datastore).getSessionFactory();
244+
ApplicationContext applicationContext = datastore.getApplicationContext();
245+
246+
Mapping mapping = GrailsDomainBinder.getMapping(entity.getClass());
247+
List<String> dataSourceNames = null;
248+
if (mapping == null) {
249+
GrailsApplication grailsApplication = applicationContext.getBean("grailsApplication", GrailsApplication.class);
250+
GrailsDomainClass dc = (GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, entity.getClass().getName());
251+
if (dc != null) {
252+
dataSourceNames = GrailsHibernateUtil.getDatasourceNames(dc);
253+
}
254+
}
255+
else {
256+
dataSourceNames = mapping.getDatasources();
257+
}
258+
259+
if (dataSourceNames == null) {
260+
return false;
261+
}
262+
263+
for (String dataSource : dataSourceNames) {
264+
if (GrailsDomainClassProperty.ALL_DATA_SOURCES.equals(dataSource)) {
265+
return true;
266+
}
267+
boolean isDefault = dataSource.equals(GrailsDomainClassProperty.DEFAULT_DATA_SOURCE);
268+
String suffix = isDefault ? "" : "_" + dataSource;
269+
String sessionFactoryBeanName = "sessionFactory" + suffix;
270+
271+
if (applicationContext.containsBean(sessionFactoryBeanName)) {
272+
SessionFactory sessionFactory = applicationContext.getBean(sessionFactoryBeanName, SessionFactory.class);
273+
if (currentDataStoreSessionFactory == sessionFactory) {
274+
return true;
275+
}
276+
}
277+
else {
278+
log.warn("Cannot resolve SessionFactory for dataSource {} and entity {}",
279+
new Object[] { dataSource, entity.getClass().getName()});
280+
}
281+
}
282+
return false;
283+
}
284+
232285
/**
233286
* {@inheritDoc}
234287
* @see org.springframework.context.event.SmartApplicationListener#supportsEventType(

0 commit comments

Comments
 (0)