404-not-find / orika

Automatically exported from code.google.com/p/orika
2 stars 0 forks source link

cannot create multiple MapperFacade instances from one DefaultMapperFactory.Builder instance #152

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The upgrade from orika 1.4.4 to 1.4.5 causes our application to throw the 
exception below.

We create multiple mappers for several subsets of classes. Our code is as 
follows:

new DefaultMapperFactory.Builder();
...
for(...)
{
    MapperFactory mapperFactory = builder.build();
    MapperFacade mapper = mapperFactory.getMapperFacade();
}

The first for iteration works, as no MapperFacade has been created yet, during 
the second iteration the call "mapperFactory.getMapperFacade()" throws the 
exception below. This is because the ConverterFactory is not recreated as one 
would expect from a Builder pattern.

Because we cannot reuse the MapperFactory we now have to recreate the 
DefaultMapperFactory.Builder for each MapperFacade (each iteration). Although 
this is a one time init, it is a bug.

Caused by: java.lang.IllegalStateException: Cannot register converters after 
MapperFacade has been initialized
    at ma.glasnost.orika.converter.DefaultConverterFactory.registerConverter(DefaultConverterFactory.java:192)
    at ma.glasnost.orika.impl.DefaultMapperFactory$ConverterFactoryFacade.registerConverter(DefaultMapperFactory.java:1602)
    at ma.glasnost.orika.converter.builtin.BuiltinConverters.register(BuiltinConverters.java:84)
    at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1185)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:859)
    at nl.topicus.digdag.versioning.VersioningMappingBuilder.init(VersioningMappingBuilder.java:125)

What steps will reproduce the problem?
1. Create one DefaultMapperFactory.Builder
2. build the MapperFactory and fetch the MapperFacade from it.

What is the expected output? What do you see instead?
We expect to be able to create multiple MapperFacade instances from one 
DefaultMapperFactory.Builder instance. Instead we can only create one 
MapperFacade from one DefaultMapperFactory.Builder instance.

What version of the product are you using? On what operating system?
orika 1.4.5

Please provide any additional information below.
We can reproduce the error using the following unit test:

log.info("unit test");
DefaultMapperFactory.Builder banana = new DefaultMapperFactory.Builder();
for (int i = 1; i < 4; i++)
{
    MapperFactory apple = banana.build();
    MapperFacade pear = apple.getMapperFacade();
    log.info("ate " + i + " pear");
}
log.info("unit test done");

Original issue reported on code.google.com by hielke.hoeve on 25 Mar 2014 at 9:34

GoogleCodeExporter commented 8 years ago
I have the same problem.

Original comment by ja...@jirutka.cz on 17 Apr 2014 at 6:38

GoogleCodeExporter commented 8 years ago
Still no response from developers? :( I’m still stuck on 1.4.4, but I’ve 
just ran into another bug which is already fixed in 1.4.5, but can’t update 
because of this bug.

Original comment by ja...@jirutka.cz on 13 May 2014 at 6:15

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Any progress? :)

Original comment by ja...@jirutka.cz on 23 May 2014 at 2:17

GoogleCodeExporter commented 8 years ago
Hello have the same problem with the spring integration:

Source:

@Component
public class SpringAwareBeanMapper extends ConfigurableMapper implements 
ApplicationContextAware {

    private static Log logger = LogFactory.getLog(SpringAwareBeanMapper.class);

    private MapperFactory factory;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void configureFactoryBuilder(final DefaultMapperFactory.Builder factoryBuilder) {
        // customize the factoryBuilder as needed
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void configure(final MapperFactory factory) {
        this.factory = factory;
        // customize the factory as needed
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        addAllSpringBeans(applicationContext);
    }

    /**
     * Adds all managed beans of type {@link Mapper} or {@link Converter} to the parent {@link MapperFactory}.
     *
     * @param applicationContext The application context to look for managed beans in.
     */ 
    @SuppressWarnings("rawtypes")
    private void addAllSpringBeans(final ApplicationContext applicationContext) {
        final Map<String, Converter> converters = applicationContext.getBeansOfType(Converter.class);
        for (final Converter<?, ?> converter : converters.values()) {
            addConverter(converter);
        }

        final Map<String, Mapper> mappers = applicationContext.getBeansOfType(Mapper.class);
        for (final Mapper<?, ?> mapper : mappers.values()) {
            addMapper(mapper);
        }
    }

    /**
     * Add a {@link Converter}.
     *
     * @param converter The converter.
     */
    public void addConverter(final Converter<?, ?> converter) {
        if (logger.isDebugEnabled())
        {
            logger.debug("Add converter to factory: " + converter);
        }

        factory.getConverterFactory().registerConverter(converter);
    }

    /**
     * Add a {@link Mapper}.
     *
     * @param mapper The mapper.
     */

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void addMapper(final Mapper<?, ?> mapper) {
        if (logger.isDebugEnabled())
        {
            logger.debug("Add mapper to factory: " + mapper);
        }

        factory.classMap(mapper.getAType(), mapper.getBType())
        .byDefault()
        .customize((Mapper) mapper)
        .register();
    }
}

Error: 

Caused by: java.lang.IllegalStateException: Cannot register converters after 
MapperFacade has been initialized
......

Thanks.

Original comment by marco.ro...@gmail.com on 1 Jul 2014 at 3:42

GoogleCodeExporter commented 8 years ago
Hi,

I also have this issue.  Is there any progress on it?

Thanks.

Original comment by carine.l...@gmail.com on 1 Oct 2014 at 8:39

GoogleCodeExporter commented 8 years ago
Hi, I see the lot of changes related to Converters in branch 
https://github.com/orika-mapper/orika/tree/orangina

Probably the issue has fixed in this branch.
Can somebody confirm this? I mean developers.

Also, as source code migrated to github, will be great to have enabled issues 
on github as well.

Thank you. very appreciate your work on this library.

Original comment by vladymyr...@gmail.com on 3 Jan 2015 at 7:29