Skip to content

Commit

Permalink
Merge pull request #104 from kaiso/feature/#97
Browse files Browse the repository at this point in the history
[feature] upgrade to spring 6 & spring-data 4
  • Loading branch information
kaiso authored Mar 21, 2024
2 parents eb5c54f + 3f1f7c1 commit 14931e1
Show file tree
Hide file tree
Showing 12 changed files with 601 additions and 563 deletions.
499 changes: 257 additions & 242 deletions pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.kaiso.relmongo.config;

import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
Expand All @@ -9,69 +11,80 @@
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;

import java.lang.reflect.Field;
import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;

public class RelMongoBeanPostProcessor implements BeanPostProcessor {

private final String mongoTemplateRef;

public RelMongoBeanPostProcessor(String mongoTemplateRef) {
super();
this.mongoTemplateRef = mongoTemplateRef;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

if (bean instanceof MongoTemplate && beanName.equals(mongoTemplateRef)) {

/*
* Enhancer enhancer = new Enhancer();
* enhancer.setSuperclass(bean.getClass());
* enhancer.setCallbacks(new MethodInterceptor[] { new
* RelMongoTemplateInvocationHandler() });
*
* Object proxy = enhancer.create(new Class<?>[] { MongoDbFactory.class,
* MongoConverter.class },
* new Object[] { ((MongoTemplate) bean).getMongoDbFactory(), ((MongoTemplate)
* bean).getConverter() });
*
* ((MongoTemplate) proxy).setApplicationContext(applicationContext);
*
* return proxy;
*/

try {
Field ep = MongoTemplate.class.getDeclaredField("eventPublisher");
ep.setAccessible(true);
ep.set(bean, new RelMongoEventPublisher((MongoTemplate) bean, (ApplicationEventPublisher) ep.get(bean)));
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new BeanInitializationException("Fatal: failed to init the RelMongo Engine", e);
}
}

return bean;
}

private static final class RelMongoEventPublisher implements ApplicationEventPublisher {

private final RelMongoProcessor relMongoProcessor;
private final MongoTemplate mongoTemplate;
private final ApplicationEventPublisher eventPublisher;

public RelMongoEventPublisher(MongoTemplate mongoTemplate, ApplicationEventPublisher eventPublisher) {
super();
this.relMongoProcessor = new RelMongoProcessor();
this.mongoTemplate = mongoTemplate;
this.eventPublisher = eventPublisher;
}

@Override
public void publishEvent(Object event) {
relMongoProcessor.onApplicationEvent((MongoMappingEvent<?>) event, mongoTemplate);
eventPublisher.publishEvent(event);
}

}
private final String mongoTemplateRef;

public RelMongoBeanPostProcessor(String mongoTemplateRef) {
super();
this.mongoTemplateRef = mongoTemplateRef;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

if (bean instanceof MongoTemplate && beanName.equals(mongoTemplateRef)) {

/*
* Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(bean.getClass());
* enhancer.setCallbacks(new MethodInterceptor[] { new
* RelMongoTemplateInvocationHandler() });
*
* Object proxy = enhancer.create(new Class<?>[] { MongoDbFactory.class,
* MongoConverter.class }, new Object[] { ((MongoTemplate)
* bean).getMongoDbFactory(), ((MongoTemplate) bean).getConverter() });
*
* ((MongoTemplate) proxy).setApplicationContext(applicationContext);
*
* return proxy;
*/

try {
Field ep = MongoTemplate.class.getDeclaredField("eventPublisher");
ep.setAccessible(true);

RelMongoEventPublisher eventPublisher = new RelMongoEventPublisher((MongoTemplate) bean,
(ApplicationEventPublisher) ep.get(bean));
ep.set(bean, eventPublisher);

Field edf = MongoTemplate.class.getDeclaredField("eventDelegate");
edf.setAccessible(true);

Object ed = edf.get(bean);
Method method = ed.getClass().getMethod("setPublisher", ApplicationEventPublisher.class);
method.setAccessible(true);
method.invoke(ed, eventPublisher);

} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| InvocationTargetException | NoSuchMethodException e) {
throw new BeanInitializationException("Fatal: failed to init the RelMongo Engine", e);
}
}

return bean;
}

private static final class RelMongoEventPublisher implements ApplicationEventPublisher {

private final RelMongoProcessor relMongoProcessor;
private final MongoTemplate mongoTemplate;
private final ApplicationEventPublisher eventPublisher;

public RelMongoEventPublisher(MongoTemplate mongoTemplate, ApplicationEventPublisher eventPublisher) {
super();
this.relMongoProcessor = new RelMongoProcessor();
this.mongoTemplate = mongoTemplate;
this.eventPublisher = eventPublisher;
}

@Override
public void publishEvent(Object event) {
relMongoProcessor.onApplicationEvent((MongoMappingEvent<?>) event, mongoTemplate);
eventPublisher.publishEvent(event);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.kaiso.relmongo.config;

import java.util.AbstractMap;

import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.FieldNamingStrategy;
import org.springframework.data.mapping.model.Property;
Expand All @@ -9,13 +11,12 @@
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;

import java.util.AbstractMap;

/**
* Default implementation of a {@link MappingContext} for MongoDB using
* {@link BasicMongoPersistentEntity} and
Expand Down Expand Up @@ -77,12 +78,12 @@ protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
* org.springframework.data.mapping.SimpleTypeHolder)
*/
@Override
public MongoPersistentProperty createPersistentProperty(Property property, BasicMongoPersistentEntity<?> owner,
SimpleTypeHolder simpleTypeHolder) {
return new CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy);
}
public MongoPersistentProperty createPersistentProperty(Property property, MongoPersistentEntity<?> owner,
SimpleTypeHolder simpleTypeHolder) {
return new CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy);
}

/*
/*
* (non-Javadoc)
*
* @see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public void doWith(Field field) throws IllegalAccessException {
return;
}

MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field));

if (field.isAnnotationPresent(OneToMany.class)) {
fillIdentifiers(field, field.getAnnotation(OneToMany.class).cascade());
} else if (field.isAnnotationPresent(OneToOne.class)) {
fillIdentifiers(field, field.getAnnotation(OneToOne.class).cascade());
}

MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public PersistentPropertyPostSavingCallback(Object source, Class<?> sourceClass,
public void apply() {
ReflectionUtils.doWithFields(sourceClass, this);
cache.stream().forEach(mongoOperations::save);
cache.clear();
}

public void doWith(Field field) throws IllegalAccessException {
Expand Down
Loading

0 comments on commit 14931e1

Please sign in to comment.