title: Why we get java.lang.ClassCastException: $Proxy, in jndi lookup of a Stateless Session Bean in Glassfish 3.1.1
body: <p>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)</p>
  
  <p>We are using Glassfish 3.1.1, JPA 2, EJB 3, JSF2</p>
  
  <p>Everything is packaged in a ear archive.</p>
  
  <p>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:</p>
  
  <pre><code>public class ValidadorParametroGlobal implements ConstraintValidator&lt;ValidacionParametroGlobal, ParametroGlobal&gt; {
  
      @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
  }
  </code></pre>
  
  <p>The session Bean:</p>
  
  <pre><code>@Stateless
  @Local(ParametroGlobalBOLocal.class)
  @Remote(ParametroGlobalBORemote.class)
  public class ParametroGlobalBOImpl extends ParametroGlobalGenericBridgeImpl implements     ParametroGlobalBOLocal,
          ParametroGlobalBORemote {
      MORE code...
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
      |#]
  </code></pre>
  
  <p>We also have defined the validator class this way:</p>
  
  <pre><code>public class ValidadorParametroGlobal implements ConstraintValidator&lt;ValidacionParametroGlobal, ParametroGlobal&gt; {
  
      @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
  }
  </code></pre>
  
  <p>But we get the same error.</p>
  
  <p>Any Ideas?</p>