4 Replies Latest reply on Apr 9, 2008 6:59 AM by jaikiran

    ClassCastException: $Proxy94 cannot be cast ...

    ttrepper

      Hi all,

      I am using JBoss 4.2.2GA and Java(TM) SE Runtime Environment (build 1.6.0_05-b13) for my development environment.

      I have a stateless session bean called UtilityBean and an interfaces called UtilityBeanLocal, marked with @Local and the bean marked with @stateless and implements serializable and the local-interface.

      When I try to make a lookup through InitalContext I get a Proxy-Object, which cannot be casted to the local interface.

      Stacktrace:


      20:05:44,843 ERROR [STDERR] java.lang.ClassCastException: $Proxy94 cannot be cast to test.ejb.stateless.UtilityBeanLocal
      20:05:44,843 ERROR [STDERR] at test.service.helper.ApplicationHelper.getApplicationSettings(ApplicationHelper.java:23)
      20:05:44,843 ERROR [STDERR] at test.service.helper.RegistrationHelper.isUsernameLongEnough(RegistrationHelper.java:32)
      20:05:44,843 ERROR [STDERR] at test.web.beans.RegistrationBean.checkUserComplete(RegistrationBean.java:395)
      20:05:44,843 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      20:05:44,843 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      20:05:44,843 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      20:05:44,843 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
      20:05:44,843 ERROR [STDERR] at org.apache.el.parser.AstValue.invoke(AstValue.java:131)
      20:05:44,843 ERROR [STDERR] at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      20:05:44,843 ERROR [STDERR] at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
      20:05:44,843 ERROR [STDERR] at javax.faces.validator.MethodExpressionValidator.validate(MethodExpressionValidator.java:84)
      20:05:44,843 ERROR [STDERR] at javax.faces.component.UIInput.validateValue(UIInput.java:1013)
      20:05:44,843 ERROR [STDERR] at javax.faces.component.UIInput.validate(UIInput.java:867)
      20:05:44,843 ERROR [STDERR] at javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
      20:05:44,843 ERROR [STDERR] at javax.faces.component.UIInput.processValidators(UIInput.java:666)
      20:05:44,843 ERROR [STDERR] at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:335)
      20:05:44,843 ERROR [STDERR] at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)
      20:05:44,843 ERROR [STDERR] at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
      20:05:44,843 ERROR [STDERR] at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
      20:05:44,843 ERROR [STDERR] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      20:05:44,843 ERROR [STDERR] at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
      20:05:44,843 ERROR [STDERR] at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      20:05:44,843 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      20:05:44,843 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
      20:05:44,843 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      20:05:44,843 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      20:05:44,843 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
      20:05:44,843 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
      20:05:44,843 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      20:05:44,843 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
      20:05:44,843 ERROR [STDERR] at java.lang.Thread.run(Thread.java:619)


      The following code shows the part, which displays all implemented interfaces of the object:
      System.out.println(obj.getClass().getName());
      Class[] interfaces = obj.getClass().getInterfaces();
      for (int i = 0; i < interfaces.length; ++i) {
       System.out.println(interfaces.getName());
       }

      Output:
      20:05:44,843 INFO [STDOUT] $Proxy94
      20:05:44,843 INFO [STDOUT] test.ejb.stateless.UtilityBeanLocal
      20:05:44,843 INFO [STDOUT] org.jboss.ejb3.JBossProxy


      Stateless Bean:
      @Stateless
      public class UtilityBean implements UtilityBeanLocal, Serializable, Constants {
       private static final long serialVersionUID = 1L;
      ......
      

      Interface:
      @Local
      public interface UtilityBeanLocal {
       public static final String JNDI_NAME = "UtilityBeanLocal";
       public ApplicationSettings getApplicationSettings();
      }


      Context-Lookup:
      public class BeanLocator {
       public static final String JNDI_PREFIX = "java:comp/env/ejb/";
       private static transient BeanLocator instance = null;
       private InitialContext initialcontext = null;
      
       //--------------------------------------------------------------------------
       private BeanLocator() {
       try {
       Properties props = new Properties();
       props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
       props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
       props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming.jnp.interfaces");
       //Context ctx = new InitialContext(props);
       this.initialcontext = new InitialContext(props);
       } catch (NamingException e) {
       //e.printStackTrace();
       this.initialcontext = null;
       }
       }
      
       //--------------------------------------------------------------------------
       public static BeanLocator getInstance() {
       if (instance == null) {
       instance = new BeanLocator();
       }
       return instance;
       }
      
       //--------------------------------------------------------------------------
       public Object locate(String jndiName, Class<?> clazz) throws NamingException {
       Object obj = null;
       if (this.initialcontext != null) {
       //obj = PortableRemoteObject.narrow(initialcontext.lookup(jndiName), clazz);
       obj = initialcontext.lookup(jndiName);
      
       System.out.println(obj.getClass().getName());
       Class[] interfaces = obj.getClass().getInterfaces();
       for (int i = 0; i < interfaces.length; ++i) {
       System.out.println(interfaces.getName());
       }
       }
       return obj;
       }
       }


      It is not working with or without passing the properties to the context.

      The main question is, why I cannot cast, even if the interface is implemented???

      What am I missing? Does anybody has a clue?

      Thanks in advance and best regards,

      Thomas


        • 1. Re: ClassCastException: $Proxy94 cannot be cast ...
          jaikiran

          You probably have the bean (local) interface packaged in more than one jar. See this http://wiki.jboss.org/wiki/ClassCastExceptions which mentions the probable cause. See if the jmx-console method mentioned there, helps.

          Also, see this related thread http://www.jboss.com/index.html?module=bb&op=viewtopic&t=132203

          • 2. Re: ClassCastException: $Proxy94 cannot be cast ...
            alrubinger

            Yes, class equality is determined by both fully-qualified class name and the defining ClassLoader.

            S,
            ALR

            • 3. Re: ClassCastException: $Proxy94 cannot be cast ...
              ttrepper

              Hi jaikiran, hi ALRubinger,

              thank you for your replies and your hints. I have the same anticipation, that in my application is something wrong with the classes (and class-loaders).

              My question now is how an ear-file is best packed and with which structure?

              I am developing in eclipse and I have a "main"-project, which is an ear-project. Then I have a components and a util-project with classes needed by the ejbs and the web. furthermore I have a web-project and an ejb-project. The structure looks similar like this:

              test (ear)
              + testEJB (holds EJBs)
              + testWeb (holds JSPs)
              + testUtil (holds Helper-Classes and Utils) (needed by web and ejb)
              + testComponents (holds components - only needed by web)

              What is the best structure to pack the libs (incl. third-party like tomahawk) into the ear? What in ear-web-inf/lib, what in web.jar-web-inf/lib, etc?

              Thank you very much for the help

              Thomas

              • 4. Re: ClassCastException: $Proxy94 cannot be cast ...
                jaikiran

                 

                "ttrepper" wrote:


                My question now is how an ear-file is best packed and with which structure?

                The structure looks similar like this:

                test (ear)
                + testEJB (holds EJBs)
                + testWeb (holds JSPs)
                + testUtil (holds Helper-Classes and Utils) (needed by web and ejb)
                + testComponents (holds components - only needed by web)

                What is the best structure to pack the libs (incl. third-party like tomahawk) into the ear? What in ear-web-inf/lib, what in web.jar-web-inf/lib, etc?

                Thank you very much for the help

                Thomas


                Going by what you describe about your application, this is how the packaging should look like:
                Test.ear
                 |
                 |--- META-INF
                 | |
                 | |--- application.xml
                 | |
                 | |--- jboss-app.xml
                 |
                 |
                 |--- lib (This will contain all libraries required by both the ejb and web)
                 | |
                 | |
                 | |--- testUtil.jar
                 | |
                 | |--- tomahawk.jar
                 |
                 |
                 |--- testEjb.jar
                 |
                 |
                 |--- testWeb.war
                 | |
                 | |--- WEB-INF
                 | | |
                 | | |--- lib (contains libraries required only by the web)
                 | | | |
                 | | | |--- testComponents.jar