|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.net.URL;
|
20 | 20 | import java.util.ArrayList;
|
| 21 | +import java.util.Arrays; |
21 | 22 | import java.util.HashMap;
|
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Map;
|
|
40 | 41 |
|
41 | 42 | /**
|
42 | 43 | * Public Spring FactoryBean that can be used by application code.
|
43 |
| - * Uses Spring InitializingBean and DisposableBean contracts to properly start-up and |
44 |
| - * release global Dozer resources. |
| 44 | + * Uses Spring InitializingBean contracts to properly start-up |
45 | 45 | *
|
46 | 46 | * @author S'ren Chittka
|
47 | 47 | * @author dmitry.buzdin
|
48 | 48 | */
|
49 |
| -public class DozerBeanMapperFactoryBean implements FactoryBean<Mapper>, |
50 |
| - InitializingBean, DisposableBean, ApplicationContextAware { |
| 49 | +public class DozerBeanMapperFactoryBean implements ApplicationContextAware, InitializingBean, FactoryBean<Mapper> { |
51 | 50 |
|
52 |
| - DozerBeanMapper beanMapper; |
53 |
| - private Resource[] mappingFiles; |
54 |
| - private List<BeanMappingBuilder> mappingBuilders; |
55 |
| - private CustomFieldMapper customFieldMapper; |
56 |
| - private List<CustomConverter> customConverters; |
57 |
| - private Map<String, CustomConverter> customConvertersWithId; |
58 |
| - private List<DozerEventListener> eventListeners; |
59 |
| - private Map<String, BeanFactory> factories; |
60 | 51 | private ApplicationContext applicationContext;
|
61 | 52 |
|
| 53 | + private CustomFieldMapper customFieldMapper; |
| 54 | + private List<String> mappingFileUrls = new ArrayList<>(1); |
| 55 | + private List<CustomConverter> customConverters = new ArrayList<>(0); |
| 56 | + private List<BeanMappingBuilder> mappingBuilders = new ArrayList<>(0); |
| 57 | + private List<DozerEventListener> eventListeners = new ArrayList<>(0); |
| 58 | + private Map<String, BeanFactory> beanFactories = new HashMap<>(0); |
| 59 | + private Map<String, CustomConverter> customConvertersWithId = new HashMap<>(0); |
| 60 | + |
| 61 | + private Mapper mapper; |
| 62 | + |
| 63 | + public void setCustomFieldMapper(CustomFieldMapper customFieldMapper) { |
| 64 | + this.customFieldMapper = customFieldMapper; |
| 65 | + } |
| 66 | + |
62 | 67 | /**
|
63 | 68 | * Spring resources definition for providing mapping file location.
|
64 | 69 | * Could be used for loading all mapping files by wildcard definition for example
|
| 70 | + * |
65 | 71 | * {@code
|
66 | 72 | * <bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
|
67 |
| - * <property name="mappingFiles" value="classpath*:/*.dozer.xml"/> |
| 73 | + * <property name="mappingFiles" value="classpath*:/*.dozer.xml"/> |
68 | 74 | * </bean>
|
69 | 75 | * }
|
70 | 76 | *
|
71 | 77 | * @param mappingFiles Spring resource definition
|
| 78 | + * @throws IOException if URL fails to resolve |
72 | 79 | */
|
73 |
| - public final void setMappingFiles(final Resource[] mappingFiles) { |
74 |
| - this.mappingFiles = mappingFiles; |
75 |
| - } |
76 |
| - |
77 |
| - public final void setMappingBuilders(final List<BeanMappingBuilder> mappingBuilders) { |
78 |
| - this.mappingBuilders = mappingBuilders; |
79 |
| - } |
80 |
| - |
81 |
| - public void setCustomFieldMapper(CustomFieldMapper customFieldMapper) { |
82 |
| - this.customFieldMapper = customFieldMapper; |
| 80 | + public void setMappingFiles(Resource[] mappingFiles) throws IOException { |
| 81 | + if (mappingFiles != null && mappingFiles.length > 0) { |
| 82 | + for (Resource mappingFile : mappingFiles) { |
| 83 | + URL url = mappingFile.getURL(); |
| 84 | + mappingFileUrls.add(url.toString()); |
| 85 | + } |
| 86 | + } |
83 | 87 | }
|
84 | 88 |
|
85 |
| - public final void setCustomConverters(final List<CustomConverter> customConverters) { |
86 |
| - this.customConverters = customConverters; |
| 89 | + public void setCustomConverters(List<CustomConverter> customConverters) { |
| 90 | + this.customConverters.addAll(customConverters); |
87 | 91 | }
|
88 | 92 |
|
89 |
| - public void setCustomConvertersWithId(Map<String, CustomConverter> customConvertersWithId) { |
90 |
| - this.customConvertersWithId = customConvertersWithId; |
| 93 | + public void setMappingBuilders(List<BeanMappingBuilder> mappingBuilders) { |
| 94 | + this.mappingBuilders.addAll(mappingBuilders); |
91 | 95 | }
|
92 | 96 |
|
93 |
| - public final void setEventListeners(final List<DozerEventListener> eventListeners) { |
94 |
| - this.eventListeners = eventListeners; |
| 97 | + public void setEventListeners(List<DozerEventListener> eventListeners) { |
| 98 | + this.eventListeners.addAll(eventListeners); |
95 | 99 | }
|
96 | 100 |
|
97 |
| - public final void setFactories(final Map<String, BeanFactory> factories) { |
98 |
| - this.factories = factories; |
| 101 | + public void setFactories(Map<String, BeanFactory> beanFactories) { |
| 102 | + this.beanFactories.putAll(beanFactories); |
99 | 103 | }
|
100 | 104 |
|
101 |
| - // ================================================================================================================================== |
102 |
| - // interface 'FactoryBean' |
103 |
| - // ================================================================================================================================== |
104 |
| - public final Mapper getObject() throws Exception { |
105 |
| - return this.beanMapper; |
| 105 | + public void setCustomConvertersWithId(Map<String, CustomConverter> customConvertersWithId) { |
| 106 | + this.customConvertersWithId.putAll(customConvertersWithId); |
106 | 107 | }
|
107 | 108 |
|
108 |
| - public final Class<Mapper> getObjectType() { |
109 |
| - return Mapper.class; |
110 |
| - } |
| 109 | + // === |
| 110 | + // Methods for: ApplicationContextAware |
| 111 | + // === |
111 | 112 |
|
112 |
| - public final boolean isSingleton() { |
113 |
| - return true; |
| 113 | + @Override |
| 114 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 115 | + this.applicationContext = applicationContext; |
114 | 116 | }
|
115 | 117 |
|
116 |
| - // ================================================================================================================================== |
117 |
| - // interface 'InitializingBean' |
118 |
| - // ================================================================================================================================== |
119 |
| - public final void afterPropertiesSet() throws Exception { |
120 |
| - // todo to be reworked in #402 |
121 |
| - this.beanMapper = (DozerBeanMapper) DozerBeanMapperBuilder.buildDefault(); |
122 |
| - |
123 |
| - loadMappingFiles(); |
| 118 | + // === |
| 119 | + // Methods for: InitializingBean |
| 120 | + // === |
124 | 121 |
|
125 |
| - List<CustomConverter> allConverters = new ArrayList<CustomConverter>(); |
126 |
| - Map<String, CustomConverter> allIdConverters = new HashMap<String, CustomConverter>(); |
127 |
| - Map<String, BeanFactory> allFactories = new HashMap<String, BeanFactory>(); |
128 |
| - List<DozerEventListener> allListeners = new ArrayList<DozerEventListener>(); |
129 |
| - List<BeanMappingBuilder> allMappingBuilders = new ArrayList<BeanMappingBuilder>(); |
130 |
| - |
131 |
| - Map<String, CustomConverter> contextConverters = applicationContext.getBeansOfType(CustomConverter.class); |
132 |
| - Map<String, BeanFactory> contextBeanFactories = applicationContext.getBeansOfType(BeanFactory.class); |
| 122 | + @Override |
| 123 | + public void afterPropertiesSet() throws Exception { |
| 124 | + Map<String, CustomConverter> contextCustomConvertersWithId = applicationContext.getBeansOfType(CustomConverter.class); |
| 125 | + Map<String, BeanMappingBuilder> contextBeanMappingBuilders = applicationContext.getBeansOfType(BeanMappingBuilder.class); |
133 | 126 | Map<String, DozerEventListener> contextEventListeners = applicationContext.getBeansOfType(DozerEventListener.class);
|
134 |
| - Map<String, BeanMappingBuilder> contextMappingBuilders = applicationContext.getBeansOfType(BeanMappingBuilder.class); |
135 |
| - |
136 |
| - allConverters.addAll(contextConverters.values()); |
137 |
| - allIdConverters.putAll(contextConverters); |
138 |
| - allFactories.putAll(contextBeanFactories); |
139 |
| - allListeners.addAll(contextEventListeners.values()); |
140 |
| - allMappingBuilders.addAll(contextMappingBuilders.values()); |
141 |
| - |
142 |
| - if (customFieldMapper != null) { |
143 |
| - this.beanMapper.setCustomFieldMapper(customFieldMapper); |
144 |
| - } |
| 127 | + Map<String, BeanFactory> contextBeanFactorys = applicationContext.getBeansOfType(BeanFactory.class); |
145 | 128 |
|
146 |
| - if (this.customConverters != null) { |
147 |
| - allConverters.addAll(this.customConverters); |
148 |
| - } |
| 129 | + customConverters.addAll(contextCustomConvertersWithId.values()); |
| 130 | + mappingBuilders.addAll(contextBeanMappingBuilders.values()); |
| 131 | + beanFactories.putAll(contextBeanFactorys); |
| 132 | + eventListeners.addAll(contextEventListeners.values()); |
| 133 | + customConvertersWithId.putAll(contextCustomConvertersWithId); |
149 | 134 |
|
150 |
| - if (this.customConvertersWithId != null) { |
151 |
| - allIdConverters.putAll(this.customConvertersWithId); |
152 |
| - } |
| 135 | + DozerBeanMapperBuilder builder = DozerBeanMapperBuilder.create(); |
153 | 136 |
|
154 |
| - if (this.eventListeners != null) { |
155 |
| - allListeners.addAll(this.eventListeners); |
| 137 | + for (String url : mappingFileUrls) { |
| 138 | + builder.withMappingFiles(url); |
156 | 139 | }
|
157 | 140 |
|
158 |
| - if (this.factories != null) { |
159 |
| - allFactories.putAll(this.factories); |
160 |
| - } |
| 141 | + builder.withCustomFieldMapper(customFieldMapper); |
161 | 142 |
|
162 |
| - if (this.mappingBuilders != null) { |
163 |
| - allMappingBuilders.addAll(this.mappingBuilders); |
| 143 | + for (CustomConverter converter : customConverters) { |
| 144 | + builder.withCustomConverter(converter); |
164 | 145 | }
|
165 | 146 |
|
166 |
| - if (!allConverters.isEmpty()) { |
167 |
| - this.beanMapper.setCustomConverters(allConverters); |
| 147 | + for (BeanMappingBuilder mappingBuilder : mappingBuilders) { |
| 148 | + builder.withMappingBuilder(mappingBuilder); |
168 | 149 | }
|
169 | 150 |
|
170 |
| - if (!allIdConverters.isEmpty()) { |
171 |
| - this.beanMapper.setCustomConvertersWithId(allIdConverters); |
| 151 | + for (DozerEventListener listener : eventListeners) { |
| 152 | + builder.withEventListener(listener); |
172 | 153 | }
|
173 | 154 |
|
174 |
| - if (!allFactories.isEmpty()) { |
175 |
| - this.beanMapper.setFactories(allFactories); |
| 155 | + for (Map.Entry<String, BeanFactory> beanFactoryEntry : beanFactories.entrySet()) { |
| 156 | + builder.withBeanFactory(beanFactoryEntry.getKey(), beanFactoryEntry.getValue()); |
176 | 157 | }
|
177 | 158 |
|
178 |
| - if (!allListeners.isEmpty()) { |
179 |
| - this.beanMapper.setEventListeners(allListeners); |
| 159 | + for (Map.Entry<String, CustomConverter> customConverterEntry : customConvertersWithId.entrySet()) { |
| 160 | + builder.withCustomConverterWithId(customConverterEntry.getKey(), customConverterEntry.getValue()); |
180 | 161 | }
|
181 | 162 |
|
182 |
| - if (!allMappingBuilders.isEmpty()) { |
183 |
| - for (BeanMappingBuilder mappingBuilder : allMappingBuilders) { |
184 |
| - this.beanMapper.addMapping(mappingBuilder); |
185 |
| - } |
186 |
| - } |
| 163 | + this.mapper = builder.build(); |
187 | 164 | }
|
188 | 165 |
|
189 |
| - private void loadMappingFiles() throws IOException { |
190 |
| - if (this.mappingFiles != null) { |
191 |
| - final List<String> mappings = new ArrayList<String>(this.mappingFiles.length); |
192 |
| - for (Resource mappingFile : this.mappingFiles) { |
193 |
| - URL url = mappingFile.getURL(); |
194 |
| - mappings.add(url.toString()); |
195 |
| - } |
| 166 | + // === |
| 167 | + // Methods for: FactoryBean<Mapper> |
| 168 | + // === |
196 | 169 |
|
197 |
| - this.beanMapper.setMappingFiles(mappings); |
198 |
| - } |
| 170 | + @Override |
| 171 | + public Mapper getObject() throws Exception { |
| 172 | + return this.mapper; |
199 | 173 | }
|
200 | 174 |
|
201 |
| - /** |
202 |
| - * Spring DisposableBean method implementation. Triggered when application context is stopped. |
203 |
| - * Used to release global Dozer resources for hot redeployment without stopping the JVM. |
204 |
| - * |
205 |
| - * @throws Exception if bean mapper fails to destory |
206 |
| - */ |
207 |
| - public void destroy() throws Exception { |
208 |
| - if (this.beanMapper != null) { |
209 |
| - this.beanMapper.destroy(); |
210 |
| - } |
| 175 | + @Override |
| 176 | + public Class<Mapper> getObjectType() { |
| 177 | + return Mapper.class; |
211 | 178 | }
|
212 | 179 |
|
213 |
| - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
214 |
| - this.applicationContext = applicationContext; |
| 180 | + @Override |
| 181 | + public boolean isSingleton() { |
| 182 | + return true; |
215 | 183 | }
|
216 | 184 | }
|
0 commit comments