eljsf-2.2java-ee-7

Set scope of returned instance in custom ELResolver


I have written an custom ELResolver which returns an instance of an Session Bean. I look up a bean by JNDI. The getValue method looks like this:

@Override
public Object getValue(ELContext ctx, Object base, Object prop) {

    if (null == base) {
        Object bean = resolveBean((String) prop);
        if (null != bean) {
            ctx.setPropertyResolved(true);
            return bean;
        }
    }
    return null;
}    

private Object resolveBean(String beanName) {

    \\Search in a RegistryService if such an bean exists

    for (ModulDescriptor md : loader.getDescriptors()) {
        for (MenuItem mi : md.getMenuItems()) {
            if (mi.getElBeanName().equals(beanName)) {
                Object bean = null;
                try {

      \\If exists lookup and return

                    bean = InitialContext.doLookup("java:global/ModulA/" + mi.getJndiBeanName());
                } catch (NamingException e) {
                    e.printStackTrace();
                }

                return bean;
            }
        }
    }

    return null;
}

This works fine so far. Now my Question:

It seems that it doesn't matter which scope is set to the looked up bean. It acts always like a SessionScope bean.

Is there a way to set a scope to such an construct? (e.g. RequestScope or ViewScope)

What is the "default scope" of an element returned by an custom ELResolver? Is it just saved in the SessionMap?

Version Stack:
Oracle JDK 1.8.0_91 x64
Java EE 7 (Wildfly 10)
Mojarra based JSF 2.2
EJB 3.2
CDI 1.2 (Weld 2.3.4 Final)


Solution

  • Ok seems that an EL resolver is the wrong approach. I now use the maps provided by faces context (RequeatMap, SessionMap, ViewMap and ApplicationMap) . The objects are now released correctly. In my case I use a custom ResourceHandler and add the corresponding beans there.

    If someone needs a code example don't hesitate to ask for.