org.dozer.spring.DozerBeanMapperFactoryBean Maven / Gradle / Ivy
/*
* Copyright 2005-2017 Dozer Project
*
* 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.dozer.spring;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dozer.BeanFactory;
import org.dozer.CustomConverter;
import org.dozer.CustomFieldMapper;
import org.dozer.DozerBeanMapper;
import org.dozer.DozerEventListener;
import org.dozer.Mapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
/**
* Public Spring FactoryBean that can be used by application code.
* Uses Spring InitializingBean and DisposableBean contracts to properly start-up and
* release global Dozer resources.
*
* @author S'ren Chittka
* @author dmitry.buzdin
*/
public class DozerBeanMapperFactoryBean implements FactoryBean,
InitializingBean, DisposableBean, ApplicationContextAware {
DozerBeanMapper beanMapper;
private Resource[] mappingFiles;
private List mappingBuilders;
private CustomFieldMapper customFieldMapper;
private List customConverters;
private Map customConvertersWithId;
private List eventListeners;
private Map factories;
private ApplicationContext applicationContext;
/**
* Spring resources definition for providing mapping file location.
* Could be used for loading all mapping files by wildcard definition for example
* {@code
*
*
*
* }
*
* @param mappingFiles Spring resource definition
*/
public final void setMappingFiles(final Resource[] mappingFiles) {
this.mappingFiles = mappingFiles;
}
public final void setMappingBuilders(final List mappingBuilders) {
this.mappingBuilders = mappingBuilders;
}
public void setCustomFieldMapper(CustomFieldMapper customFieldMapper) {
this.customFieldMapper = customFieldMapper;
}
public final void setCustomConverters(final List customConverters) {
this.customConverters = customConverters;
}
public void setCustomConvertersWithId(Map customConvertersWithId) {
this.customConvertersWithId = customConvertersWithId;
}
public final void setEventListeners(final List eventListeners) {
this.eventListeners = eventListeners;
}
public final void setFactories(final Map factories) {
this.factories = factories;
}
// ==================================================================================================================================
// interface 'FactoryBean'
// ==================================================================================================================================
public final Mapper getObject() throws Exception {
return this.beanMapper;
}
public final Class getObjectType() {
return Mapper.class;
}
public final boolean isSingleton() {
return true;
}
// ==================================================================================================================================
// interface 'InitializingBean'
// ==================================================================================================================================
public final void afterPropertiesSet() throws Exception {
this.beanMapper = new DozerBeanMapper();
loadMappingFiles();
List allConverters = new ArrayList();
Map allIdConverters = new HashMap();
Map allFactories = new HashMap();
List allListeners = new ArrayList();
List allMappingBuilders = new ArrayList();
Map contextConverters = applicationContext.getBeansOfType(CustomConverter.class);
Map contextBeanFactories = applicationContext.getBeansOfType(BeanFactory.class);
Map contextEventListeners = applicationContext.getBeansOfType(DozerEventListener.class);
Map contextMappingBuilders = applicationContext.getBeansOfType(BeanMappingBuilder.class);
allConverters.addAll(contextConverters.values());
allIdConverters.putAll(contextConverters);
allFactories.putAll(contextBeanFactories);
allListeners.addAll(contextEventListeners.values());
allMappingBuilders.addAll(contextMappingBuilders.values());
if(customFieldMapper != null){
this.beanMapper.setCustomFieldMapper(customFieldMapper);
}
if (this.customConverters != null) {
allConverters.addAll(this.customConverters);
}
if (this.customConvertersWithId != null) {
allIdConverters.putAll(this.customConvertersWithId);
}
if (this.eventListeners != null) {
allListeners.addAll(this.eventListeners);
}
if (this.factories != null) {
allFactories.putAll(this.factories);
}
if (this.mappingBuilders != null) {
allMappingBuilders.addAll(this.mappingBuilders);
}
if (!allConverters.isEmpty()) {
this.beanMapper.setCustomConverters(allConverters);
}
if (!allIdConverters.isEmpty()) {
this.beanMapper.setCustomConvertersWithId(allIdConverters);
}
if (!allFactories.isEmpty()) {
this.beanMapper.setFactories(allFactories);
}
if (!allListeners.isEmpty()) {
this.beanMapper.setEventListeners(allListeners);
}
if (!allMappingBuilders.isEmpty()) {
for (BeanMappingBuilder mappingBuilder : allMappingBuilders) {
this.beanMapper.addMapping(mappingBuilder);
}
}
}
private void loadMappingFiles() throws IOException {
if (this.mappingFiles != null) {
final List mappings = new ArrayList(this.mappingFiles.length);
for (Resource mappingFile : this.mappingFiles) {
URL url = mappingFile.getURL();
mappings.add(url.toString());
}
this.beanMapper.setMappingFiles(mappings);
}
}
/**
* Spring DisposableBean method implementation. Triggered when application context is stopped.
* Used to release global Dozer resources for hot redeployment without stopping the JVM.
*
* @throws Exception if bean mapper fails to destory
*/
public void destroy() throws Exception {
if (this.beanMapper != null) {
this.beanMapper.destroy();
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy