Skip to content

Commit

Permalink
When versioning optimisticly lock setContent with resource invocations
Browse files Browse the repository at this point in the history
Fixes #373
  • Loading branch information
paulcwarren committed Mar 19, 2021
1 parent 2eb2a2e commit aaf7680
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package org.springframework.versions.interceptors;

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Version;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.content.commons.repository.ContentStore;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Version;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class OptimisticLockingInterceptor implements MethodInterceptor {

private static Method getContentMethod;
private static Method setContentMethod;
private static Method setContentMethodWithResource;
private static Method unsetContentMethod;

static {
getContentMethod = ReflectionUtils.findMethod(ContentStore.class, "getContent", Object.class);
Assert.notNull(getContentMethod);
setContentMethod = ReflectionUtils.findMethod(ContentStore.class, "setContent", Object.class, InputStream.class);
Assert.notNull(setContentMethod);
setContentMethodWithResource = ReflectionUtils.findMethod(ContentStore.class, "setContent", Object.class, Resource.class);
Assert.notNull(setContentMethodWithResource);
unsetContentMethod = ReflectionUtils.findMethod(ContentStore.class,"unsetContent", Object.class);
Assert.notNull(unsetContentMethod);
}
Expand Down Expand Up @@ -60,6 +64,16 @@ else if (setContentMethod.equals(methodInvocation.getMethod())) {
return entity;
}
}
else if (setContentMethodWithResource.equals(methodInvocation.getMethod())) {
if (methodInvocation.getArguments().length > 0) {
Object entity = methodInvocation.getArguments()[0];
entity = lock(entity);
((ProxyMethodInvocation)methodInvocation).setArguments(entity, methodInvocation.getArguments()[1]);
entity = methodInvocation.proceed();
touch(entity, Version.class);
return entity;
}
}
else if (unsetContentMethod.equals(methodInvocation.getMethod())) {
if (methodInvocation.getArguments().length > 0) {
Object entity = methodInvocation.getArguments()[0];
Expand Down

0 comments on commit aaf7680

Please sign in to comment.