Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate CDI Model support for view engines #261

Open
ivargrimstad opened this issue May 6, 2021 · 3 comments
Open

Investigate CDI Model support for view engines #261

ivargrimstad opened this issue May 6, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@ivargrimstad
Copy link
Member

No description provided.

@chkal
Copy link
Contributor

chkal commented May 8, 2021

My thoughts about this:

In most cases a map-like data structure is used to provide data to the template engine. In many cases it is even a java.util.Map. What we could to is to provide some kind of lazy evaluating map which looks up the bean for all calls to get(String).

Something like:

public class CdiMap implements Map<String, Object> {

  private final BeanManager beanManager;

  public CdiMap( BeanManager beanManager ) {
    this.beanManager = beanManager;
  }

  @Override
  public Object get( Object key ) {

    Set<Bean<?>> beans = beanManager.getBeans( key.toString() );
    if( !beans.isEmpty() ) {
      Bean<?> bean = beans.iterator().next();
      CreationalContext<?> ctx = beanManager.createCreationalContext( bean );
      return beanManager.getReference( bean, bean.getClass(), ctx );
    }
    return null;

  }

  /* ... */

}

However, this only works if the engine allows to pass a map. If it just provides a set(String name, Object obj) like API, this won't be possible. In theory, we could collect ALL @Named beans and pass all of them to the engine, but this could lead to performance issues. We could create some kind of proxy for these beans and obtain them lazily, but this could require some kind of bytecode level proxy.

@dmaidaniuk
Copy link
Contributor

@ivargrimstad, @chkal, I will take a look to this enhancement. Probably, exists some nice way how it could be realized.

@chkal
Copy link
Contributor

chkal commented Jan 15, 2022

@dmaidaniuk Thanks a lot! It would be awesome if we could move this forward.

@erdlet erdlet added the enhancement New feature or request label May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants