Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When versioning, optimisticly lock setContent (with resource) invocations #508

Merged
merged 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.runner.RunWith;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.content.commons.repository.ContentStore;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ReflectionUtils;

import javax.persistence.EntityManager;
Expand Down Expand Up @@ -97,6 +99,33 @@ public class OptimisticLockingInterceptorTest {
});
});
});
Context("when the method invocation is setContent (with resource)", () -> {
BeforeEach(() -> {
entity = new TestEntity();
when(mi.getMethod()).thenReturn(ReflectionUtils.findMethod(ContentStore.class, "setContent", Object.class, Resource.class));
when(mi.getArguments()).thenReturn(new Object[]{entity, new FileSystemResource("")});
when(em.merge(entity)).thenReturn(entity);
when(mi.proceed()).thenReturn(entity);
});
JustBeforeEach(() -> {
result = interceptor.invoke(mi);
});
It("should lock the entity and proceed", () -> {
assertThat(result, is(not(nullValue())));
verify(em).lock(entity, LockModeType.OPTIMISTIC);
verify(mi).setArguments(eq(entity), anyObject());
verify(mi).proceed();
assertThat(((TestEntity)entity).getVersion(), is(1L));
});
Context("when the entity is not @Versioned", () -> {
BeforeEach(() -> {
entity = new TestEntityUnversionsed();
});
It("should still proceed", () -> {
verify(mi).proceed();
});
});
});
Context("when the method invocation is unsetContent", () -> {
BeforeEach(() -> {
entity = new TestEntity();
Expand Down