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

<channel>
	<title>Tellaw.org &#187; Java</title>
	<atom:link href="http://www.tellaw.org/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tellaw.org</link>
	<description>La technologie doit devenir simple...</description>
	<lastBuildDate>Fri, 02 Apr 2010 14:48:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spring : Generer un document Excel comme vue</title>
		<link>http://www.tellaw.org/2008/04/25/spring-generer-un-document-excel-comme-vue/</link>
		<comments>http://www.tellaw.org/2008/04/25/spring-generer-un-document-excel-comme-vue/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 12:07:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=48</guid>
		<description><![CDATA[L&#8217;objectif de cet article est de présente une façon très simple de générer des rapports Excel en Java, via l&#8217;utilisation du framework MVC Spring.

1) Une petite explication :
Le framework spring (http://www.springframework.org) met à notre disposition tous les outils utiles à la création de documents PDF. Spring etant un framework MVC propre, la creation d&#8217;un Fichier [...]]]></description>
			<content:encoded><![CDATA[<p>L&#8217;objectif de cet article est de présente une façon très simple de générer des rapports Excel en Java, via l&#8217;utilisation du framework MVC Spring.</p>
<p><span id="more-48"></span></p>
<p><b>1) Une petite explication :</b><br/></p>
<p>Le framework spring (http://www.springframework.org) met à notre disposition tous les outils utiles à la création de documents PDF. Spring etant un framework MVC propre, la creation d&#8217;un Fichier Excel passe par <strong>la réalisation d&#8217;un Controller et d&#8217;une Vue qui etend AbstractExcelView</strong>.</p>
<p>Voici un exemple de code&#8230;</p>
<p><b>2) Un controller&#8230; :</b><br/><br />
Alors voici un exemple de controller Spring très simple, qui va juste deposer dans le model une valeur de test, utile pour valider le fonctionnement de notre vue.<br/><br />
<i><b>Chemin</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;Ma String de test de ma vue Excel !!!&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) Une vue excel :</b><br/><br />
Voici la vue, qui va permettre de mettre en forme le document Excel. La vue se base sur l&#8217;API POI de Apache, permettant l&#8217;exploitation de documents microsofts (excels, words, powerpoints&#8230;) en Java.<br/><br />
<i><b>Chemin</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;feuille&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;valeur de test du model ?&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) Configurer sur contexte Spring :</b><br/><br />
Il suffit ensuite de faire le wiring de ces objets dans le contexte de Spring&#8230;<br/><br />
<i><b>Chemin</b> : WEB-INF/springapp-servlet.xml (propre à mon test)</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) Mapper la vue via un properties :</b><br/><br />
J&#8217;aime bien séparer le mapping des vues dans un fichier Properties externe, voici le détail du mien :<br/><br />
<i><b>Chemin</b> : ressources/views.properties (propre à mon test et à ma compilation maven)</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) Annèxes, et la suite ????</b><br/></p>
<ul>
<li>Framework Spring : <a href="http://www.springframework.org" target="_blank">http://www.springframework.org</a></li>
<li>Apache POI (Apache POI &#8211; 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://www.tellaw.org/2008/04/25/spring-generer-un-document-excel-comme-vue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gerer un cache avec EhCache.</title>
		<link>http://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/</link>
		<comments>http://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 09:58:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[ehcache]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=47</guid>
		<description><![CDATA[La gestion d&#8217;un cache applicatif est souvent une question demandant pas mal de ressources pour être résolue. EhCache est une solution simple et rapide à mettre en oeuvre, capable de s&#8217;interfacer facilement avec vos applications, et même le système de logs de votre application&#160;!
Comme à mon habtude, voici n extrait du site de ehcache définissant [...]]]></description>
			<content:encoded><![CDATA[<p>La gestion d&#8217;un cache applicatif est souvent une question demandant pas mal de ressources pour être résolue. EhCache est une solution simple et rapide à mettre en oeuvre, capable de s&#8217;interfacer facilement avec vos applications, et même le système de logs de votre application&nbsp;!<br/><br />
Comme à mon habtude, voici n extrait du site de ehcache définissant ce produit&nbsp;:<br/><br />
<i>&laquo;&nbsp;Ehcache is a pure Java, in-process cache&nbsp;&raquo;</i>, <i>&laquo;&nbsp;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.&nbsp;&raquo;</i></p>
<p><span id="more-47"></span></p>
<p><strong>1) Les dépendances :</strong></p>
<ul>
<li>Le Jar de EhCache, actuellement&nbsp;: ehcache-1.1.jar.</li>
<li>Le jar de log4j, actuellement&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) Configurer le système de logs (pour mon exemple 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>Notez que pour le passage en production de votre application, il faut remonter le niveau d&#8217;alerte de log4j, sinon le serveur va logguer les messages de débug.</p>
<p><strong>3) Création d&#8217;un objet serializable à mettre dans le 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;">// Constructeur par défaut de movie.	</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) Configuration de EhCache par un fichier de conf externe :</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) Création d&#8217;un servlet utilisant EhCache pour mémoriser un objet :</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;">// Creation du writer de sortie.		</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 d'un objet Movie pour tester notre cache</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;">// Mise en cache de cet 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;">// Lecture du cache pour extraire l'objet</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 chargé dans l'objet 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;Nom : &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;Acteur : &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) Le fichier web.xml mappant la 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>
		Démonstration de l'utilisation du cache : EhCache par 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>La documentation officielle ne donne pas comme dépendance Jakarta Commons Collections. En ce qui me concerne, j&#8217;ai une exception type ClassNotFound sur <em>LRUMap</em>.</li>
<li>Notez que pour le passage en production de votre application, il faut remonter le niveau d&#8217;alerte de log4j, sinon le serveur va loguer les messages de debug.</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://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Maven &#8211; une alternative à ANT pour la compilation&#8230;</title>
		<link>http://www.tellaw.org/2008/04/24/maven-une-alternative-a-ant-pour-la-compilation/</link>
		<comments>http://www.tellaw.org/2008/04/24/maven-une-alternative-a-ant-pour-la-compilation/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 15:32:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=44</guid>
		<description><![CDATA[En utilisant Maven vous pourrez automatiser les taches r&#233;barbatives d&#8217;un projet web. Il prendra en charge par exemple la version de librairies de vos d&#233;pendances, le projet Eclipse, les builds et les tests unitaires. Cet article va vous montrer dans un projet simple l&#8217;utilisation de Maven avec des librairies externes. A la grande diff&#233;rence de [...]]]></description>
			<content:encoded><![CDATA[<p>En utilisant Maven vous pourrez automatiser les taches r&eacute;barbatives d&#8217;un projet web. Il prendra en charge par exemple la version de librairies de vos d&eacute;pendances, le projet Eclipse, les builds et les tests unitaires. Cet article va vous montrer dans un projet simple l&#8217;utilisation de Maven avec des librairies externes. A la grande diff&eacute;rence de Ant, ou vous d&eacute;crivez les taches &agrave; r&eacute;aliser, dans Maven vous d&eacute;crivez votre projet, et lui connait les taches &agrave; r&eacute;aliser. C&#8217;est une approche diff&eacute;rente, amenant l&#8217;utilisation de fichiers de configurations plus l&eacute;gers, plus lisible, tr&egrave;s efficace&#8230;</p>
<p><span id="more-44"></span></p>
<p><strong>1) Installation de Maven :</strong><br/><br />
L&#8217;installation de Maven sous Windows est particuli&egrave;rement simple. Il suffit de t&eacute;l&eacute;charger l&#8217;install depuis le site officiel (<a href="http://maven.apache.org/" hreflang="fr">http://maven.apache.org/</a>), et d&#8217;ajouter dans votre path le chemin vers son r&eacute;pertoire bin (chez moi : C:\Program Files\Apache Software Foundation\Maven 1.0.2\bin).  A ce stade vous pouvez tester maven en lan&ccedil;ant en ligne de commande : <strong>Maven</strong>.  Lors de son installation Maven doit cr&eacute;er les variables d&#8217;environement :</p>
<blockquote><p>
<b>MAVEN_HOME</b> -&gt; C:\Program Files\Apache Software Foundation\Maven 1.0.2
</p></blockquote>
<p>Vous devrez configurer une variable dzns eclipse pour permettre une parfaite interaction entre les deux :</p>
<blockquote><p>
Window -&gt; Preferences -&gt; Java -&gt; Build Path -&gt; Classpath Variables -&gt; New<br/><br />
<b>ajouter :</b> MAVEN_REPO -&gt; votre repository<br/><br />
(chez moi : C:`\Documents and Settings\Eric\.maven\repository)
</p></blockquote>
<p><em>Attention le repository n&#8217;existe qu&#8217;apr&egrave;s la premi&egrave;re ex&eacute;cution de maven</em>  <strong>2) Cr&eacute;ation de l&#8217;arborescence de votre projet :</strong>  L&#8217;arborescence des projets sous Maven doit respecter une recommandation dsponible ici : <a href="http://maven.apache.org/reference/conventions.html#Directory_Structure" hreflang="en">Maven Conventions</a>  Voici l&#8217;arborescence cible :</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) Le fichier de configuration de Maven (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, c'est le modÃ¨le objet du projet.	
	--&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;!--		
		C'est le nom de l'artifact  réaliser. 
		Sous Maven, les livrables à réaliser sont des 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;!--		
		Les dépendances : Maven va tenir à jour vos dépendances depuis son repository.		
		- Il téléchargera depuis un repository distant la version voulue si vouq ne la possédez pas encore.		
		- Notez la propriété WAR.BUNDLE = true, permettant d'embarquer la dépendance dans le war de livraison. 	
	--&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;!--		
		Définition du projet.	
	--&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 du projet pour le workspace d&#8217;eclipse :</strong> <br/></p>
<p>Tous les utilisateurs d&#8217;&eacute;clipse connaissent le soucis de d&eacute;pendances perdues en cours de projets, a re-centraliser r&eacute;guli&eacute;rement, ou dont les versions sont diff&eacute;rentes entre les intervenant du projet !!!!<br/><br />
Nous avons tous un jour hurler notre rage face &agrave; un Eclipse qui n&#8217;avait plus ses fichiers projets : <em>.classpath</em> et <em>.project</em>&#8230;  Et bien c&#8217;est termin&eacute;, Maven peux g&eacute;n&eacute;rer pour vous les fichiers de configuration projet d&#8217;Eclipse, la commande magique ???? simpleeee :</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>R&eacute;sultat :</strong></p>
<ul>
<li>Maven va v&eacute;rifier les d&eacute;pendances et t&eacute;l&eacute;charger celles dont le projet &agrave; besoin.</li>
<li>Maven va reconstruire les fichiers projets : .classpath et .project</li>
<li>Eclipse se basera sur les librairies de Maven.</li>
</ul>
<p><strong>5) Compilation et livraison sous Maven :</strong>  La compilation est simple avec Maven :</p>
<blockquote><p>
<strong>maven war</strong> -&gt; Cr&eacute;ation d&#8217;un war (Compilation, Tests Unitaires&#8230;).<br/><br />
<strong> maven jar</strong> -&gt; Cr&eacute;ation d&#8217;un jar avec le projet.<br/><br />
<strong> maven java:compile</strong>-&gt; Simple compilation du projet.<br/><br />
<strong> maven site</strong> -&gt; Cr&eacute;ation d&#8217;un site d&#8217;information sur la compilation et la documentation (sortie dans target/docs).<br/><br />
<strong> maven clean</strong> -&gt; Nettoyage des fichers de sortie de maven.<br/><br />
<strong> maven eclipse</strong> -&gt; Reconstruit le projet eclipse.</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>Lors de chaque tache Maven validera que toutes ses d&eacute;pendances sont pr&eacute;sentes, si ce n&#8217;est pas le cas, il les t&eacute;l&eacute;charges depuis <a target="_blank" href="http://www.ibiblio.org/maven/" hreflang="fr">http://www.ibiblio.org/maven/</a> <strong>Maven utilise le repertoire target pour sortir ses fichiers.</strong> Beaucoup d&#8217;options et de plugins existent pour Maven, je ne peux &eacute;videmment pas en faire un listing. Cet article donne un kit de quickstart, pas une formation compl&egrave;te.  <strong>Liens utiles  :</strong></p>
<ul>
<li><a target="_blank" href="http://maven.apache.org/" hreflang="fr">Site officiel</a></li>
<li><a target="_blank" href="http://www.theserverside.com/articles/article.tss?l=MavenMagic" hreflang="en">Tutorial Maven</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/24/maven-une-alternative-a-ant-pour-la-compilation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CAPTCHA : Validation de formulaire &#8211; une image dynamique en java&#8230;</title>
		<link>http://www.tellaw.org/2008/04/23/captcha-validation-de-formulaire-une-image-dynamique-en-java/</link>
		<comments>http://www.tellaw.org/2008/04/23/captcha-validation-de-formulaire-une-image-dynamique-en-java/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 10:16:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=41</guid>
		<description><![CDATA[Voici une application capable de faire la difference entre un humain et un bot. Les formulaires d&#8217;inscription ont souvent besoin de ce genre d&#8217;outils afin de securiser les serveurs. l&#8217;idée est de generer une image contenant un texte, et de le comparer à une valeur saisie par l&#8217;utilisateur.
Il est en effet très difficile de faire [...]]]></description>
			<content:encoded><![CDATA[<p>Voici une application capable de faire la difference entre un humain et un bot. Les formulaires d&#8217;inscription ont souvent besoin de ce genre d&#8217;outils afin de securiser les serveurs. l&#8217;idée est de generer une image contenant un texte, et de le comparer à une valeur saisie par l&#8217;utilisateur.<br />
Il est en effet très difficile de faire un script capable de passer ce genre de securité simple à mettre en oeuvre.</p>
<p><span id="more-41"></span></p>
<p><strong>Voici un exemple d&#8217;image générée par notre programme :</strong></p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-43" title="getimage" src="http://www.tellaw.org/wp-content/uploads/2008/04/getimage.jpg" alt="" width="178" height="71" /></p>
<p><strong>Voici le principe de fonctionnement :</strong></p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-42" title="flowchart_captcha" src="http://www.tellaw.org/wp-content/uploads/2008/04/flowchart_captcha.gif" alt="" width="183" height="549" /></p>
<p><strong>Pour fonctionner l&#8217;application est composée de :</strong></p>
<ul>
<pre>
<li>Une servlet permettant de realiser l'image dynamique et d'écrire en session sa valeur.</li>
<li>Une JSP de validation comparant la valeur en session à la valeur soumise via le formulaire.</li>
</pre>
</ul>
<p>Mon exemple tourne actuellement sur une architécture TomCat 5.5.7, JDK 5.0.</p>
<p><strong>1) Le code de ma servlet :</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;">// Sauvegarde de la string en 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;">// Calcul de la taille du texte</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;">// Preparation de la sortie</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 en PNG de l'image</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;">// Traitement Erreur</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;">// Génération d'un texte aléatoire</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 d'une table avec les majuscules de l'alphabet.</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;">// Parametres par défaut</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) La vérification de la valeur :</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;">// lecture de la valeur du champs nommé imgValue du formulaire de saisie.</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;">// Valeur exacte !!!!</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;">// Valeur fausse !!!!</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><em>Attention : Si votre serveur est positionné derriere un load-balancer, vous devez prendre garde à le configurer de sorte que l&#8217;utilisateur tombe systématiquement sur la même machine, dans le cas contraire, la valeur en session ne correspondrait a rien.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/23/captcha-validation-de-formulaire-une-image-dynamique-en-java/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Programmation par contraintes generation de grille de Sudoku</title>
		<link>http://www.tellaw.org/2008/04/18/programmation-par-contraintes-generation-de-grille-de-sudoku/</link>
		<comments>http://www.tellaw.org/2008/04/18/programmation-par-contraintes-generation-de-grille-de-sudoku/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 15:56:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[toturial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=33</guid>
		<description><![CDATA[Je me suis posé la question de la génération en Java d&#8217;un grille de Sudoku. Pour arrivé à obtenir une grille aléatoire, j&#8217;ai entrevu deux solutions :
- Utiliser un algorithme complexe utilisant le &#171;&#160;backtracking&#160;&#187; pour revenir en arrière si aucune solution ne permet de résoudre le problème.
- Utiliser un framework de programmation par contrainte.
C&#8217;est la [...]]]></description>
			<content:encoded><![CDATA[<p>Je me suis posé la question de la génération en Java d&#8217;un grille de Sudoku. Pour arrivé à obtenir une grille aléatoire, j&#8217;ai entrevu deux solutions :<br />
- Utiliser un algorithme complexe utilisant le &laquo;&nbsp;backtracking&nbsp;&raquo; pour revenir en arrière si aucune solution ne permet de résoudre le problème.<br />
- Utiliser un framework de programmation par contrainte.<br />
C&#8217;est la seconde solution que j&#8217;ai mis en oeuvre, et que je détaille&#8230;</p>
<p><span id="more-33"></span></p>
<h2>Qu&#8217;est-ce que le Sudoku ?</h2>
<p>&laquo;&nbsp;Le principe semble en avoir été découvert en 1979 par Howard Games sous le nom de Number place. Les premières grilles ont été publiées dans la revue Math, puzzles and logic problems avec un modeste succès&#8230;</p>
<p>&#8230;Sudoku est connu sous sa forme actuelle depuis 1984 au Japon et s&#8217;est fortement propagé dans le monde en 2005&#8243; (cf : <a href="http://www.mots-croises.ch/Manuels/Sudoku/histoire.htm" target="_blank">http://www.mots-croises.ch/Manuels/Sudoku/histoire.htm</a>)</p>
<p>Une grille de Sudoku contient des chiffres qui doivent respecter les trois règles :</p>
<ul>
<li>Chaque colonne doit contenir tous les chiffres de 1 à 9</li>
<li>Chaque ligne doit contenir tous les chiffres de 1 à 9</li>
<li>Chaque pavé de 3&#215;3 doit contenir tous les chiffres de 1 à 9</li>
</ul>
<p>Exemple de grille :</p>
<p style="text-align: center;"><a href="http://www.tellaw.org/wp-content/uploads/2008/04/sudoku.gif"><img class="alignnone size-full wp-image-34" title="sudoku" src="http://www.tellaw.org/wp-content/uploads/2008/04/sudoku.gif" alt="" width="342" height="343" /></a></p>
<p><strong>L&#8217;objectif est donc de trouver tous les chiffres manquants</strong>.</p>
<h2>Utiliser la programmation par contraintes.</h2>
<p>Aprés études des différente solutions, j&#8217;ai opté pour la programmation par contraintes. Ce type de programmation permet via l&#8217;utilisation d&#8217;un framework adapté, de définir les contraintes d&#8217;un problème pour en obtenir une ou des solutions. Ces frameworks embarquent des algorithmes de résolution bien loin de mes compétences.</p>
<p>L&#8217;objectif via l&#8217;utilisation de la programmation pas contraintes, est de definir l&#8217;univers de notre problème et d&#8217;en définir les contraintes. Le framework se charge ensuite de trouver l&#8217;algorithme le plus adéquat pour trouver la solution.</p>
<p>Voici sur wikipedia la liste des frameworks : <a href="http://fr.wikipedia.org/wiki/Programmation_par_contraintes" target="_blank">http://fr.wikipedia.org/wiki/Programmation_par_contraintes</a></p>
<p>J&#8217;ai sélectionné CHOCO car c&#8217;est un framework non-commercial (url : <a href="http://choco.sourceforge.net/" target="_blank">http://choco.sourceforge.net/</a>).</p>
<h2>La définition des contraintes via CHOCO</h2>
<h3>Définition d&#8217;une valeur pour une case (hypothèse de départ)</h3>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #aaaadd; font-weight: bold;">Iterator</span> iter = predefSlots.<span style="color: #006600;">iterator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>iter.<span style="color: #006600;">hasNext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	Slot slot = <span style="color: #66cc66;">&#40;</span>Slot<span style="color: #66cc66;">&#41;</span> iter.<span style="color: #006600;">next</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;">this</span>.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">eq</span><span style="color: #66cc66;">&#40;</span>px<span style="color: #66cc66;">&#91;</span>slot.<span style="color: #006600;">getX</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>slot.<span style="color: #006600;">getY</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>, slot.<span style="color: #006600;">getValue</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;">;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hypothese : &quot;</span> + slot<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<ul>
<li>predefSlots est une liste d&#8217;objets Slots définissant une case par ses valeurs x,y,value.</li>
<li>Ma classe de Sudoku étends la classe Problem du framework, ce qui me permet d&#8217;utiliser this.post pour ajouter une contrainte.</li>
<li>j&#8217;ai définis deux Matrices (pour me simplifier la vie) l&#8217;une définissant p(x)[x][y] et la seconde p(y)[y][x].</li>
</ul>
<p>Cette contrainte indique donc que P(x)[valeurPrédéfinieX][valeurPrédéfinieY] doit etre égale à : valeurPrédéfinie.</p>
<h3>Les chiffres des colonnes doivent être uniques.</h3>

<div class="wp_syntax"><div class="code"><pre class="java"><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;">9</span><span style="color: #66cc66;">;</span> i++<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;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">allDifferent</span><span style="color: #66cc66;">&#40;</span>px<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</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;">this</span>.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">allDifferent</span><span style="color: #66cc66;">&#40;</span>py<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<ul>
<li>Contrainte : P(x)[Colonne] doivent tous être différent (this.allDifferent)</li>
<li>Contrainte : P(y)[Ligne] doivent tous être différent (this.allDifferent)</li>
</ul>
<p>C&#8217;est la première règle du jeu, chaque ligne et colonne ne doit contenir qu&#8217;une fois chaque valeurs!!!</p>
<h3>Chaque case de P(x) doit être équivalente à son opposée dans P(y).</h3>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">// b : px[i][j] = py[j][i]</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;">9</span><span style="color: #66cc66;">;</span> i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> j = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> j <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">;</span> j++<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;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">eq</span><span style="color: #66cc66;">&#40;</span>px<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>, py<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</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></pre></div></div>

<ul>
<li>Contrainte : P(y)[i][j] = P(x)[j][i]</li>
</ul>
<p>Pourquoi ? me simplifier le travail pour valider que chaque case de chaque colonne est unique, j&#8217;utilise deux Matrices, qui doivent bien sùr être totalement identiques.</p>
<h3>Chaque pavé de 3&#215;3 ne doit contenir que une seule fois chaque chiffre !!!</h3>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">// Constraint 2 : 3x3 Girds should be unique.</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;">3</span><span style="color: #66cc66;">;</span> i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #993333;">int</span> it = i <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> j = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> j <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">;</span> j++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #993333;">int</span> jt = j <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">allDifferent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> IntVar<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span> px<span style="color: #66cc66;">&#91;</span>it<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt<span style="color: #66cc66;">&#93;</span>,
		px<span style="color: #66cc66;">&#91;</span>it<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, px<span style="color: #66cc66;">&#91;</span>it<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>, px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt<span style="color: #66cc66;">&#93;</span>,
		px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>, px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt<span style="color: #66cc66;">&#93;</span>,
		px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, px<span style="color: #66cc66;">&#91;</span>it + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>jt + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#125;</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></pre></div></div>

<ul>
<li>Parcours de chaque lignes / colonnes (1 à 3 car fonctionnement par groupe de 3).</li>
<li>Doivent être différent : P(x)[x][y],  P(x)[x][y + 1], P(x)[x][y + 2], P(x)[x + 1][y], P(x)[x + 1][y + 1],  P(x)[x + 1][y + 2],  P(x)[x + 2][y], P(x)[x + 2][y + 1], P(x)[x + 2][y + 2]</li>
<li>Pa</li>
</ul>
<p>Et voila, c&#8217;est terminé&#8230;. simple non ? <img src='http://www.tellaw.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Maintenant il suffit de demander à Choco de résoudre le problème :</p>
<h3>Résolution par Choco</h3>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
	problem.<span style="color: #006600;">propagate</span><span style="color: #66cc66;">&#40;</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>ContradictionException e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">err</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This problem is over-constrained&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #808080; font-style: italic;">// System.exit(-1);</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;-----------------------------------------&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;   Solve   &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;-----------------------------------------&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>problem.<span style="color: #006600;">solve</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #aaaadd; font-weight: bold;">Boolean</span>.<span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the problem has at least one solution&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>problem.<span style="color: #006600;">toString</span><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: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the problem has no solution&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the problem has &quot;</span>	+ problem.<span style="color: #006600;">getSolver</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getNbSolutions</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>	+ <span style="color: #ff0000;">&quot; solutions&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<ul>
<li>1) Propagate, permet de propager les contraintes à l&#8217;ensemble du problème, afin de savoir si deja le probleme est insoluble.</li>
<li>2) Solve, permet de trouver 1 réponse au problème. bien sùr l&#8217;utilisation de solveAll est possible, mais peut être très coûteuse en temps car nous ne controllons pas le nombre de solutions possible à notre grille&#8230;.</li>
</ul>
<h3>Exemple de résultat de mon code JAVA :</h3>

<div class="wp_syntax"><div class="code"><pre class="text">Hypothese: [5][2] : 8
Hypothese: [3][2] : 2
Hypothese: [5][7] : 6
Hypothese: [6][5] : 8
Hypothese: [1][5] : 5
-------------------------------
----------   Solve   ----------
-------------------------------
the problem has at least one solution
-------------------------------------
| 7 | 8 | 2 | 4 | 5 | 1 | 9 | 3 | 6 |
| 5 | 9 | 1 | 6 | 7 | 3 | 2 | 4 | 8 |
| 4 | 3 | 6 | 2 | 9 | 8 | 1 | 5 | 7 |
| 6 | 7 | 9 | 8 | 3 | 4 | 5 | 2 | 1 |
| 1 | 2 | 8 | 9 | 6 | 5 | 3 | 7 | 4 |
| 3 | 5 | 4 | 7 | 1 | 2 | 8 | 6 | 9 |
| 8 | 4 | 3 | 1 | 2 | 7 | 6 | 9 | 5 |
| 9 | 1 | 5 | 3 | 4 | 6 | 7 | 8 | 2 |
| 2 | 6 | 7 | 5 | 8 | 9 | 4 | 1 | 3 |
-------------------------------------
the problem has 1 solutions
-- solve =&amp;gt; 1 solutions
-- 203[+0] millis.
-- 40[+0] nodes</pre></div></div>

<p>Voila, bientôt une application en ligne de Sudoku <img src='http://www.tellaw.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Si vous voulez le code source, n&#8217;hesitez pas à me contacter, un simple lien vers mon site et il sera à vous.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/18/programmation-par-contraintes-generation-de-grille-de-sudoku/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Riot &#8211; Comment Java peut devenir très simple.</title>
		<link>http://www.tellaw.org/2008/04/15/riot-comment-java-peux-devenir-tres-simple/</link>
		<comments>http://www.tellaw.org/2008/04/15/riot-comment-java-peux-devenir-tres-simple/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 07:52:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[riot]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=19</guid>
		<description><![CDATA[Riot est un framework open source mettant à disposition un CMS (Content Management System). Riot est une surcouche à Spring ajoutant des fonctionnalitées que nous allons passer en revue&#8230;

Alors, riot, ca permet de faire quoi ?
1) C&#8217;est un CMS très simple à mettre en oeuvre.
En fait Riot propose un service de CRUD (CReate Update Delete) [...]]]></description>
			<content:encoded><![CDATA[<p>Riot est un framework open source mettant à disposition un CMS (Content Management System). Riot est une surcouche à Spring ajoutant des fonctionnalitées que nous allons passer en revue&#8230;</p>
<p><span id="more-19"></span></p>
<h1><strong>Alors, riot, ca permet de faire quoi ?</strong></h1>
<p><strong>1) C&#8217;est un CMS très simple à mettre en oeuvre.</strong></p>
<p>En fait Riot propose un service de CRUD (CReate Update Delete) de données pour vos POJO (Plain Old Java Object) via l&#8217;utilisation d&#8217;un DAO générique (en général, il est très simple de mapper ses POJO sur Hibernate avec riot).</p>
<p>Pour cela il met à votre disposition 3 fichiers :</p>
<ul>
<li>list-config.xml -&gt; Qui configure les listes d&#8217;objets de l&#8217;interface.</li>
<li>form-config.xml -&gt; Qui configure le formulaire d&#8217;edition du pojo, et des regles de validations de données.</li>
<li>editor-config.xml -&gt; Qui sert de colle entre les deux éléments précédents.</li>
</ul>
<p>Un article complet va détailler toutes ces fonctionnalitées.</p>
<p style="text-align: center;"><a href="http://www.tellaw.org/wp-content/uploads/2008/04/00060047.gif"><img class="alignnone size-full wp-image-20" title="riot-list" src="http://www.tellaw.org/wp-content/uploads/2008/04/00060047.gif" alt="" width="470" height="215" /></a></p>
<p style="text-align: center;"><a href="http://www.tellaw.org/wp-content/uploads/2008/04/00060053.gif"><img class="alignnone size-full wp-image-21" title="riot - form" src="http://www.tellaw.org/wp-content/uploads/2008/04/00060053.gif" alt="" width="470" height="170" /></a></p>
<p style="text-align: left;clear:both"> </p>
<p style="text-align: center;"> </p>
<p style="text-align: left;"><strong>2) Un système basé sur l&#8217;utilisation de modules.</strong></p>
<p style="text-align: left;">Riot propose de simplifier l&#8217;utilisation de modules dans vos templates. Il suffit de définir des zone, et d&#8217;indiquer dans la configuration XML spring (soit via la syntaxe compacte riot, soit la syntaxe Spring), quels modules doivent etres empilés.</p>
<blockquote>
<p style="text-align: left;">&lt;template:definition name=&nbsp;&raquo;/posts.html&nbsp;&raquo; view-name=&nbsp;&raquo;default.ftl&nbsp;&raquo;&gt;<br />
&lt;template:insert slot=&nbsp;&raquo;navigation&nbsp;&raquo; url=&nbsp;&raquo;/inc/navigation.html&nbsp;&raquo; /&gt;<br />
&lt;template:insert slot=&nbsp;&raquo;content&nbsp;&raquo; url=&nbsp;&raquo;/inc/all-posts.ftl&nbsp;&raquo; /&gt;<br />
&lt;/template:definition&gt;</p>
</blockquote>
<p><strong>3) Un ré-ecriture des URLs très simple.</strong></p>
<p>La ré-écriture d&#8217;url tourne souvent à une enorme usine à gaz, alors que c&#8217;est en réalité quelque chose de très très simple, et systématique sur un site web.</p>
<p>Riot met donc en oeuvre des variable dans le nommage des bean, afin de mettre des URL &laquo;&nbsp;SEO compliantes&nbsp;&raquo;.</p>
<blockquote><p>&lt;bean name=&nbsp;&raquo;/some/@{myparam}/page.html&nbsp;&raquo; id=&nbsp;&raquo;myController&nbsp;&raquo; class=&nbsp;&raquo;eg.MyController&nbsp;&raquo; /&gt;</p></blockquote>
<p>Votre page aura l&#8217;url : /some/45/page.html. Pas mal non ? Ligne de code ????? 0!</p>
<p>Pour appeler ce controlleur depuis un template, pas question de codé l&#8217;url en dur, utilisez :</p>
<blockquote><p>&lt;a href=&nbsp;&raquo;${common.urlForHandler(&#8217;myController&#8217;, myparam)}&nbsp;&raquo;&gt;<br />
${category.title}<br />
&lt;/a&gt;</p></blockquote>
<p><strong>4) les controlleurs génériques.</strong></p>
<p>Généralement, une majoritée de pages Web, se contente de recuperer des objets et d&#8217;en afficher le contenu. Alors pourquoi utiliser de multiple controlleur pour faire cette opération ?</p>
<p>Riot propose un controlleur générique, suivant la syntaxe :</p>
<blockquote><p>&lt;generic:controller id=&nbsp;&raquo;postDetail&nbsp;&raquo; name=&nbsp;&raquo;/post-@{id:Long}.html&nbsp;&raquo;<br />
view-name=&nbsp;&raquo;/inc/post-detail.ftl&nbsp;&raquo;&gt;<br />
&lt;generic:hql hql=&nbsp;&raquo;from Post p where p.id = :id&nbsp;&raquo; /&gt;<br />
&lt;/generic:controller&gt;</p></blockquote>
<p>Qui va recuperer l&#8217;objet &laquo;&nbsp;post&nbsp;&raquo; et le passer simplement à la vue.</p>
<p><strong>5) Overriding de configuration Spring</strong></p>
<p>La c&#8217;est assez sympa, le principe est de se dire, que parfois, il faut pouvoir redéfinir un élément de configuration, sans redéfinir le bean complet. Pour cela, Riot propose quelques éléments :</p>
<blockquote><p>&lt;override:properties ref=&nbsp;&raquo;someBean&nbsp;&raquo;&gt;<br />
&lt;property name=&nbsp;&raquo;foo&nbsp;&raquo; value=&nbsp;&raquo;bar&nbsp;&raquo; /&gt;<br />
&lt;/override:properties&gt;</p></blockquote>
<p><strong>6) Plus beaucoup d&#8217;autres choses&#8230;</strong></p>
<ul>
<li>Push-up qui permettent la communication entre &laquo;&nbsp;modules&nbsp;&raquo;</li>
<li>Squelette de base pour débuter un projet.</li>
<li>Plus beaucoup d&#8217;élements à decouvrir.</li>
</ul>
<p><strong>Attention tout bonheur à ses petit defauts&#8230;</strong></p>
<p>Un gros manque de documentation sur le projet. Mais une communauté plus que réactive.</p>
<ul>
<li><span style="border-collapse: separate; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; font-family: 'Lucida Grande'; color: #000000;">Site : <a href="http://www.riotfamily.org/" target="_blank">http://www.riotfamily.org/</a></span></li>
<li><span style="border-collapse: separate; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; font-family: 'Lucida Grande'; color: #000000;">Le groupe google : </span><a href="http://groups.google.com/group/riotfamily" target="_blank"><span style="border-collapse: separate; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; font-family: 'Lucida Grande'; color: #000000;">http://groups.google.com/group/riotfamily</span></a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/15/riot-comment-java-peux-devenir-tres-simple/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
