use of org.dozer.DozerBeanMapper in project camel by apache.
the class DozerComponent method createDozerBeanMapper.
public static DozerBeanMapper createDozerBeanMapper(List<String> mappingFiles) {
GlobalSettings settings = GlobalSettings.getInstance();
try {
LOG.info("Configuring GlobalSettings to use Camel classloader: {}", DozerThreadContextClassLoader.class.getName());
Field field = settings.getClass().getDeclaredField("classLoaderBeanName");
ReflectionHelper.setField(field, settings, DozerThreadContextClassLoader.class.getName());
} catch (Exception e) {
throw new IllegalStateException("Cannot configure Dozer GlobalSettings to use DozerThreadContextClassLoader as classloader due " + e.getMessage(), e);
}
try {
LOG.info("Configuring GlobalSettings to enable EL");
Field field = settings.getClass().getDeclaredField("elEnabled");
ReflectionHelper.setField(field, settings, true);
} catch (NoSuchFieldException nsfEx) {
throw new IllegalStateException("Failed to enable EL in global Dozer settings", nsfEx);
}
return new DozerBeanMapper(mappingFiles);
}
use of org.dozer.DozerBeanMapper in project useful-java-links by Vedenin.
the class DozerHelloWorldAnnotation method main.
public static void main(String[] args) {
// init mapper
Mapper mapper = new DozerBeanMapper();
// convert
Source source = new Source("Hello World!");
Destination destObject = mapper.map(source, Destination.class);
// print Hello World!
destObject.print();
}
use of org.dozer.DozerBeanMapper in project useful-java-links by Vedenin.
the class DozerHelloWorldXML method test.
private void test() {
// init mapper
List<String> myMappingFiles = new ArrayList<String>();
myMappingFiles.add("mapping.xml");
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.setMappingFiles(myMappingFiles);
// convert
Source source = new Source("Hello World!");
Destination destObject = mapper.map(source, Destination.class);
// print Hello World!;
destObject.print();
}
use of org.dozer.DozerBeanMapper in project mastering-java by Kingminghuang.
the class Mapping method main.
public static void main(String[] args) {
SourceBean source = new SourceBean(1001L, "issue", "2017-04-21");
TargetBean target = new TargetBean();
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.map(source, target);
System.out.println(target);
}
use of org.dozer.DozerBeanMapper in project camel by apache.
the class DozerTypeConverterLoader method init.
/**
* Doses the actual querying and registration of {@link DozerTypeConverter}s
* with the {@link CamelContext}.
*
* @param camelContext the context to register the
* {@link DozerTypeConverter} in
* @param mapper the DozerMapperBean to be wrapped as a type converter.
*/
public void init(CamelContext camelContext, DozerBeanMapper mapper) {
this.camelContext = camelContext;
if (mapper != null) {
this.mapper = mapper;
}
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
ClassLoader appcl = camelContext.getApplicationContextClassLoader();
if (appcl != null) {
Thread.currentThread().setContextClassLoader(appcl);
}
Map<String, DozerBeanMapper> mappers = lookupDozerBeanMappers();
// only add if we do not already have it
if (mapper != null && !mappers.containsValue(mapper)) {
mappers.put("parameter", mapper);
}
// add any dozer bean mapper configurations
Map<String, DozerBeanMapperConfiguration> configurations = lookupDozerBeanMapperConfigurations();
if (configurations != null && configuration != null) {
// filter out existing configuration, as we do not want to use it twice
String key = null;
for (Map.Entry<String, DozerBeanMapperConfiguration> entry : configurations.entrySet()) {
if (entry.getValue() == configuration) {
key = entry.getKey();
break;
}
}
if (key != null) {
configurations.remove(key);
}
}
if (configurations != null) {
if (configurations.size() > 1) {
log.warn("Loaded " + configurations.size() + " Dozer configurations from Camel registry." + " Dozer is most efficient when there is a single mapper instance. Consider amalgamating instances.");
}
for (Map.Entry<String, DozerBeanMapperConfiguration> entry : configurations.entrySet()) {
String id = entry.getKey();
DozerBeanMapper beanMapper = createDozerBeanMapper(entry.getValue());
// only add if we do not already have it
if (!mappers.containsValue(beanMapper)) {
mappers.put(id, beanMapper);
}
}
}
if (mappers.size() > 1) {
log.warn("Loaded " + mappers.size() + " Dozer mappers from Camel registry." + " Dozer is most efficient when there is a single mapper instance. Consider amalgamating instances.");
} else if (mappers.size() == 0) {
log.warn("No Dozer mappers found in Camel registry. You should add Dozer mappers as beans to the registry of the type: " + DozerBeanMapper.class.getName());
}
TypeConverterRegistry registry = camelContext.getTypeConverterRegistry();
for (Map.Entry<String, DozerBeanMapper> entry : mappers.entrySet()) {
String mapperId = entry.getKey();
DozerBeanMapper dozer = entry.getValue();
List<ClassMap> all = loadMappings(camelContext, mapperId, dozer);
registerClassMaps(registry, mapperId, dozer, all);
}
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
}
Aggregations