forked from michaellavelle/spring-data-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
646a441
commit c238d39
Showing
17 changed files
with
910 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...main/java/org/socialsignin/spring/data/dynamodb/config/AbstractDynamoDBConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.socialsignin.spring.data.dynamodb.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Base class for Spring Data DynamoDB configuration using JavaConfig. | ||
* | ||
* @author Vito Limandibhrata | ||
*/ | ||
@Configuration | ||
public abstract class AbstractDynamoDBConfiguration { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDynamoDBConfiguration.class); | ||
|
||
public abstract AmazonDynamoDB amazonDynamoDB(); | ||
|
||
public abstract AWSCredentials amazonAWSCredentials(); | ||
|
||
/** | ||
* Return the base packages to scan for mapped {@link DynamoDBTable}s. Will return the package name of the configuration | ||
* class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending | ||
* {@link AbstractDynamoDBConfiguration} the base package will be considered {@code com.acme} unless the method is | ||
* overriden to implement alternate behaviour. | ||
* | ||
* @return the base package to scan for mapped {@link DynamoDBTable} classes or {@literal null} to not enable scanning for | ||
* entities. | ||
*/ | ||
protected String[] getMappingBasePackages() { | ||
|
||
Package mappingBasePackage = getClass().getPackage(); | ||
String basePackage = mappingBasePackage == null ? null : mappingBasePackage.getName(); | ||
|
||
return new String[]{basePackage}; | ||
} | ||
|
||
/** | ||
* Creates a {@link DynamoDBMappingContext} equipped with entity classes scanned from the mapping base package. | ||
* | ||
* @see #getMappingBasePackages() | ||
* @return | ||
* @throws ClassNotFoundException | ||
*/ | ||
@Bean | ||
public DynamoDBMappingContext dynamoDBMappingContext() throws ClassNotFoundException { | ||
|
||
DynamoDBMappingContext mappingContext = new DynamoDBMappingContext(); | ||
mappingContext.setInitialEntitySet(getInitialEntitySet()); | ||
|
||
return mappingContext; | ||
} | ||
|
||
/** | ||
* Scans the mapping base package for classes annotated with {@link DynamoDBTable}. | ||
* | ||
* @see #getMappingBasePackages() | ||
* @return | ||
* @throws ClassNotFoundException | ||
*/ | ||
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException { | ||
|
||
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>(); | ||
|
||
String[] basePackages = getMappingBasePackages(); | ||
|
||
for (String basePackage:basePackages) { | ||
LOGGER.trace("getInitialEntitySet. basePackage: {}", basePackage); | ||
|
||
if (StringUtils.hasText(basePackage)) { | ||
ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( | ||
false); | ||
componentProvider.addIncludeFilter(new AnnotationTypeFilter(DynamoDBTable.class)); | ||
|
||
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { | ||
LOGGER.trace("getInitialEntitySet. candidate: {}", candidate.getBeanClassName()); | ||
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), | ||
AbstractDynamoDBConfiguration.class.getClassLoader())); | ||
} | ||
} | ||
} | ||
|
||
return initialEntitySet; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/socialsignin/spring/data/dynamodb/config/BeanNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.socialsignin.spring.data.dynamodb.config; | ||
|
||
/** | ||
* Constants to declare bean names used by the namespace configuration. | ||
* | ||
* @author Vito Limandibhrata | ||
*/ | ||
public abstract class BeanNames { | ||
|
||
public static final String MAPPING_CONTEXT_BEAN_NAME = "dynamoDBMappingContext"; | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...va/org/socialsignin/spring/data/dynamodb/config/DynamoDBAuditingBeanDefinitionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.socialsignin.spring.data.dynamodb.config; | ||
|
||
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext; | ||
import org.socialsignin.spring.data.dynamodb.mapping.event.AuditingEventListener; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.springframework.data.auditing.config.IsNewAwareAuditingHandlerBeanDefinitionParser; | ||
import org.springframework.util.StringUtils; | ||
import org.w3c.dom.Element; | ||
|
||
import static org.socialsignin.spring.data.dynamodb.config.BeanNames.MAPPING_CONTEXT_BEAN_NAME; | ||
import static org.springframework.data.config.ParsingUtils.getObjectFactoryBeanDefinition; | ||
|
||
/** | ||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} to register a {@link AuditingEventListener} to transparently set auditing information on | ||
* an entity. | ||
* | ||
* @author Vito Limandibhrata | ||
*/ | ||
public class DynamoDBAuditingBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#getBeanClass(org.w3c.dom.Element) | ||
*/ | ||
@Override | ||
protected Class<?> getBeanClass(Element element) { | ||
return AuditingEventListener.class; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#shouldGenerateId() | ||
*/ | ||
@Override | ||
protected boolean shouldGenerateId() { | ||
return true; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#doParse(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext, org.springframework.beans.factory.support.BeanDefinitionBuilder) | ||
*/ | ||
@Override | ||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { | ||
|
||
String mappingContextRef = element.getAttribute("mapping-context-ref"); | ||
|
||
if (!StringUtils.hasText(mappingContextRef)) { | ||
|
||
BeanDefinitionRegistry registry = parserContext.getRegistry(); | ||
|
||
if (!registry.containsBeanDefinition(MAPPING_CONTEXT_BEAN_NAME)) { | ||
registry.registerBeanDefinition(MAPPING_CONTEXT_BEAN_NAME, new RootBeanDefinition(DynamoDBMappingContext.class)); | ||
} | ||
|
||
mappingContextRef = MAPPING_CONTEXT_BEAN_NAME; | ||
} | ||
|
||
IsNewAwareAuditingHandlerBeanDefinitionParser parser = new IsNewAwareAuditingHandlerBeanDefinitionParser( | ||
mappingContextRef); | ||
parser.parse(element, parserContext); | ||
|
||
builder.addConstructorArgValue(getObjectFactoryBeanDefinition(parser.getResolvedBeanName(), | ||
parserContext.extractSource(element))); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
src/main/java/org/socialsignin/spring/data/dynamodb/config/DynamoDBAuditingRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.socialsignin.spring.data.dynamodb.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
import org.springframework.data.auditing.IsNewAwareAuditingHandler; | ||
import org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport; | ||
import org.springframework.data.auditing.config.AuditingConfiguration; | ||
import org.springframework.data.config.ParsingUtils; | ||
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext; | ||
import org.socialsignin.spring.data.dynamodb.mapping.event.AuditingEventListener; | ||
import org.springframework.util.Assert; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import static org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE; | ||
import static org.socialsignin.spring.data.dynamodb.config.BeanNames.MAPPING_CONTEXT_BEAN_NAME; | ||
|
||
/** | ||
* {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar} to enable {@link EnableDynamoDBAuditing} annotation. | ||
* | ||
* @author Vito Limandibhrata | ||
*/ | ||
class DynamoDBAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport { | ||
|
||
private static Logger LOGGER = LoggerFactory.getLogger(DynamoDBAuditingRegistrar.class); | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAnnotation() | ||
*/ | ||
@Override | ||
protected Class<? extends Annotation> getAnnotation() { | ||
return EnableDynamoDBAuditing.class; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAuditingHandlerBeanName() | ||
*/ | ||
@Override | ||
protected String getAuditingHandlerBeanName() { | ||
return "dynamoDBAuditingHandler"; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry) | ||
*/ | ||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) { | ||
LOGGER.trace("registerBeanDefinitions"); | ||
Assert.notNull(annotationMetadata, "AnnotationMetadata must not be null!"); | ||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!"); | ||
|
||
defaultDependenciesIfNecessary(registry, annotationMetadata); | ||
super.registerBeanDefinitions(annotationMetadata, registry); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAuditHandlerBeanDefinitionBuilder(org.springframework.data.auditing.config.AuditingConfiguration) | ||
*/ | ||
@Override | ||
protected BeanDefinitionBuilder getAuditHandlerBeanDefinitionBuilder(AuditingConfiguration configuration) { | ||
LOGGER.trace("getAuditHandlerBeanDefinitionBuilder"); | ||
Assert.notNull(configuration, "AuditingConfiguration must not be null!"); | ||
|
||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(IsNewAwareAuditingHandler.class); | ||
builder.addConstructorArgReference(MAPPING_CONTEXT_BEAN_NAME); | ||
return configureDefaultAuditHandlerAttributes(configuration, builder); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#registerAuditListener(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry) | ||
*/ | ||
@Override | ||
protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandlerDefinition, | ||
BeanDefinitionRegistry registry) { | ||
LOGGER.trace("registerAuditListenerBeanDefinition"); | ||
Assert.notNull(auditingHandlerDefinition, "BeanDefinition must not be null!"); | ||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!"); | ||
|
||
BeanDefinitionBuilder listenerBeanDefinitionBuilder = BeanDefinitionBuilder | ||
.rootBeanDefinition(AuditingEventListener.class); | ||
listenerBeanDefinitionBuilder.addConstructorArgValue(ParsingUtils.getObjectFactoryBeanDefinition( | ||
getAuditingHandlerBeanName(), registry)); | ||
|
||
registerInfrastructureBeanWithId(listenerBeanDefinitionBuilder.getBeanDefinition(), | ||
AuditingEventListener.class.getName(), registry); | ||
} | ||
|
||
/** | ||
* Register default bean definitions for a {@link DynamoDBMappingContext} and an {@link org.springframework.data.support.IsNewStrategyFactory} in case we | ||
* don't find beans with the assumed names in the registry. | ||
* | ||
* @param registry the {@link BeanDefinitionRegistry} to use to register the components into. | ||
* @param source the source which the registered components shall be registered with | ||
*/ | ||
private void defaultDependenciesIfNecessary(BeanDefinitionRegistry registry, Object source) { | ||
LOGGER.trace("defaultDependenciesIfNecessary. source:{}", source); | ||
LOGGER.trace("is registry.containsBeanDefinition {} {}", MAPPING_CONTEXT_BEAN_NAME, registry.containsBeanDefinition(MAPPING_CONTEXT_BEAN_NAME)); | ||
if (!registry.containsBeanDefinition(MAPPING_CONTEXT_BEAN_NAME)) { | ||
|
||
RootBeanDefinition definition = new RootBeanDefinition(DynamoDBMappingContext.class); | ||
definition.setRole(ROLE_INFRASTRUCTURE); | ||
definition.setSource(source); | ||
|
||
registry.registerBeanDefinition(MAPPING_CONTEXT_BEAN_NAME, definition); | ||
} | ||
} | ||
} |
Oops, something went wrong.