<?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>LogikDevelopment &#187; Java</title>
	<atom:link href="http://www.logikdev.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Fri, 27 Aug 2010 08:54:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to personalise the URLs with Faces Navigation?</title>
		<link>http://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/</link>
		<comments>http://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 20:01:29 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EL expressions]]></category>
		<category><![CDATA[Faces Navigation]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[view handler]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=418</guid>
		<description><![CDATA[This is an important question if you want to have search engine friendly URLs on your website! But unfortunately, the solution is not straightforward with JavaServer Faces (JSF). First of all, for security reason JSF doesn&#8217;t allow you to use the GET method for your forms. I didn&#8217;t really understand why but this is very [...]]]></description>
			<content:encoded><![CDATA[<p>This is an important question if you want to have search engine friendly URLs on your website!<br />
But unfortunately, the solution is not straightforward with JavaServer Faces (JSF). <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>First of all, for security reason JSF doesn&#8217;t allow you to use the GET method for your forms. I didn&#8217;t really understand why but this is very (too) restrictive!<br />
Can you imagine Google doing the same thing? We would have the same URL for every search terms <code>http://www.google.co.uk/search</code> instead of something like <code>http://www.google.co.uk/search?hl=en&amp;safe=off&amp;esrch=FT1&amp;q=test&amp;meta=&amp;aq=f&amp;aqi=g10&amp;aql=&amp;oq=&amp;gs_rfai=</code>.<br />
It wouldn&#8217;t be very easy to share a search page with a friend if Google was using the POST method. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So the question is how to get around this JSF limitation?</p>
<p>Let&#8217;s take a look at how would look our <code>faces-navigation.xml</code> file for a search page:</p>
<pre class="brush: xml;">
&lt;navigation-case&gt;
    &lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
    &lt;from-outcome&gt;success&lt;/from-outcome&gt;
    &lt;to-view-id&gt;/search.xhtml&lt;/to-view-id&gt;
    &lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>In this example, all the JSF elements calling the action <code>searchBean.searchAction</code> will be redirected to the <code>search.xhtml</code> page.<br />
But, how are we going to get the search parameters into the URL?</p>
<p>Ideally, it would be great to be able to do something like the following:</p>
<pre class="brush: xml;">
&lt;navigation-case&gt;
    &lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
    &lt;from-outcome&gt;success&lt;/from-outcome&gt;
    &lt;to-view-id&gt;/search.xhtml?q=#{param.q}&lt;/to-view-id&gt;
    &lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>This solution would allow us to inject EL expressions into the URL before the page is redirected to the destination page.<br />
In order to do this, we need to create our own view handler and register it to our application.  <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_cool.gif' alt='8-)' class='wp-smiley' /> </p>
<p>The code below is the view handler class which also includes some comments:</p>
<pre class="brush: java;">
package com.logikdev.gui.handler;

import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.ViewHandler;
import javax.faces.context.FacesContext;

import com.sun.facelets.FaceletViewHandler;

/**
 * Overrides the Facelet view handler to support EL expressions in URLs.
 * @author Stéphane Moreau
 */
public class DynamicViewHandler extends FaceletViewHandler {

	public DynamicViewHandler(ViewHandler parent) {
		super(parent);
	}

	/* (non-Javadoc)
	 * @see com.sun.facelets.FaceletViewHandler#getActionURL(javax.faces.context.FacesContext, java.lang.String)
	 */
	@Override
	public String getActionURL(FacesContext context, String viewId) {
		String queryString = null;

		// Separate the query string from the URL
		int dotIndex = viewId.lastIndexOf('.');
		int questionMarkIndex = viewId.indexOf('?');
		if (questionMarkIndex != -1) {
			queryString = viewId.substring(questionMarkIndex, dotIndex);
			viewId = viewId.substring(0, questionMarkIndex) + viewId.substring(dotIndex);
		}

		// Call the parent without the query string
		String result = super.getActionURL(context, viewId);

		// Replace the EL expressions in the URL
		ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
		ValueExpression valueExpression = expressionFactory.createValueExpression(context.getELContext(), result, String.class);
		result = (String) valueExpression.getValue(context.getELContext());

		// Replace the EL expressions in the query string
		if (queryString != null) {
			valueExpression = expressionFactory.createValueExpression(context.getELContext(), queryString, String.class);
			queryString = (String) valueExpression.getValue(context.getELContext());

	        result += queryString;
		}

	    return result;
	}

}
</pre>
<p>And the following is the code to put in the <code>faces-config.xml</code> file in order to register the newly created view handler to the application:</p>
<pre class="brush: xml;">
&lt;application&gt;
	&lt;variable-resolver&gt;org.springframework.web.jsf.DelegatingVariableResolver&lt;/variable-resolver&gt;
	&lt;view-handler&gt;com.logikdev.gui.handler.DynamicViewHandler&lt;/view-handler&gt;
&lt;/application&gt;
</pre>
<p><strong>One last thing!</strong><br />
This view handler also has a limitation which I wasn&#8217;t able to fix. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  The file extension has to ALWAYS be placed at the end of the <code>to-view-id</code> URL! The view handler will then put it back before the question mark.<br />
For example:</p>
<pre class="brush: xml;">
&lt;navigation-case&gt;
	&lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
	&lt;from-outcome&gt;success&lt;/from-outcome&gt;
	&lt;!-- The extension has to be at the end --&gt;
	&lt;to-view-id&gt;/search?q=#{param.q}.xhtml&lt;/to-view-id&gt;
	&lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>If you perform a search on &#8216;jsf&#8217; with the above navigation rule, the user will be redirected to the page <code>/search.xhtml?q=jsf</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>limitToList attribute prevents flashing</title>
		<link>http://www.logikdev.com/2010/03/02/limittolist-attribute-prevents-flashing/</link>
		<comments>http://www.logikdev.com/2010/03/02/limittolist-attribute-prevents-flashing/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 18:56:36 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[flash/twinkle]]></category>
		<category><![CDATA[limitToList]]></category>
		<category><![CDATA[RichFaces]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=395</guid>
		<description><![CDATA[If you have some elements (or even the whole page) that flash/twinkle using RichFaces, it probably means that these elements are AJAX-rendering. The question is by whom and how to fix it? A lot of tags in RichFaces can AJAX-render elements such as: &#60;a4j:form&#62; &#60;a4j:jsFunction name=&#34;updateName&#34; reRender=&#34;showname&#34;&#62; &#60;a4j:actionparam name=&#34;param1&#34; assignTo=&#34;#{userBean.name}&#34; /&#62; &#60;/a4j:jsFunction&#62; &#60;/a4j:form&#62; On the above [...]]]></description>
			<content:encoded><![CDATA[<p>If you have some elements (or even the whole page) that flash/twinkle using RichFaces, it probably means that these elements are AJAX-rendering. The question is by whom and how to fix it?</p>
<p>A lot of tags in RichFaces can AJAX-render elements such as:</p>
<pre class="brush: xml;">
&lt;a4j:form&gt;
    &lt;a4j:jsFunction name=&quot;updateName&quot; reRender=&quot;showname&quot;&gt;
        &lt;a4j:actionparam name=&quot;param1&quot; assignTo=&quot;#{userBean.name}&quot;  /&gt;
    &lt;/a4j:jsFunction&gt;
&lt;/a4j:form&gt;
</pre>
<p>On the above example, the JavaScript function <code>updateName</code> will AJAX-render the element which has the ID <code>showname</code>.</p>
<p>In some cases, you would have some elements that would AJAX-render without asking them to do so!<br />
I still didn&#8217;t figure it out why. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  (if anybody has an idea, please don&#8217;t hesitate to tell me!)</p>
<p>But, I found a way to prevent this!<br />
You simply can add the following attribute to your tag:</p>
<pre class="brush: xml; light: true;">limitToList=&quot;true&quot;</pre>
<p>You even can add it to the tags that don&#8217;t have a <code>reRender</code> attribute.<br />
For example:</p>
<pre class="brush: xml;">
&lt;a4j:form&gt;
    &lt;a4j:poll id=&quot;poll&quot; interval=&quot;1000&quot; limitToList=&quot;true&quot; /&gt;
&lt;/a4j:form&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2010/03/02/limittolist-attribute-prevents-flashing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WritableFont doesn&#8217;t like to be static!</title>
		<link>http://www.logikdev.com/2010/01/18/writablefont-doesnt-like-to-be-static/</link>
		<comments>http://www.logikdev.com/2010/01/18/writablefont-doesnt-like-to-be-static/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 18:24:21 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ArrayIndexOutOfBoundsException]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[JExcelAPI]]></category>
		<category><![CDATA[JXL]]></category>
		<category><![CDATA[WritableFont]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=265</guid>
		<description><![CDATA[While using the tool JExcelAPI within my Java application to generate Excel spreadsheets, I got the following exception: java.lang.ArrayIndexOutOfBoundsException: 5 at jxl.biff.IndexMapping.getNewIndex(IndexMapping.java:68) at jxl.biff.FormattingRecords.rationalize(FormattingRecords.java:388) at jxl.write.biff.WritableWorkbookImpl.rationalize(WritableWorkbookImpl.java:988) at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:692) ... It appears that this exception occurs only when you try to generate more than one Excel spreadsheet! How strange is that! After a bit of investigation, [...]]]></description>
			<content:encoded><![CDATA[<p>While using the tool <a href="http://jexcelapi.sourceforge.net/"><strong>JExcelAPI</strong></a> within my Java application to generate Excel spreadsheets, I got the following exception:</p>
<pre>java.lang.ArrayIndexOutOfBoundsException: 5
	at jxl.biff.IndexMapping.getNewIndex(IndexMapping.java:68)
	at jxl.biff.FormattingRecords.rationalize(FormattingRecords.java:388)
	at jxl.write.biff.WritableWorkbookImpl.rationalize(WritableWorkbookImpl.java:988)
	at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:692)
	...</pre>
<p><br/>It appears that this exception occurs only when you try to generate more than one Excel spreadsheet!  How strange is that! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_eek.gif' alt='8-O' class='wp-smiley' /><br />
After a bit of investigation, it seems that the problem comes from the use of the static modifier with a <code>jxl.write.WritableFont</code> variable.</p>
<p>I unfortunately don&#8217;t have the time to check the JExcelAPI code source to understand the root cause.<br />
So my advice would be: &#8220;if you get EXACTLY the same stack trace, simply delete any static modifier you used with the <code>WritableFont</code> variables&#8221;. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2010/01/18/writablefont-doesnt-like-to-be-static/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploy your app to the root context</title>
		<link>http://www.logikdev.com/2010/01/06/deploy-your-app-to-the-root-context/</link>
		<comments>http://www.logikdev.com/2010/01/06/deploy-your-app-to-the-root-context/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 20:10:49 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[root context]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=243</guid>
		<description><![CDATA[This is an easy trick which I am sure most of you already know. Let&#8217;s take a Java application called MyAddressBook. Its generated war file could be called myaddressbook.war. By default, when you deploy this web application to Tomcat, the URL to access it will be http://localhost:8080/myaddressbook/. And if you point a domain name such [...]]]></description>
			<content:encoded><![CDATA[<p>This is an easy trick which I am sure most of you already know.</p>
<p>Let&#8217;s take a Java application called MyAddressBook. Its generated war file could be called <code>myaddressbook.war</code>.</p>
<p>By default, when you deploy this web application to Tomcat, the URL to access it will be <em>http://localhost:8080/myaddressbook/</em>. And if you point a domain name such as &#8216;addressbook.com&#8217; to this server, the URL would be <em>http://addressbook.com/myaddressbook/</em>.</p>
<p>I don&#8217;t know for you but I don&#8217;t like to systematically have the subfolder &#8216;myaddressbook&#8217; after my domain. But maybe I am too picky! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The idea is to deploy our application in the Tomcat root context.<br />
You have two ways of doing this:</p>
<ul>
<li>Define a <code>ROOT.xml</code> file in your <code>conf/Catalina/localhost</code> folder, or;</li>
<li>Rename your war file to <code>ROOT.war</code>.</li>
</ul>
<p>Note that the case is important, it has to be ROOT in <strong>UPPERCASE</strong>! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Once this is done, you will be able to call your application via the URL <em>http://localhost:8080/</em><br />
or <em>http://addressbook.com/</em>.  Way better! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_cool.gif' alt='8-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2010/01/06/deploy-your-app-to-the-root-context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to monitor your Java application</title>
		<link>http://www.logikdev.com/2009/12/17/how-to-monitor-your-java-application/</link>
		<comments>http://www.logikdev.com/2009/12/17/how-to-monitor-your-java-application/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 22:04:35 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Hibernate MBean]]></category>
		<category><![CDATA[JMX Remote]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[Zabbix]]></category>
		<category><![CDATA[Zapcat]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=207</guid>
		<description><![CDATA[It is very good to have your application and your database running on a Linux or Windows server, but who will tell you if your website is down? Who said &#8220;the users&#8221;? No, you won&#8217;t look very professional if you wait for a user complaint. What you need to do is to monitor your server. [...]]]></description>
			<content:encoded><![CDATA[<p>It is very good to have your application and your database running on a Linux or Windows server, but who will tell you if your website is down? Who said &#8220;the users&#8221;? <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_eek.gif' alt='8-O' class='wp-smiley' />  No, you won&#8217;t look very professional if you wait for a user complaint.</p>
<p>What you need to do is to monitor your server. But which tools to use? There are so many on the market&#8230; <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>I tested a few of them (Hyperic, Nagios, Zenoss, Cacti, Monitis) but the one I choose is <a href="http://www.zabbix.com/" target="_blank"><strong>Zabbix</strong></a>. What I like with Zabbix is its price (free  <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_cool.gif' alt='8-)' class='wp-smiley' /> ) and the fact that it is easy to configure and very flexible. Indeed, you can monitor anything you want on the machine (CPU, network, disk space, services, etc)! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>However, if you also want to monitor your Tomcat application server or even Hibernate Java library, you need some more work.</p>
<p><br/>To monitor Tomcat, you first need to enable <strong>JMX Remote</strong>. To do so, you can have a look at the <a href="http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html" target="_blank">official documentation</a> or simply add the following parameters to your Tomcat startup script:</p>
<pre class="brush: bash; light: true;">
export CATALINA_OPTS='-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false'
</pre>
<p><br/>Then, I will recommend you to deploy <a href="http://www.kjkoster.org/zapcat/Zapcat_JMX_Zabbix_Bridge.html" target="_blank"><strong>Zapcat JMX Zabbix Bridge</strong></a> in your Tomcat web server. This tool has been developed to simplify the communication between the JMX management API inside Java applications and the Zabbix monitoring tool.</p>
<p>To read the installation instructions for Tomcat, please click on the following link:<br />
<a href="http://www.kjkoster.org/zapcat/Tomcat_How_To.html" target="_blank">http://www.kjkoster.org/zapcat/Tomcat_How_To.html</a></p>
<p><br/>Finally, if you are using Hibernate in your Java application and would like to monitor it, you have to instantiate and configure a Hibernate MBean (<code>org.jboss.hibernate.jmx.Hibernate</code>) that will be responsible for constructing a Hibernate <code>SessionFactory</code> and exposing it to your application through JNDI.</p>
<p>Here are the lines you need to add to your context XML file:</p>
<pre class="brush: xml;">
&lt;bean id=&quot;mbeanExporter&quot; class=&quot;org.springframework.jmx.export.MBeanExporter&quot; lazy-init=&quot;false&quot;&gt;
    &lt;property name=&quot;server&quot; ref=&quot;mbeanServerFactory&quot;/&gt;
    &lt;property name=&quot;beans&quot;&gt;
        &lt;map&gt;
            &lt;entry key=&quot;org.hibernate:type=statistics&quot;  value-ref=&quot;hibernateMBean&quot;/&gt;
        &lt;/map&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id=&quot;mbeanServerFactory&quot; class=&quot;org.springframework.jmx.support.MBeanServerFactoryBean&quot;&gt;
    &lt;property name=&quot;locateExistingServerIfPossible&quot; value=&quot;true&quot;/&gt;
&lt;/bean&gt;

&lt;bean id=&quot;hibernateMBean&quot; class=&quot;org.hibernate.jmx.StatisticsService&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
    &lt;property name=&quot;statisticsEnabled&quot; value=&quot;true&quot;/&gt;
&lt;/bean&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2009/12/17/how-to-monitor-your-java-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
