1 Reply Latest reply on Nov 14, 2004 3:40 PM by tmarrs

    ClassCastException - JavaMail & JNDI in Application Client

    tmarrs

      Hello,
      I'm getting a ClassCastException when I do a JNDI lookup (using J2EE-style JNDI ENC names) of a JavaMail Session from an external application client outside the JBoss container.

      The JNDI lookup for the JavaMail Session was successful, but the cast failed. Because I was getting a ClassCastException, I checked the Class Name and Package of the Object that I looked up:

      org.jnp.interfaces.Naming Context

      Why don't I get an Object of type: javax.mail.Session?

      Please note that I'm able to lookup URLs and Environment Variables using J2EE-style JNDI names and they work properly. JavaMail Sessions are the only things that don't completely work.


      Here's the client code sample:

       try {
       Context jndiContext = new InitialContext();
      
       // Look up the JavaMail Session.
      
       System.out.println("Looking up " +
       "java:comp/env/mail/JavaMailSession" +
       " ...\n");
       Session javaMailSession = (Session)
       jndiContext.lookup("java:comp/env/mail/JavaMailSession");
       } catch (NamingException ne) {
       System.err.println("Couldn't find " +
       "java:comp/env/mail/JavaMailSession:" + ne);
       }


      Here's application-client.xml:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE application-client PUBLIC
       "-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.3//EN"
       "http://java.sun.com/dtd/application-client_1_3.dtd">
      <application-client>
       <display-name>MjwClientJAR</display-name>
       <env-entry>
       <env-entry-name>var/SalesCommissionPct</env-entry-name>
       <env-entry-type>java.lang.Float</env-entry-type>
       <env-entry-value>.05</env-entry-value>
       </env-entry>
      
       <resource-ref>
       <res-ref-name>url/BigAutoManufacturer</res-ref-name>
       <res-type>java.net.URL</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
      
       <resource-ref>
       <res-ref-name>mail/JavaMailSession</res-ref-name>
       <res-type>javax.mail.Session</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
      
      </application-client>


      Here's jboss-client.xml:
      <!DOCTYPE jboss-client PUBLIC
       "-//JBoss//DTD Application Client 3.2//EN"
       "http://www.jboss.org/j2ee/dtd/jboss-client_3_2.dtd">
      <jboss-client>
       <jndi-name>jndi-client</jndi-name>
      
       <resource-ref>
       <res-ref-name>url/BigAutoManufacturer</res-ref-name>
       <res-url>http://www.bigAutoManufacturer.com</res-url>
       </resource-ref>
      
       <resource-ref>
       <res-ref-name>mail/JavaMailSession</res-ref-name>
       <jndi-name>java:/Mail</jndi-name>
       </resource-ref>
      
      </jboss-client>
      


      Here's jndi.properties:
      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      java.naming.factory.url.pkgs=org.jboss.naming.client:org.jnp.interfaces
      java.naming.provider.url=jnp://localhost:1099
      j2ee.clientName=jndi-client
      


      Both application-client.xml and jboss-client.xml reside in the META-INF directory of a JAR file. This JAR file is on my CLASSPATH and is also included in my deployed EAR file (and is listed as a module in application.xml).

      I'm including mail.jar and activation.jar (from JBoss release), jboss-j2ee.jar, jbossall-client.jar, and the directory path for jndi.properties on the client CLASSPATH.

      What am I doing wrong? Does anyone have any ideas? Thanks.

      Tom


        • 1. Re: ClassCastException - JavaMail & JNDI in Application Clie
          tmarrs

          OK, I can answer my own question. I set everything up correctly (that?s why I could use the Environment Variable and URL with J2EE-style JNDI ENC names) on the client side. However, according to Ingo Klöckl, you can't access JNDI resources bound under the java:/ namespace. Here's an excerpt from Ingo Klöckl's web page
          (http://www.2k-software.de/ingo/jbossjndi.html):



          ?java: is an ENC which is visible only in the JBoss VM. It holds references to objects which are only [sic] senseful in the JBoss environment, like data sources or other resources.?



          So, the short answer is: you can?t access a JavaMail Session (a DataSource) from outside the container because JBoss binds this under its internal java: namespace, so you can?t access outside of JBoss? VM.



          Duh! I should've remembered this sooner. If only I?d read my own research more closely and much sooner.



          Tom