1 16 package net.sf.dozer.util.mapping; 17 18 import java.lang.reflect.Proxy ; 19 import java.net.URL ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 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 63 public class DozerBeanMapper implements MapperIF { 64 65 private static final Log log = LogFactory.getLog(DozerBeanMapper.class); 67 68 71 private List mappingFiles; private List customConverters; 73 private List eventListeners; 74 private CustomFieldMapperIF customFieldMapper; 75 76 79 private Map customMappings; 80 private Configuration globalConfiguration; 81 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 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 mappingFiles) { 98 this.mappingFiles = mappingFiles; 99 init(); 100 } 101 102 public void map(Object sourceObj, Object destObj, String mapId) throws MappingException { 103 getMappingProcessor().map(sourceObj, destObj, mapId); 104 } 105 106 public Object map(Object sourceObj, Class destClass, String mapId) throws MappingException { 107 return getMappingProcessor().map(sourceObj, destClass, mapId); 108 } 109 110 public Object map(Object sourceObj, Class destClass) throws MappingException { 111 return getMappingProcessor().map(sourceObj, destClass); 112 } 113 114 public void map(Object sourceObj, Object destObj) throws MappingException { 115 getMappingProcessor().map(sourceObj, destObj); 116 } 117 118 public List getMappingFiles() { 119 return mappingFiles; 120 } 121 122 public void setMappingFiles(List mappingFiles) { 123 this.mappingFiles = mappingFiles; 124 } 125 126 public void setFactories(Map factories) { 127 mappingUtils.addFactories(factories); 128 } 129 130 public void setCustomConverters(List customConverters) { 131 this.customConverters = customConverters; 132 } 133 134 public List getCustomConverters() { 135 return customConverters; 136 } 137 138 private void init() { 139 cacheManager.addCache(MapperConstants.CONVERTER_BY_DEST_TYPE_CACHE, settings.getConverterByDestTypeCacheMaxSize()); 142 cacheManager.addCache(MapperConstants.SUPER_TYPE_CHECK_CACHE, settings.getSuperTypesCacheMaxSize()); 143 144 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 (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 getCustomMappings() { 162 if (this.customMappings == null) { 164 this.customMappings = Collections.synchronizedMap(loadCustomMappings()); 165 } 166 return this.customMappings; 167 } 168 169 private synchronized Map loadCustomMappings() { 170 Map customMappings = new HashMap (); 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 iter = mappingFiles.iterator(); 180 while (iter.hasNext()) { 181 String mappingFileName = (String ) iter.next(); 182 InitLogger.log(log,"Trying to find xml mapping file: " + mappingFileName); 183 URL 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 globalConfiguration = mappings.getConfiguration(); 191 if (mappings.getConfiguration() != null && mappings.getConfiguration().getCustomConverters() != null 193 && mappings.getConfiguration().getCustomConverters().getConverters() != null) { 194 Iterator 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 if (customMappings != null) { 208 classMapBuilder.addDefaultFieldMappings(customMappings); 209 } 210 Iterator keyIter = customMappings.keySet().iterator(); 212 while (keyIter.hasNext()) { 213 String key = (String ) 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 getEventListeners() { 231 return eventListeners; 232 } 233 234 public void setEventListeners(List 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 |