wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes ClassCastException - JavaMail & JNDI in Application Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "ClassCastException - JavaMail & JNDI in Application Client" Watch "ClassCastException - JavaMail & JNDI in Application Client" New topic
Author

ClassCastException - JavaMail & JNDI in Application Client

Tom Marrs
Author
Ranch Hand

Joined: Sep 20, 2000
Posts: 67
Hello,
I'm using JBoss 3.2.5.

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:

Code:

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:
Code:

<?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:
Code:

<!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:
Code:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming.client rg.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
Tom Marrs
Author
Ranch Hand

Joined: Sep 20, 2000
Posts: 67
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException - JavaMail & JNDI in Application Client
 
Similar Threads
javax.mail.Session and DataSource (Apache Tomcat)
ExceptionInInitializerError with JavaMail
javax.mail.Session and DataSource (Apache Tomcat)
JNDI problem
MDB @PostConstruct error