Weblogic 10.3.0.0 + JSF 2.0 + @EJB Injection

Java EE, Programação 5 Comments

Ao se colocar o JSF 2.0 no Weblogic 10.3.0.0, os EJB’s não são injetados nos manageds beans do JSF através da anotação @EJB.

Para corrigir esse problema, basta utilizar a classe abaixo:

import java.lang.reflect.Field;
import java.util.*;

import javax.ejb.EJB;
import javax.el.*;
import javax.naming.NamingException;

import com.sun.faces.el.ManagedBeanELResolver;

public class VariableResolver extends CompositeELResolver {

    private final Map, List> ejbFields;

    public VariableResolver() {
        this.ejbFields = new HashMap, List>();
        add(new ManagedBeanELResolver());
    }

    private void preencherEjbFields(List fields, Class clazz, Set> visitados) {
        if (clazz != null && !visitados.contains(clazz)) {
            visitados.add(clazz);

            for (Field field : clazz.getDeclaredFields()) {
                EJB ejb = field.getAnnotation(EJB.class);
                if (ejb != null) {
                    field.setAccessible(true);
                    fields.add(new EJBField(field, ejb.mappedName(), field.getType()));
                }
            }

            preencherEjbFields(fields, clazz.getSuperclass(), visitados);

            for (Class i : clazz.getInterfaces()) {
                preencherEjbFields(fields, i, visitados);
            }
        }
    }

    private List getEjbFields(Object obj) {
        List fields = ejbFields.get(obj.getClass());

        if (fields == null) {
            fields = new ArrayList();
            Set> visitados = new HashSet>();
            preencherEjbFields(fields, obj.getClass(), visitados);

            synchronized (ejbFields) {
                ejbFields.put(obj.getClass(), fields);
            }
        }

        return fields;
    }

    private void processarEjbs(Object obj) throws IllegalAccessException, ClassCastException, NamingException, IllegalArgumentException {
        for (EJBField field : getEjbFields(obj)) {
            if (field.getField().get(obj) == null) {
                Object ejb = ServiceLocator.lookup(field.getType(), field.getMappedName());
                field.getField().set(obj, ejb);
            }
        }
    }

    @Override
    public Object getValue(ELContext arg0, Object arg1, Object arg2) {
        try {
            Object obj = super.getValue(arg0, arg1, arg2);
            if (obj != null) {
                processarEjbs(obj);
            }

            return obj;
        } catch (IllegalArgumentException ex) {
            throw new ELException(ex.getMessage(), ex);
        } catch (IllegalAccessException ex) {
            throw new ELException(ex.getMessage(), ex);
        } catch (NamingException ex) {
            throw new ELException(ex.getMessage(), ex);
        }
    }

    private class EJBField {

        private Field field;

        private String mappedName;

        private Class type;

        public EJBField(Field field, String mappedName, Class type) {
            this.field = field;
            this.mappedName = mappedName;
            this.type = type;
        }

        public Field getField() {
            return field;
        }

        public String getMappedName() {
            return mappedName;
        }

        public Class getType() {
            return type;
        }

    }

}

E declarar o el-resolver no faces-config.xml:

<faces-config>
 <application>
  <el-resolver>VariableResolver</el-resolver>
 </application>
</faces-config>

Weblogic + JPA + Hibernate = ClassNotFoundException: org.hibernate.hql.ast.HqlToken

Java, Java EE, Programação 1 Comment

Ao tentar efetuar o deploy de uma aplicação com JPA (implementação: Hibernate), ocorre o seguinte erro:

org.springframework.orm.hibernate3.HibernateQueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken [SELECT p FROM PARAMETRO p WHERE UPPER(RTRIM(p.parametroPK.parametroGeral)) = :parametroGeral AND UPPER(RTRIM(p.parametroPK.codigo)) = :codigo]; nested exception is org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken [SELECT p FROM PARAMETRO p WHERE UPPER(RTRIM(p.parametroPK.parametroGeral)) = :parametroGeral AND UPPER(RTRIM(p.parametroPK.codigo)) = :codigo]
Caused by: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken [SELECT p FROM PARAMETRO p WHERE UPPER(RTRIM(p.parametroPK.parametroGeral)) = :parametroGeral AND UPPER(RTRIM(p.parametroPK.codigo)) = :codigo]
at org.hibernate.hql.ast.HqlLexer.panic(HqlLexer.java:57)
at antlr.CharScanner.setTokenObjectClass(CharScanner.java:340)
at org.hibernate.hql.ast.HqlLexer.setTokenObjectClass(HqlLexer.java:31)
at antlr.CharScanner.<init>(CharScanner.java:51)
at antlr.CharScanner.<init>(CharScanner.java:60)
at org.hibernate.hql.antlr.HqlBaseLexer.<init>(HqlBaseLexer.java:56)
at org.hibernate.hql.antlr.HqlBaseLexer.<init>(HqlBaseLexer.java:53)
at org.hibernate.hql.antlr.HqlBaseLexer.<init>(HqlBaseLexer.java:50)
at org.hibernate.hql.ast.HqlLexer.<init>(HqlLexer.java:26)
at org.hibernate.hql.ast.HqlParser.getInstance(HqlParser.java:44)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:242)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:837)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:835)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:827)
at db.ProductManagerDaoHB.getProductList(ProductManagerDaoHB.java:14)
at bus.ProductManager.getProducts(ProductManager.java:25)
at web.controllers.SpringappController.handleRequest(SpringappController.java:33)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:819)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:754)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:399)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:354)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

Para resolver este problema basta alterar o persistence.xml e adicionar a seguinte linha:

<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>

Fonte: http://forum.springsource.org/showthread.php?t=36860