KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > DozerBeanMapper


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping;
17
18 import java.lang.reflect.Proxy JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import net.sf.dozer.util.mapping.cache.CacheManagerIF;
27 import net.sf.dozer.util.mapping.cache.DozerCacheManager;
28 import net.sf.dozer.util.mapping.config.GlobalSettings;
29 import net.sf.dozer.util.mapping.config.Settings;
30 import net.sf.dozer.util.mapping.converters.CustomConverterContainer;
31 import net.sf.dozer.util.mapping.converters.CustomConverterDescription;
32 import net.sf.dozer.util.mapping.fieldmap.ClassMap;
33 import net.sf.dozer.util.mapping.fieldmap.Configuration;
34 import net.sf.dozer.util.mapping.fieldmap.Mappings;
35 import net.sf.dozer.util.mapping.interceptor.StatisticsInterceptor;
36 import net.sf.dozer.util.mapping.stats.GlobalStatistics;
37 import net.sf.dozer.util.mapping.stats.StatisticTypeConstants;
38 import net.sf.dozer.util.mapping.stats.StatisticsManagerIF;
39 import net.sf.dozer.util.mapping.util.ClassMapBuilder;
40 import net.sf.dozer.util.mapping.util.InitLogger;
41 import net.sf.dozer.util.mapping.util.MapperConstants;
42 import net.sf.dozer.util.mapping.util.MappingFileReader;
43 import net.sf.dozer.util.mapping.util.MappingUtils;
44 import net.sf.dozer.util.mapping.util.MappingValidator;
45 import net.sf.dozer.util.mapping.util.MappingsParser;
46
47 import org.apache.commons.collections.set.ListOrderedSet;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 /**
52  * Mapper Implementation. This should be used/defined as a singleton within
53  * your application. This class perfoms several one time initializations and loads
54  * the custom xml mappings, so you will not want to create many instances of it for performance reasons.
55  * Typically a system will only have one DozerBeanMapper instance per VM.
56  * If you are using an IOC framework(i.e Spring), define the MapperIF as singleton="true".
57  * If you are not using an IOC framework, a DozerBeanMapperSingletonWrapper convenience class
58  * has been provided in the Dozer jar.
59  *
60  *
61  * @author tierney.matt
62  */

63 public class DozerBeanMapper implements MapperIF {
64
65   //private static final Logge log = Logger.getLogger(DozerBeanMapper.class);
66
private static final Log log = LogFactory.getLog(DozerBeanMapper.class);
67
68   /*
69    * Accessible for custom injection
70    */

71   private List JavaDoc mappingFiles; // String file names
72
private List JavaDoc customConverters;
73   private List JavaDoc eventListeners;
74   private CustomFieldMapperIF customFieldMapper;
75
76   /*
77    * Not accessible for injection
78    */

79   private Map JavaDoc customMappings;
80   private Configuration globalConfiguration;
81   //There are no global caches. Caches are per bean mapper instance
82
private final CacheManagerIF cacheManager = DozerCacheManager.createNew();
83   private final MappingUtils mappingUtils = new MappingUtils();
84   private final MappingValidator mappingValidator = new MappingValidator();
85   private final ClassMapBuilder classMapBuilder = new ClassMapBuilder();
86   
87   /*
88    * Not accessible for injection. Global
89    */

90   private static final StatisticsManagerIF statsMgr = GlobalStatistics.getInstance().getStatsMgr();
91   private static final Settings settings = GlobalSettings.getInstance().getSettings();
92
93   public DozerBeanMapper() {
94     this(null);
95   }
96
97   public DozerBeanMapper(List JavaDoc mappingFiles) {
98     this.mappingFiles = mappingFiles;
99     init();
100   }
101
102   public void map(Object JavaDoc sourceObj, Object JavaDoc destObj, String JavaDoc mapId) throws MappingException {
103     getMappingProcessor().map(sourceObj, destObj, mapId);
104   }
105
106   public Object JavaDoc map(Object JavaDoc sourceObj, Class JavaDoc destClass, String JavaDoc mapId) throws MappingException {
107     return getMappingProcessor().map(sourceObj, destClass, mapId);
108   }
109
110   public Object JavaDoc map(Object JavaDoc sourceObj, Class JavaDoc destClass) throws MappingException {
111     return getMappingProcessor().map(sourceObj, destClass);
112   }
113
114   public void map(Object JavaDoc sourceObj, Object JavaDoc destObj) throws MappingException {
115     getMappingProcessor().map(sourceObj, destObj);
116   }
117
118   public List JavaDoc getMappingFiles() {
119     return mappingFiles;
120   }
121
122   public void setMappingFiles(List JavaDoc mappingFiles) {
123     this.mappingFiles = mappingFiles;
124   }
125
126   public void setFactories(Map JavaDoc factories) {
127     mappingUtils.addFactories(factories);
128   }
129
130   public void setCustomConverters(List JavaDoc customConverters) {
131     this.customConverters = customConverters;
132   }
133
134   public List JavaDoc getCustomConverters() {
135     return customConverters;
136   }
137
138   private void init() {
139     // initialize any bean mapper caches. These caches are only visible to the bean mapper instance and
140
// are not shared across the VM.
141
cacheManager.addCache(MapperConstants.CONVERTER_BY_DEST_TYPE_CACHE, settings.getConverterByDestTypeCacheMaxSize());
142     cacheManager.addCache(MapperConstants.SUPER_TYPE_CHECK_CACHE, settings.getSuperTypesCacheMaxSize());
143
144     // stats
145
statsMgr.increment(StatisticTypeConstants.MAPPER_INSTANCES_COUNT);
146   }
147
148   protected MapperIF getMappingProcessor() {
149     MapperIF processor = new MappingProcessor(getCustomMappings(), globalConfiguration, cacheManager,
150         statsMgr, customConverters, getEventListeners(), getCustomFieldMapper());
151
152     // If statistics are enabled, then Proxy the processor with a statistics interceptor
153
if (statsMgr.isStatisticsEnabled()) {
154       processor = (MapperIF) Proxy.newProxyInstance(processor.getClass().getClassLoader(), processor.getClass()
155           .getInterfaces(), new StatisticsInterceptor(processor, statsMgr));
156     }
157
158     return processor;
159   }
160   
161   private synchronized Map JavaDoc getCustomMappings() {
162     //loadCustomMappings() has to be called outside of init() method because the custom converters are injected.
163
if (this.customMappings == null) {
164       this.customMappings = Collections.synchronizedMap(loadCustomMappings());
165     }
166     return this.customMappings;
167   }
168
169   private synchronized Map JavaDoc loadCustomMappings() {
170     Map JavaDoc customMappings = new HashMap JavaDoc();
171
172     synchronized (customMappings) {
173       ListOrderedSet customConverterDescriptions = new ListOrderedSet();
174       InitLogger.log(log,"Initializing a new instance of the dozer bean mapper. Version: " + MapperConstants.CURRENT_VERSION
175           + ", Thread Name:" + Thread.currentThread().getName());
176
177       if (mappingFiles != null && mappingFiles.size() > 0) {
178         InitLogger.log(log, "Using the following xml files to load custom mappings for the bean mapper instance: " + mappingFiles);
179         Iterator JavaDoc iter = mappingFiles.iterator();
180         while (iter.hasNext()) {
181           String JavaDoc mappingFileName = (String JavaDoc) iter.next();
182           InitLogger.log(log,"Trying to find xml mapping file: " + mappingFileName);
183           URL JavaDoc url = mappingValidator.validateURL(MapperConstants.DEFAULT_PATH_ROOT + mappingFileName);
184           InitLogger.log(log, "Using URL [" + url + "] to load custom xml mappings");
185           MappingFileReader mappingFileReader = new MappingFileReader(url);
186           Mappings mappings = mappingFileReader.read();
187           InitLogger.log(log,"Successfully loaded custom xml mappings from URL: [" + url + "]");
188
189           // the last configuration is the 'global' configuration
190
globalConfiguration = mappings.getConfiguration();
191           // build up the custom converters to make them global
192
if (mappings.getConfiguration() != null && mappings.getConfiguration().getCustomConverters() != null
193               && mappings.getConfiguration().getCustomConverters().getConverters() != null) {
194             Iterator JavaDoc iterator = mappings.getConfiguration().getCustomConverters().getConverters().iterator();
195             while (iterator.hasNext()) {
196               CustomConverterDescription cc = (CustomConverterDescription) iterator.next();
197               customConverterDescriptions.add(cc);
198             }
199           }
200           MappingsParser mappingsParser = new MappingsParser();
201           customMappings.putAll(mappingsParser.parseMappings(mappings));
202         }
203       }
204
205       // Add default mappings using matching property names if wildcard policy
206
// is true. The addDefaultFieldMappings will check the wildcard policy of each classmap
207
if (customMappings != null) {
208         classMapBuilder.addDefaultFieldMappings(customMappings);
209       }
210       // iterate through the classmaps and set all of the customconverters on them
211
Iterator JavaDoc keyIter = customMappings.keySet().iterator();
212       while (keyIter.hasNext()) {
213         String JavaDoc key = (String JavaDoc) keyIter.next();
214         ClassMap classMap = (ClassMap) customMappings.get(key);
215         if (classMap.getConfiguration() == null) {
216           classMap.setConfiguration(new Configuration());
217         }
218         if (classMap.getConfiguration().getCustomConverters() != null) {
219           classMap.getConfiguration().getCustomConverters().setConverters(customConverterDescriptions.asList());
220         } else {
221           classMap.getConfiguration().setCustomConverters(new CustomConverterContainer());
222           classMap.getConfiguration().getCustomConverters().setConverters(customConverterDescriptions.asList());
223         }
224       }
225     }
226     return customMappings;
227   }
228
229
230   public List JavaDoc getEventListeners() {
231     return eventListeners;
232   }
233
234   public void setEventListeners(List JavaDoc eventListeners) {
235     this.eventListeners = eventListeners;
236   }
237
238   public CustomFieldMapperIF getCustomFieldMapper() {
239     return customFieldMapper;
240   }
241
242   public void setCustomFieldMapper(CustomFieldMapperIF customFieldMapper) {
243     this.customFieldMapper = customFieldMapper;
244   }
245
246 }
Popular Tags