<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>www.jugpadova.it: Tag bug</title>
    <link>http://www.jugpadova.it/articles/tag/bug</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 -0500</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>
    <item>
      <title>Bug storico...</title>
      <description>&lt;p&gt;...presente nel &lt;span class="caps"&gt;JDK&lt;/span&gt;, e probabilmente nella maggior parte delle implementazioni di algoritmi con approccio &amp;#8220;divide et impera&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Quanti di noi non hanno mai scritto questa semplice e innocente riga di codice (con &lt;code&gt;low&lt;/code&gt; e &lt;code&gt;high&lt;/code&gt; di tipo &lt;code&gt;int&lt;/code&gt;)?&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;int mid = (low+high)/2;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Il problema è che la somma dei due interi può risultare in un overflow&amp;#8230;gosh&amp;#8230;&lt;/p&gt;


	&lt;p&gt;L&amp;#8217;implementazione corretta sarebbe qualcosa di simile a:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;int mid = low + ((high-low)/2);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Provate a cercare in qualunque libro di algoritmi e vedrete che anche i grandi sbagliano. Io ho guardato in &lt;a href="http://www.awprofessional.com/title/0201896850" title="Volume 3"&gt;&amp;#8216;The Art of Computer Programming&amp;#8217;&lt;/a&gt;), e anche l&amp;#8217;implementazione dell&amp;#8217;algoritmo di ricerca binaria di &lt;a href="http://www-cs-faculty.stanford.edu/~knuth/"&gt;Donald E. Knuth&lt;/a&gt; soffre dello stesso bug.&lt;/p&gt;


	&lt;p&gt;Per quanto riguarda il &lt;span class="caps"&gt;JDK&lt;/span&gt;, il bug si trova (almeno) nel metodo &lt;code&gt;binarySearch&lt;/code&gt; della classe &lt;code&gt;java.utils.Arrays&lt;/code&gt;. A me non è mai capitato, ma ve l&amp;#8217;immaginate la sorpresa (per non dire altro) di vedervi sollevare un ArrayIndexOutOfBoundsException da un metodo di quella classe?&lt;/p&gt;


	&lt;p&gt;L&amp;#8217;annuncio e la discussione del bug li fa lo stesso autore della classe, Josh Bloch, in questo &lt;a href="http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html"&gt;blog&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Fri, 16 Jun 2006 07:03:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:7f51b64c-cec4-4722-a2f4-027b31a9f1fd</guid>
      <author>Lucio Benfante</author>
      <link>http://www.jugpadova.it/articles/2006/06/16/bug-storico</link>
      <category>Programmazione</category>
      <category>bug</category>
      <category>binary</category>
      <category>search</category>
      <category>overflow</category>
      <trackback:ping>http://www.jugpadova.it/articles/trackback/923</trackback:ping>
    </item>
    <item>
      <title>findbugs to discover bugs</title>
      <description>&lt;p&gt;&lt;a href="http://findbugs.sourceforge.net/index.html"&gt;&lt;img src="http://findbugs.sourceforge.net/umdFindbugs.png" alt="FindBugs" width="100" border="0" align="left" HSPACE="10" VSPACE="10" /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I was investigating about a good tool for profiling when I met with &lt;a href="http://findbugs.sourceforge.net/"&gt;findbugs&lt;/a&gt;.
To be honest I found discussion about this tool in a thread of &lt;a href="http://www.jugmilano.it"&gt;jug milano&lt;/a&gt; mailing list.
&lt;b&gt;findbugs&lt;/b&gt; is a free software  which looks for bugs in Java code.
In order to execute this program is enough you have installed on your machine &lt;a href="http://java.sun.com/products/javawebstart/"&gt;Java Web Start&lt;/a&gt;. Thereby try to click &lt;a href="http://findbugs.sourceforge.net/jnlp/findbugs.jnlp"&gt;here&lt;/a&gt;  , after set parameter of your project (src, jar, etc) and finally push on &amp;#8216;Find Bugs&amp;#8217; button.&lt;br&gt; 
I was amazed at how many bugs it found like comparison of String objects using == or != instead of using &lt;i&gt;equals()&lt;/i&gt; method. &lt;br&gt;
By the way&amp;#8230;I didn&amp;#8217;t generate that code :-)&lt;/p&gt;
</description>
      <pubDate>Wed, 17 May 2006 12:15:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:239d813d-c2fe-43db-b4ff-dcb8d3ef6309</guid>
      <author>Enrico Giurin</author>
      <link>http://www.jugpadova.it/articles/2006/05/17/findbugs-to-discover-bugs</link>
      <category>Software</category>
      <category>bug</category>
      <category>tool</category>
      <trackback:ping>http://www.jugpadova.it/articles/trackback/369</trackback:ping>
    </item>
  </channel>
</rss>

