<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>ioannis cherouvim</title>
	<atom:link href="http://blog.cherouvim.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cherouvim.com</link>
	<description>software engineering for beginners</description>
	<pubDate>Sat, 26 Apr 2008 21:51:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Logging website subsections with Apache</title>
		<link>http://blog.cherouvim.com/logging-website-subsections-with-apache/</link>
		<comments>http://blog.cherouvim.com/logging-website-subsections-with-apache/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 21:51:29 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[apache httpd]]></category>

		<category><![CDATA[deployment]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/?p=28</guid>
		<description><![CDATA[This is a typical VirtualHost for example.com in Apache HTTP Server:
&#60;VirtualHost xx.xxx.xx.xx:80&#62;
    DocumentRoot /home/example.com/site
    ServerName example.com
    ErrorLog /home/example.com/logs/error.log
    CustomLog /home/example.com/logs/access.log &#34;combined&#34;
&#60;/VirtualHost&#62;
This CustomLog directive will produce something like this on the logfile:
64.229.111.27 - - [08/Aug/2007:16:12:54 +0300] GET /foo/index.html HTTP/1.1
64.229.111.27 - - [08/Aug/2007:16:12:54 +0300] GET [...]]]></description>
			<content:encoded><![CDATA[<p>This is a typical <a href="http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost">VirtualHost</a> for example.com in <a href="http://httpd.apache.org/">Apache HTTP Server</a>:</p>
<pre>&lt;VirtualHost xx.xxx.xx.xx:80&gt;
    DocumentRoot /home/example.com/site
    ServerName example.com
    ErrorLog /home/example.com/logs/error.log
    CustomLog /home/example.com/logs/access.log &quot;combined&quot;
&lt;/VirtualHost&gt;</pre>
<p>This <a href="http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#customlog">CustomLog</a> directive will produce something like this on the logfile:</p>
<pre>64.229.111.27 - - [08/Aug/2007:16:12:54 +0300] GET /foo/index.html HTTP/1.1
64.229.111.27 - - [08/Aug/2007:16:12:54 +0300] GET /bar/index.html HTTP/1.1
64.229.111.27 - - [08/Aug/2007:16:12:54 +0300] GET /bar/about.html HTTP/1.1</pre>
<p>Suppose that you wanted to log requests for /foo and /bar subsections in 2 separate files. That way you (or your client) may find it easier later on to analyze web traffic for the subsites individually.</p>
<p>What you can do is log conditionally using environment variables that you&#8217;ve set using <a href="http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html#setenvif">SetEnvIf</a> based on the request.</p>
<pre>SetEnvIf Request_URI &quot;/foo.*&quot; subsite_foo
SetEnvIf Request_URI &quot;/bar.*&quot; subsite_bar
CustomLog /home/example.com/logs/access.foo.log combined env=subsite_foo
CustomLog /home/example.com/logs/access.bar.log combined env=subsite_bar</pre>
<p>This works nice, but not when the urls for your individual subsites look like this:</p>
<pre>http://example.com/?site=foo&amp;page=5
http://example.com/?site=bar&amp;page=8</pre>
<p>That&#8217;s because the Request_URI does not contain the query string (whatever comes after the ? character).</p>
<p>The solution comes with a little bit of <a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond">RewriteCond</a> magic, which helps us set an environment variable using ${QUERY_STRING}:</p>
<pre>RewriteEngine on
RewriteCond %{QUERY_STRING} .*site=foo.*
RewriteRule (.*) $1 [E=subsite_foo:1]
RewriteCond %{QUERY_STRING} .*site=bar.*
RewriteRule (.*) $1 [E=subsite_bar:1]
CustomLog /home/example.com/logs/access.foo.log combined env=subsite_foo
CustomLog /home/example.com/logs/access.bar.log combined env=subsite_bar</pre>
<p>Happy logging&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/logging-website-subsections-with-apache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Bad and Good (code snippets)</title>
		<link>http://blog.cherouvim.com/bad-and-good-code-snippets/</link>
		<comments>http://blog.cherouvim.com/bad-and-good-code-snippets/#comments</comments>
		<pubDate>Sat, 18 Aug 2007 18:05:05 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/bad-and-good-code-snippets/</guid>
		<description><![CDATA[What follows is usual modifications I apply to java code, when I code review. Most of the following changes have a stylistic nature, but some of them can impact performance. Bad code is in red and good code is in green. Feel free to comment.
Unnecessary if statement
if (foo&#62;bar) {
  return true;
} else {
  [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is usual modifications I apply to java code, when I code review. Most of the following changes have a stylistic nature, but some of them can impact performance. Bad code is <strong class="error">in red</strong> and good code is <strong class="good">in green</strong>. Feel free to comment.</p>
<h3>Unnecessary if statement</h3>
<pre class="error">if (foo&gt;bar) {
  return true;
} else {
  return false;
}</pre>
<pre class="good">return foo&gt;bar;</pre>
<h3>Unnecessary nesting</h3>
<pre class="error">if (foo) {
  if (bar) {
    // do something
  }
}</pre>
<pre class="good">if (foo &#038;&#038; bar) {
  // do something
}</pre>
<h3>A bit of redundancy here</h3>
<pre class="error">if (foo!=null &#038;&#038; foo.equals("bar")) {
  // do something
}</pre>
<pre class="good">if ("bar".equals(foo)) {
  // do something
}</pre>
<h3>Looks ugly</h3>
<pre class="error">String comment = request.getParameter("post");
if (comment==null) {
  comment = "n/a";
}</pre>
<pre class="good">String comment = request.getParameter("post")!=null?
        request.getParameter("post"):
        "n/a";</pre>
<p><small>This only holds if <code>request.getParameter("post")</code> is cheap.</small></p>
<h3>Unnecessary variable allocation</h3>
<pre class="error">Something something = new Something(foo);
return something;</pre>
<pre class="good">return new Something(foo);</pre>
<h3>Too much variable scope</h3>
<pre class="error">Something something = new Something();
if (foo) {
  something.prepare();
  request.setAttribute("something", something);
}</pre>
<pre class="good">if (foo) {
  Something something = new Something();
  something.prepare();
  request.setAttribute("something", something);
}</pre>
<h3>Method exposes implementation</h3>
<pre class="error">public ArrayList getSomething() {
  ...
}</pre>
<pre class="good">public List getSomething() {
  ...
}</pre>
<h3>A bit slow</h3>
<pre class="error">String foo = "something";
if (!foo.equals("")) {
  // do something
}</pre>
<pre class="good">String foo = "something";
if (foo.length()&gt;0) {
  // do something
}</pre>
<h3>Too many lines</h3>
<pre class="error">List immutableList = new ArrayList();
immutableList.add(foo);
immutableList.add(bar);
immutableList.add(example);
Something something = new Something(immutableList);</pre>
<pre class="good">Something something = new Something(
        Arrays.asList(new Object[] {foo, bar, example}));</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/bad-and-good-code-snippets/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Disabling foreign key generation in hbm2ddl</title>
		<link>http://blog.cherouvim.com/disabling-foreign-key-generation-in-hbm2ddl/</link>
		<comments>http://blog.cherouvim.com/disabling-foreign-key-generation-in-hbm2ddl/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 10:47:49 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/disable-foreign-key-generation-in-hbm2ddl/</guid>
		<description><![CDATA[When generating the database schema using the hbm2ddl tools, foreign keys are being created for every relation. You can enhance the schema by using the foreign-key attribute to specify your own name for a particular foreign key. 
But, what happens when you don&#8217;t want a foreign key generated?
There is an undocumented behavior of the foreign-key [...]]]></description>
			<content:encoded><![CDATA[<p>When generating the database schema using the <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/tool/hbm2ddl/package-summary.html">hbm2ddl tools</a>, foreign keys are being created for every relation. You can enhance the schema by using the <strong>foreign-key</strong> attribute to <a href="http://www.hibernate.org/hib_docs/reference/en/html_single/#toolsetguide-s1-2">specify your own name for a particular foreign key</a>. </p>
<p>But, what happens when you don&#8217;t want a foreign key generated?</p>
<p>There is an undocumented behavior of the foreign-key attribute, and that is to specify <strong>foreign-key=&#8221;none&#8221;</strong>. hbm2ddl will not create an FK for a relation which has such an attribute. The <a href="http://www.hibernate.org/hib_docs/reference/en/html_single/">hibernate documentation</a> and the two bibles <a href="http://www.manning.com/bauer/">Hibernate in Action</a> and <a href="http://www.manning.com/bauer2/">Java Persistence with Hibernate</a> state absolutely nothing about this feature. I found it after hardcore googling in the following hibernate changelog:</p>
<pre>Changes in version 2.1.9 (xx.x.xxxx)
------------------------------------
* foreign-key="none" can be used to disable generation of a foreign key.</pre>
<p>Later on, I found another blog mentioning this behavior: <a href="http://blog.xebia.com/2007/02/05/let-hibernate-connect-your-world/">http://blog.xebia.com/2007/02/05/let-hibernate-connect-your-world/</a></p>
<p>The most logical question now is &#8220;why don&#8217;t you want an FK?&#8221;. That depends on the system you are building. Sometimes you might have a table in your application which will be <a href="http://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete">CRUD</a>ed from another system, which you do not control. You may want to be able to keep references (ids) to entities which may be deleted. foreign-key=&#8221;none&#8221; together with not-found=&#8221;ignore&#8221; can solve these kind of problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/disabling-foreign-key-generation-in-hbm2ddl/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Test First (#2)</title>
		<link>http://blog.cherouvim.com/test-first-2/</link>
		<comments>http://blog.cherouvim.com/test-first-2/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 20:27:52 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[ide]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[junit]]></category>

		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/test-first-2/</guid>
		<description><![CDATA[You want to create a helper class to analyse an HttpServletRequest and provide you with helper methods which determine the type of request. You basically care about GET and POST request methods. More specifically you want to be able to tell whether a POST is Multipart (usually containing file uploads).
Without losing a second, you create [...]]]></description>
			<content:encoded><![CDATA[<p>You want to create a helper class to analyse an <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html">HttpServletRequest</a> and provide you with helper methods which determine the type of request. You basically care about <a href="http://en.wikipedia.org/wiki/Http_request#Request_message">GET and POST request methods</a>. More specifically you want to be able to tell whether a POST is <a href="http://en.wikipedia.org/wiki/Multipart#Multipart_Messages">Multipart</a> (usually containing file uploads).</p>
<p>Without losing a second, you create the class and make it compile. Right now you only care about the <abbr title="Application programming interface">API</abbr> of the class, and how will it be used from the client side of view. The class is <a href="http://en.wikipedia.org/wiki/Immutable_class">immutable</a>.</p>
<pre>import javax.servlet.http.HttpServletRequest;

public class HttpRequestAnalyser {

  private final boolean get;
  private final boolean post;
  private final boolean multipart;

  public HttpRequestAnalyser(HttpServletRequest request) {
    // do nothing of value (yet)
    this.get = false;
    this.post = false;
    this.multipart = false;
  }

  public boolean isGet() {
    return get;
  }

  public boolean isPost() {
    return post;
  }

  public boolean isMultipart() {
    return multipart;
  }

}</pre>
<p>Press F9, it compiles (<a href="http://www.netbeans.org/">NetBeans</a>).<br />
No matter what kind of request we construct this class with, it will never give us a true answer on whether the method was GET, POST or POST-MULTIPART. That&#8217;s because the implementation is the absolute minimum we need in order to define our API and have something that compiles.</p>
<p>Press CTRL+SHIFT+U to create the following empty test.</p>
<pre>import junit.framework.*;

public class HttpRequestAnalyserTest extends TestCase {

  public HttpRequestAnalyserTest(String testName) {
    super(testName);
  }

  public void setUp() {
  }

  public void testIsGet() {
  }

  public void testIsPost() {
  }

  public void testIsMultipart() {
  }

}</pre>
<p>Press SHIFT+F6, the test passes because there are no assertions yet.</p>
<p>The point is to create good tests for our HttpRequestAnalyser class. The tests will fail, and then we&#8217;ll try to make them pass by doing real work in our class, which is the <a href="http://en.wikipedia.org/wiki/System_Under_Test">System Under Test</a>.</p>
<p>In order to test our class we will need instances of HttpServletRequest. It&#8217;s not (easily) possible to create such instances, as this is an interface, and usually the <a href="http://en.wikipedia.org/wiki/Web_container">servlet container</a> generates concrete implementations. We don&#8217;t want to incorporate a servlet container in our tests. What we are going to do is mock an HttpServletRequest, as if it was coming over http from a client.</p>
<p>In order to mock such a request we could provide our own implementation, possibly in an inner static class, inside our test and we&#8217;d have to implement all methods.</p>
<p>Another way is to create a <a href="http://en.wikipedia.org/wiki/Mock_object">mock object</a> which will implement HttpServletRequest and program it with canned answers for our class. There exist <a href="http://www.mockobjects.com/">many frameworks out there for mocking</a> and we&#8217;ll use <a href="https://mocquer.dev.java.net/">mocquer</a>.</p>
<p>We need to add the servlet-api.jar and the mocquer dependencies on our project&#8217;s test libraries.</p>
<p>In order to fake HTTP request data, we need to know how these look like. <a href="http://www.mozilla.com/en-US/firefox/">Firefox</a> and many other http header monitor tools out there help us gather the following interesting parts of the requests:</p>
<h4>GET method</h4>
<pre>GET /example.html HTTP/1.1
Host: www.domain.com</pre>
<h4>POST method</h4>
<pre>POST /example.html HTTP/1.1
Host: www.domain.com
Content-Type: application/x-www-form-urlencoded</pre>
<h4>POST method with file upload</h4>
<pre>POST /example.html HTTP/1.1
Host: www.domain.com
Content-Type: multipart/form-data; boundary=...</pre>
<p>The test now becomes:</p>
<pre><strong>import javax.servlet.http.HttpServletRequest;</strong>
import junit.framework.*;
<strong>import org.jingle.mocquer.MockControl;</strong>

public class HttpRequestAnalyserTest extends TestCase {

  public HttpRequestAnalyserTest(String testName) {
    super(testName);
  }

<strong>  private MockControl requestControl;
  private HttpServletRequest request;</strong>

  public void setUp() {
<strong>    requestControl = MockControl.createControl(HttpServletRequest.class);
    request = (HttpServletRequest)requestControl.getMock();</strong>
  }

  public void testIsGet() {
<strong>    request.getMethod();
    requestControl.setReturnValue(&#8221;GET&#8221;);
    requestControl.replay();
    HttpRequestAnalyser analyser = new HttpRequestAnalyser(request);

    assertTrue(analyser.isGet());
    assertFalse(analyser.isPost());
    assertFalse(analyser.isMultipart());</strong>
  }

  public void testIsPost() {
<strong>    request.getMethod();
    requestControl.setReturnValue(&#8221;POST&#8221;);
    request.getHeader(&#8221;Content-Type&#8221;);
    requestControl.setReturnValue(&#8221;application/x-www-form-urlencoded&#8221;);
    requestControl.replay();
    HttpRequestAnalyser analyser = new HttpRequestAnalyser(request);

    assertFalse(analyser.isGet());
    assertTrue(analyser.isPost());
    assertFalse(analyser.isMultipart());</strong>
  }

  public void testIsMultipart() {
<strong>    request.getMethod();
    requestControl.setReturnValue(&#8221;POST&#8221;);
    request.getHeader(&#8221;Content-Type&#8221;);
    requestControl.setReturnValue(&#8221;multipart/form-data; boundary=&#8230;&#8221;);
    requestControl.replay();
    HttpRequestAnalyser analyser = new HttpRequestAnalyser(request);

    assertFalse(analyser.isGet());
    assertTrue(analyser.isPost());
    assertTrue(analyser.isMultipart());</strong>
  }

}</pre>
<p>We&#8217;ve provided method call expectations, and canned answers on the mock. The tests fail.<br />
We fill in the HttpRequestAnalyser constructor implementation which now becomes:</p>
<pre>import javax.servlet.http.HttpServletRequest;

public class HttpRequestAnalyser {

  private final boolean get;
  private final boolean post;
  private final boolean multipart;

  public HttpRequestAnalyser(HttpServletRequest request) {
<strong>    this.get = request.getMethod().equals(&#8221;GET&#8221;);
    this.post = request.getMethod().equals(&#8221;POST&#8221;);
    this.multipart =
        request.getHeader(&#8221;Content-Type&#8221;).startsWith(&#8221;multipart/&#8221;);</strong>
  }

  public boolean isGet() {
    return get;
  }

  public boolean isPost() {
    return post;
  }

  public boolean isMultipart() {
    return multipart;
  }

}</pre>
<p>Post and multipart tests pass. The get test fails, because we haven&#8217;t set an expectation for getHeader(&#8221;Content-Type&#8221;) to be called. That is because the GET Http method doesn&#8217;t have such a header.<br />
The multipart check should only happen if the request has been recognised to be a post:</p>
<pre>import javax.servlet.http.HttpServletRequest;

public class HttpRequestAnalyser {

  private final boolean get;
  private final boolean post;
  private final boolean multipart;

  public HttpRequestAnalyser(HttpServletRequest request) {
    this.get = request.getMethod().equals("GET");
    this.post = request.getMethod().equals("POST");
    this.multipart = <strong>post ?</strong>
      request.getHeader(&#8221;Content-Type&#8221;).startsWith(&#8221;multipart/&#8221;) <strong>:
      false;</strong>
  }

  public boolean isGet() {
    return get;
  }

  public boolean isPost() {
    return post;
  }

  public boolean isMultipart() {
    return multipart;
  }

}</pre>
<p>Tests pass, and the class is ready to rock! Happy testing (before coding).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/test-first-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Good Javadoc use</title>
		<link>http://blog.cherouvim.com/good-javadoc-use/</link>
		<comments>http://blog.cherouvim.com/good-javadoc-use/#comments</comments>
		<pubDate>Sun, 29 Jul 2007 07:13:39 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[ide]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/good-javadoc-use/</guid>
		<description><![CDATA[Javadoc is a tool that you have in your %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (UNIX) directory. It is being used to extract API documentation from your Java source code, into HTML.
Most IDEs fully support Javadoc. They assist the developer in writing, displaying and generating the Javadoc documentation. Javadoc is part of the development lifecycle. Good and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://java.sun.com/j2se/javadoc/">Javadoc</a> is a tool that you have in your <code>%JAVA_HOME%\bin</code> (Windows) or <code>$JAVA_HOME/bin</code> (UNIX) directory. It is being used to extract <abbr title="Application Programming Interface">API</abbr> documentation from your Java source code, into <abbr title="HyperText Markup Language">HTML</abbr>.</p>
<p>Most <abbr title="Integrated Development Environment">IDE</abbr>s fully support Javadoc. They assist the developer in writing, displaying and generating the Javadoc documentation. Javadoc is part of the development lifecycle. Good and up-to-date Javadoc is a sign of a healthy project.</p>
<h3>API Users</h3>
<p>The worst thing an API user can do is to ignore Javadoc. Having an easily accessible, local copy of the Javadoc for the library you are working with (Spring, Hibernate, Lucene etc) is the only way to work efficiently. Vast amounts of time have been put into compiling quality documentation for these libraries. Not using it will only slow you down.</p>
<h3>Development Teams</h3>
<p>One of the worst <a href="http://www.netbeans.org/">Netbeans</a> feature is that it allows you to collapse all comments and even set this as the default behavior when opening a source file. Team members, working in the same piece of code, can no longer see the documentation written by other developers.<br />
I actually had a developer asking me about what one of my methods did. For a moment I was confused and asked him whether I had written poor documentation. He then told me that he didn&#8217;t know that there was any Javadoc for that method. I don&#8217;t (want to) know whether Javadoc hiding is a feature of Netbeans or is a plugin, but that was definitely one of the most ineffective behaviors I&#8217;ve seen in a development team.</p>
<h3>API designers</h3>
<p>Writing good Javadoc involves many things which are <a href="http://java.sun.com/j2se/javadoc/writingdoccomments/">documented by SUN</a>. The general idea is that you don&#8217;t want to expose the implementation of the method through the documentation. API users will not care <strong>how</strong> you do it, but only <strong>what</strong> (and maybe why) you do.<br />
So the following comment is bad:</p>
<pre>/**
 * This method uses StringBuffer to merge the name with surname and return
 * the person's full name
 */
 public String getFullName() {</pre>
<p>A better version is:</p>
<pre>/**
 * Returns the full name of the Person. Will return "Nick Cave" if
 * firstname is "Nick" and lastname is "Cave".
 *
 * @return      the full name of the Person
 */
 public String getFullName() {</pre>
<h3>Conclusion</h3>
<p>We (Java developers) are very lucky that documentation is so tightly integrated with the language. Not using it though, takes all this goodness away.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/good-javadoc-use/feed/</wfw:commentRss>
		</item>
		<item>
		<title>FreeMarker exception handling</title>
		<link>http://blog.cherouvim.com/freemarker-exception-handling/</link>
		<comments>http://blog.cherouvim.com/freemarker-exception-handling/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 15:25:11 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[freemarker]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/freemarker-exception-handling/</guid>
		<description><![CDATA[FreeMarker is a very flexible templating engine for Java. Exception handling (while rendering the template) is a very important issue for a templating engine. As with JSP the default behaviour of FreeMarker is to completely cancel rendering and display an error page. When developing a webapp this might not be very helpful. Sometimes errors might [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://freemarker.sourceforge.net/" title="FreeMarker templating engine">FreeMarker</a> is a very flexible templating engine for Java. Exception handling (while rendering the template) is a very important issue for a templating engine. As with <a href="http://java.sun.com/products/jsp/" title="JavaServer Pages">JSP</a> the default behaviour of FreeMarker is to completely cancel rendering and display an error page. When developing a webapp this might not be very helpful. Sometimes errors might need to be tolerated; at least for the development phase of the application development.</p>
<p>FreeMarker provides the <a href="http://freemarker.org/docs/api/freemarker/template/TemplateExceptionHandler.html">TemplateExceptionHandler</a> interface with some implementations but we&#8217;ll define our own in order to provide a more failsafe and usable behaviour.</p>
<pre>public class MyTemplateExceptionHandler
          implements TemplateExceptionHandler {

  public void handleTemplateException(TemplateException te,
          Environment env, Writer out) {
    freemarkerlog.error("template error", te);
    try {
      out.write("&lt;span style=\"cursor:help; color: red\" " +
                "title=\"" + ExceptionUtils.getMessage(te) + "\"&gt;" +
                "[e]" +
                "&lt;/span&gt;\n");
    } catch (IOException ignored) { }
  }

}</pre>
<p>Then, in the code where you configure FreeMarker you need:</p>
<pre>config.setTemplateExceptionHandler(new MyTemplateExceptionHandler());</pre>
<p>This is what you&#8217;ll see whenever there is an exception thrown while rendering the template:<br />
<img src="/images/2007-06-19-freemarker-exception-handling.png" alt="FreeMarker exception handling" width="522" height="165" /><br />
A nice little <span style="cursor:help; color:red" title="InvalidReferenceException: Expression foo is undefined on line 29, column 17 in index.ftl.">[e]</span> with a tooltip containing the exception message.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/freemarker-exception-handling/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unit testing JavaScript with JsUnit</title>
		<link>http://blog.cherouvim.com/unit-testing-javascript-with-jsunit/</link>
		<comments>http://blog.cherouvim.com/unit-testing-javascript-with-jsunit/#comments</comments>
		<pubDate>Sun, 17 Jun 2007 17:31:08 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[javascript]]></category>

		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/unit-testing-javascript-with-jsunit/</guid>
		<description><![CDATA[my-script.js
function capitalize(str) {
  return str?
    str.length&#62;1?str[0].toUpperCase()+str.substring(1):
      str.toUpperCase():str;
}
This is my JavaScript function for capitalizing the first letter of a String. I was sure that I got capitalization right, but I wasn&#8217;t too sure about how this function would handle null/empty values.
So I wrote a unit test, for [...]]]></description>
			<content:encoded><![CDATA[<h3>my-script.js</h3>
<pre>function capitalize(str) {
  return str?
    str.length&gt;1?str[0].toUpperCase()+str.substring(1):
      str.toUpperCase():str;
}</pre>
<p>This is my JavaScript function for capitalizing the first letter of a String. I was sure that I got capitalization right, but I wasn&#8217;t too sure about how this function would handle null/empty values.</p>
<p>So I wrote a unit test, for <a href="http://www.jsunit.net/">JsUnit</a>:</p>
<h3>my-script.test.html</h3>
<pre>&lt;script language="javascript" src="jsUnitCore.js"&gt;&lt;/script&gt;
&lt;script language="javascript" src="my-script.js"&gt;&lt;/script&gt;
&lt;script language="javascript"&gt;

function testCapitalize() {
  assertEquals("Foo bar", capitalize("foo bar"));
  assertEquals("Foobar", capitalize("foobar"));
  assertEquals("F", capitalize("f"));
  assertEquals("", capitalize(""));
  assertEquals(JSUNIT_UNDEFINED_VALUE, capitalize(JSUNIT_UNDEFINED_VALUE));
}

&lt;/script&gt;</pre>
<p>The test requires loading the jsUnitCore.js script. This test loads a local copy but you could use <a href="http://www.jsunit.net/runner/app/jsUnitCore.js" title="jsUnitCore.js from JsUnit">the online version</a> as well. Then I can run the test with my local <a href="http://www.jsunit.net/runner/testRunner.html">TestRunner</a>.</p>
<pre>Status: Done (0.156 seconds)
Runs: 1&nbsp;&nbsp;&nbsp;Errors: 0&nbsp;&nbsp;&nbsp;Failures: 0</pre>
<p>Now I know that my function is correct!</p>
<p>When a test fails the assertion error looks like this:</p>
<pre><strong>1. my-script.test.html:testCapitalize failed</strong>

Expected &lt;Foobar&gt; (String) but was &lt;foobar&gt; (String)

Stack trace follows:
&gt; JsUnitException
&gt; _assert
&gt; assertEquals
&gt; testCapitalize</pre>
<p>Happy testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/unit-testing-javascript-with-jsunit/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Overcoming the MySQL BIT datatype problems with hibernate</title>
		<link>http://blog.cherouvim.com/overcoming-the-mysql-bit-datatype-problems-with-hibernate/</link>
		<comments>http://blog.cherouvim.com/overcoming-the-mysql-bit-datatype-problems-with-hibernate/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 18:38:45 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/overcoming-the-mysql-bit-datatype-problems-with-hibernate/</guid>
		<description><![CDATA[I&#8217;m fond of optimization. When I code or design my database schema I try to avoid waisting CPU cycles or storage space (at least without a good reason). So when my domain class has the following field:

private boolean active; // determines whether this Person is active

I will let hibernate and the MySQL5InnoDBDialect choose what is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m fond of optimization. When I code or design my database schema I try to avoid waisting CPU cycles or storage space (at least without a good reason). So when my domain class has the following field:</p>
<pre>
private boolean active; // determines whether this Person is active
</pre>
<p>I will let hibernate and the <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/MySQL5InnoDBDialect.html">MySQL5InnoDBDialect</a> choose what is most appropriate:</p>
<pre>&lt;property name="active" not-null="true" /&gt;</pre>
<p>In that case it will generate a BIT:</p>
<pre>...
active bit not null,
...</pre>
<h4>The problem</h4>
<p>So far so good&#8230;<br />
&#8230;until you read the blog post called <a href="http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/">&#8220;Why you should not use BIT columns in MySQL&#8221;</a> by <a href="http://www.xaprb.com/blog/">Xaprb</a>.<br />
Another serious deficiency is the fact that a database dump will not export bit data as &#8220;0&#8243; or &#8220;1&#8243;. Depending on the tool used to dump and the MySQL server version you may find one of the following:</p>
<pre>INSERT INTO `person` VALUES (1,&quot;foo&quot;,&quot;\\0&quot;);
INSERT INTO `person` VALUES (2,&quot;bar&quot;,&quot;<span style="font-size:1.3em">&#9786;</span>&quot;);</pre>
<p>or</p>
<pre>INSERT INTO `person` VALUES (1,&quot;foo&quot;,&quot;<span style="border: 1px solid black">&nbsp;</span>&quot;);
INSERT INTO `person` VALUES (2,&quot;bar&quot;,&quot;<span style="border: 1px solid black">&nbsp;</span>&quot;);</pre>
<p>The third field is of datatype BIT. Row number 1 is false and row number 2 is true. The problem with that is that some MySQL client tools cannot import such things. It gives you an <em>ERROR 1064 (42000) at line 23: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8221; at line 1</em></p>
<h4>Solution #1</h4>
<p>Hand edit the sql script and change all false bits to 0 and all true bits to 1, the script can be imported like a charm.</p>
<h4>Solution #2</h4>
<p>Extend MySQL5InnoDBDialect and make all BITs rendered as TinyInt(1). The code is very simple:</p>
<pre>package com.foo.hibernate;

import java.sql.Types;
import org.hibernate.dialect.MySQL5InnoDBDialect;

public class MySQL5InnoDBDialectBitFixed extends MySQL5InnoDBDialect {

  public MySQL5InnoDBDialectBitFixed() {
    super();
    registerColumnType(Types.BIT, "tinyint(1)");
  }

}</pre>
<p>Now when using the <code>MySQL5InnoDBDialectBitFixed</code> dialect, hbm2ddl will generate:</p>
<pre>...
active tinyint(1) not null,
...</pre>
<p>Until we get better 5.x MySQL versions, with better BIT support, this plan should do the job nicely.</p>
<p>Good luck</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/overcoming-the-mysql-bit-datatype-problems-with-hibernate/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Runtime dispatching freemarker macros for pojo views</title>
		<link>http://blog.cherouvim.com/runtime-dispatching-freemarker-macros-for-pojo-views/</link>
		<comments>http://blog.cherouvim.com/runtime-dispatching-freemarker-macros-for-pojo-views/#comments</comments>
		<pubDate>Sun, 10 Jun 2007 06:14:21 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[freemarker]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/runtime-dispatching-freemarker-macros-for-pojo-views/</guid>
		<description><![CDATA[One of the (many) reasons I switched from JSP to FreeMarker is that I couldn&#8217;t achieve what I describe in this post. Tutorials or blog posts regarding this situation were never to be found, and in addition it was really hard to find anyone considering this issue a real problem.
The problem
Suppose we are building an [...]]]></description>
			<content:encoded><![CDATA[<p>One of the (many) reasons I switched from <a href="http://java.sun.com/products/jsp/" title="JavaServer Pages">JSP</a> to <a href="http://freemarker.sourceforge.net/">FreeMarker</a> is that I couldn&#8217;t achieve what I describe in this post. Tutorials or blog posts regarding this situation were never to be found, and in addition it was really hard to find anyone considering this issue a real problem.</p>
<h4>The problem</h4>
<p>Suppose we are building an <a href="http://en.wikipedia.org/wiki/Issue_tracking_system">issue tracking system</a>. We have a rich <a href="http://www.martinfowler.com/eaaCatalog/domainModel.html">Domain Model</a> which includes entities such as <code>User</code>, <code>Project</code>, <code>Account</code>, <code>Role</code> etc. We&#8217;ve also got an abstract <code>Issue</code> object which is the root of the issue&#8217;s hierarchy. Concrete classes extending <code>Issue</code> include <code>Bug</code>, <code>Feature</code>, <code>Request</code> and <code>Change</code>. These 4 <a href="http://www.martinfowler.com/bliki/POJO.html" title="Plain Old Java Object">POJO</a>s inherit common fields from <code>Issue</code> but add fields, methods and logic of their own.</p>
<p>Each of the issue&#8217;s subclass will need to have a slightly different <a href="http://en.wikipedia.org/wiki/HTML" title="Hypertext Markup Language">HTML</a> view. I tend to use the <a href="http://www.javaworld.com/javaworld/jw-09-2002/jw-0913-designpatterns.html">composite design pattern</a> for my views, so I can break the HTML down to small reusable components. So it is obvious that we&#8217;re going to need 4 different views, one for each of them. Here are those issue rendering methods <small>(presented in an imaginary pseudolanguage which combines <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html" title="Expression Language">EL</a>, HTML and functions)</small>:</p>
<pre>renderBug(bug) {
  &lt;fieldset&gt;
    &lt;legend&gt;Bug #${bug.id}&lt;/legend&gt;
    &lt;p&gt;Author: ${bug.author}&lt;/p&gt;
    &lt;p&gt;Date: ${bug.date}&lt;/p&gt;
    &lt;p&gt;Description: ${bug.description}&lt;/p&gt;
    &lt;p&gt;Steps to recreate bug: ${bug.stepsToRecreate}&lt;/p&gt;
  &lt;/fieldset&gt;
}

renderFeature(feature) {
  &lt;fieldset&gt;
    &lt;legend&gt;feature #${feature.id}&lt;/legend&gt;
    &lt;p&gt;Author: ${feature.author}&lt;/p&gt;
    &lt;p&gt;Date: ${feature.date}&lt;/p&gt;
    &lt;p&gt;Description: ${feature.description}&lt;/p&gt;
    &lt;p&gt;Related URL: ${feature.url}&lt;/p&gt;
    &lt;p&gt;Screenshot upload: &lt;img src="${feature.screenshot}" /&gt;&lt;/p&gt;
  &lt;/fieldset&gt;
}

...</pre>
<p>Our <a href="http://java.sun.com/blueprints/patterns/DAO.html" title="Data Access Object">DAO</a> (probably called IssueDao) is going to fetch a <code>Collection&lt;Issue&gt;</code> (a bunch of issues) from the database for a particular use case. The runtime type of each of those entities cannot be <code>Issue</code> but it will be <code>Bug</code>, <code>Feature</code>, <code>Request</code> or <code>Change</code>. The problem is that we are presenting them altogether in the same screen, so in order to render them we have to write code such as this:</p>
<pre>foreach(issues as issue) {
  if (issue instanceof Bug) renderBug(issue); continue;
  if (issue instanceof Feature) renderFeature(issue); continue;
  if (issue instanceof Request) renderRequest(issue); continue;
  if (issue instanceof Change) renderChange(issue);
}</pre>
<p>If this doesn&#8217;t seem very bad to you, here is an actual view implementation of a slightly bigger hierarchy using <a href="http://www.oracle.com/technology/pub/articles/cioroianu_tagfiles.html">JSP 2.0 Tag Files</a>:</p>
<pre>if (t instanceof ActivityInternal) {%&gt;&lt;p:activityInternalView pojo="${t}" /&gt;&lt;%;}
if (t instanceof ActivityExternal) {%&gt;&lt;p:activityExternalView pojo="${t}" /&gt;&lt;%;}
if (t instanceof ActivityMilestone) {%&gt;&lt;p:activityMilestoneView pojo="${t}" /&gt;&lt;%;}
if (t instanceof Review) {%&gt;&lt;p:reviewView pojo="${t}" /&gt;&lt;%;}
if (t instanceof PublicationReport) {%&gt;&lt;p:publicationReportView pojo="${t}" /&gt;&lt;%;}
if (t instanceof PublicationWebsite) {%&gt;&lt;p:publicationWebsiteView pojo="${t}" /&gt;&lt;%;}
if (t instanceof InfoConference) {%&gt;&lt;p:infoConferenceView pojo="${t}" /&gt;&lt;%;}
if (t instanceof InfoBase) {%&gt;&lt;p:infoBaseView pojo="${t}" /&gt;&lt;%;}
if (t instanceof InfoChannel) {%&gt;&lt;p:infoChannelView pojo="${t}" /&gt;&lt;%;}
if (t instanceof Meeting) {%&gt;&lt;p:meetingView pojo="${t}" /&gt;&lt;%;}
if (t instanceof Interpretation) {%&gt;&lt;p:interpretationView pojo="${t}" /&gt;&lt;%;}
if (t instanceof BudgetItem) {%&gt;&lt;p:budgetItemView pojo="${t}" /&gt;&lt;%;}
if (t instanceof FocusGeneral) {%&gt;&lt;p:focusGeneralView pojo="${t}" /&gt;&lt;%;}
if (t instanceof FocusResearch) {%&gt;&lt;p:focusResearchView pojo="${t}" /&gt;&lt;%;}
if (t instanceof Risk) {%&gt;&lt;p:riskView pojo="${t}" /&gt;&lt;%;}
if (t instanceof QAChecklist) {%&gt;&lt;p:QAChecklistView pojo="${t}" /&gt;&lt;%;}
if (t instanceof TargetAudience) {%&gt;&lt;p:targetAudienceView pojo="${t}" /&gt;&lt;%;}
if (t instanceof LessonsLearned) {%&gt;&lt;p:lessonsLearnedView pojo="${t}" /&gt;&lt;%;}</pre>
<p>If you still don&#8217;t think this is bad, you can stop reading ;)</p>
<h4>What not to do</h4>
<p>In a project I did in my early JSP days, what I did was to put all the view logic in the Java class! So it looked like this (this is actual Java):</p>
<pre>public class Bug extends Issue {

  ...

  public String renderMe() {
    return "&lt;fieldset&gt;&lt;legend&gt;" + this.getName() + "&lt;/legend&gt;" +
           "&lt;p&gt;Author: " + this.getAuthor() + "&lt;/p&gt;" +
           "&lt;p&gt;Date: " + this.getDate() + "&lt;/p&gt;" +
           "&lt;p&gt;Description: " + this.getDescription() + "&lt;/p&gt;" +
           "&lt;/fieldset&gt;";
  }
}</pre>
<p>Although this type of code is a perfect candidate for <a href="http://worsethanfailure.com/" title="Worse Than Failure">The Daily WTF</a>, the (only) advantage was that I could now render my pojos using (pseudocode):</p>
<pre>foreach(issues as issue) {
  issue.renderMe();
}</pre>
<h4>The solution</h4>
<p>It seems that all we want is the ability to construct and dynamically (reflectively in Java terms) call the appropriate render tag each time. In freemarker we define macros which look like this:</p>
<pre>&lt;#macro renderBug bug&gt;
  &lt;fieldset&gt;
    &lt;legend&gt;Bug #${bug.id}&lt;/legend&gt;
    &lt;p&gt;Author: ${bug.author}&lt;/p&gt;
    &lt;p&gt;Date: ${bug.date}&lt;/p&gt;
    &lt;p&gt;Description: ${bug.description}&lt;/p&gt;
    &lt;p&gt;Steps to recreate bug: ${bug.stepsToRecreate}&lt;/p&gt;
  &lt;/fieldset&gt;
&lt;/#macro&gt;</pre>
<p>We need a way to call renderXXX where XXX is the <a href="http://jakarta.apache.org/commons/lang/api-2.0/org/apache/commons/lang/ClassUtils.html#getShortClassName(java.lang.Class)">short class name</a> of the issue in question. And here is how you can do this in freemarker:</p>
<pre>&lt;#local macroname='render' + issue.class.name?split(".")?last /&gt;
&lt;@.vars[macroname] issue /&gt;</pre>
<p>For an issue of runtime type com.example.Foo, it concatenates the word &#8220;render&#8221; with &#8220;Foo&#8221; and calls the macro with that name. The magic happens with the help of the <a href="http://freemarker.sourceforge.net/docs/ref_specvar.html"><strong>.vars</strong> special variable</a>. It allows us to access variables by name. The full code now becomes:</p>
<pre>&lt;#macro renderIssue issue&gt;
  &lt;#local macroname='render' + issue.class.name?split(".")?last /&gt;
  &lt;@.vars[macroname] issue /&gt;
&lt;/#macro&gt;

&lt;#list issues as issue&gt;
  &lt;#renderIssue issue /&gt;
&lt;/#list&gt;</pre>
<p>By the way, this capability is usually present in dynamic scripting languages. So for example there are many ways to do that in <a href="http://php.net">PHP</a>.</p>
<h5>using dynamic evaluation</h5>
<pre>$functionName = "renderBug";
$functionName($issue);</pre>
<h5>using <a href="http://php.net/eval">eval</a></h5>
<pre>eval("renderBug($issue);");</pre>
<h5>using <a href="http://php.net/call_user_func">call_user_func</a> (probably safest of all)</h5>
<pre>call_user_func("renderBug", $issue);</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/runtime-dispatching-freemarker-macros-for-pojo-views/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caching pages using ehcache</title>
		<link>http://blog.cherouvim.com/caching-pages-using-ehcache/</link>
		<comments>http://blog.cherouvim.com/caching-pages-using-ehcache/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 18:44:09 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/caching-pages-using-ehcache/</guid>
		<description><![CDATA[When an http request to your /rss page needs 400 milliseconds to complete, it seems obvious that your website could benefit from some caching. Ehcache is a well known cache provider, which most of us know from hibernate. Since we are already &#8220;bound&#8221; to ehcache, lets see how we can benefit from caching some dynamically [...]]]></description>
			<content:encoded><![CDATA[<p>When an http request to your /rss page needs 400 milliseconds to complete, it seems obvious that your website could benefit from some caching. <a href="http://ehcache.sourceforge.net/">Ehcache</a> is a well known cache provider, which most of us know from <a href="http://hibernate.org/">hibernate</a>. Since we are already &#8220;bound&#8221; to ehcache, lets see how we can benefit from caching some dynamically generated pages:</p>
<h3>web.xml</h3>
<pre>&lt;filter&gt;
  &lt;filter-name&gt;SimplePageCachingFilter&lt;/filter-name&gt;
  &lt;filter-class&gt;net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;SimplePageCachingFilter&lt;/filter-name&gt;
  &lt;url-pattern&gt;/rss&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
<p>We set up the <a href="http://ehcache.sourceforge.net/javadoc/net/sf/ehcache/constructs/web/filter/SimplePageCachingFilter.html">SimplePageCachingFilter</a> in the web.xml of the web application and map it to one or more url patterns or servlets. All requests to /rss will be intercepted by the SimplePageCachingFilter.</p>
<h3>ehcache.xml</h3>
<pre>&lt;ehcache&gt;
  &lt;diskStore path="java.io.tmpdir" /&gt;
  &lt;cache name="SimplePageCachingFilter"
         maxElementsInMemory="0"
         eternal="false"
         timeToIdleSeconds="600"
         timeToLiveSeconds="600"
         overflowToDisk="true"/&gt;
&lt;/ehcache&gt;</pre>
<p>We then configure the cache region for pages. We don&#8217;t want any elements kept in memory. Everything should be written to disk at the java.io.tmpdir location. The cache expires every 10 minutes.</p>
<p>Now hitting http://example.com/rss (our default rss page) results in a cache miss. The content is being generated from scratch but before returning to the client, the filter stores it locally. The second time we&#8217;ll get a cache hit. The content now is being fetched from the cache and its very fast. 10 minutes later this cache element will be invalidated. Note that the default implementation uses the <a href="http://en.wikipedia.org/wiki/URI" title="Uniform Resource Identifier">URI</a> together with the <a href="http://en.wikipedia.org/wiki/Query_string">query string</a> to calculate the cache key, so /rss?type=news and /rss?type=forum will result in two different cache elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/caching-pages-using-ehcache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>5 sins of subversion usage</title>
		<link>http://blog.cherouvim.com/5-sins-of-subversion-usage/</link>
		<comments>http://blog.cherouvim.com/5-sins-of-subversion-usage/#comments</comments>
		<pubDate>Sat, 05 May 2007 06:36:15 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[subversion]]></category>

		<category><![CDATA[workplace]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/the-5-sins-of-subversion-usage/</guid>
		<description><![CDATA[Whether you develop alone, or with others, there are some things that you should avoid doing when using subversion (or any other version control system).
1. Break the build and/or tests
The problem
It&#8217;s very common to break the build because you removed a class or changed a method contract. This can sometimes get frustrating. Others, who continue [...]]]></description>
			<content:encoded><![CDATA[<p>Whether you develop alone, or with others, there are some things that you should avoid doing when using <a href="http://subversion.tigris.org/">subversion</a> (or any other version control system).</p>
<h3>1. Break the build and/or tests</h3>
<h4>The problem</h4>
<p>It&#8217;s very common to break the build because you removed a class or changed a method contract. This can sometimes get frustrating. Others, who continue working on the project, will either have to tell you that you broke the build (thus asking you to fix it), or fix it themselves. This is something that requires communication and time. If more than one person fixes the problems, the possibility of conflicts is high.</p>
<h4>Solution</h4>
<p>a) Do a clean build and run all tests before committing. That is Shift+F11 and ALT+F6 in NetBeans.<br />
b) Use a <a href="http://www.martinfowler.com/articles/continuousIntegration.html" title="Continuous Integration article by Martin Fowler">Continuous Integration</a> system which does this for you.</p>
<h3>2. No comments or bad comments</h3>
<h4>The problem</h4>
<p>Examples of bad comments (yes, the first one is an empty string):</p>
<ul>
<li>&nbsp;</li>
<li>uploaded Member.java</li>
<li>Deleted some files</li>
<li>Improvements</li>
</ul>
<p>These comments are useless. Everybody can see from the log that Member.java was &#8220;uploaded&#8221;, or that some files where deleted. It is also not useful to say &#8220;improvements&#8221;, because it is very general, and also because it&#8217;s common sense that you are working on a project only to improve it (and not to make it worse).</p>
<h4>Solution</h4>
<p>Always write comments about changes in the software, and not about which files have been changed. Try to comment in terms of software modules, requests and bug fixes. You could even use issue numbers from your bug tracking software. Examples of good comments would include:</p>
<ul>
<li>Changed authentication to use cookies instead of the http session</li>
<li>Changed from dbcp to c3p0</li>
<li>First stages of html/css layout integration</li>
<li>mock dao tests</li>
<li>fixed mail scheduler (Issue 5532)</li>
<li>Reporting engine now supports PDF export</li>
</ul>
<p>One of the advantages of having comments such as the above is that you can easily select a range of dates and produce a change log.</p>
<h3>3. Committing useless files</h3>
<h4>The problem</h4>
<p>Some people commit stuff such as:</p>
<ul>
<li><a href="http://www.ofzenandcomputing.com/zanswers/98" title="Thumbs.db - a Windows specific database file">Thumbs.db</a></li>
<li><a href="http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_15237" title="_notes - a Dreamweaver specific folder">_notes</a></li>
<li>/build</li>
<li>/dist</li>
<li>nbproject</li>
</ul>
<p>Sometimes Thumbs.db and _notes end up deployed on the production server. This is useless, bad and inappropriate.</p>
<h4>Solution</h4>
<p>Never commit stuff that should not be committed. Learn how to use the ignore/exclude patterns of your favourite svn client. In <a href="http://tortoisesvn.tigris.org/" title="TortoiseSVN is an SVN client for Windows">TortoiseSVN</a> a the exclude/ignore pattern &#8220;<em>thumbs.db _notes dist build nbproject</em>&#8221; will do the job.</p>
<h3>4. Not committing for days (or weeks)</h3>
<h4>The problem</h4>
<p>A developer does not commit for days (or weeks) because he is lazy, always leaves from work in a rush, or hasn&#8217;t got a compiling application (for days&#8230; or weeks). Some reasons why this is very bad are:</p>
<ol>
<li>Code reviewer can&#8217;t review the code.</li>
<li>Developer might be working on something that has already been done before, but nobody can tell him so.</li>
<li>Developer might be trying to solve problem in a wrong way, but nobody can see that.</li>
<li>Anxiety levels of Project Lead increases because he doesn&#8217;t know about work being done.</li>
<li>Merging becomes really hard due to many (and difficult to resolve) conflicts.</li>
<li>The possibility of completely loosing work increases as hard drives tend to fail when you least expect it.</li>
</ol>
<h4>Solution</h4>
<p>Commit at least once a day. If you are working on a feature or fix that is completely unrelated with work that others do, consider <a href="http://nedbatchelder.com/text/quicksvnbranch.html" title="Subversion branching quick start">branching</a>.</p>
<h3>5. Not following naming/structure conventions</h3>
<h4>The problem</h4>
<p>When a repository is shared between many different people with many different projects, things can get messy:</p>
<pre>svn://192.168.1.44/2006-playboy_website
svn://192.168.1.44/COCA_COLA
svn://192.168.1.44/JAVA-DEVELOPMENT/coca-cola-project
svn://192.168.1.44/JAVA-DEVELOPMENT/coca-cola-project-DELETE_THIS
svn://192.168.1.44/JAVA-DEVELOPMENT/coca-cola-project-static-layout
svn://192.168.1.44/Project_1
svn://192.168.1.44/backup/Project_1
svn://192.168.1.44/backup/coca-cola-project-DELETE_THIS
svn://192.168.1.44/dynamic site coca cola
svn://192.168.1.44/important_docs
svn://192.168.1.44/java-projects
svn://192.168.1.44/java-projects/bar
svn://192.168.1.44/java-projects/foo
svn://192.168.1.44/java-projects/foo_1
svn://192.168.1.44/java-projects/foo_old
svn://192.168.1.44/playboy_PROPOSAL_2002
svn://192.168.1.44/project1
svn://192.168.1.44/project1_
svn://192.168.1.44/οι εικόνες</pre>
<h4>Solution</h4>
<p>Use naming and structure conventions. Make up your own, propose them in your team and adopt them. A possible convention could be:</p>
<ol>
<li>Only lower case in folders</li>
<li>No native characters in folders</li>
<li>Only dashes in folders (no spaces or underscores)</li>
<li>Folder structure always uses <em>root/account name/project name/project artifact</em></li>
</ol>
<p>These simple rules greatly improve company&#8217;s xyz repository structure:</p>
<pre>svn://192.168.1.44/coca-cola/dynamic-site/documents
svn://192.168.1.44/coca-cola/dynamic-site/project
svn://192.168.1.44/coca-cola/old-stuff
svn://192.168.1.44/coca-cola/static-site/old-site
svn://192.168.1.44/coca-cola/static-site/site1
svn://192.168.1.44/playboy/dynamic-site/community
svn://192.168.1.44/playboy/dynamic-site/forum
svn://192.168.1.44/playboy/dynamic-site/project
svn://192.168.1.44/playboy/proposals
svn://192.168.1.44/playboy/static-site/old-site
svn://192.168.1.44/playboy/static-site/site1
svn://192.168.1.44/xyz/cms/project
svn://192.168.1.44/xyz/cms/static-layout
svn://192.168.1.44/xyz/interns/documents
svn://192.168.1.44/xyz/interns/nick
svn://192.168.1.44/xyz/interns/project-1
svn://192.168.1.44/xyz/interns/test-project
svn://192.168.1.44/xyz/website/layout-1
svn://192.168.1.44/xyz/website/layout-2
svn://192.168.1.44/xyz/website/layout-3</pre>
<p>Good luck.</p>
<p><small>Interesting reads:<br />
- <a href="http://developer.kde.org/policies/commitpolicy.html" title="KDE SVN Commit Policy">KDE SVN Commit Policy</a><br />
- <a href="http://svnbook.red-bean.com/nightly/en/index.html" title="Version Control with Subversion">Version Control with Subversion</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/5-sins-of-subversion-usage/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQL Server + hbm2ddl + unicode columns</title>
		<link>http://blog.cherouvim.com/sql-server-hbm2ddl-unicode-columns/</link>
		<comments>http://blog.cherouvim.com/sql-server-hbm2ddl-unicode-columns/#comments</comments>
		<pubDate>Tue, 01 May 2007 08:21:25 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/sql-server-hbm2ddl-unicode-columns/</guid>
		<description><![CDATA[Hibernate offers org.hibernate.dialect.SQLServerDialect as the dialect for SQL Server. When generating the database schema, using hbm2ddl, the string type columns do not support native characters. So the following mapping:
&#60;property name="title" length="128" /&#62;
will produce the following SQL:
...
title varchar(128) null,
...
By extending the org.hibernate.dialect.SQLServerDialect we can achieve the generation of NCHAR, NVARCHAR, and NTEXT columns instead of CHAR, [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate offers <code><a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/SQLServerDialect.html">org.hibernate.dialect.SQLServerDialect</a></code> as the dialect for <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server" title="Microsoft SQL Server">SQL Server</a>. When generating the database schema, using <a href="http://www.hibernate.org/hib_docs/v3/reference/en/html/toolsetguide.html">hbm2ddl</a>, the string type columns do not support native characters. So the following mapping:</p>
<pre>&lt;property name="title" length="128" /&gt;</pre>
<p>will produce the following SQL:</p>
<pre>...
title varchar(128) null,
...</pre>
<p>By extending the <code>org.hibernate.dialect.SQLServerDialect</code> we can achieve the generation of NCHAR, NVARCHAR, and NTEXT columns instead of CHAR, VARCHAR and TEXT.</p>
<pre>package com.foo.hibernate;

import java.sql.Types;
import org.hibernate.dialect.SQLServerDialect;

public class SQLServerNativeDialect extends SQLServerDialect{

  public SQLServerNativeDialect() {
    super();
    registerColumnType(Types.CHAR, "nchar(1)");
    registerColumnType(Types.VARCHAR, "nvarchar($l)");
    registerColumnType(Types.LONGVARCHAR, "nvarchar($l)");
    registerColumnType(Types.CLOB, "ntext");
  }

}</pre>
<p>All we need to do now is plug this dialect in our hibernate configuration:</p>
<pre>&lt;property name="hibernate.dialect"&gt;
  com.foo.hibernate.SQLServerNativeDialect
&lt;/property&gt;</pre>
<p><small>Related hibernate forums thread: <a href="http://forum.hibernate.org/viewtopic.php?t=972518">http://forum.hibernate.org/viewtopic.php?t=972518</a><br />
Related API method: <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/Dialect.html#registerColumnType(int,%20int,%20java.lang.String)">http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/Dialect.html</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/sql-server-hbm2ddl-unicode-columns/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn to use the debugger</title>
		<link>http://blog.cherouvim.com/learn-to-use-the-debugger/</link>
		<comments>http://blog.cherouvim.com/learn-to-use-the-debugger/#comments</comments>
		<pubDate>Mon, 30 Apr 2007 19:51:27 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[ide]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/learn-to-use-the-debugger/</guid>
		<description><![CDATA[Your favourite IDE has a powerful debugger which you can use to debug your programs. If you are new to programming, chances are that you are aware of it&#8217;s existence, but never use it.
The problem
Here is an example method, which has a problem. (It actually does nothing of value, but demonstrates the problem case). The [...]]]></description>
			<content:encoded><![CDATA[<p>Your favourite <a href="http://en.wikipedia.org/wiki/Integrated_Development_Environment" title="Integrated Development Environment">IDE</a> has a powerful debugger which you can use to debug your programs. If you are new to programming, chances are that you are aware of it&#8217;s existence, but never use it.</p>
<h3>The problem</h3>
<p>Here is an example method, which has a problem. (It actually does nothing of value, but demonstrates the problem case). The method <em>doSomething</em> is called with a parameter, but the expected result is not returned.</p>
<pre>public Box doSomething(Box foo, Box bar) {
  Box temp = foo.cloneMe();
  if (bar!=null) {
    Box newBox = new Box(bar);
    if (newBox!=null) {
      temp = doSomethingElse(foo);
      if (foo==null) {
        temp = bar;
      }
    }
  }
  return temp;
}</pre>
<p>Here you can see the most frequent (ab)use of <em>System.out.println</em> statements. </p>
<pre>public Box doSomething(Box foo, Box bar) {
<strong>  System.out.println(&#8221;1&#8243;);</strong>
  Box temp = foo.cloneMe();
  if (bar!=null) {
<strong>    System.out.println(&#8221;2&#8243;);</strong>
    Box newBox = new Box(bar);
    if (newBox!=null) {
<strong>      System.out.println(&#8221;3&#8243;);</strong>
      temp = doSomethingElse(foo);
      if (foo==null) {
<strong>        System.out.println(&#8221;4&#8243;);</strong>
        temp = bar;
      }
    }
  }
  return temp;
}</pre>
<p>The developer&#8217;s intent is to trace which if-statements execute, so as to find the bug. If <em>1 2 3</em> is displayed in the console, the developer knows that <em>foo==null</em> evaluated to false.</p>
<p>An &#8220;enhancement&#8221; of this method is to add variables of interest in those System.out.println statements.</p>
<pre>public Box doSomething(Box foo, Box bar) {
  System.out.println(<strong>&#8220;1: &#8221; + foo + &#8221; &#8221; + bar</strong>);
  Box temp = foo.cloneMe();
  if (bar!=null) {
    System.out.println(<strong>&#8220;2: &#8221; + temp</strong>);
    Box newBox = new Box(bar);
    if (newBox!=null) {
      System.out.println(<strong>&#8220;3: &#8221; + newBox</strong>);
      temp = doSomethingElse(foo);
      if (foo==null) {
        System.out.println(<strong>&#8220;4: &#8221; + temp</strong>);
        temp = bar;
      }
    }
  }
  return temp;
}
</pre>
<p>This is one of the most crude ways to debug a program. Unfortunately it&#8217;s quite common between junior developers. Note that if logging needs to be performed (for monitoring or historical purposes) a <a href="http://logging.apache.org/log4j/docs/">proper logging framework</a> has to be used.</p>
<p>Things get interesting when the developer forgets to delete those System.out.println statements. The application is deployed, in a servlet container which hosts more applications, featuring code &#8220;debugged&#8221; in this way.<br />
It&#8217;s not rare to see catalina.out logs which look like this:</p>
<pre>INFO: Find registry server-registry.xml at classpath resource
20 Απρ 2007 10:23:24 μμ org.apache.catalina.startup.Catalina start
INFO: Server startup in 14063 ms
20 Απρ 2007 10:23:24 μμ org.apache.catalina.core.StandardContext reload
INFO: Reloading this Context has started
1
2
is null
3
copying file pic_01.jpg->temp/pic_01.jpg
copying file pic_02.jpg->temp/pic_02.jpg
copying file pic_03.jpg->temp/pic_03.jpg
copying file pic_06.jpg->temp/pic_06.jpg
1
2
is null
3
true
4
5
6

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
...
is null
false
java.lang.NullPointerException
BoxExample$Box@126b249
1
2
is null
3
resultset was null
1
resultset was null
...</pre>
<h3>The solution</h3>
<p>Learn to use your debugger. All you have to do is go to the line you want debugging to start, set a breakpoint (CTRL+F8 in <a href="http://www.netbeans.org/">Netbeans</a>) and start the debug process. You will either debug the whole application (F5) or that single file/unit test (CTRL+SHIFT+F5).<br />
You can set watches, see the stacktrace and examine the contents of all the local variables at any time in the program execution. You get orders of magnitude more power, in less time; for free!</p>
<p>Try it out. When you get used to it, you&#8217;ll never look back.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/learn-to-use-the-debugger/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Test First (#1)</title>
		<link>http://blog.cherouvim.com/test-first-1/</link>
		<comments>http://blog.cherouvim.com/test-first-1/#comments</comments>
		<pubDate>Sat, 28 Apr 2007 07:44:00 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[ide]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[junit]]></category>

		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/test-first-1/</guid>
		<description><![CDATA[Unit testing not only ensures that the code you write is correct, but also helps you develop your code. This can be achieved by testing unimplemented methods and functionalities first, and then &#8220;filling in&#8221; the code to satisfy the tests. This is the &#8220;test first&#8221; rule that your XP coworker will always remind you, at [...]]]></description>
			<content:encoded><![CDATA[<p>Unit testing not only ensures that the code you write is correct, but also helps you develop your code. This can be achieved by testing unimplemented methods and functionalities first, and then &#8220;filling in&#8221; the code to satisfy the tests. This is the <a href="http://www.extremeprogramming.org/rules.html">&#8220;test first&#8221; rule</a> that your <a href="http://en.wikipedia.org/wiki/Extreme_programming" title="Extreme Programming">XP</a> coworker will always remind you, at any occasion. <small>(You do have an XP coworker, don&#8217;t you? :)</small></p>
<h3>The Requirement</h3>
<p>Suppose you have a webapp which displays some data from a database. Some of those texts are long, and you are asked to be able to truncate them at predefined lengths on some particular views. So instead of printing &#8220;Lorem ipsum dolor sit&#8221; you should be able to fit that in 12 chars and print &#8220;Lorem ips&#8230;&#8221;.</p>
<h3>Wait</h3>
<p>The first worst thing you could do there would be to stick that logic straight into the view (JSP, velocity or whatever templating engine you use). You will not be able to test that piece of logic, nor easily reuse it in some other project. You will need to do this in a Java class and then find a way to call it from the template. </p>
<p>The second worst thing you could do would be to implement this functionality yourself, as it already exists in <a href="http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/StringUtils.html#abbreviate(java.lang.String,%20int)">commons lang</a>. You should know what APIs exist out there and try to reuse as often as possible. But anyhow, we&#8217;ll assume that you want to do it yourself.</p>
<h3>Think</h3>
<p>You start by thinking of where to place this method and whether it will be a helper (static?) method or part of a full fledged class with state etc. Then you choose a good method name and what parameters it will accept. Think of how you would like to use this method.</p>
<h3>Code</h3>
<p>You write the method signature:</p>
<pre>public static String truncate(String text, int length) {
}</pre>
<p>Then fill it&#8217;s body to the absolute minimum to make it &#8220;compilable&#8221;:</p>
<pre>public static String truncate(String text, int length) {
<strong>  throw new RuntimeException(&#8221;not implemented&#8221;);</strong>
}</pre>
<p>&#8230;or&#8230;</p>
<pre>public static String truncate(String text, int length) {
<strong>  return &#8220;&#8221;;</strong>
}</pre>
<p>And then <strong>stop</strong>, because that&#8217;s all you need to implement for now.</p>
<h3>Test</h3>
<p>You create a test (hit CTRL+SHIFT+U if you use <a href="http://www.netbeans.org/" title="NetBeans">http://www.netbeans.org/</a>) and create a test for the method <code>truncate</code></p>
<pre>public void testTruncate() {
}</pre>
<p>This test passes, because it doesn&#8217;t test anything. You make things more interesting by adding some basic assertions of what you expect from this method.</p>
<pre>public void testTruncate() {
<strong>  assertEquals(&#8221;Lorem ip&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 11));
  assertEquals(&#8221;Lorem ips&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 12));
  assertEquals(&#8221;Lorem ipsu&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 13));</strong>
}</pre>
<p>You run the test and it fails.<br />
You go back to the source code and implement some logic to satisfy this test.</p>
<h3>Code</h3>
<pre>public static final String truncate(String text, int length) {
<strong>  return text.substring(0, length-3) + &#8220;&#8230;&#8221;;</strong>
}</pre>
<p>You run the test and it now passes.</p>
<h3>Test</h3>
<p>It&#8217;s time to test for some corner cases.</p>
<pre>public void testTruncate() {
  <strong>assertEquals(&#8221;", Demo.truncate(&#8221;Lorem&#8221;, 0));
  assertEquals(&#8221;.&#8221;, Demo.truncate(&#8221;Lorem&#8221;, 1));
  assertEquals(&#8221;..&#8221;, Demo.truncate(&#8221;Lorem&#8221;, 2));
  assertEquals(&#8221;&#8230;&#8221;, Demo.truncate(&#8221;Lorem&#8221;, 3));</strong>
  assertEquals(&#8221;Lorem ip&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 11));
  assertEquals(&#8221;Lorem ips&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 12));
  assertEquals(&#8221;Lorem ipsu&#8230;&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 13));
<strong>  assertEquals(&#8221;Lorem ipsum dolor sit&#8221;, Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, 21));</strong>
}</pre>
<p>All new assertions fail with a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringIndexOutOfBoundsException.html">StringIndexOutOfBoundsException</a>. You write code to satisfy these corner cases.</p>
<h3>Code</h3>
<pre>public static final String truncate(String text, int length) {
<strong>  switch(length) {
    case 0: return &#8220;&#8221;;
    case 1: return &#8220;.&#8221;;
    case 2: return &#8220;..&#8221;;
    case 3: return &#8220;&#8230;&#8221;;
    default:
      return length&lt;text.length() ?
        text.substring(0, length-3) + &#8220;&#8230;&#8221; :
        text;
  }</strong>
}</pre>
<p>You run the test and it now passes.</p>
<h3>Test</h3>
<p>Then you test even more.</p>
<pre>public void testTruncate() {
  assertEquals("", Demo.truncate("Lorem", 0));
  assertEquals(".", Demo.truncate("Lorem", 1));
  assertEquals("..", Demo.truncate("Lorem", 2));
  assertEquals("...", Demo.truncate("Lorem", 3));
  assertEquals("Lorem ip...", Demo.truncate("Lorem ipsum dolor sit", 11));
  assertEquals("Lorem ips...", Demo.truncate("Lorem ipsum dolor sit", 12));
  assertEquals("Lorem ipsu...", Demo.truncate("Lorem ipsum dolor sit", 13));
  assertEquals("Lorem ipsum dolor sit", Demo.truncate("Lorem ipsum dolor sit", 21));
<strong>  try {
    Demo.truncate(&#8221;Lorem ipsum dolor sit&#8221;, -1);
    fail(&#8221;Should have thrown illegal argument exception&#8221;);
  } catch (IllegalArgumentException expected) { }</strong>
}</pre>
<p>This is a standard idiom for testing that an exception should be thrown. If this method is called with a negative length parameter, we&#8217;d like an <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</a> to be thrown. It is not mandatory to test for things like these, but some people like to seal their methods from really bad usage.</p>
<h3>Code</h3>
<pre>public static final String truncate(String text, int length) {
<strong>  if (length&lt;0) {
    throw new IllegalArgumentException(&#8221;Length cannot be negative&#8221;);
  }</strong>
  switch(length) {
    case 0: return &#8220;&#8221;;
    case 1: return &#8220;.&#8221;;
    case 2: return &#8220;..&#8221;;
    case 3: return &#8220;&#8230;&#8221;;
    default:
      return length&lt;text.length() ?
        text.substring(0, length-3) + &#8220;&#8230;&#8221; :
        text;
  }
}</pre>
<p>You run the test and it now passes. You are done.</p>
<h3>Conclusion</h3>
<p>This implementation might not be the best in the world, but right now this doesn&#8217;t matter. The code runs, and it&#8217;s robust. Whenever you feel like, you can <a href="http://en.wikipedia.org/wiki/Code_refactoring">refactor</a> it and make it perform better. The test will be there to guide you.</p>
<p>p.s What we&#8217;ve omitted when we wrote the signature of this method, was to write <a href="http://java.sun.com/j2se/javadoc/">Javadoc</a>. It is very important to document your API, and we&#8217;ll discuss that in another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/test-first-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn to use Google</title>
		<link>http://blog.cherouvim.com/learn-to-use-google/</link>
		<comments>http://blog.cherouvim.com/learn-to-use-google/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 19:01:20 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[google]]></category>

		<category><![CDATA[workplace]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/learn-to-use-google/</guid>
		<description><![CDATA[What is Google
Google is the best search engine around. It is very user friendly, easy to use and has tons of features. By using Google you can have all the knowledge in the world, available to you; in a snap.
The Problem
Although the facts above sound very nice, and everybody seems to be using Google, it [...]]]></description>
			<content:encoded><![CDATA[<h3>What is Google</h3>
<p><a href="http://google.com/">Google</a> is the best search engine around. It is very user friendly, easy to use and has tons of <a href="http://www.google.com/help/features.html">features</a>. By using Google you can have all the knowledge in the world, available to you; in a snap.</p>
<h3>The Problem</h3>
<p>Although the facts above sound very nice, and everybody seems to be using Google, it is still (on 2007) considered natural for non technical people (my mom, your mom etc) to <b>be hands tied</b> and <b>feel &#8220;blind&#8221;</b> when using the <a href="http://en.wikipedia.org/wiki/Internet">Internet</a>. It is not expected from them to be able to find the information they want, easily and accurately, although this is not hard at all.</p>
<p>When talking for technical people though (developers, designers, analysts, managers etc) it is <b>completely unacceptable</b> when one (or more) of the following behaviors are observed:</p>
<ol>
<li>Complete ignorance of the search engine (rare).</li>
<li>Boredom to such degree where searching is not an option (common).</li>
<li>Inability to find correct search keywords for the topic in question (common).</li>
<li>Incompetence to use <a href="http://www.mozilla.com/en-US/firefox/">Firefox</a> properly (Tabs, Search Bar, maximal use of keyboard) in a degree that makes searching slower than it should be, thus turning away the individual from searching as often as possible (common).</li>
<li>&#8220;Can&#8217;t be bothered-I&#8217;ll ask my co-worker&#8221; syndrome (very common).</li>
<li>&#8220;I give up, this is not possible - has not been done before&#8221; syndrome (rare).</li>
</ol>
<h3>The Facts</h3>
<p>These behaviors are definite showstoppers. They make you a less productive and irritated person. No need to analyze that further.</p>
<h3>The Solutions</h3>
<ul>
<li>Do you observe such behaviors on your employees? Time for a chat with them. Have someone show them how <a href="http://thinkprogress.org/2006/10/23/bush-says-he-uses-the-google/">&#8220;the Google&#8221; works</a>.</li>
<li>Do you observe such behaviors on you; on your daily work routine? You can do better - and please start <b>today</b>!</li>
</ul>
<h3>And why do you care?</h3>
<p>I&#8217;ve been watching people, engaging into long discussions with other people about that tool&#8230; that <a href="http://www.w3.org/Style/CSS/">css</a> compressor tool that has been mentioned once in a meeting&#8230; which meeting? Yes, that meeting, oh yes&#8230; and what does it do? It strips whitespace and makes the css file less readable and blah blah blah&#8230;<br />
- google: <a href="http://www.google.com/search?q=css+compressor">css compressor</a><br />
1st result</p>
<p>I&#8217;ve been watching people, trying to explain what they want to achieve, and whether it is possible. They want an <a href="http://en.wikipedia.org/wiki/AJAX">ajax</a> thingy which will update part of the screen without refreshing the whole screen (irony?), which will be displaying chat messages from many people, possibly by polling the server every so ofter&#8230; blah blah blah<br />
- google: <a href="http://www.google.com/search?q=ajax+chat+example+poll+server">ajax chat example poll server</a><br />
1st result</p>
<p>I&#8217;ve seen people lifting themselves (literally) from their seat, walking down the aisle, to ask a colleague to give them (yes <i>give them</i>) the <a href="http://en.wikipedia.org/wiki/URL">URL</a> where from they can download <a href="http://www.netbeans.org/">NetBeans</a>!<br />
- google: <a href="http://www.google.com/search?q=download netbeans">download netbeans</a><br />
1st result</p>
<h3>Conclusion</h3>
<p>Man&#8230; it&#8217;s not that hard. Make your lives easier, and let the people around you work! If you don&#8217;t know how to use Google, then google for a google tutorial :)</p>
<p>Happy googling&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/learn-to-use-google/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unit testing needs time?</title>
		<link>http://blog.cherouvim.com/unit-testing-needs-time/</link>
		<comments>http://blog.cherouvim.com/unit-testing-needs-time/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 15:59:23 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[junit]]></category>

		<category><![CDATA[workplace]]></category>

		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/unit-testing-needs-time/</guid>
		<description><![CDATA[New developers will sometimes complain about how Unit Testing  requires a lot of time. How much it slows them down, and how they cannot  see any good in writing tests for their code.
There are many scenarios which prove that unit testing is necessary. These include speed of development, ability to refactor easily, test-driven [...]]]></description>
			<content:encoded><![CDATA[<p>New developers will sometimes complain about how <a href="http://en.wikipedia.org/wiki/Unit_testing">Unit Testing</a>  requires a lot of time. How much it slows them down, and how they cannot  see any good in writing tests for their code.<br />
There are many scenarios which prove that unit testing is necessary. These include speed of development, ability to refactor easily, <a href="http://en.wikipedia.org/wiki/Test_driven_development">test-driven  development</a>, testing without the need of web container, <a href="http://www.mockobjects.com/">testing with mock objects</a> etc.</p>
<p>My favorite though, is the &#8220;client calls to report something weird&#8221; scenario. I&#8217;ve seen it many times, and it goes something like this:</p>
<h3>Scenario:</h3>
<ol>
<li>Your webapp is deployed, weeks ago, and you&#8217;ve moved on with a new, exciting project. Everything is feels good, as you&#8217;ve completely forgotten about sins of the past (not writing tests)</li>
<li>Client calls you to report &#8220;something weird&#8221;.</li>
<li>You stop whatever you are doing at that moment to switch to that project.</li>
<li>You connect (VPN or whatever) to the remote server to see possibly logged exceptions.</li>
<li>You collect your data.</li>
<li>You try to reproduce the error locally, on the development server.</li>
<li>You find the bug.</li>
<li>You issue the bug in the bug tracking system.</li>
<li>You fix the bug.</li>
<li>You build for deployment.</li>
<li>You deploy (while solving any possible application version issues).</li>
<li>You contact the client.</li>
<li>Wait for his confirmation that the bug has been corrected.</li>
<li>You deliver the bug in the issue tracking system.</li>
<li>Finally you commit the code back to <a href="http://en.wikipedia.org/wiki/Subversion_%28software%29" title="Subversion">SVN</a>.</li>
</ol>
<p><small><em>&#8230;or in whatever order feels more natural to you.</em></small></p>
<p>If the above scenario feels OK, and you need some hints on why you should try to minimize such cases, have a look at the costs involved:</p>
<h3>Costs:</h3>
<ul>
<li>All of these actions need time. Your time.</li>
<li>Most of them require a context switch. Not only you lose X minutes from your previous work, but also need Y minutes to get back into the flow (mind state) you had previously.</li>
<li>Some of these steps might not be what you really want to be doing (talking directly to the client).</li>
<li>You become a slow worker producing bad code.</li>
<li>People will never trust you with that mission critical application, because your code has a tendency to develop &#8220;random features&#8221; on runtime (usually involving exciting names such as <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html" title="java.lang.NullPointerException">NullPointerException</a>).</li>
</ul>
<h3>Facts:</h3>
<p>This scenario can definitely happen for tested code. Bugs will always creep into your code no matter what. The point is try to at least minimize the stupid ones. Cases which can easily be covered by unit tests.<br />
Unit testing <strong>is important</strong> (if not mandatory). If you feel that it needs time, you have to press yourself and do it. It&#8217;s a matter of weeks until you <a href="http://junit.sourceforge.net/doc/testinfected/testing.htm">test infected</a> and experience how your software becomes better in less time.</p>
<h3>Hints for NetBeans users:</h3>
<ul>
<li>Got a class that you want to create a <a href="http://junit.sourceforge.net/">JUnit</a> test for? <strong>CTRL+SHIFT+U</strong></li>
<li>Got a class and want to jump to the unit test (and vice versa)? <strong>ALT+SHIFT+E</strong></li>
<li>Want to test the project? <strong>ALT+F6</strong></li>
</ul>
<p>Happy testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/unit-testing-needs-time/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unobtrusive JavaScript</title>
		<link>http://blog.cherouvim.com/unobtrusive-javascript/</link>
		<comments>http://blog.cherouvim.com/unobtrusive-javascript/#comments</comments>
		<pubDate>Mon, 19 Mar 2007 19:42:01 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[javascript]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/unobtrusive-javascript/</guid>
		<description><![CDATA[JavaScript. Yes, it does happen sometimes. Someone asks you to add some client side functionality in your webapp, and you start vomiting js code straight into your (x)html templates. That is very very bad, and you should stop immediately. Since it&#8217;s 2007, and given that you want to evolve and keep enjoying what you are [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Javascript">JavaScript</a>. Yes, it does happen sometimes. Someone asks you to add some client side functionality in your webapp, and you start vomiting js code straight into your (x)html templates. That is very very bad, and you should stop immediately. Since it&#8217;s 2007, and given that you want to evolve and keep enjoying what you are doing, here is how you should start thinking about client side code.</p>
<p><a href="http://en.wikipedia.org/wiki/Separation_of_concerns">Separation of concerns</a>. Years ago, we used to do our presentation inside our (x)html templates. Then we figured our <a href="http://csszengarden.com/" title="CSS Zen Garden">how to do it using CSS</a>. Years ago, we used to do all our persisted data (from database) to Objects translation, inside our <acronym title="Plain Old Java Objects">POJOs</acronym>. Then we started using <a href="http://en.wikipedia.org/wiki/Data_Access_Object" title="Data Access Objects">DAOs</a>. Separation is what makes software scale. Unless you are only &#8220;engineering&#8221; basic contact forms and guestbooks, you should try to embrace separation of concerns and layering, which will make your life easier.</p>
<p>So, back to JavaScript. The idea is to <a href="http://onlinetools.org/articles/unobtrusivejavascript/">apply your behaviors in an unobtrusive way</a>. Add a class, if necessary, to the elements you want to enhance, and apply the new functionality when the document loads.</p>
<h3>Example:</h3>
<p>You want to apply duplication functionality in some paragraphs. Clicking on those paragraphs, which have a class &#8220;duplicate&#8221;, will make them duplicate themselves.</p>
<h3>HTML:</h3>
<pre>&lt;p class="duplicate"&gt;Click me!&lt;/p&gt;</pre>
<p>You&#8217;ve got a little piece of nice (x)html. Google does not care what you are going to do on the client side. Screen readers and most mobiles won&#8217;t care as well. Why feed them anything more than pure accessible semantic markup? All client side code goes on a separate file, which on load will apply the behavior.</p>
<h3>JavaScript:</h3>
<pre>// when window has loaded
window.onload=init;

  function init() {

  // if browser is capable
  if(document.getElementById) {

    // get all &lt;p&gt;
    var ps = document.getElementsByTagName('p');
    for(var i=0,length=ps.length; i&lt;length; i++) {

      // if paragraph element has class 'duplicate'
      if (p[i].className.indexOf('duplicate')&gt;=0) {

        // apply behavior
        ps[i].onclick=function() {
          this.parentNode.appendChild(this.cloneNode(true));
        };
      }
    }
  }
}</pre>
<p><a href="/demos/duplicate/" title="A demo for unobtrusive JavaScript">Live Demo</a><br />
The first disadvantage here is that we have to write tedious <acronym title="Document Object Model">DOM</acronym> manipulation code. The second is that we have to do browser detection. In this simple example this is not so painful, but when you want to do complex stuff (possibly with asynchronous calls) there will be lots of code for browser compatibility. The third (and most important) problem is that this code will start executing only when the window has finished rendering. In complex documents and slow connections this will be seen.<br />
The answer to all those problems comes with lightweight JavaScript libraries. Have a look at the same code, written in <a href="http://jquery.com/">jQuery</a>:</p>
<pre>
// when window has loaded
$(function(){

  // for all p with class duplicate which will be clicked
  $('p.duplicate').click(function() {

    // apply behavior
    $(this).parent().append($(this).clone());
  })
})</pre>
<p>The best advice of course is to avoid use of JavaScript at any cost. <a href="http://en.wikipedia.org/wiki/IE6" title="Internet Exploder 6">IE6</a>, a very popular browser, <a href="http://www.bazon.net/mishoo/articles.epl?art_id=824">memory leaks</a> very badly, which is another reason to try and keep JavaScript code to the absolute minimum.<br />
Good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/unobtrusive-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tomcat vs JBoss Web</title>
		<link>http://blog.cherouvim.com/tomcat-vs-jboss-web/</link>
		<comments>http://blog.cherouvim.com/tomcat-vs-jboss-web/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 14:58:14 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[jboss]]></category>

		<category><![CDATA[jmeter]]></category>

		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/tomcat-vs-jboss-web/</guid>
		<description><![CDATA[JBoss Web is a web server and servlet container at the same time. It&#8217;s promise is that it can serve static and dynamic content, very fast, without the need of an Apache HTTPD fronting it. If that&#8217;s true, its party time, and I personally live for the day where it will be easy to get [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jboss.com/products/jbossweb">JBoss Web</a> is a web server and servlet container at the same time. It&#8217;s promise is that it can serve static and dynamic content, very fast, without the need of an <a href="http://httpd.apache.org/">Apache HTTPD</a> fronting it. If that&#8217;s true, its <strong>party time</strong>, and I personally live for the day where it will be easy to get Java 5 enabled hosting for ~5USD/month (as it is the case today with <a href="http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29" title="Linux Apache MySQL PHP">LAMP</a> stacks).</p>
<p>JBoss Web uses <a href="http://apr.apache.org/" title="Apache Native Runtime">APR</a> and native extensions in order to achieve better utilization of the resources of the O/S. Note that APR is also <a href="http://tomcat.apache.org/tomcat-5.5-doc/apr.html" title="APR and Tomcat">available for Tomcat</a> now.</p>
<p>I&#8217;ve decided to give JBoss Web a try, locally, and stress test it against a regular Tomcat. Note that what I did was done for pure fun (and out of curiosity). I do not own a lab, I am definitely not a stress test expert and I do not understand many things at the low level (I/O, threads etc).</p>
<h3>Test info</h3>
<ol>
<li><a href="http://jakarta.apache.org/jmeter/">JMeter</a> was used and it was running on the same machine with the servers tested.</li>
<li>During tests JMeter would use ~30% of cpu, and the server would consume the rest ~70%.</li>
<li>O/S was Windows XP SP2 on an AMD64 3000+ with 1.5GB ram.</li>
<li>Java 1.5.0_06 on -server mode for both servers.</li>
<li>Default installations of JBoss Web 1.0.1 GA and Tomcat 5.5.23 were used.</li>
<li>-Xms and -Xmx settings were not altered. Don&#8217;t think it mattered.</li>
<li>I stress tested 10 URLs of a very small webapp with a front controller delegating to cached <a href="http://freemarker.sourceforge.net/">freemarker</a> views. No logging, no persistence or database calls. JBoss&#8217; CONSOLE appender&#8217;s threshold was changed to FATAL, to avoid any logging output which would slow down things. The most interesting operations in the webapp would be the GZIP filter, and multipart request using <a href="http://jakarta.apache.org/commons/fileupload/">commons fileupload</a>.</li>
<li>Warm up of the servers was performed. I found out that even for small amount of concurrent threads hitting the server, if these all start immediately, it&#8217;s most likely you&#8217;ll get some <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes" title="500 Internal Server Error">500s</a> at the beginning. The warm up would be anything between 2500-5000 requests until the server throughput was stabilized.</li>
<li>When the server was warmed up, I would get my sample from the next 5000-10000 requests.</li>
<li>The &#8220;threads&#8221; column in the results table, is the amount of concurrent threads which where hitting the server.</li>
<li>An http cookie manager was used on JMeter, so 10000 sessions were <em>not</em> being created.</li>
</ol>
<h3>Results</h3>
<table class="table">
<tr>
<th>threads</th>
<th>Tomcat 5.5.23</th>
<th>JBoss Web 1.0.1</th>
</tr>
<tr>
<td>50</td>
<td class="good">95 requests/sec</td>
<td class="good">88 requests/sec</td>
</tr>
<tr>
<td>75</td>
<td class="good">105 requests/sec</td>
<td class="good">95 requests/sec</td>
</tr>
<tr>
<td>100</td>
<td class="good">123 requests/sec</td>
<td class="good">100 requests/sec</td>
</tr>
<tr>
<td>125</td>
<td class="good">75 requests/sec</td>
<td class="good">104 requests/sec</td>
</tr>
<tr>
<td>150</td>
<td class="good">110 requests/sec<br />
<em><small>at this point I had to increase the maxThreads</small></em></td>
<td class="good">110 requests/sec</td>
</tr>
<tr>
<td>200</td>
<td class="good">62 requests/sec</td>
<td class="good">97 requests/sec</td>
</tr>
<tr>
<td>300</td>
<td class="good">115 requests/sec</td>
<td class="good">108 requests/sec</td>
</tr>
<tr>
<td>400</td>
<td class="error">n/a<br />
<em><small>at this point JMeter would block.</small></em><br />
<em><small>[25 seconds per page]</small></em></td>
<td class="good">80 requests/sec</td>
</tr>
<tr>
<td>500</td>
<td class="error">n/a</td>
<td class="good">75 requests/sec</td>
</tr>
<tr>
<td>600</td>
<td class="error">n/a</td>
<td class="good">84 requests/sec</td>
</tr>
<tr>
<td>700</td>
<td class="error">n/a</td>
<td class="good">55 requests/sec<br />
<em><small>[10 seconds per page]</small></em></td>
</tr>
<tr>
<td>800</td>
<td class="error">n/a</td>
<td class="good">48 requests/sec<br />
<em><small>[13 seconds per page]</small></em></td>
</tr>
<tr>
<td>1000</td>
<td class="error">n/a</td>
<td class="error">n/a<br />
<em><small>at this point JMeter would block</small></em></td>
</tr>
</table>
<h3>Findings</h3>
<p>Even this test can be considered rudimentary, JBoss Web looks very good. The biggest problem with the whole procedure is that JMeter was on the same machine as the servers. JMeter supports <a href="http://jakarta.apache.org/jmeter/usermanual/remote-test.html">Remote Testing</a> and <a href="http://jakarta.apache.org/jmeter/usermanual/jmeter_distributed_testing_step_by_step.pdf">Distributed Testing</a> which would have produced more accurate results.</p>
<p>In any case, it was fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/tomcat-vs-jboss-web/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Default Servlet and Resin</title>
		<link>http://blog.cherouvim.com/default-servlet-and-resin/</link>
		<comments>http://blog.cherouvim.com/default-servlet-and-resin/#comments</comments>
		<pubDate>Thu, 08 Mar 2007 16:15:28 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[deployment]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[resin]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/default-servlet-and-resin/</guid>
		<description><![CDATA[Suppose you use a servlet as a front controller to catch and process all urls in a web app. If you want clean URLs you may have mapped it using:
&#60;servlet-mapping&#62;
  &#60;servlet-name&#62;FrontController&#60;/servlet-name&#62;
  &#60;url-pattern&#62;/&#60;/url-pattern&#62;
&#60;/servlet-mapping&#62;
Your front controller will now attempt to serve all URLs, and this is something you don&#8217;t want. Static content (png, html, ico, [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you use a servlet as a <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html">front controller</a> to catch and process all urls in a web app. If you want clean URLs you may have mapped it using:</p>
<pre>&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;FrontController&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;</pre>
<p>Your front controller will now attempt to serve <strong>all URLs</strong>, and this is something you don&#8217;t want. Static content (png, html, ico, css&#8230;) are being served by a default servlet. In tomcat that is <a href="http://tomcat.apache.org/tomcat-5.5-doc/default-servlet.html">org.apache.catalina.servlets.DefaultServlet</a>, and has been configured for you in <em>conf/web.xml</em> with the name &#8220;default&#8221;.</p>
<p>So, in order to exclude all static content from the catch-all of your front controller, you have to map static content to the default servlet, before the mapping of the front controller:</p>
<pre>&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;default&lt;/servlet-name&gt;&lt;url-pattern&gt;*.css&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;default&lt;/servlet-name&gt;&lt;url-pattern&gt;*.js&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;default&lt;/servlet-name&gt;&lt;url-pattern&gt;*.png&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;default&lt;/servlet-name&gt;&lt;url-pattern&gt;*.jpg&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
...</pre>
<p>That works nicely, when deploying in <a href="http://tomcat.apache.org/">Tomcat</a>, <a href="http://www.mortbay.org/">Jetty</a> and <a href="http://www.jboss.org/products/jbossas">JBoss Application Server</a>.<br />
On <a href="http://www.caucho.com/">Resin</a>, deployment fails with the following message:<br />
<strong>WEB-INF/web.xml:89: `default&#8217; is an unknown servlet-name. servlet-mapping requires that the named servlet be defined in a &lt;servlet&gt; configuration before the &lt;servlet-mapping&gt;.&lt;/servlet-mapping&gt;&lt;/servlet&gt;</strong><br />
Resin&#8217;s static content servlet is <a href="http://www.caucho.com/resin-javadoc/com/caucho/servlets/FileServlet.html">com.caucho.servlets.FileServlet</a> and until 3.0 was mapped using the name &#8220;file&#8221;. Then, on 3.1, and after some people complained that they couldn&#8217;t have a servlet called &#8220;file&#8221;, the name was changed to &#8220;resin-file&#8221;.<br />
So, there are 2 solutions to make your application function properly. You can either change all references from &#8220;default&#8221; to &#8220;resin-file&#8221; in your web.xml, or change the FileServlet&#8217;s name from &#8220;resin-file&#8221; to &#8220;default&#8221; in Resin&#8217;s <a href="http://www.caucho.com/resin-3.0/config/webapp.xtp">conf\app-default.xml</a>.</p>
<p>Happy deployments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/default-servlet-and-resin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Time to get Groovy</title>
		<link>http://blog.cherouvim.com/time-to-get-groovy/</link>
		<comments>http://blog.cherouvim.com/time-to-get-groovy/#comments</comments>
		<pubDate>Fri, 02 Mar 2007 21:23:34 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[groovy]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/time-to-get-groovy/</guid>
		<description><![CDATA[Groovy is a dynamic language for Java. It allows you to do all funky stuff that you can do in dynamic languages, but still be able to write Java and execute inside the JVM.
Let&#8217;s get busy and write a simple Person Java class, in Java:
Person.java
package com.cherouvim.blog;
public abstract class Person {
  private int age;

  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://groovy.codehaus.org/">Groovy</a> is a dynamic language for Java. It allows you to do all funky stuff that you can do in <a href="http://en.wikipedia.org/wiki/Dynamic_language">dynamic languages</a>, but still be able to write Java and execute inside the JVM.</p>
<p>Let&#8217;s get busy and write a simple Person Java class, in Java:<br />
<code><strong>Person.java</strong></code></p>
<pre>package com.cherouvim.blog;
public abstract class Person {
  private int age;

  public Person() {
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getAge() {
    return this.age;
  }

  /**
   * Determines whether this person should
   * start smoking.
   * Always returns false!
   */
  public boolean shouldStartSmoking() {
    return false;
  }

  /**
   * Determines whether this person is allowed
   * to drive a car.
   * To be implemented on subclass.
   */
  public abstract boolean isAllowedToDriveCar();

}</pre>
<p>This is an abstract class; it could have also been an interface. We want to add more behaviour now with Groovy code, so we are going to extend it and define the <code>isAllowedToDriveCar</code> method:<br />
<code><strong>Person.groovy</strong></code></p>
<pre>package com.cherouvim.blog
public class PersonGroovy extends Person {
  public boolean isAllowedToDriveCar() {
    return this.getAge()&gt;18
  }
}</pre>
<p>In order to make this groovy script available to our Java program, we need to load it via the <a href="http://groovy.codehaus.org/api/groovy/lang/GroovyClassLoader.html">GroovyClassLoader</a>. I define a helper method so I can easily get instances of Groovy classes:<br />
<code><strong>GroovyLoader</strong></code></p>
<pre>public static final Object loadGroovyObject(String groovyScriptLocation) {
  GroovyClassLoader gc = new GroovyClassLoader();
  Class groovyClass = gc.parseClass(
      ClassLoader.getSystemResourceAsStream(groovyScriptLocation));
  Object groovyObject = null;
  try {
    groovyObject = groovyClass.newInstance();
  } catch (Exception ex) {
    // log exception
    ex.printStackTrace();
  }
  return groovyObject;
}</pre>
<p>Now let&#8217;s test our concrete PersonGroovy class:<br />
<code><strong>PersonTest.java</strong></code></p>
<pre>  Person p;
  public void setUp() {
    p = (Person)GroovyLoader.loadGroovyObject(
        "com/cherouvim/blog/Person.groovy");
  }

  /**
   * Test java method
   */
  public void testShouldStartSmoking() {
    assertFalse(p.shouldStartSmoking());
  }

  /**
   * Test groovy method
   */
  public void testIsAllowedToDriveCar() {
    p.setAge(14);
    assertFalse(p.isAllowedToDriveCar());
    p.setAge(27);
    assertTrue(p.isAllowedToDriveCar());
  }</pre>
<p>This test gets an instance of the PersonGroovy class, casts it to the known type of Person and we are ready to go. The <code>isAllowedToDriveCar</code> method is available because we defined it in the abstract superclass. The tests pass.</p>
<p><em>Note that the way I&#8217;ve presented parses the groovy file every time we call <code>loadGroovyObject</code> which is slow. You can cache the class.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/time-to-get-groovy/feed/</wfw:commentRss>
		</item>
		<item>
		<title>An http session impersonation protection filter</title>
		<link>http://blog.cherouvim.com/an-http-session-impersonation-protection-filter/</link>
		<comments>http://blog.cherouvim.com/an-http-session-impersonation-protection-filter/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 08:00:32 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[security]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/an-http-session-impersonation-protection-filter/</guid>
		<description><![CDATA[Session Impersonation is an attack which works for webapps and dynamic websites. Someone steals your session cookie (possibly by using XSS - Cross Site Scripting), injects it into his browser visits the site and suddenly appears to be you. If you happened to be logged in as the single superadmin of the system, then he [...]]]></description>
			<content:encoded><![CDATA[<p>Session Impersonation is an attack which works for webapps and dynamic websites. Someone steals your session cookie (possibly by using <a href="http://en.wikipedia.org/wiki/XSS">XSS - Cross Site Scripting</a>), injects it into his browser visits the site and suddenly appears to be you. If you happened to be logged in as the single superadmin of the system, then he is a superadmin as well.</p>
<p>One of the ways to avoid this problem is by storing a hash (or token) the first time the http session is created. That hash will contain the user&#8217;s IP address and his user agent (the browser he uses). On each following request, the hash is being recalculated, and must match the hash previously stored in the http session. If it does not match, any of the 3 things might have happened:</p>
<ol>
<li>Client has changed his IP.</li>
<li>Client has changed his user agent String.</li>
<li>Client is using another clients session (session impersonation attack).</li>
</ol>
<p>Changing you IP is hard (unless your ISP is <a href="http://en.wikipedia.org/wiki/AOL">AOL</a> or you use an anonymity service such as <a href="http://tor.eff.org/">TOR</a>). Changing browsers will initiate a new http session anyway, and changing your user-agent String is rare. It can be done in Firefox using the <a href="http://www.mozillazine.org/misc/about:config/">about:config page</a> but that&#8217;s not a thing that users do everyday.</p>
<p>Note that session impersonation protection is hard (impossible?) to do when people use the same IP. That can be the case in universities, companies and netcafes.</p>
<p>Here is the doFilter method of an http filter which you can use to protect your application from session impersonation attacks. It will invalidate the session when this happens.</p>
<pre>if (request instanceof HttpServletRequest) {
  HttpServletRequest httpRequest = (HttpServletRequest)request;
  // get the session, without creating one if there isn't any
  HttpSession session = httpRequest.getSession(false);
  // if there is a session
  if (session!=null) {
    //calculate a hash using ip and user agent
    String hash = getHash(httpRequest.getRemoteAddr(),
        httpRequest.getHeader(USER_AGENT_KEY));
    if (session.getAttribute(HASH_KEY)==null) {
      // put hash in session
      session.setAttribute(HASH_KEY, hash);
    } else {
      // session does not contain hash
      if(!hash.equals(session.getAttribute(HASH_KEY))) {
        // TODO log session impersonation attempt?
        session.invalidate();
      }
    }
  }
}
chain.doFilter(request, response);</pre>
<p>The getHash method could just return the two Strings concatenated, but ideally you should hash them.</p>
<pre>public static final String getHash(String ip, String agent) {
  return Integer.toString(ip.hashCode()) + agent.hashCode();
}</pre>
<p>MD5 would be good but usually it&#8217;s costly. Here I just used String#hashCode.<br />
You&#8217;ll also need two constants for the filter:</p>
<pre>public static final String HASH_KEY = "HASH";
public static final String USER_AGENT_KEY = "user-agent";</pre>
<p>Thats it. Set the filter on the top of your filters chain and you are ready.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/an-http-session-impersonation-protection-filter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Java Code Conventions</title>
		<link>http://blog.cherouvim.com/java-code-conventions/</link>
		<comments>http://blog.cherouvim.com/java-code-conventions/#comments</comments>
		<pubDate>Sat, 24 Feb 2007 12:49:05 +0000</pubDate>
		<dc:creator>cherouvim</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[workplace]]></category>

		<guid isPermaLink="false">http://blog.cherouvim.com/java-code-conventions/</guid>
		<description><![CDATA[When writing Java code you must follow the Code Conventions for the Java Programming Language. It&#8217;s a document written by Sun back in 1997. Why should you read such an ancient (in computer science terms) document?
Code conventions are important to programmers for a number of reasons:
* 80% of the lifetime cost of a piece of [...]]]></description>
			<content:encoded><![CDATA[<p>When writing Java code you <strong>must</strong> follow the <a href="http://java.sun.com/docs/codeconv/">Code Conventions for the Java Programming Language</a>. It&#8217;s a document written by <a href="http://www.sun.com/" title="Sun Microsystems, Inc.">Sun</a> back in 1997. Why should you read such an ancient (in computer science terms) document?</p>
<blockquote><p>Code conventions are important to programmers for a number of reasons:</p>
<p>* 80% of the lifetime cost of a piece of software goes to maintenance.<br />
* Hardly any software is maintained for its whole life by the original author.<br />
* Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.</p></blockquote>
<p>Some people tell me that they cannot change their style, because that&#8217;s how they are used to coding. Fair enough. Do whatever you want when coding alone, in your home. But not in a professional environment. If you can&#8217;t be bothered, do us a favour and leave. Resign. Start selling popcorn. Whatever.</p>
<p>I have a serious problem working with people who commit Java code which looks like this:</p>
<pre>public class persons {</pre>
<pre>public void PersonSave(persons p) {</pre>
<pre>package foo.bar.personUtils;</pre>
<pre>public static final String foo = "whatever";</pre>
<pre>whatever()
{
  // do stuff
}</pre>
<p>Seriously, please try to read the following piece of code found in a real life project. Does this look like Java?</p>
<pre>if(e&lt;0.0)d= -d;
if(d!=0.0)for(int i=0;i&lt;dim;i++)this.n[i] = this.n[i]/d;
else this.tW.writeString("normalise: non simplex");
Object leftList = null, rightList = null;
try{ leftList = c.newInstance();}
catch(Exception e){tW.writeString("sort:error1 ");return null;};</pre>
<p>OK, this style might have been good in an <a href="http://en.wikipedia.org/wiki/Obfuscated_code">Obfuscated Code</a> Contest, but it definitely does not get you going in the workplace.</p>
<p>Good luck</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/java-code-conventions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Database Connection Pooling</title>
		<link>http://blog.cherouvim.com/database-connection-pooling/</link>
		<comments>http://blog.cherouvim.com/database-connection-pooling/#comments</comments>
		<pubDate>Fri, 23 Feb 2007 19:51:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[jdbc]]></category>

		<category><![CDATA[jndi]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://localblog.cherouvim.com/database-connection-pooling/</guid>
		<description><![CDATA[You are building a webapp. You want database connectivity. You want pooling (because its an expensive resource). You start building your own database connection pool. STOP!!!
Who told you that you can do it well? Why did you hack your own connection pool implementation, which is seriously broken, spawns thousands of threads and turns the server [...]]]></description>
			<content:encoded><![CDATA[<p>You are building a webapp. You want database connectivity. You want pooling (because its an expensive resource). You start building your own database connection pool. <strong>STOP!!!</strong></p>
<p>Who told you that you can do it well? Why did you hack your own connection pool implementation, which is seriously broken, spawns thousands of threads and turns the server into a miserable piece of shit that needs restart every 24h?</p>
<p>Wrong choice my friend. Next time do us all a favour and use one of the following:</p>
<ul>
<li><a href="http://jakarta.apache.org/commons/dbcp/" title="DBCP">DBCP</a></li>
<li><a href="http://sourceforge.net/projects/c3p0" title="c3p0">c3p0</a></li>
<li><a href="http://proxool.sourceforge.net/" title="proxool">proxool</a></li>
</ul>
<p>And don&#8217;t forget: The standard idiom for releasing a connection is to close (return) the connection in a finally block.</p>
<pre>Connection con = getPooledConnectionFromSomewhere();
try {
  // do stuff with connection
} catch (SQLException e) {
  // handle problems
} finally {
  con.close();
}</pre>
<p>Of course, closing the connection can throw an SQLException, but it&#8217;s up to you on how you will handle it.</p>
<p>Good luck</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/database-connection-pooling/feed/</wfw:commentRss>
		</item>
		<item>
		<title>DataSource exposed through JNDI</title>
		<link>http://blog.cherouvim.com/javax-sql-datasource-exposed-through-jndi/</link>
		<comments>http://blog.cherouvim.com/javax-sql-datasource-exposed-through-jndi/#comments</comments>
		<pubDate>Fri, 23 Feb 2007 19:36:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[jdbc]]></category>

		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://localblog.cherouvim.com/javaxsqldatasource-exposed-through-jndi/</guid>
		<description><![CDATA[You are building a webapp and you want database connection pooling. Your container can help you manage this javax.sql.DataSource by configuring it and exposing it through the JNDI tree.
Containers usually come with Jakarta Commons DBCP out of the box. In order to use it edit the context.xml file of your webapp and set your datasource [...]]]></description>
			<content:encoded><![CDATA[<p>You are building a webapp and you want database connection pooling. Your container can help you manage this javax.sql.DataSource by configuring it and exposing it through the JNDI tree.</p>
<p>Containers usually come with Jakarta Commons <a href="http://jakarta.apache.org/commons/dbcp/" title="Database Connection Pool">DBCP</a> out of the box. In order to use it edit the context.xml file of your webapp and set your datasource there.</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 &lt;Context path="/foo-app"&gt;

 &lt;!-- TOMCAT 5.5.xx DESCRIPTOR --&gt;
 &lt;Resource name="foo"
  auth="Container"
  type="javax.sql.DataSource"
  maxActive="1"
  maxIdle="1"
  maxWait="3000"
  username="user"
  password="pass"
  driverClassName="net.sourceforge.jtds.jdbc.Driver"
  url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=foobar;charset=utf8"
 /&gt;

 &lt;!-- TOMCAT 5.0.28 DESCRIPTOR --&gt;
 &lt;!--
  &lt;Resource name="foo" type="javax.sql.DataSource"/&gt;
  &lt;ResourceParams name="foo"&gt;
   &lt;parameter&gt;&lt;name&gt;maxActive&lt;/name&gt;&lt;value&gt;1&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;maxIdle&lt;/name&gt;&lt;value&gt;1&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;maxWait&lt;/name&gt;&lt;value&gt;3000&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;username&lt;/name&gt;&lt;value&gt;user&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;password&lt;/name&gt;&lt;value&gt;pass&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;driverClassName&lt;/name&gt;&lt;value&gt;net.sourceforge.jtds.jdbc.Driver&lt;/value&gt;&lt;/parameter&gt;
   &lt;parameter&gt;&lt;name&gt;url&lt;/name&gt;&lt;value&gt;jdbc:jtds:sqlserver://localhost:1433;DatabaseName=foobar;charset=utf8&lt;/value&gt;&lt;/parameter&gt;
  &lt;/ResourceParams&gt;
  --&gt;

&lt;/Context&gt;</pre>
<p>Tomcat 5.0.xx and 5.5.xx uses different xml syntax for most of it&#8217;s configuration. Here I&#8217;m presenting both with 5.0.xx&#8217;s block commented out.</p>
<p>So, you&#8217;ve got your app called &#8220;foo-app&#8221;. Next time you&#8217;ll deploy it, tomcat will copy context.xml to ${catalina.home}/conf/localhost/foo-app.xml where from it will be reading the configuration on each context or container initialization. Your Datasource is called &#8220;foo&#8221;.<br />
In your java code now, what you need to get a reference to &#8220;foo&#8221; is:</p>
<pre>Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/foo");</pre>
<p>And then something along the lines:</p>
<pre>Connection con = ds.getConnection();</pre>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cherouvim.com/javax-sql-datasource-exposed-through-jndi/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
