Search in sources :

Example 1 with DozerBeanMapper

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);
}
Also used : Field(java.lang.reflect.Field) GlobalSettings(org.dozer.config.GlobalSettings) DozerThreadContextClassLoader(org.apache.camel.converter.dozer.DozerThreadContextClassLoader) DozerBeanMapper(org.dozer.DozerBeanMapper)

Example 2 with DozerBeanMapper

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();
}
Also used : Mapper(org.dozer.Mapper) DozerBeanMapper(org.dozer.DozerBeanMapper) DozerBeanMapper(org.dozer.DozerBeanMapper)

Example 3 with DozerBeanMapper

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();
}
Also used : ArrayList(java.util.ArrayList) DozerBeanMapper(org.dozer.DozerBeanMapper)

Example 4 with DozerBeanMapper

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);
}
Also used : DozerBeanMapper(org.dozer.DozerBeanMapper)

Example 5 with DozerBeanMapper

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);
    }
}
Also used : TypeConverterRegistry(org.apache.camel.spi.TypeConverterRegistry) DozerClassLoader(org.dozer.util.DozerClassLoader) ClassMap(org.dozer.classmap.ClassMap) DozerBeanMapper(org.dozer.DozerBeanMapper) HashMap(java.util.HashMap) Map(java.util.Map) ClassMap(org.dozer.classmap.ClassMap)

Aggregations

DozerBeanMapper (org.dozer.DozerBeanMapper)9 BeanMappingBuilder (org.dozer.loader.api.BeanMappingBuilder)2 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DozerTestArtifactsFactory.createDtoCustomer (org.apache.camel.converter.dozer.DozerTestArtifactsFactory.createDtoCustomer)1 DozerThreadContextClassLoader (org.apache.camel.converter.dozer.DozerThreadContextClassLoader)1 CustomerDTO (org.apache.camel.converter.dozer.dto.CustomerDTO)1 Customer (org.apache.camel.converter.dozer.model.Customer)1 TypeConverterRegistry (org.apache.camel.spi.TypeConverterRegistry)1 Mapper (org.dozer.Mapper)1 ClassMap (org.dozer.classmap.ClassMap)1 GlobalSettings (org.dozer.config.GlobalSettings)1 DozerClassLoader (org.dozer.util.DozerClassLoader)1