Thursday, November 19, 2015

RequestFactory Services asJava EE managed Beans

Guys,
after experimenting with subclassing RequestFactoryServlet for a while, I devised a new trick to have RequestFactory Services be managed Beans.
It is a simple trick where a ServiceLocator obtains Services from the BeanManager.
I'd like to share it with the community.

-- x --
public final class BeanManagerServiceLocator implements ServiceLocator
{
    @Override public Object getInstance (Class<?> clazz)
    {
        return getReference(clazz);
    }
   
    public static <T> T getReference (Class<T> clazz)
    {
        try
        {
            InitialContext context = new InitialContext();
            BeanManager beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
            Set<Bean<?>> beans = beanManager.getBeans(clazz);
            Bean<?> bean = beanManager.resolve(beans);
            CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
            Object object = beanManager.getReference(bean, bean.getBeanClass(), creationalContext);
            return clazz.cast(object);
        }
        catch (NamingException e)
        {
            throw new RuntimeException(e);
        }
    }
}
-- x --

Thanks!
P.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment