We are having problems looking up a Local Stateles session Bean instance in a utility class. We always get a ClassCastException: $ProxyXYZ (XYZ is any number)

We are using Glassfish 3.1.1, JPA 2, EJB 3, JSF2

Everything is packaged in a ear archive.

We also use Bean Validation in our project, so in one of our validator class we try to get an instance of a Statateless session bean. The validator is defined this way:

public class ValidadorParametroGlobal implements ConstraintValidator<ValidacionParametroGlobal, ParametroGlobal> {

    @Override
    public void initialize(final ValidacionParametroGlobal constraintAnnotation) {
        try {

            //Lookup works, jndi name is correct, but cast fails
            ParametroGlobalBOImpl pgbo =
                    (ParametroGlobalBOImpl) new InitialContext().lookup(
                    "java:global/esipren-ear/esipren-ejb/ParametroGlobalBOImpl!" +
                    "ec.gob.mf.esipren2.bo.entidad.ParametroGlobalBOLocal");
            LOG.info(pgbo);
        } catch (NamingException ex) {
            LOG.error("No se pudo recuperar el bo de parametro global en el validador");
            throw new IllegalStateException("No se puede ejecutar la validación", ex);
        }
        LOG.debug("Inicializando validador");
    }
    ... MORE CODE
}

The session Bean:

@Stateless
@Local(ParametroGlobalBOLocal.class)
@Remote(ParametroGlobalBORemote.class)
public class ParametroGlobalBOImpl extends ParametroGlobalGenericBridgeImpl implements     ParametroGlobalBOLocal,
        ParametroGlobalBORemote {
    MORE code...
}

The lookup process works well because we get an Instancce of a Proxy class, but it is not posible to cast it to a Session Bean and when this validator is executed, we get this exception:

java.lang.ClassCastException: $Proxy1519
    at     ec.gob.mf.esipren2.validacion.ValidadorParametroGlobal.initialize(ValidadorParametroGlobal.java:64)
    at ec.gob.mf.esipren2.validacion.ValidadorParametroGlobal.initialize(ValidadorParametroGlobal.java:41)
    at org.hibernate.validator.engine.ConstraintTree.initializeConstraint(ConstraintTree.java:302)
    at org.hibernate.validator.engine.ConstraintTree.createAndInitializeValidator(ConstraintTree.java:212)
at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:189)
    at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree|#]

            [#|2011-10-28T11:46:10.814-0500|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=19;_ThreadName=Thread-4;|.java:135)
    at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:327)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:273)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:256)
    at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:210)
    at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
    at ec.gob.mf.esipren2.validacion.BeanValidationEventListener.validate(BeanValidationEventListener.java:56)
at ec.gob.mf.esipren2.persistencia.dao.jpa.JpaGenericDAOImpl.crear(JpaGenericDAOImpl.java:113)
    ... 95 more
    |#]

We also have defined the validator class this way:

public class ValidadorParametroGlobal implements ConstraintValidator<ValidacionParametroGlobal, ParametroGlobal> {

    @Override
    public void initialize(final ValidacionParametroGlobal constraintAnnotation) {
        try {
            ParametroGlobalBOLocal pgbo =
                    (ParametroGlobalBOLocal) new InitialContext().lookup(
                    "java:global/esipren-ear/esipren-ejb/ParametroGlobalBOImpl!" +
                    "ec.gob.mf.esipren2.bo.entidad.ParametroGlobalBOLocal");
            LOG.info(pgbo);
        } catch (NamingException ex) {
            LOG.error("No se pudo recuperar el bo de parametro global en el validador");
            throw new IllegalStateException("No se puede ejecutar la validación", ex);
        }
        LOG.debug("Inicializando validador");
    }
    ... MORE CODE
}

But we get the same error.

Any Ideas?

2

There are 2 best solutions below

0
On

As a summary for the first answer. The problem occurs because you cast to a EJB itself and you have to cast to a business interface of the EJB. I ran into the same trouble after studing FAQ here http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB. Fortunately, collegues on java.net helped me out. Thanks them a lot.

0
On

One way to get a local or remote ejb reference in glassfish 3.1.1 is via a Bean Locator (see http://www.adam-bien.com/roller/abien/entry/ejb_3_1_beanlocator_when)

My Beanlocator is (see the getBean method, it expects two arguments: the Ejb's Local or Remote class and the portable ejb name):

package ec.gob.mf.esipren2.util;

import java.io.Serializable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.apache.log4j.Logger;

/**
 *  Utilitario para recuperacion de instancias de Session Beans.
 * @author juan
 */
public final class BeanLocator implements Serializable {

    /**
     * Version de la clase.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logger de la clase.
     */
    private static final Logger LOG = Logger.getLogger(BeanLocator.class);

    /**
     * Constructor por defecto.
     */
    private BeanLocator() {
    }

    /**
     * Metodo para recuperacion de Session Beans Locales usando un archivo de
     * propiedades.
     * @param <T> Interfaz del Tipo de Session Bean que se quiere recuperar
     * @param clazz La clase que representa la interfaz del Session Bean
     * @param nombreServicio
     *      Nombre del servicio que se desea recuperar
         * @return Una Instancia del Session Bean cuya interfaz es igual a la
     * interfaz pasada como parametro
     * @throws NamingException En caso de encontrarse un error al intentar
     * recuperar la instancia del Session Bean
     */
    public static <T> T getBean(final Class<T> clazz, final String nombreServicio)
            throws NamingException {
        LOG.trace("Recuperando servicio Local" + nombreServicio);
        Context c = new InitialContext();
        Object o = c.lookup(nombreServicio);
        LOG.trace("Se recuperó un proxy: " + o.getClass().getName());
        return clazz.cast(PortableRemoteObject.narrow(o, clazz));
    }
}

So in order to get a local ejb reference:

    public String despachar() {
    String result;
    try {

        ParametroGlobalBOLocal prboLocal = BeanLocator.getBean(ParametroGlobalBOLocal.class,
                "java:global/esipren-ear/esipren-ejb/ParametroGlobalBOImpl!" +
                "ec.gob.mf.esipren2.bo.entidad.ParametroGlobalBOLocal");
        prboLocal.buscarTodos();
        result = "Something";
    } catch (NamingException ex) {
        ex.printStackTrace();
        result = "error";
    }
    return result;
}

The same approach could be used to get a remote ejb reference

public String despachar1() {
    String result;
    try {

        ParametroGlobalBORemote prboRemote = BeanLocator.getBean(ParametroGlobalBORemote.class,
                "java:global/esipren-ear/esipren-ejb/ParametroGlobalBOImpl!" +
                "ec.gob.mf.esipren2.bo.entidad.ParametroGlobalBORemote");
        prboRemote.buscarTodos();
        result = "Something";
    } catch (NamingException ex) {
        ex.printStackTrace();
        result = "error";
    }
    return result;    }

Finally this could interest you

http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB
http://www.java.net/node/703676#comment-797861
http://www.jguru.com/faq/view.jsp?EID=734137

Best regards Juan