<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>www.jugpadova.it: Tag JNDI</title>
    <link>http://www.jugpadova.it/articles/tag/jndi</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Java User Group [Padova]</description>
    <item>
      <title>Tomcat 5.5.20 and JavaMail Sessions</title>
      <description>&lt;p&gt;&lt;em&gt;You&amp;#8217;ll find an article in italian on the same topic in &lt;a href="http://benfante.blogspot.com/2007/03/tomcat-castrato.html"&gt;my personal blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="http://tomcat.apache.org"&gt;Tomcat&lt;/a&gt; you can define a JNDI Resource for a JavaMail session putting the following code in you Context definition:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_xml "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;Resource&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;mail/Session&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;auth&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Container&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
            &lt;span class="attribute"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;javax.mail.Session&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
            &lt;span class="attribute"&gt;mail.smtp.host&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;localhost&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And in you web.xml:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_xml "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;resource-ref&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;res-ref-name&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;mail/Session&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;res-ref-name&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;res-type&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;javax.mail.Session&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;res-type&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;res-auth&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;Container&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;res-auth&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;resource-ref&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then you can use it in you code:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;Context initCtx = new InitialContext();
Session session =
    (Session) envCtx.lookup(&amp;quot;java:comp/env/mail/Session&amp;quot;);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using Tomcat 5.5.20 (and 5.5.17, and 5.5.23&amp;#8230;and maybe other versions) the last statement will produce a &lt;code&gt;ClassNotFoundException: org.apache.naming.factory.MailSessionFactory&lt;/code&gt;. The cause is obviously that class is missing from the &lt;code&gt;common/lib/naming-factory.jar&lt;/code&gt; distributed by Tomcat. I don&amp;#8217;t know if it was a (repeated) problem in building Tomcat. Luckly the source code of that class is still present in the source distribution of Tomcat. Simply it isn&amp;#8217;t included in the build process if you don&amp;#8217;t have the &lt;a href="http://java.sun.com/products/javamail/"&gt;JavaMail&lt;/a&gt; and the &lt;a href="http://java.sun.com/products/javabeans/jaf/"&gt;JavaBeans Activation Framework&lt;/a&gt; in the build classpath. So I re-built the &lt;code&gt;naming-factory.jar&lt;/code&gt; file and substituted in &lt;code&gt;common/lib&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can rebuild your own JAR, or download my new &lt;a href="http://snipurl.com/nfjar"&gt;naming-factory.jar&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I discovered this problem (bug?) installing &lt;a href="http://www.liferay.com/"&gt;LifeRay&lt;/a&gt; 4.1.2 using the WAR distribution. On the contrary the LifeRay-Tomcat bundle contains the correct &lt;code&gt;naming-factory.jar&lt;/code&gt; yet. The LifeRay error in this case is more obscure. It reports a &lt;code&gt;javax.naming.NameNotFoundException: Name mail is not bound in this Context&lt;/code&gt;! Of course the JNDI Resource is correctly configured. This happens because LifeRay try to guess the JNDI name of the resource, that changes for the different application servers. So that exception comes from one of the attempts, and the real problem is lost and hidden. So, if you&amp;#8217;ll see a such message, don&amp;#8217;t spend time re-re-re-configuring your JNDI resources&amp;#8230;but patch Tomcat!&lt;/p&gt;</description>
      <pubDate>Tue, 13 Mar 2007 07:40:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:8264d2f3-ac82-4f1e-9212-4686009a2acc</guid>
      <author>Lucio Benfante</author>
      <link>http://www.jugpadova.it/articles/2007/03/13/tomcat-5-5-20-and-javamail-sessions</link>
      <category>Tips &amp; Tricks</category>
      <category>Programmazione</category>
      <category>Tomcat</category>
      <category>JavaMail</category>
      <category>JNDI</category>
      <category>bug</category>
      <trackback:ping>http://www.jugpadova.it/articles/trackback/21441</trackback:ping>
    </item>
  </channel>
</rss>
