<?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>Tellaw.org</title>
	<atom:link href="http://en.tellaw.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.tellaw.org</link>
	<description>Coding and Programming will help us discovering the web world</description>
	<pubDate>Tue, 03 Jun 2008 15:49:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>CAPTCHA : Form Validation - Creation of Dynamic image in Java</title>
		<link>http://en.tellaw.org/2008/06/03/captcha-form-validation-creation-of-dynamic-image-in-java/</link>
		<comments>http://en.tellaw.org/2008/06/03/captcha-form-validation-creation-of-dynamic-image-in-java/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 15:32:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Java]]></category>

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=9</guid>
		<description><![CDATA[here is a sample application used to challenge users to be able to see if they are a bot or not. Subscription forms are always needing this kind of tools for server security. The goal is to build a picture containing text, and to compare user&#8217;s input with it.
It is very difficult to build a [...]]]></description>
			<content:encoded><![CDATA[<p>here is a sample application used to challenge users to be able to see if they are a bot or not. Subscription forms are always needing this kind of tools for server security. The goal is to build a picture containing text, and to compare user&#8217;s input with it.<br />
It is very difficult to build a bot able to go throught that kind of security (very difficult doesn&#8217;t mean impossible!).</p>
<p><span id="more-9"></span></p>
<p><strong>Here is a sample of generated picture :</strong></p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-43" title="getimage" src="http://en.tellaw.org/wp-content/uploads/2008/04/getimage.jpg" alt="" width="178" height="71" /></p>
<p><strong>How it works :</strong></p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-42" title="flowchart_captcha" src="http://en.tellaw.org/wp-content/uploads/2008/04/flowchart_captcha.gif" alt="" width="183" height="549" /></p>
<p><strong>To work is application is composed of :</strong></p>
<ul>
<pre>
<li>A servlet used to generate the picture and write its value inside session.</li>
<li>A JSP file used to compare session&#8217;s value and user input.</li>
</pre>
</ul>
<p>This sample is running and has been tested on Tomcat 5.5.7 JDK 5.</p>
<p><strong>1) The Servlet&#8217;s code :</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">/* * Created on 28 mars 2005 * */</span>
<span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">utils</span>.<span style="color: #006600;">dynimage</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import javax.servlet.ServletException;</span>
<span style="color: #a1a100;">import javax.servlet.http.*;</span>
<span style="color: #a1a100;">import java.util.*;</span>
<span style="color: #a1a100;">import java.awt.*;</span>
<span style="color: #a1a100;">import java.io.*;</span>
<span style="color: #a1a100;">import java.awt.image.*;</span>
<span style="color: #a1a100;">import java.awt.font.*;</span>
<span style="color: #a1a100;">import javax.imageio.*;</span>
<span style="color: #a1a100;">import java.awt.geom.*;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/** * @author Eric * */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetDynImage <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//System.out.println(&quot;Servlet started&quot;);</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//System.out.println(&quot;Servlet is stopping now&quot;);</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doPost<span style="color: #66cc66;">&#40;</span>HttpServletRequest arg0, HttpServletResponse arg1<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
		doGet<span style="color: #66cc66;">&#40;</span>arg0, arg1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #aaaadd; font-weight: bold;">String</span> text = getRandomText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// String saving in HTTP session</span>
		HttpSession session = request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		session.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dynImageTxt&quot;</span>, text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #aaaadd; font-weight: bold;">String</span> font_file = getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRealPath</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;WEB-INF/ttf/acmesa.ttf&quot;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #993333;">float</span> size = <span style="color: #cc66cc;">50</span>.0f<span style="color: #66cc66;">;</span>
		<span style="color: #aaaadd; font-weight: bold;">Color</span> background = <span style="color: #aaaadd; font-weight: bold;">Color</span>.<span style="color: #006600;">white</span><span style="color: #66cc66;">;</span>
		<span style="color: #aaaadd; font-weight: bold;">Color</span> color = <span style="color: #aaaadd; font-weight: bold;">Color</span>.<span style="color: #006600;">black</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #aaaadd; font-weight: bold;">Font</span> font = <span style="color: #aaaadd; font-weight: bold;">Font</span>.<span style="color: #006600;">createFont</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Font</span>.<span style="color: #006600;">TRUETYPE_FONT</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">FileInputStream</span><span style="color: #66cc66;">&#40;</span>font_file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			font = font.<span style="color: #006600;">deriveFont</span><span style="color: #66cc66;">&#40;</span>size<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #aaaadd; font-weight: bold;">BufferedImage</span> buffer = <span style="color: #000000; font-weight: bold;">new</span>       <span style="color: #aaaadd; font-weight: bold;">BufferedImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #aaaadd; font-weight: bold;">BufferedImage</span>.<span style="color: #006600;">TYPE_INT_RGB</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #aaaadd; font-weight: bold;">Graphics2D</span> g2 = buffer.<span style="color: #006600;">createGraphics</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			g2.<span style="color: #006600;">setRenderingHint</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">RenderingHints</span>.<span style="color: #006600;">KEY_ANTIALIASING</span>,       <span style="color: #aaaadd; font-weight: bold;">RenderingHints</span>.<span style="color: #006600;">VALUE_ANTIALIAS_ON</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #aaaadd; font-weight: bold;">FontRenderContext</span> fc = g2.<span style="color: #006600;">getFontRenderContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #aaaadd; font-weight: bold;">Rectangle2D</span> bounds = font.<span style="color: #006600;">getStringBounds</span><span style="color: #66cc66;">&#40;</span>text,fc<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// Trying to find the size of the picture</span>
			<span style="color: #993333;">int</span> width = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> bounds.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #993333;">int</span> height = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> bounds.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// preparing output</span>
			buffer = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">BufferedImage</span><span style="color: #66cc66;">&#40;</span>width, height, <span style="color: #aaaadd; font-weight: bold;">BufferedImage</span>.<span style="color: #006600;">TYPE_INT_RGB</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			g2 = buffer.<span style="color: #006600;">createGraphics</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			g2.<span style="color: #006600;">setRenderingHint</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">RenderingHints</span>.<span style="color: #006600;">KEY_ANTIALIASING</span>,       <span style="color: #aaaadd; font-weight: bold;">RenderingHints</span>.<span style="color: #006600;">VALUE_ANTIALIAS_ON</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			g2.<span style="color: #006600;">setFont</span><span style="color: #66cc66;">&#40;</span>font<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			g2.<span style="color: #006600;">drawString</span><span style="color: #66cc66;">&#40;</span>text,<span style="color: #cc66cc;">0</span>,<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>-bounds.<span style="color: #006600;">getY</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// Set content type et get de l'output stream</span>
			response.<span style="color: #006600;">setContentType</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;image/jpeg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #aaaadd; font-weight: bold;">OutputStream</span> os = response.<span style="color: #006600;">getOutputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// Output image as a JPG picture.</span>
			ImageIO.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span>buffer, <span style="color: #ff0000;">&quot;jpg&quot;</span>, os<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			os.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Exception</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Error catching... oups empty</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/***
	*
	* Method used to get a random UpperCase text of 5 letters.
	*
	* @return	String text.
	*/</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getRandomText <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creation of a random text</span>
		<span style="color: #993333;">int</span> length = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">;</span>
		<span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> array = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span> length <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
		<span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> chars = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">26</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//for (int i = 0; i &amp;lt; 26; i ++) {chars[i] = (char) (97 + i);chars[i + 26] = (char) (65 + i);}</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creation of an Array with Upper Caps letters.</span>
		<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #cc66cc;">26</span><span style="color: #66cc66;">;</span> i ++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			chars<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">97</span> + i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #aaaadd; font-weight: bold;">Random</span> random = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Random</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> length<span style="color: #66cc66;">;</span> i ++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			array<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = chars<span style="color: #66cc66;">&#91;</span>random.<span style="color: #006600;">nextInt</span> <span style="color: #66cc66;">&#40;</span>chars.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Default parameters</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">String</span> <span style="color: #66cc66;">&#40;</span>array<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>2) Validation of the input :</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">// Lecture de la session, de l'attribut captcha</span>
HttpSession session = request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #aaaadd; font-weight: bold;">String</span> value = <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#41;</span> session.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dynImageTxt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Reading &quot;imgValue&quot; field from input form.</span>
<span style="color: #aaaadd; font-weight: bold;">String</span> formValue = request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;imgValue&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>formValue.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Good value job here !!!!</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Bad value job here !!!!</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><em><br />
Be Aware, that if your server is behing a load balancing hardware, you should take care of keeping the user on the same server. If not, CAPTCHA would never be validated.</em></p>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/06/03/captcha-form-validation-creation-of-dynamic-image-in-java/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MAVEN - A Java build system</title>
		<link>http://en.tellaw.org/2008/05/22/maven-a-java-build-system/</link>
		<comments>http://en.tellaw.org/2008/05/22/maven-a-java-build-system/#comments</comments>
		<pubDate>Thu, 22 May 2008 13:10:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Java]]></category>

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=8</guid>
		<description><![CDATA[Maven will help you to automate boring tasks of a java project. It will take in charge librairies versions, Eclipse project files, builds, and unit testing validation. This article intends to give you an overview of Maven usage in a simple project. Instead of its old parent ANT, Maven wants you to describe tasks to [...]]]></description>
			<content:encoded><![CDATA[<p>Maven will help you to automate boring tasks of a java project. It will take in charge librairies versions, Eclipse project files, builds, and unit testing validation. This article intends to give you an overview of Maven usage in a simple project. Instead of its old parent ANT, Maven wants you to describe tasks to do, and not the way to do them. I believe this is a more interesting way to work.</p>
<p><span id="more-8"></span></p>
<p><strong>1) Installing maven :</strong><br/><br />
Installing Maven on Windows is really easy. It just needs to download the Install from the official website (<a href="http://maven.apache.org/" hreflang="fr">http://maven.apache.org/</a>), and to add its bin directory to your path. Now you can test your new Maven install with the following command line :<br/></p>

<div class="wp_syntax"><div class="code"><pre class="text">&gt;Maven</pre></div></div>

<p>During the installation, You have to creat Env variable for Maven :</p>
<blockquote><p>
<b>MAVEN_HOME</b> -&gt; C:\Program Files\Apache Software Foundation\Maven 1.0.2
</p></blockquote>
<p>You should also configure in eclipse the following variable in order to have a good communication beetween Eclipse and Maven:</p>
<blockquote><p>
Window -&gt; Preferences -&gt; Java -&gt; Build Path -&gt; Classpath Variables -&gt; New<br/><br />
<b>ajouter :</b> MAVEN_REPO -&gt; your repository<br/><br />
(In my computer : C:`\Documents and Settings\Eric\.maven\repository)
</p></blockquote>
<p><em>Warning : The directory will be created at the first Maven execution.</em><br />
<strong>2) Creating the project directory structure :</strong>  Maven project has to follow a common directory structure: <a href="http://maven.apache.org/reference/conventions.html#Directory_Structure" hreflang="en">Maven Conventions</a>  Here is the target structure :</p>

<div class="wp_syntax"><div class="code"><pre class="text">/+- src
/|  +- main
/|  |  +- java
/|  |  |  +- ...|  |  +- resources
/|  |     +- ...|  +- test
/|  |  +- java
/|  |  |  +- ...|  |  +- resources
/|  |     +- ...|  +- site
/|     +- xdoc
/|        +- ...+- target
/|  +- ...+- project.xml
+- README.tx
+- LICENSE.txt</pre></div></div>

<p><strong>3) Maven configuration file (project.xml) :</strong>  <em>project.xml</em></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;project<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--		
		POM : Project Object Model.	
	--&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;pomVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span>1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/pomVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>tellaw.org<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;currentVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span>0.1-DEMO<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/currentVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;organization</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;repository</span><span style="font-weight: bold; color: black;">/&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span>TagLib Sample<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--		
		This is the artifact to create
		In Maven, delivery item are artifacts.	
	--&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>taglib_sample<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--		
		Dependencies : Maven will keep them up to date in its local repository.		
		- It will download the correct version from a distant repository.		
		- Look at the property - WAR.BUNDLE = true, which let's you embed the dependency inside a war. 	
	--&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependencies<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
&nbsp;
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>1.2.8<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;properties<span style="font-weight: bold; color: black;">&gt;</span></span></span>				
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;war</span>.bundle<span style="font-weight: bold; color: black;">&gt;</span></span>true<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/war</span>.bundle<span style="font-weight: bold; color: black;">&gt;</span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/properties<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
&nbsp;
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id<span style="font-weight: bold; color: black;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/id<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>3.8.1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
&nbsp;
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>servletapi<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>servletapi<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>2.4-20040521<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
&nbsp;
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>jspapi<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>jsp-api<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>2.0-20040521<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependencies<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--		
		Project definition.	
	--&gt;</span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;build<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sourceDirectory<span style="font-weight: bold; color: black;">&gt;</span></span></span>src/main/java<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sourceDirectory<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;unitTestSourceDirectory<span style="font-weight: bold; color: black;">&gt;</span></span></span>src/test/java<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/unitTestSourceDirectory<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;resources<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;resource<span style="font-weight: bold; color: black;">&gt;</span></span></span>				
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;directory<span style="font-weight: bold; color: black;">&gt;</span></span></span>src/main/webapp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/directory<span style="font-weight: bold; color: black;">&gt;</span></span></span>				
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filtering<span style="font-weight: bold; color: black;">&gt;</span></span></span>false<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filtering<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resource<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resources<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;unitTest<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;includes<span style="font-weight: bold; color: black;">&gt;</span></span></span>				
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include<span style="font-weight: bold; color: black;">&gt;</span></span></span>**/*Test.java<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/include<span style="font-weight: bold; color: black;">&gt;</span></span></span>			
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/includes<span style="font-weight: bold; color: black;">&gt;</span></span></span>		
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/unitTest<span style="font-weight: bold; color: black;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/build<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/project<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>
<strong>4) Configuration of the project for eclipse workspace :</strong> <br/></p>
<p>Each eclipse user know how boring it is to relink lost dependencies during project creation, and to check that their releases number is the good one for everybody<br/><br />
Maven can generate for you the &#8220;.project&#8221; and &#8220;.classpath&#8221; files of eclipse:</p>
<blockquote><p>maven eclipse</p></blockquote>
<p style="text-align: center;"><a href="http://www.tellaw.org/wp-content/uploads/2008/04/maven_eclipse.gif"><img width="300" height="211" alt="" src="http://www.tellaw.org/wp-content/uploads/2008/04/maven_eclipse.gif" title="maven_eclipse" class="alignnone size-full wp-image-45" /></a></p>
<p><strong>Result :</strong></p>
<ul>
<li>Maven will check dependencies, and download the required one.</li>
<li>Maven will rebuild the files : .classpath et .project</li>
<li>Eclipse will use maven libraries.</li>
</ul>
<p><strong>5) Compiling and Artifact creation with maven :</strong>  Compilation is really easy with maven :</p>
<blockquote><p>
<strong>maven war</strong> -&gt; Creation of a war file (Compilation, Unit tests&#8230;).<br/><br />
<strong> maven jar</strong> -&gt; creation of a jar file.<br/><br />
<strong> maven java:compile</strong>-&gt; Compilation of the project.<br/><br />
<strong> maven site</strong> -&gt; Creation of a documentation website about the project.<br/><br />
<strong> maven clean</strong> -&gt; Clean the project build files.<br/><br />
<strong> maven eclipse:eclipse</strong> -&gt; Build eclipse files.</p></blockquote>
<p style="text-align: center;"><a href="http://www.tellaw.org/wp-content/uploads/2008/04/maven_war.gif"><img width="300" height="209" alt="" src="http://www.tellaw.org/wp-content/uploads/2008/04/maven_war.gif" title="maven_war" class="alignnone size-full wp-image-46" /></a></p>
<p>Maven is a build tool, which will manage project dependencies for you. In every task, Maven will check the dependencies, and will try to resolve them if they are not present (or in wrong version).<br/><br />
In order to do this, Maven uses differents repositories, the biggest one, is IBiblio (<a target="_blank" href="http://www.ibiblio.org/maven/" hreflang="fr">http://www.ibiblio.org/maven/</a>). A lot of Maven Plugins and options already exist, I cannot clearly list them here. This article intend to be a quickstart with Maven.</p>
<p><strong>Usefull links  :</strong></p>
<ul>
<li><a target="_blank" href="http://maven.apache.org/" hreflang="fr">Offical website</a></li>
<li><a target="_blank" href="http://www.theserverside.com/articles/article.tss?l=MavenMagic" hreflang="en">Maven Tutorial</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/05/22/maven-a-java-build-system/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Manage a Cache System with EHCache</title>
		<link>http://en.tellaw.org/2008/05/22/manage-a-cache-system-with-ehcache/</link>
		<comments>http://en.tellaw.org/2008/05/22/manage-a-cache-system-with-ehcache/#comments</comments>
		<pubDate>Thu, 22 May 2008 07:12:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Java]]></category>

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=7</guid>
		<description><![CDATA[The implementation of an Application cache is always a task which needs to impact many peoples and ressources to be done. EHCache is a simple and fast solution to implement, easy to connect with any existing application.
here is a sale-pitch which in my mind, defines the best EHCache: 
&#8220;Ehcache is a pure Java, in-process cache&#8221;, [...]]]></description>
			<content:encoded><![CDATA[<p>The implementation of an Application cache is always a task which needs to impact many peoples and ressources to be done. EHCache is a simple and fast solution to implement, easy to connect with any existing application.<br/><br />
here is a sale-pitch which in my mind, defines the best EHCache: <br/><br />
<i>&#8220;Ehcache is a pure Java, in-process cache&#8221;</i>, <i>&#8220;Available under the Apache 1.1 license. Ehcache&#8217;s copyright and licensing has been reviewed and approved by the Apache Software Foundation, making ehcache suitable for use in Apache projects.&#8221;</i><br />
<span id="more-7"></span></p>
<p><strong>1) Dependencies:</strong></p>
<ul>
<li>EHCache JAR, For now&nbsp;: ehcache-1.1.jar.</li>
<li>Log4J JAR, For now&nbsp;: log4j-1.2.9.jar.</li>
<li>Jakarta Commons Collections&nbsp;: <a href="http://jakarta.apache.org/commons/collections/" hreflang="fr">http://jakarta.apache.org/commons/collections/</a>.</li>
<li>Jakarta Commons Loggins&nbsp;: <a href="http://jakarta.apache.org/commons/logging/" hreflang="fr">http://jakarta.apache.org/commons/logging/</a></li>
</ul>
<p><strong>2) Configuring the logging service (In this tutorial log4j) :</strong></p>
<p>
<i>Fichier : WEB-INF/log4j.properties</i></p>

<div class="wp_syntax"><div class="code"><pre class="ini"># Set root logger level to DEBUG and its only appender to A1.
log4j.<span style="color: #000099;">rootLogger</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">DEBUG, A1</span>
&nbsp;
# A1 is set to be a ConsoleAppender.
log4j.appender.<span style="color: #000099;">A1</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">org.apache.log4j.ConsoleAppender</span>
&nbsp;
# A1 uses PatternLayout.
log4j.appender.A1.<span style="color: #000099;">layout</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">org.apache.log4j.PatternLayout</span>
log4j.appender.A1.layout.<span style="color: #000099;">ConversionPattern</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">%-4r <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>%t<span style="">&#93;</span></span> %-5p %c %x - %m%n</span></pre></div></div>

<p>Please, note that for moving the application to a production server, we have to change the alerting level to avoid logging debug and info messages.</p>
<p><strong>3) Creation of a sample serialyzable object to cache :</strong></p>
<p>
<i>Fichier : Movie.java</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">/*
 * Created on 22 avr. 2005
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">ehCacheSample</span><span style="color: #66cc66;">;</span>
&nbsp;
 <span style="color: #a1a100;">import java.io.Serializable;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/**
 * @author Eric
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Movie <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #aaaadd; font-weight: bold;">Serializable</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> name = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> description = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> actor = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Movie <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Default constructor.	</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getActor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> actor<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setActor<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> actor<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">actor</span> = actor<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setDescription<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> description<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">description</span> = description<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setName<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> = name<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</p>
<p><strong>4) External file configuration dedicated to EHCache :</strong></p>
<p><i>Fichier : WEB-INF/ehCache.xml</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ehcache<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Sets the path to the directory where cache .data files are created.
&nbsp;
		If the path is a Java System Property it is replaced by
		its value in the running VM.
&nbsp;
		The following properties are translated:
		user.home - User's home directory
		user.dir - User's current working directory
		java.io.tmpdir - Default temp file path 
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;diskStore</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;java.io.tmpdir&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--
&nbsp;
		Default Cache configuration. 
&nbsp;
		These will applied to caches programmatically created through the CacheManager.
&nbsp;
		The following attributes are required:        
		maxInMemory                    - Sets the maximum number of objects that will be created in memo
		eternal                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element is never expired.
		overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit.
&nbsp;
		The following attributes are optional:
		timeToIdleSeconds              - Sets the time to idle for an element before it expires.  i.e. The maximum amount of time between accesses before an element expires Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that an Element can idle for infinity.
		The default value is 0.
		timeToLiveSeconds              - Sets the time to live for an element before it expires.
		i.e. The maximum time between creation time and when an element expires.
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that and Element can live for infinity.
		The default value is 0.
&nbsp;
		diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.
&nbsp;
		The default value is false.
		diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. 
&nbsp;
		The default value
		is 120 seconds.
&nbsp;
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;defaultCache</span>
&nbsp;
		<span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;10000&quot;</span>
		<span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
		<span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
		<span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
		<span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;true&quot;</span>
		<span style="color: #000066;">diskPersistent</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
		<span style="color: #000066;">diskExpiryThreadIntervalSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
&nbsp;
	<span style="font-weight: bold; color: black;">/&gt;</span></span>    
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--Predefined caches.  
		Add your cache configuration settings here.
&nbsp;
		If you do not have a configuration for your cache a WARNING will be issued when the
		CacheManager starts
&nbsp;
		The following attributes are required:
		name                           - Sets the name of the cache. This is used to identify the cache.
		It must be unique.
&nbsp;
		maxInMemory                    - Sets the maximum number of objects that will be created in memory
		eternal                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the
		element is never expired.
&nbsp;
		overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache
		has reached the maxInMemory limit.
&nbsp;
		The following attributes are optional:
		timeToIdleSeconds              - Sets the time to idle for an element before it expires.
		i.e. The maximum amount of time between accesses before an element expires
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that an Element can idle for infinity.
&nbsp;
		The default value is 0.
&nbsp;
		timeToLiveSeconds              - Sets the time to live for an element before it expires.
		i.e. The maximum time between creation time and when an element expires.
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that and Element can live for infinity.
		The default value is 0.
&nbsp;
		diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.
		The default value is false.
&nbsp;
		diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
		is 120 seconds.
	--&gt;</span></span>
&nbsp;
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Sample cache named sampleCache1
&nbsp;
		This cache contains a maximum in memory of 10000 elements, and will expire
		an element if it is idle for more than 5 minutes and lives for more than
		10 minutes.
&nbsp;
		If there are more than 10000 elements it will overflow to the
		disk cache, which in this configuration will go to wherever java.io.tmp is
		defined on your system. On a standard Linux system this will be /tmp&quot;
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;cache</span> 	<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sampleCache1&quot;</span>
			<span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;10000&quot;</span>
			<span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
			<span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
			<span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span>
			<span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;600&quot;</span>
	<span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ehcache<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

</p>
<p><strong>5) Creating a test servlet which will try to load Objects from EHCache :</strong></p>
<p>
<i>Fichier : TestServlet.java</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">/*
 * Created on 22 avr. 2005
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">ehCacheSample</span>.<span style="color: #006600;">servlets</span><span style="color: #66cc66;">;</span>
&nbsp;
 <span style="color: #a1a100;">import java.io.IOException;</span>
 <span style="color: #a1a100;">import java.io.PrintWriter;</span>
&nbsp;
 <span style="color: #a1a100;">import javax.servlet.ServletException;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServlet;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span>
 <span style="color: #a1a100;">import org.apache.log4j.Logger;</span>
 <span style="color: #a1a100;">import org.apache.log4j.PropertyConfigurator;</span>
 <span style="color: #a1a100;">import net.sf.ehcache.*;</span>
&nbsp;
 <span style="color: #a1a100;">import com.tellaw.ehCacheSample.*;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/**
 * @author Eric
 *
 */</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger logger = Logger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>TestServlet.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> CacheManager manager = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Cache cache = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Configuration de log4J.</span>
		PropertyConfigurator.<span style="color: #006600;">configure</span><span style="color: #66cc66;">&#40;</span> getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRealPath</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;WEB-INF/log4j.properties&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creating the cache manager from configuration file</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			manager = CacheManager.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span>getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRealPath</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;WEB-INF/ehCache.xml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			cache = manager.<span style="color: #006600;">getCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sampleCache1&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Exception</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			logger.<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Unable to load EHCACHE configuration file&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doPost<span style="color: #66cc66;">&#40;</span>HttpServletRequest arg0, HttpServletResponse arg1<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
		doGet<span style="color: #66cc66;">&#40;</span>arg0, arg1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Output Writer creation		</span>
		<span style="color: #aaaadd; font-weight: bold;">PrintWriter</span> out = response.<span style="color: #006600;">getWriter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creation of a test Movie Object</span>
		Movie myMovie = <span style="color: #000000; font-weight: bold;">new</span> Movie <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Ring&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Film fantastique&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setActor</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Naomi Watts&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Caching this element</span>
		<span style="color: #808080; font-style: italic;">// Attention ce que la classe Movie implémente java.io.Serializable</span>
		<span style="color: #aaaadd; font-weight: bold;">Element</span> element = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Element</span><span style="color: #66cc66;">&#40;</span>myMovie.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, myMovie<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		cache.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		out.<span style="color: #006600;">println</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Objet mis en cache&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Reading the cache</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			element = cache.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Ring&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Exception</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
		Movie mySecondMovie = <span style="color: #66cc66;">&#40;</span>Movie<span style="color: #66cc66;">&#41;</span>element.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&lt;br/&gt;&lt;br/&gt;Cache loaded into the object mySecondMovie&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Name : &quot;</span> + mySecondMovie.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Description : &quot;</span> + mySecondMovie.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Actor : &quot;</span> + mySecondMovie.<span style="color: #006600;">getActor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</p>
<p><strong>6) Web.xml File used to map the servlet :</strong></p>
<p>
<i>Fichier : WEB-INF/web.xml</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web-app</span> 	<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span>
			<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
			<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;</span>
			<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;display-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>ehCacheSample<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/display-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		EHCache tutorial from tellaw.org
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Declaration des servlets --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>com.tellaw.ehCacheSample.servlets.TestServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Declaration du mapping --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web-app<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

</p>
<p><strong>Notes</strong>&nbsp;:</p>
<ul>
<li>The official documentation doesn&#8217;t ask to provide Jakarta Commons Collections as a dependency..</li>
<li>Please do not forget to change the logging level of the application when deploying on production platforms.</li>
</ul>
<p><strong>Sites utiles</strong>&nbsp;:</p>
<ul>
<li>Le site de ehCache&nbsp;: <a href="http://ehcache.sourceforge.net/" hreflang="fr">ehcache</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/05/22/manage-a-cache-system-with-ehcache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Spring : Generate an excel document as a Spring view</title>
		<link>http://en.tellaw.org/2008/05/21/spring-generate-an-excel-document-as-a-spring-view/</link>
		<comments>http://en.tellaw.org/2008/05/21/spring-generate-an-excel-document-as-a-spring-view/#comments</comments>
		<pubDate>Wed, 21 May 2008 20:24:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Java]]></category>

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

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

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

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

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=6</guid>
		<description><![CDATA[
scoopeo_url="http://www.tellaw.org/2008/04/25/spring-generer-un-document-excel-comme-vue/"

This article&#8217;s goal, is to show you you to simply generate a report readable with Excel in Java technology using Spring MVC Framwork.

1) Some details :
Spring Framework (http://www.springframework.org) gives to the user the tools for PDF, MS Words, MS Excel, etc documents creation.  As Spring is a clean MVC Framework, the creation of an [...]]]></description>
			<content:encoded><![CDATA[<div style="float:left;margin-right:10px">
<script>scoopeo_url="http://www.tellaw.org/2008/04/25/spring-generer-un-document-excel-comme-vue/"</script><script language="javascript" src="http://scoopeo.com/clicker/insert/large"></script>
</div>
<p>This article&#8217;s goal, is to show you you to simply generate a report readable with Excel in Java technology using Spring MVC Framwork.</p>
<p><span id="more-6"></span></p>
<p><b>1) Some details :</b><br/></p>
<p>Spring Framework (http://www.springframework.org) gives to the user the tools for PDF, MS Words, MS Excel, etc documents creation.  As Spring is a clean MVC Framework, the creation of an Excel document is done by <strong>creating a Controller and a View which extends AbstractExcelView</strong></p>
<p>Here is a code sample</p>
<p><b>2) The controller&#8230; :</b><br/><br />
Here is a Spring controller sample really easy, which will just add to the model a test value, usefull for the view rendering mechanism validation.<br />
<br/><br />
<i><b>Path</b> : com.tellaw.test.controllers</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">controllers</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import java.util.*;</span>
<span style="color: #a1a100;">import javax.servlet.http.*;</span>
&nbsp;
<span style="color: #a1a100;">import org.apache.commons.logging.Log;</span>
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span>
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span>
&nbsp;
@SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unchecked&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExcelController <span style="color: #000000; font-weight: bold;">extends</span> AbstractController
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #aaaadd; font-weight: bold;">Exception</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    	Map<span style="color: #66cc66;">&lt;</span>String, Object<span style="color: #66cc66;">&gt;</span> model = <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #66cc66;">&lt;</span>String, Object<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	model.<span style="color: #006600;">put</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;testValue&quot;</span>, <span style="color: #ff0000;">&quot;Test String for Excel View !!!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;excel-view&quot;</span>, <span style="color: #ff0000;">&quot;model&quot;</span>, model<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>3) An excel view:</b><br/><br />
Here is the view, which will format and render the MS Excel Document. The view is based on the POI Apache API, making as easy as possible the work on MS files format (Excel, word, Powerpoint) in Java techologies.<br />
<br/><br />
<i><b>Path</b> : com.tellaw.test.controllers</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">view</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #a1a100;">import java.util.*;</span>
&nbsp;
<span style="color: #a1a100;">import javax.servlet.http.*;</span>
&nbsp;
<span style="color: #a1a100;">import org.apache.commons.logging.Log;</span>
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.usermodel.HSSFCell;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.usermodel.HSSFCellStyle;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.usermodel.HSSFFont;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.usermodel.HSSFSheet;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.usermodel.HSSFWorkbook;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.util.HSSFColor;</span>
<span style="color: #a1a100;">import org.apache.poi.hssf.util.Region;</span>
<span style="color: #a1a100;">import org.springframework.web.servlet.view.document.AbstractExcelView;</span>
&nbsp;
@SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unchecked&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExcelExportView <span style="color: #000000; font-weight: bold;">extends</span> AbstractExcelView  <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #aaaadd; font-weight: bold;">String</span> SHEET_NAME = <span style="color: #ff0000;">&quot;sheet&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> buildExcelDocument<span style="color: #66cc66;">&#40;</span>	  <span style="color: #aaaadd; font-weight: bold;">Map</span> model, 
    							HSSFWorkbook workbook, 
    							HttpServletRequest request, 
    							HttpServletResponse response <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        model = <span style="color: #66cc66;">&#40;</span>Map<span style="color: #66cc66;">&lt;</span>String, Object<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>model.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;model&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Get Active Datas</span>
        workbook = addSkeleton<span style="color: #66cc66;">&#40;</span> workbook <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Define Style</span>
    	HSSFCellStyle style = workbook.<span style="color: #006600;">createCellStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        HSSFFont boldFont = workbook.<span style="color: #006600;">createFont</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        boldFont.<span style="color: #006600;">setBoldweight</span><span style="color: #66cc66;">&#40;</span>HSSFFont.<span style="color: #006600;">BOLDWEIGHT_NORMAL</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        style.<span style="color: #006600;">setFillForegroundColor</span><span style="color: #66cc66;">&#40;</span> HSSFColor.<span style="color: #006600;">LIGHT_TURQUOISE</span>.<span style="color: #006600;">index</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        style.<span style="color: #006600;">setFillPattern</span><span style="color: #66cc66;">&#40;</span>HSSFCellStyle.<span style="color: #006600;">SOLID_FOREGROUND</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        style.<span style="color: #006600;">setFont</span><span style="color: #66cc66;">&#40;</span>boldFont<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Define headers</span>
        cell = getCell<span style="color: #66cc66;">&#40;</span> sheet, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        cell.<span style="color: #006600;">setCellStyle</span><span style="color: #66cc66;">&#40;</span> style <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Number</span>
    	setText <span style="color: #66cc66;">&#40;</span> cell , model.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;testValue&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> HSSFWorkbook addSkeleton <span style="color: #66cc66;">&#40;</span> HSSFWorkbook workbook <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    	HSSFSheet sheet<span style="color: #66cc66;">;</span>
        HSSFCell cell<span style="color: #66cc66;">;</span>
&nbsp;
    	sheet = workbook.<span style="color: #006600;">createSheet</span><span style="color: #66cc66;">&#40;</span> ExcelExportView.<span style="color: #006600;">SHHET_NAME</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Define Style</span>
    	HSSFCellStyle style = workbook.<span style="color: #006600;">createCellStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        HSSFFont boldFont = workbook.<span style="color: #006600;">createFont</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        boldFont.<span style="color: #006600;">setBoldweight</span><span style="color: #66cc66;">&#40;</span>HSSFFont.<span style="color: #006600;">BOLDWEIGHT_BOLD</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        style.<span style="color: #006600;">setFont</span><span style="color: #66cc66;">&#40;</span>boldFont<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Define headers</span>
        cell = getCell<span style="color: #66cc66;">&#40;</span> sheet, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        cell.<span style="color: #006600;">setCellStyle</span><span style="color: #66cc66;">&#40;</span>style<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    	setText <span style="color: #66cc66;">&#40;</span> cell , <span style="color: #ff0000;">&quot;Model test value is ?&quot;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	autoSizeCols<span style="color: #66cc66;">&#40;</span> sheet <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> workbook<span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>4) Configuring Spring context :</b><br/><br />
We just have now to do the wiring Spring stuff&#8230;<br/><br />
<i><b>Path</b> : WEB-INF/springapp-servlet.xml (only for my test application)</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span>
		<span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span>
       	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
       	<span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span>
       	<span style="color: #000066;">xmlns:tx</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/tx&quot;</span>
       	<span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span>
       	<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;
       		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Abstract Controller
	 --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;abstractController&quot;</span> <span style="color: #000066;">abstract</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.tellaw.test.controllers.AbstractController&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- EXCEL Export controller --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> 	<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;excelView&quot;</span> 
    		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.tellaw.test.controllers.ExcelExportView&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> 	<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;excelController&quot;</span> 
    		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.tellaw.test.controllers.ExcelController&quot;</span>
    		<span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;abstractController&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Url Mapping Definition With Authentication
	 --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;urlMapping&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mappings&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;props<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;/report.xls&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>excelController<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/prop<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/props<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		View Resolver
	 --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.ResourceBundleViewResolver&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;basename&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;views&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p><b>5) Mapping a view in a properties file :</b><br/><br />
I like to put the mapping of view in an external Properties file, so here is my file :<br/></p>
<p><i><b>Path</b> : ressources/views.properties (Only for my test and my maven configuration)</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml">excel-view.(class)=com.tellaw.test.controllers.ExcelExportView</pre></div></div>

<p><b>5) What&#8217;s next ????</b><br/></p>
<ul>
<li>Spring Framework : <a href="http://www.springframework.org" target="_blank">http://www.springframework.org</a></li>
<li>Apache POI (Apache POI - Java API To Access Microsoft Format Files): <a href="http://poi.apache.org/" target="_blank">http://poi.apache.org/</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/05/21/spring-generate-an-excel-document-as-a-spring-view/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP5 Databases ORM (Objects Relation Models) - Doctrine</title>
		<link>http://en.tellaw.org/2008/05/21/php5-databases-orm-objects-relation-models-doctrine/</link>
		<comments>http://en.tellaw.org/2008/05/21/php5-databases-orm-objects-relation-models-doctrine/#comments</comments>
		<pubDate>Wed, 21 May 2008 13:06:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

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

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=5</guid>
		<description><![CDATA[
scoopeo_url="http://www.tellaw.org/2008/05/13/php5-databases-orm-relation-models-objets-doctrine/"

This is a short introduction to a great databases abstraction in PHP5. It can handle the mapping of database fields to php objects (ORM). It is called &#8220;Doctrine&#8221;.
Instead of Propel, Doctrine support two modes :
1) Pre-generation of the model.
2) Runtime in-memory creation of the model.

The second mode is really interresting for debuging.

1: Describe your Object [...]]]></description>
			<content:encoded><![CDATA[<div style="float:left;margin-right:10px">
<script>scoopeo_url="http://www.tellaw.org/2008/05/13/php5-databases-orm-relation-models-objets-doctrine/"</script><script language="javascript" src="http://scoopeo.com/clicker/insert/large"></script>
</div>
<p>This is a short introduction to a great databases abstraction in PHP5. It can handle the mapping of database fields to php objects (ORM). It is called &#8220;Doctrine&#8221;.<br/><br />
Instead of Propel, Doctrine support two modes :<br/><br />
1) Pre-generation of the model.<br/><br />
2) Runtime in-memory creation of the model.<br/><br />
<br/><br />
The second mode is really interresting for debuging.<br />
<span id="more-5"></span><br />
<b>1: Describe your Object (you can use PHP object or Yaml language)</b></p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Email <span style="color: #000000; font-weight: bold;">extends</span> Doctrine_Record
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setTableDefinition<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// setting custom table name:</span>
        <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">setTableName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'emails'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">hasColumn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'address'</span><span style="color: #66cc66;">,</span>         <span style="color: #808080; font-style: italic;">// name of the column</span>
                         <span style="color: #ff0000;">'string'</span><span style="color: #66cc66;">,</span>          <span style="color: #808080; font-style: italic;">// column type</span>
                         <span style="color: #ff0000;">'200'</span><span style="color: #66cc66;">,</span>             <span style="color: #808080; font-style: italic;">// column length</span>
                         <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notblank'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This means that our &#8220;email&#8221; table has a column type &#8220;String&#8221; called &#8220;address&#8221; not empty. For more detailled informations, please take a look at the Doctrine documentation.<br/></p>
<p><b>2: Now, use it !</b></p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #ff0000">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Email<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$user</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">address</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'toto@toto.fr'</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">// Saves the reccord</span>
<span style="color: #ff0000">$user</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If you want to know more about Doctrine (and I am sure that you want to), please following the bottom links.</p>
<ul>
<li><a href="http://www.phpdoctrine.org/" target="_blank">Official website of Doctrine</a></li>
<li><a href="http://www.phpdoctrine.org/documentation/manual/0_11?one-page" target="_blank">Tutorials and documentation of Doctrine</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/05/21/php5-databases-orm-objects-relation-models-doctrine/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Magic Methods and PHP5 OOP</title>
		<link>http://en.tellaw.org/2008/05/20/magic-methods-and-php5-oop/</link>
		<comments>http://en.tellaw.org/2008/05/20/magic-methods-and-php5-oop/#comments</comments>
		<pubDate>Tue, 20 May 2008 20:28:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

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

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

		<guid isPermaLink="false">http://en.tellaw.org/?p=3</guid>
		<description><![CDATA[
scoopeo_url="http://www.tellaw.org/2008/05/16/les-methodes-%c2%ab-magiques-%c2%bb-en-php5/"

PHP5 is introducing the Object Oriented Programmation foundments. This include the creation of some methods used to help the manipulation of objects. They can make automated tasks (usually boring), catch lost calls to unexisting methods or preperties&#8230;
In an Architect side, I would say that the usage of theses methods is the inverse of a best [...]]]></description>
			<content:encoded><![CDATA[<div style="float:left;margin-right:10px">
<script>scoopeo_url="http://www.tellaw.org/2008/05/16/les-methodes-%c2%ab-magiques-%c2%bb-en-php5/"</script><script language="javascript" src="http://scoopeo.com/clicker/insert/large"></script>
</div>
<p>PHP5 is introducing the Object Oriented Programmation foundments. This include the creation of some methods used to help the manipulation of objects. They can make automated tasks (usually boring), catch lost calls to unexisting methods or preperties&#8230;<br />
In an Architect side, I would say that the usage of theses methods is the inverse of a best pratice, but in some specifics jobs, they could really help.</p>
<p><span id="more-3"></span></p>
<p class="MsoNormal">&#8220;Magic methods&#8221; List for PHP5:</p>
<ul type="disc">
<li class="MsoNormal"><strong>__construct()</strong></li>
<li class="MsoNormal"><strong>__destruct()</strong></li>
<li class="MsoNormal"><strong>__set()</strong></li>
<li class="MsoNormal"><strong>__get()</strong></li>
<li class="MsoNormal"><strong>__call()</strong></li>
<li class="MsoNormal"><strong>__toString()</strong></li>
<li class="MsoNormal"><strong>__sleep()</strong></li>
<li class="MsoNormal"><strong>__wakeup()</strong></li>
<li class="MsoNormal"><strong>__isset()</strong></li>
<li class="MsoNormal"><strong>__unset()</strong></li>
<li class="MsoNormal"><strong>__autoload()</strong></li>
<li class="MsoNormal"><strong>__clone()</strong></li>
</ul>
<p><strong>__construct()</strong><br />
Method called during object building.</p>
<p><strong>__destruct()</strong><br />
Method called during object destruction.</p>
<p><strong>__set()</strong><br />
Method called while trying to assign a value to a non-existing property.</p>
<p><strong>__get()</strong><br />
Method called while trying to read a non-existing property.</p>
<p><strong>__call()</strong><br />
Method called while trying to calla non-existing method.</p>
<p><strong>__toString()</strong><br />
Method called when someone tries to transform an object to string (using echo or print)&#8230;</p>
<p><strong>__sleep()</strong><br />
Method used to serialyze objects in PHP5. It has to define to correct way to serialyze.</p>
<p><strong>__wakeup()</strong><br />
De-serialyze method. It has to define the de-serialyzing way of the object.</p>
<p><strong>__isset()</strong><br />
This method helps to check the existence of a method.</p>
<p><strong>__unset()</strong><br />
Method used for object destruction.</p>
<p><strong>__autoload()</strong><br />
This method is called while trying to load an object from a separated file without having it already loaded with &#8220;include&#8221;, &#8220;require&#8221; or &#8220;require once&#8221;.</p>
<p><strong><span style="font-size: 12pt;">__clone()</span></strong><span style="font-size: 12pt; font-family: &quot;Times New Roman&quot;;"><br />
This method define the way to clone the object..</span><br />
<br/><br />
<strong>Example :</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php"> <span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #ff0000">$Name</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$var</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$val</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
             <span style="color: #000066;">echo</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The property : &quot;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$var</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot; - Value : &quot;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$val</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #ff0000">$systemvars</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff0000">$systemvars</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">adminEmail</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'toto@toto.fr'</span><span style="color: #66cc66;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This will output</p>

<div class="wp_syntax"><div class="code"><pre class="text">        The property : adminEmail - Value: toto@toto.fr</pre></div></div>

<p><br/><br />
<b>This is not a best practice  !</b><br/><br />
In fact, despite some very few cases, this methods shouldn&#8217;t be used to define beans. The only correct way is to create a file containing the &#8220;hardcoded&#8221; bean definition.<br />
<br/><br />
<b>Best Practice is :</b><br/></p>

<div class="wp_syntax"><div class="code"><pre class="php"> <span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #ff0000">$adminEmail</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAdminEmail <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$var</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
             <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">adminEmail</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$var</span><span style="color: #66cc66;">;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAdminEmail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
             <span style="color: #b1b100;">return</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">adminEmail</span><span style="color: #66cc66;">;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #ff0000">$systemvars</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff0000">$systemvars</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">setAdminEmail</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'toto@toto.fr'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>It is really important for code stability to respect best practices, but sometimes, flexibility is need&#8230; This is what I try to explain in this article.</p>]]></content:encoded>
			<wfw:commentRss>http://en.tellaw.org/2008/05/20/magic-methods-and-php5-oop/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
