<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christian Schenk&#187; Programming</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ideas about clean if statements</title>
		<link>http://www.christianschenk.org/blog/ideas-clean-if-statements/</link>
		<comments>http://www.christianschenk.org/blog/ideas-clean-if-statements/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 03:40:51 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[statements]]></category>
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=59</guid>
		<description><![CDATA[If you&#8217;re reading a lot of code you may get to the point where you&#8217;d like it to be clean, so it&#8217;s easier for you to read. There&#8217;re a lot of resources about the beauty of code around and in this post I&#8217;d like to share some ideas about writing concise if statements.
Although these ideas [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re reading a lot of code you may get to the point where you&#8217;d like it to be clean, so it&#8217;s easier for you to read. There&#8217;re a lot of resources about the beauty of code around and in this post I&#8217;d like to share some ideas about writing concise <code>if</code> statements.</p>
<p>Although these ideas may apply to a lot of different programming languages I chose to give examples written in Java, PHP or Python. There should be no problem to translate this into other, similar languages.</p>
<p><span id="more-59"></span></p>
<h2>Indentation hell</h2>
<p>A lot of years ago I read a text file describing the preferred coding style for the Linux kernel: it said that you&#8217;re screwed anyway if you need more than three levels of indentation. This stems from hackers using a screen resolution that allows a certain number of characters only. Writing code that uses a lot of nested statements causes the problem that you can&#8217;t read everything on a 80&#215;25 terminal; wrapping lines may make things worse.</p>
<p>Back then the solution was to either increase the screen&#8217;s resolution <em>or</em> write clean code. Even today the latter is a good idea not only when writing code but maybe configuration files in general. Imagine a sysadmin in the server room with an old monitor only &#8211; he&#8217;ll thank you that you formatted the comments with a text width of 80 characters or less.</p>
<p>Let&#8217;s have a look at an example like the following. You&#8217;ll notice that I&#8217;ve use four levels of indentation: a for loop, an if statement, another for loop and another if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> x<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> z<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// do something special</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can spot this pattern by looking at it&#8217;s tail: a lot of closing curly brackets. Refactoring this snippet consists of splitting the code into various functions and calling them at the right time. One solution might look like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> x<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> y<span style="color: #009900;">&#41;</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> z<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> do_something_special<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> do_something_special<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// do something special</span>
  <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, the code is much easier to read and unit testing it will be more fun too. Don&#8217;t be afraid of adding <em>extra</em> functions into your program when you could write it all into one method/function. The latter is harder to maintain and rest assured that more method/function calls won&#8217;t add a serious performance penalty to your application in any way.</p>
<h2>Inverting</h2>
<p>It&#8217;s likely that you&#8217;ve seen a method that starts with an if statement, almost all the method&#8217;s code is inside that statement and at the end of the method you find the else block with only one statement in it, maybe some error handling code.</p>
<p>Instead of writing if statements that span multiple pages and contain lots and lots of code the following technique allows you to have the if statement on one line. As discussed above this avoids an unnecessary level of indentation as well.</p>
<p>Let&#8217;s have a look at an example: a simple method containing an if statement with another if statement which holds the real code of the method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> doIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something_else <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;"># do something...
</span>      <span style="color: #666666; font-style: italic;"># ...
</span>    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Simply by inverting the if statements we can write them on one line, producing the following, much cleaner code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> doIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something_else <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;"># do something...
</span>  <span style="color: #666666; font-style: italic;"># ...
</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, not just the statement but the whole method is easier to read. If you&#8217;re checking preconditions, this is the way to go. This also prevents the indentation hell mentioned above.</p>
<h2>Splitting</h2>
<p>Speaking of checks for the preconditions of certain methods, one might tend to put all checks into one single if statement. This may save you from e.g. writing code to throw an exception more than once but may result in a hard to understand if statement.</p>
<p>I recommend writing if statements that test only one or two conditions. Have a look at the following code which contains a giant if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">10</span> <span style="color: #ff7700;font-weight:bold;">or</span> a <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">20</span> <span style="color: #ff7700;font-weight:bold;">or</span> \
     b == <span style="color: #008000;">False</span> <span style="color: #ff7700;font-weight:bold;">or</span> \
     c == <span style="color: #483d8b;">'unknown'</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Invalid arguments'</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># do something...</span>
  <span style="color: #808080; font-style: italic;"># ...</span></pre></div></div>

<p>We could argue that the code is pretty clean and easy to understand. True in this case, but the semantics aren&#8217;t obvious. What about introducing two more methods and raising more specific exceptions? Would result in this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> inRange<span style="color: black;">&#40;</span>a, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Argument a is out of bounds'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> isValid<span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> isValid<span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Argument b or c are invalid'</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># do something...</span>
  <span style="color: #808080; font-style: italic;"># ...</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> inRange<span style="color: black;">&#40;</span>a, start, end<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">return</span> a <span style="color: #66cc66;">&gt;</span>= start <span style="color: #ff7700;font-weight:bold;">and</span> a <span style="color: #66cc66;">&lt;</span>= end
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> isValid<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span> a == <span style="color: #008000;">False</span> <span style="color: #ff7700;font-weight:bold;">or</span> a == <span style="color: #483d8b;">'unknown'</span> <span style="color: black;">&#41;</span></pre></div></div>

<p>This may bloat your code a little bit but helps you reading the code almost like a sentence. Splitting one large if statement into smaller ones may result in sensible error messages too. And the introduction of small helper methods makes unit testing fun again.</p>
<h2>Sorting</h2>
<p>If you&#8217;re using the previous technique &#8211; splitting a single if statement into smaller, more concise portions &#8211; you should consider the order of the if statements. Let&#8217;s think about a simple example: in a <a href="/blog/friday-13th-joda-time/">recent post</a> I tried finding occurrences of Friday the 13th dates by iterating over a given time span. The method looked like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/* ... */</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    date <span style="color: #339933;">=</span> date.<span style="color: #006633;">plusDays</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getDayOfWeek</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> DateTimeConstants.<span style="color: #006633;">FRIDAY</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The two if statements test whether the current day is a Friday the 13th date. First checking whether the current day is the 13th day of a month speeds things up a little bit because we&#8217;ll only check whether this day happens to be a friday &#8211; the other way around would be more expensive in terms of performance.</p>
<p>This technique also applies when your if statements call <em>expensive</em> methods to test a certain condition. You may want to call the ones with the least overhead first thus reducing the time spent in your if statements.</p>
<h2>Conclusion</h2>
<p>In this post I presented some simple solutions that should help you writing cleaner, better code regarding if statements. Following these rules will make your code much easier to read and to maintain. Just give it a try and you&#8217;ll definitely notice the positive difference.</p>
<p>Sure, I haven&#8217;t given any references to real projects helping you to verify that the examples given in this post exist in real projects. I think searching in real projects for code like the above shouldn&#8217;t be much of a problem. And I&#8217;m pretty sure you&#8217;ll agree on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/ideas-clean-if-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>German translation for xRecurseDiff</title>
		<link>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/</link>
		<comments>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/#comments</comments>
		<pubDate>Tue, 06 May 2008 06:09:06 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[translation]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/german-translation-for-xrecursediff/</guid>
		<description><![CDATA[If you'd like to compare the files from two directories you may want to try xRecurseDiff that comes with a German translation now]]></description>
			<content:encoded><![CDATA[<p>I was searching for a simple Windows application that compares the files of two directories. This comes in handy if you&#8217;ve downloaded an update to some Open Source software and want to know what exactly changed in the new version. So I found <a href="http://www.matteolucarelli.net/xrecursediff/index_en.htm">xRecurseDiff</a> and it was perfect for my situation.</p>
<p>Since there wasn&#8217;t a German translation for it I created one and the author, Matteo Lucarelli, integrated it into a new release. If you&#8217;re from Germany and don&#8217;t speak English you may want to give this software a shot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterating over the characters in a string</title>
		<link>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</link>
		<comments>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 13:20:19 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[characters]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</guid>
		<description><![CDATA[Evaluates the fastest method to iterate over the characters in a string with Java]]></description>
			<content:encoded><![CDATA[<p>I was wondering what would be the fastest method to iterate over the characters in a string with Java. A small test implements the following things:</p>
<ul>
<li>using an Iterable</li>
<li><code>toCharArray()</code></li>
<li><code>charAt()</code></li>
<li>calling <code>charAt()</code> on a <code>CharSequence</code></li>
</ul>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.zip">zip</a> or browse the code online <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/xref/">here</a>; have a look at the <code>StringIteratorTest</code> class first.</p>
<p><span id="more-88"></span></p>
<h2>Results</h2>
<p>Without further ado here are the results:</p>
<table>
<tr>
<th>Variant</th>
<th>Time in ms.</th>
</tr>
<tr>
<td>Iterator</td>
<td>5.5</td>
</tr>
<tr>
<td><code>toCharArray()</code></td>
<td>1.1</td>
</tr>
<tr>
<td><code>charAt()</code></td>
<td>1.6</td>
</tr>
<tr>
<td><code>CharSequence.charAt()</code></td>
<td>2.2</td>
</tr>
</table>
<p><img src="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/stringIterator.jpg" alt="Bar chart of the test results"/></p>
<h2>Conclusion</h2>
<p>If speed is what you want you shouldn&#8217;t use Iterators but one of the other solutions instead. On the other hand if you <em>just like</em> to play with Iterators and classes that implement <code>Iterable</code> you may want to choose the <em>slower</em> solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching with dynamic proxy classes</title>
		<link>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/</link>
		<comments>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 08:25:10 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/</guid>
		<description><![CDATA[Read about a solution to cache objects with a dynamic proxy class]]></description>
			<content:encoded><![CDATA[<p>In my <a title="Caching with AspectJ" href="http://www.christianschenk.org/blog/caching-with-aspectj/">last post</a> I used AspectJ to implement a cache that stored the returned result of methods with a special annotation (<code>@Cachable</code>). If you can&#8217;t use AspectJ you may want to use a dynamic proxy class: in this post I&#8217;ll present a solution for this.</p>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/CachingWithProxyInstances-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/CachingWithProxyInstances-1.0.zip">zip</a> file or view the code online <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/xref/">here</a>.</p>
<p><span id="more-82"></span></p>
<h2>Implementation</h2>
<p>If you don&#8217;t know how <a title="Dynamic Proxy Classes" href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html">dynamic proxy classes</a> work here&#8217;s a short overview. Let&#8217;s say you want to do some extra work if the methods <code>foo</code> and <code>bar</code> from the class <code>Tee</code> are called. You would extract the methods into an interface and let <code>Tee</code> implement this interface.</p>
<p>Next, you&#8217;d implement a factory that produces a proxy instance for <code>Tee</code> with a custom <code>InvocationHandler</code>. This handler would have a look at the method&#8217;s name and check whether it&#8217;s <code>foo</code> or <code>bar</code>: you can now implement any extra actions in this handler.</p>
<p>You can also examine the annotations of the invoked method and that&#8217;s what I did: if the method has got the <code>@Cachable</code> annotation we&#8217;ll utilize a cache. But how do we know whether we can safely return an object from the cache?</p>
<h3>Constructing a unique method identifier</h3>
<p>This is crucial since we don&#8217;t want to return the same result from the cache if the method was called with different parameters. So we&#8217;ll have to add the values of the parameters to a identifier like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;package-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;class-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;method-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;param1-param2-[...]&quot;</span></pre></div></div>

<p>This way we&#8217;ll create a unique entry in the cache for different method calls.</p>
<h2>How to</h2>
<p>All we have to do is to add <code>@Cachable</code> to some methods:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Foo <span style="color: #009900;">&#123;</span>
  @Cachable
  <span style="color: #000000; font-weight: bold;">public</span> SomeObject foo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  @Cachable
  <span style="color: #000000; font-weight: bold;">public</span> AnotherObject bar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param1, <span style="color: #000066; font-weight: bold;">long</span> param2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once we&#8217;ve done that we can use the factory to produce a new proxy instance with our custom <code>InvocationHandler</code>. The handler will use the cache, i.e. the method calls will return faster.</p>
<h2>Conclusion</h2>
<p>In this post I presented a simple solution for a cache that may speed up method calls. Although I recommend using AspectJ for this kind of job you can use dynamic proxy instances if your environment (in most cases read: your project leader) doesn&#8217;t permit you to use AspectJ.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Caching with AspectJ</title>
		<link>http://www.christianschenk.org/blog/caching-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/caching-with-aspectj/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 08:28:59 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/caching-with-aspectj/</guid>
		<description><![CDATA[Shows how to use AspectJ and annotations to implement a simple cache that speeds up your application]]></description>
			<content:encoded><![CDATA[<p>In my <a title="Implementing a simple cache with Java" href="http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/">last post</a> I presented a very simple cache that stores objects. Now I want to use this cache with <a href="http://www.eclipse.org/aspectj/">AspectJ</a> in a small sample application.</p>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.zip">zip</a> file or browse the code online <a href="http://data.christianschenk.org/caching-with-aspectj/xref/">here</a>.</p>
<p><span id="more-72"></span></p>
<h2>Implementing the aspect</h2>
<p>I decided to use an annotation and an aspect with a pointcut that matches this annotation. All I had to do was to annotate the methods that should be cached.</p>
<p>Here&#8217;s the very generic version of a pointcut that matches the execution of all methods annotated with <code>@Cachable</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(@Cachable * *.*(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> cache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The logic is this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cache()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> aroundProfileMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// construct genericIdentifier</span>
  <span style="color: #003399;">Object</span> obj <span style="color: #339933;">=</span> cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>genericIdentifier<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    obj <span style="color: #339933;">=</span> thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>genericIdentifier, obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> obj<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Everytime a cachable method is executed we&#8217;ll first have a look at the cache. If there&#8217;s a cache hit we&#8217;ll immediately return the object. If there&#8217;s no such object in the cache, we&#8217;ll call the cachable method, put the returned object in the cache and finally return the object.</p>
<p>The <code>genericIdentifier</code> is a string consisting of the package-, class- and method-name and the supplied arguments. For a method named <code>foo</code> from a class <code>bar</code> inside a package called <code>baz</code> this might result in:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;baz.bar.foo-Integer-42&quot;</span></pre></div></div>

<p>The method <code>foo</code> takes one argument and at the time of this execution it was <code>42</code>. This way we&#8217;ll only return objects from the cache if this <code>generic identifier</code> matches.</p>
<h2>Using the aspect</h2>
<p>All we have to do is to use the <code>@Cachable</code> annotation. Have a look at it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> Cachable <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>It can be used with any method that returns something:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Cachable
<span style="color: #000000; font-weight: bold;">public</span> BigObject getObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> BigObject<span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first time this method is called we&#8217;ll store the object in the cache. If it&#8217;s called again with the same parameter we&#8217;ll return the object from the cache.</p>
<h2>Conclusion</h2>
<p>Implementing a cache is simple and so is using it with AspectJ. I presented a straightforward way to speed up any application that deals with <em>big</em> objects which take a long time to load. All you have to do is to use the <code>@Cachable</code> annotation along with the aspect. Implementing another cache in the aspect should be simple, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/caching-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing a simple cache with Java</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/</link>
		<comments>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 13:38:24 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/</guid>
		<description><![CDATA[I present a simple cache written in Java that can be used to store objects of any kind]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll present a simple cache written in Java. It can be used to store objects of any kind or specific types only. The objects will expire after a certain time that can be customized too. Since this cache is really simple I didn&#8217;t implement a caching strategy nor an option for a maximum capacity.</p>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/SimpleCache-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/SimpleCache-1.0.tar.gz">zip</a> or browse the code online <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/xref/">here</a>.</p>
<p><span id="more-70"></span></p>
<h2>Basic operations</h2>
<p>The cache supports these basic operations: you can <code>put</code> and <code>get</code> objects. If you put an object you can specify a time in seconds &#8211; after this time period the object will be remove from the cache.</p>
<p>Getting an object from the cache is simple too and you&#8217;ve also got the possibility to specify the type of the object you&#8217;re retrieving from the cache &#8211; this adds some type safety.</p>
<p>Have a look at this sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SimpleCache<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> intCache <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCache<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intCache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>, intCache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This creates a <code>SimpleCache</code> that holds objects derived from <code>Integer</code>. Since you parameterized this cache with <code>Integer</code> you&#8217;ll only be able to <code>put</code> and <code>get</code> integers.</p>
<p>If you want to save all different kinds of objects in the cache that&#8217;s no problem at all. If you do this you can use the convenience method <code>get</code> which performs a cast for you. Have a look at this example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SimpleCache cache <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str&quot;</span>, <span style="color: #0000ff;">&quot;Some string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>, cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #003399;">Integer</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Some string&quot;</span>, cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, I think it&#8217;s better to create several instances of <code>SimpleCache</code> that stores objects of a specific type.</p>
<h2>Removing expired objects</h2>
<p>Once an object is expired we want to remove it from the cache. The <code>SimpleCache</code> class supports two ways of removing old objects:</p>
<ol>
<li>if the <code>get</code> method is called we&#8217;ll check whether the object that&#8217;s about to be returned is expired. If that&#8217;s the case we&#8217;ll remove it from the cache.</li>
<li>a thread that runs periodically removes expired objects from the cache.</li>
</ol>
<p>Both features reside in implementations of <code>Runnable</code> so they can be executed in separate threads. This way the basic operations will not be slowed down by this kind of housekeeping.</p>
<p>With Java 5 it&#8217;s easy to schedule recurring tasks with the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executors.html">Executors</a> class. To remove expired objects from the cache we can use this code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Runnable</span> cleaner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* remove expired objects here */</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Executors.<span style="color: #006633;">newScheduledThreadPool</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">scheduleWithFixedDelay</span><span style="color: #009900;">&#40;</span>cleaner, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">30</span>, TimeUnit.<span style="color: #006633;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>I presented a very simple cache written in Java that stores objects of any kind. It&#8217;s configurable, type safe and easy to use; it should be pretty easy to integrate this cache into an existing project.</p>
<p>I&#8217;ve tried to keep the cache fast: certain tasks will be executed in threads to speed things up. But don&#8217;t worry because the threads are easy to maintain and to understand.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Monitor and administer Apache Tomcat with Lambda Probe</title>
		<link>http://www.christianschenk.org/blog/monitor-apache-tomcat-lambda-probe/</link>
		<comments>http://www.christianschenk.org/blog/monitor-apache-tomcat-lambda-probe/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 06:21:54 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lambda probe]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/monitor-and-administer-apache-tomcat-with-lambda-probe/</guid>
		<description><![CDATA[This post summarizes the functionality of Lambda Probe: the ultimate tool for monitoring and management of Apache Tomcat instance in real time]]></description>
			<content:encoded><![CDATA[<p>If you want to monitor and administer web applications that&#8217;re running inside Apache <a title="Apache Tomcat" href="http://tomcat.apache.org/">Tomcat</a> you should have a look at <a title="Lambda Probe for Apache Tomcat" href="http://www.lambdaprobe.org/">Lambda Probe</a>: it&#8217;s a small <abbr title="Web Application Archive">WAR</abbr> that can be dropped inside your Tomcat instance and once you&#8217;ve done that you can use it to perform administrative tasks.</p>
<p><span id="more-64"></span></p>
<h2>Installation</h2>
<p>The <a title="Install Lambda Probe" href="http://www.lambdaprobe.org/d/installation.shtml#tomcat">installation</a> for Lambda Probe is pretty easy: <a title="Download Lambda Probe" href="http://www.lambdaprobe.org/d/download.htm">download</a> <code>probe.war</code> and copy it to <code>$CATALINA_HOME/webapps/</code>.</p>
<p>Once you&#8217;ve done that make sure that the user <code>tomcat</code> has the role <code>manager</code>. Have a look at your <code>tomcat-users.xml</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tomcat-users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">rolename</span>=<span style="color: #ff0000;">&quot;manager&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">rolename</span>=<span style="color: #ff0000;">&quot;poweruser&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">rolename</span>=<span style="color: #ff0000;">&quot;poweruserplus&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">rolename</span>=<span style="color: #ff0000;">&quot;probeuser&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tomcat&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;tomcat&quot;</span> <span style="color: #000066;">roles</span>=<span style="color: #ff0000;">&quot;manager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tomcat-users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can use the other roles to configure access to Lambda Probe. Have a look at this <a href="http://www.lambdaprobe.org/d/installation.shtml">page</a> if you want to do that.</p>
<p>If you want to have a look at the memory graphs and everything JMX related you&#8217;ll have to enable <a title="Enable JMX Remote" href="http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html#Enabling%20JMX%20Remote">JMX Remote</a>. This comes down to these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CATALINA_OPTS</span>=<span style="color: #ff0000;">&quot;-Dcom.sun.management.jmxremote 
       -Dcom.sun.management.jmxremote.port=9080 
       -Dcom.sun.management.jmxremote.ssl=false 
       -Dcom.sun.management.jmxremote.authenticate=false&quot;</span></pre></div></div>

<p>That&#8217;s it. Now you&#8217;re ready to use Lambda Probe.</p>
<h2>Conclusion</h2>
<p>If you want to monitor and administer web applications inside Apache Tomcat, Lambda Probe seems to be a very nice solution. I recommend to definitely give it a try.</p>
<p><em>Update</em>: There&#8217;s a pretty active fork of Lambda Probe called PSI Probe around. I recommend using <a href="http://code.google.com/p/psi-probe/">this</a> instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/monitor-apache-tomcat-lambda-probe/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Suppress logging for Velocity</title>
		<link>http://www.christianschenk.org/blog/suppress-logging-for-velocity/</link>
		<comments>http://www.christianschenk.org/blog/suppress-logging-for-velocity/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 08:12:12 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Velocity]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/suppress-logging-for-velocity/</guid>
		<description><![CDATA[Shows how to suppress logging for Velocity altogether by implementing LogSystem or LogChute]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t want Velocity to log anything, you can change its default logging behavior. All you have to do is to implement an interface and set a property before you call <code>Velocity.init()</code>.</p>
<p>Depending on the version of Velocity you&#8217;re using you&#8217;ll have to implement either <code>LogSystem</code> or <code>LogChute</code>.</p>
<p><span id="more-53"></span></p>
<h2>Howto</h2>
<p>If you&#8217;re using Velocity <em>1.4 or older</em> use this class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.velocity.runtime.RuntimeServices</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.velocity.runtime.log.LogSystem</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> VelocityNoOutputLogger <span style="color: #000000; font-weight: bold;">implements</span> LogSystem <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>RuntimeServices arg0<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logVelocityMessage<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> arg0, <span style="color: #003399;">String</span> arg1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you&#8217;re using Velocity <em>1.5 or newer</em> use this class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.velocity.runtime.RuntimeServices</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.velocity.runtime.log.LogChute</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> VelocityNoOutputLogger <span style="color: #000000; font-weight: bold;">implements</span> LogChute <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>RuntimeServices arg0<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isLevelEnabled<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> arg0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> log<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> arg0, <span style="color: #003399;">String</span> arg1, <span style="color: #003399;">Throwable</span> arg2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> log<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> arg0, <span style="color: #003399;">String</span> arg1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally you&#8217;ll have to tell Velocity about the new logger:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Velocity.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span>Velocity.<span style="color: #006633;">RUNTIME_LOG_LOGSYSTEM</span>,
                     <span style="color: #000000; font-weight: bold;">new</span> VelocityNoOutputLogger<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Velocity.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>We&#8217;ve implemented a simple way to suppress logging altogether. This comes in handy sometimes, so decide whether you want to use it too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/suppress-logging-for-velocity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Bean mapper performance tests</title>
		<link>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/</link>
		<comments>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/#comments</comments>
		<pubDate>Sun, 02 Dec 2007 15:49:40 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[beans]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[dozer]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/</guid>
		<description><![CDATA[Performance comparison between Java Introspector, Commons BeanUtils, Spring BeanUtils and Dozer]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my <a title="Performance tests for introspection of JavaBeans" href="http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/">tests</a> with the <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> from the Java API, I did some more testing with the following Java Bean to Java Bean mappers:</p>
<ul>
<li><a title="Commons - BeanUtils" href="http://commons.apache.org/beanutils/">Commons BeanUtils</a></li>
<li><a title="Spring Framework" href="http://www.springframework.org/">Spring BeanUtils</a></li>
<li><a title="Dozer - Java Bean to Java Bean mapper" href="http://dozer.sourceforge.net/">Dozer</a></li>
</ul>
<p>Furthermore I used my own <a title="Source code of the BeanIntrospector class" href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/xref/org/christianschenk/beanintrospect/BeanIntrospector.html">implementation</a> and an implementation that&#8217;s hardcoded by hand.</p>
<p>The Eclipse project with the sample code for this post can be downloaded as <a href="http://data.christianschenk.org/java-bean-mapper-performance-tests/BeanMappingPerfTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/java-bean-mapper-performance-tests/BeanMappingPerfTest-1.0.zip">zip</a> or can be viewed online <a title="BeanMappingPerfTest - Code Reference" href="http://data.christianschenk.org/java-bean-mapper-performance-tests/xref/">here</a>.</p>
<p><span id="more-38"></span></p>
<h2>Testing</h2>
<p>The performance test creates two beans: one is filled and the other gets filled; this is done over and over again in a <em>big for loop</em>. I coded a <em>by hand</em> implementation as a reference to check whether the process of creating all these new objects is slow. This implementation copies the properties from one bean to the other like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">bean2.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bean2.<span style="color: #006633;">setBar</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bean2.<span style="color: #006633;">setTee</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getTee</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All the other frameworks had to do the same thing. Have a look at the results (note that I set the y-axis to logarithmic scale):<br />
<img src="http://data.christianschenk.org/java-bean-mapper-performance-tests/perf.png" alt="Performance tests"/></p>
<p>Obviously there&#8217;s no problem with all the created objects: the <em>by hand</em> implementation just consists of the created objects and some method calls to copy the properties. This seems to be pretty fast compared to the other solutions.</p>
<p>Let us have a look at a comparison between the <em>by hand</em> implementation and the frameworks when they have to fill 100k objects:</p>
<table>
<tr>
<th></th>
<th><em>by Hand</em></th>
<th>Introspector</th>
<th>Commons</th>
<th>Spring</th>
<th>Dozer</th>
</tr>
<tr>
<td><em>by Hand</em></td>
<td>1</td>
<td>120</td>
<td>610</td>
<td>110</td>
<td>970</td>
</tr>
</table>
<p>For example: the <em>by hand</em> implementation beats Dozer by a factor of 970, i.e. it&#8217;s 970 times faster.</p>
<h2>Conclusion</h2>
<p>The results can be explained with the different richness of features. My Introspector and the implementation from the Springframework have very few features and are pretty fast. The Commons BeanUtils implementation has a lot of features (e.g. all this <a title="Google - Common BeanUtils - Dynamic Beans" href="http://www.google.de/search?q=site%3Acommons.apache.org%2Fbeanutils%2F+%22Dynamic+Beans%22&#038;btnI">DynaBean</a> stuff). And finally Dozer: I haven&#8217;t looked at the code yet, but it seems to be very powerful.</p>
<p>So once more it&#8217;s a trade-off between speed and features. Obviously you can&#8217;t have both so decide which one your application needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Singletons with AspectJ</title>
		<link>http://www.christianschenk.org/blog/singletons-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/singletons-with-aspectj/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 11:18:41 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/singletons-with-aspectj/</guid>
		<description><![CDATA[Although I wouldn't recommend it this post shows you how to implement the Singleton-Pattern with AspectJ 5]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://www.stumbleupon.com/url/www.sophisticated-it.de/blog/index.php/2007/07/17/singleton-pattern-mit-aspectj/">stumbled</a> upon this <a title="Singleton-Pattern mit AspectJ" rel="nofollow" href="http://www.sophisticated-it.de/index.php/2007/07/17/singleton-pattern-mit-aspectj/">post</a> (<em>german</em>) that shows how to implement the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>. Although you may want to use <a title="Category: Dependency Injection" href="/blog/tag/dependency-injection/">dependency injection</a> with <a title="Springframework.org" href="http://www.springframework.org/">Spring</a> or <a title="google-guice" href="http://code.google.com/p/google-guice/">Guice</a>, there might be (hopefully rare) occasions on which you opt for this solution. In this post we&#8217;ll check out how to do this with AspectJ 5.</p>
<p>The Eclipse project with the sample code for this post can be downloaded as <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">here</a>.</p>
<p><span id="more-37"></span></p>
<h2>Classic singleton pattern</h2>
<p>The traditional way to implement a singleton consists of a <code>private</code> constructor, a <code>static final</code> field that holds the instance of the class and a <code>getInstance</code> method that returns this instance. In Java this looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Singleton <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Singleton singleton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Singleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> singleton<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This gets really annoying if you&#8217;ve got this code more than once in your application. So lets have a look at an aspect that resembles this functionality.</p>
<h2>Implementing the aspect</h2>
<p>If you&#8217;ve got more than one singleton in your application you may think of a singleton as a crosscutting concern. Consequently, you&#8217;ll write an aspect that intercepts calls to the constructor of the singleton class and instead of creating a new instance every time you&#8217;ll return the instance that was created at the very fist call to the constructor.</p>
<p>The logic for this idea can be expressed with the following code fragment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;?&gt;</span>, Object<span style="color: #339933;">&gt;</span> singletons<span style="color: #339933;">;</span>
...
<span style="color: #006633;">Class</span><span style="color: #339933;">&lt;?&gt;</span> singletonClass <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDeclaringType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Object</span> singletonObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>singletonClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>singletonObject <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  singletonObject <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>singletonClass, singletonObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">return</span> singletonObject<span style="color: #339933;">;</span></pre></div></div>

<p>We have a map that holds references to all instances of our singleton classes. The method that intercepts the calls to the constructor of a given class stores a reference to the created instance in the map and returns that instance on the following calls to the constructor of the same class.</p>
<p>So, you can do the following in the code and will get references to the same instance of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Singleton s1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Singleton s2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// s1 == s2</span></pre></div></div>

<p>All that&#8217;s left to do, is to define a pointcut and to remove the boilerplate code from the singleton class. A pointcut can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call(*.SingletonAop.new(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> singletons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Conclusion</h2>
<p>We <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">implemented</a> a solution for the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a> 5. The used approach caches the instances of the singleton classes and returns the cached ones once the constructor has been called for the first time. Note: to simplify matters we left out thread safety.</p>
<p>Although this solution is somehow elegant and may be useful to clean up some code in a legacy system, we should consider the following points:</p>
<dl>
<dt>Can we use a dependency injection framework?</dt>
<dd>Spring uses singletons by default and with Guice you can mark singletons with the <code>@Singleton</code> annotation. So, we might want to choose one of these frameworks instead of implementing this functionality on our own.</dd>
<dt>Java skill of the team?</dt>
<dd>The presented implementation should be well documented, because nothing would be more irritating than a novice developer in your team who doesn&#8217;t know about this <em>magic</em> behind the scenes and relies on different instances of the <em>singleton</em> class (<em>&#8220;I thought <code>new</code> would be doing&#8230;&#8221;</em>).</dd>
</dl>
<p>There are <em>other</em> solutions available to this problem. So, I wouldn&#8217;t recommend to implement this kind of <em>caching</em> with AspectJ.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/singletons-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
