Skip to content

Commit

Permalink
Mongo storage factory bean initializes locking and versioning for its…
Browse files Browse the repository at this point in the history
… storage when available

Fixes #373
  • Loading branch information
paulcwarren committed Oct 29, 2020
1 parent 2d6d0d2 commit 5f34e02
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package internal.org.springframework.content.mongo.config;

import internal.org.springframework.content.mongo.store.DefaultMongoStoreImpl;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.repository.factory.AbstractStoreFactoryBean;
import org.springframework.content.commons.utils.PlacementService;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.versions.LockingAndVersioningProxyFactory;

import internal.org.springframework.content.mongo.store.DefaultMongoStoreImpl;

public class MongoStoreFactoryBean extends AbstractStoreFactoryBean {

@Autowired
private GridFsTemplate gridFs;

@Autowired
private PlacementService mongoStorePlacementService;

@Autowired(required=false)
private LockingAndVersioningProxyFactory versioning;

@Override
protected void addProxyAdvice(ProxyFactory result, BeanFactory beanFactory) {
if (versioning != null) {
versioning.apply(result);
}
}

@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package internal.org.springframework.content.mongo.store;

import internal.org.springframework.content.mongo.io.GridFsStoreResource;
import static java.lang.String.format;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.content.commons.annotations.ContentId;
Expand All @@ -15,18 +25,10 @@
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.io.Resource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.UUID;

import static java.lang.String.format;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;
import internal.org.springframework.content.mongo.io.GridFsStoreResource;

public class DefaultMongoStoreImpl<S, SID extends Serializable>
implements Store<SID>, AssociativeStore<S, SID>, ContentStore<S, SID> {
Expand Down Expand Up @@ -93,6 +95,7 @@ public boolean matches(Field field) {
}

@Override
@Transactional
public S setContent(S property, InputStream content) {
Object contentId = BeanUtils.getFieldWithAnnotation(property, ContentId.class);
if (contentId == null) {
Expand Down Expand Up @@ -128,6 +131,7 @@ public S setContent(S property, InputStream content) {
}

@Override
@Transactional
public S setContent(S property, Resource resourceContent) {
try {
return setContent(property, resourceContent.getInputStream());
Expand All @@ -138,6 +142,7 @@ public S setContent(S property, Resource resourceContent) {
}

@Override
@Transactional
public InputStream getContent(S entity) {
if (entity == null)
return null;
Expand All @@ -160,6 +165,7 @@ public InputStream getContent(S entity) {
}

@Override
@Transactional
public S unsetContent(S property) {
if (property == null)
return property;
Expand Down
23 changes: 23 additions & 0 deletions spring-content-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@
<version>1.1.0.M5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-mongo</artifactId>
<version>1.1.0.M5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-versions-jpa</artifactId>
Expand All @@ -195,6 +201,23 @@
<version>4.1.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<repositories>
Expand Down
Loading

0 comments on commit 5f34e02

Please sign in to comment.