<?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>My IT-blog &#187; tutorial</title>
	<atom:link href="http://dev.eek.be/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.eek.be</link>
	<description>IT knowledge exchange</description>
	<lastBuildDate>Fri, 04 Jun 2010 05:34:15 +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>Change folders for Synology media server</title>
		<link>http://dev.eek.be/2010/01/change-folders-for-synology-media-server/</link>
		<comments>http://dev.eek.be/2010/01/change-folders-for-synology-media-server/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 22:07:17 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[mount]]></category>
		<category><![CDATA[nas]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[Synology]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=264</guid>
		<description><![CDATA[When you enable the Synology Media Server on your NAS, there are 3 directories added to your file-system (video, music and photo) which will be used for the DLNA/UPnP media server to play the corresponding files. I wasn&#8217;t very happy with these 3 directories because I wanted to categorize my media in other directories. In [...]]]></description>
			<content:encoded><![CDATA[<p>When you enable the Synology Media Server on your NAS, there are 3 directories added to your file-system (video, music and photo) which will be used for the DLNA/UPnP media server to play the corresponding files.</p>
<p>I wasn&#8217;t very happy with these 3 directories because I wanted to categorize my media in other directories. In this post, I will post how I&#8217;ve resolved this problem.</p>
<p>First, enable SSH access to the NAS and log in.<br />
<a href="http://dev.eek.be/wp-content/uploads/2010/01/syn1.png"><img src="http://dev.eek.be/wp-content/uploads/2010/01/syn1.png" alt="" title="syn1" width="640" height="400" class="alignleft size-full wp-image-265" /></a></p>
<p>My first idea to tackle this issue was to use of symbolic links</p>
<pre class="prettyprint" lang-java>
ln -s /volume1/MyMusicDirectory /volume1/music
</pre>
<p>I&#8217;ve re-indexed my files on the NAS and everything worked instantly. All the files in MyMusicDirectory where indexed so I was very happy&#8230;but not for long.<br />
I noticed quickly that the new files I&#8217;ve placed in MyMusicDirectory weren&#8217;t auto-indexed by the NAS. I always had to re-index through the webinterface which can take hours when there is a lot of data to index so that was a big issue.<br />
I figured out what the problem was: symbolic links will be considered as files and not as directories so the autoindexer wouldn&#8217;t follow the symbolic link.</p>
<p>Up to idea 2: mount &#8211;bind<br />
I used mount &#8211;bind to create an unbreakable link which normally would be used as a directory by the NAS so the contents could be auto-indexed.</p>
<pre class="prettyprint" lang-java>
mount --bind /volume1/MyMusicDirectory /volume1/music
</pre>
<p>It worked!! YES!!<br />
Till I&#8217;ve rebooted the NAS. The mount was gone and I had to manually add it again. Not something I want to do at every reboot.</p>
<p>Up to idea 3: changing fstab<br />
The file /etc/fstab will be loaded at startup to mount the filesystem so I&#8217;ve added the following rule in the file:</p>
<pre class="prettyprint" lang-java>
/volume1/MyMusicDirectory /volume1/music bind defaults,bind 0 0
</pre>
<p>I saved the file and rebooted and it didn&#8217;t worked. The fstab file will be overridden at startup with Synology&#8217;s default fstab-file so the rule I&#8217;ve entered before was deleted.</p>
<p>And then idea 3: <b>The solution</b><br />
Create a file rc.local</p>
<pre class="prettyprint" lang-java>
touch /etc/rc.local
</pre>
<p>open the file and add the following line:</p>
<pre class="prettyprint" lang-java>
mount --bind /volume1/MyMusicDirectory /volume1/music
</pre>
<p>Now restart the NAS and you&#8217;re done.<br />
The files in MyMusicDirectory will be auto-indexed by the media server.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2010/01/change-folders-for-synology-media-server/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The lifecycle of a Spring bean</title>
		<link>http://dev.eek.be/2009/11/the-lifecycle-of-a-spring-bean/</link>
		<comments>http://dev.eek.be/2009/11/the-lifecycle-of-a-spring-bean/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 17:30:42 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[Dependency injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[Inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=218</guid>
		<description><![CDATA[In this blogpost, I&#8217;ll talk and give a few examples of the lifecycle of a bean in a Spring container. The examples are tested with Spring 3.0RC1 but should work with Spring 2.5+ Xml Attributes In this first example, I&#8217;ll show you how the lifecycle of a bean happens within the xml-configfile. In the xml-file, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.eek.be/wp-content/uploads/2009/11/beans-150x150.jpg" alt="Beans" title="beans" width="150" height="150" class="size-thumbnail wp-image-228" /><br />
In this blogpost, I&#8217;ll talk and give a few examples of the lifecycle of a bean in a Spring container.<br />
The examples are tested with Spring 3.0RC1 but should work with Spring 2.5+</p>
<p><h1>Xml Attributes</h1>
<p>In this first example, I&#8217;ll show you how the lifecycle of a bean happens within the xml-configfile.</p>
<p>In the xml-file, we can define an init- and destroy-method to the bean, which will be called automatically by Spring.<br />
<b>Config.xml</b></p>
<pre class="prettyprint" lang-xml>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;

    &lt;bean id="attributesTest" class="a.Test" init-method="initMethod" destroy-method="destroyMethod"&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</pre>
<p><b>Main program</b><br />
In this case, I&#8217;ll use AbstractApplicationContext because this Context has a function to destroy the Context which a normal ApplicationContext doesn&#8217;t have.</p>
<pre class="prettyprint" lang-java>
package a;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XmlAttributesTest {
    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("Config.xml");
        ctx.registerShutdownHook();
        Test test =  ctx.getBean("attributesTest",Test.class);
    }
}
</pre>
<p><b>Test.java</b></p>
<pre class="prettyprint" lang-java>
package a;

public class Test{

    public Test(){
        System.out.println("We are in the constructor of Test");
    }

    public void initMethod(){
        System.out.println("We are in initMethod of Test");
    }

    public void destroyMethod(){
        System.out.println("We are in destroyMethod of Test");
    }

}
</pre>
<p>The output will be:</p>
<pre class="prettyprint" lang-java>
We are in the constructor of Test
We are in initMethod of Test
We are in destroyMethod of Test
</pre>
<p>It&#8217;s also possible to declare default init- and destroy-methods in the xml-file. This is done in the beans-tag:</p>
<pre class="prettyprint" lang-xml>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       default-init-method="initMethod"
       default-destroy-method="destroyMethod"&gt;
...
</pre>
</p>
<p><h1>LifeCycles with interfaces</h1>
<p>It&#8217;s possible to call the lifecyclemethods by implemnting a Spring interface.<br />
It&#8217;s very easy to do but I don&#8217;t recommend this because when you use the interfaces, your code will be highly coupled to the Spring Framework which isn&#8217;t always a good case.<br />
<b>Config.xml</b><br />
You don&#8217;t have to set an init and destroy method in the xml-file</p>
<pre class="prettyprint" lang-xml>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;

    &lt;bean id="interfacesTest" class="b.Test"&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</pre>
<p><b>Main program</b><br />
This is always the same&#8230;</p>
<pre class="prettyprint" lang-java>
package b;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InterfaceTest {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("Config.xml");
        ctx.registerShutdownHook();
        Test test =  ctx.getBean("interfacesTest",Test.class);
    }
}
</pre>
<p><b>Test.java</b><br />
We have to implement InitializingBean for init-methods and DisposableBean for destroy-methods</p>
<pre class="prettyprint" lang-java>
package b;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Test implements InitializingBean, DisposableBean {

    public Test(){
        System.out.println("We are in the constructor of Test");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("We are in afterPropertiesSet of Test");
    }

    public void destroy() throws Exception {
        System.out.println("We are in destroy of Test");
    }
}
</pre>
</p>
<p><h1>Lifecycle with annotations</h1>
<p><b>Config.xml</b><br />
Don&#8217;t forget to include to enable annotations with the Contextparameters in the xml-file</p>
<pre class="prettyprint" lang-xml>
&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
   &lt;context:annotation-config/&gt;

    &lt;bean id="annotationsTest" class="c.Test"&gt;
    &lt;/bean&gt;

&lt;/beans&gt;
</pre>
<p><b>Main program</b><br />
Still the same&#8230;</p>
<pre class="prettyprint" lang-java>
package c;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationsTest {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("Config.xml");
        ctx.registerShutdownHook();
        Test test = ctx.getBean("annotationsTest", Test.class);
    }
}
</pre>
<p><b>Test.java</b><br />
We can use the lifecyclemethods with the annotations @PostConstruct for init-methods and @PostDestroy for destroy-methods.</p>
<pre class="prettyprint" lang-java>
package c;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Test {

    public Test() {
        System.out.println("We are in the constructor of Test");
    }

    @PostConstruct
    public void start(){
        System.out.println("We are in the start-method of Test");
    }

    @PreDestroy
    public void stop(){
        System.out.println("We are in the stop-method of Test");
    }
}
</pre>
</p>
<p><h1>Putting them all together</h1>
<p>It&#8217;s possible to use the 3 methods all together<br />
<b>Config.xml</b></p>
<pre class="prettyprint" lang-xml>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
   &lt;context:annotation-config/&gt;

    &lt;bean id="allTogether" class="d.Test" init-method="initMethod" destroy-method="destroyMethod"&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</pre>
<p><b>Main program</b></p>
<pre class="prettyprint" lang-java>
package d;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AllTogetherTest {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("Config.xml");
        ctx.registerShutdownHook();
        Test test = ctx.getBean("allTogether", Test.class);
    }
}
</pre>
<p><b>Test.java</b><br />
We are using interfaces AND annotations.<br />
It&#8217;s even possible to use multiple times the same annotation:</p>
<pre class="prettyprint" lang-java>
package d;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Test implements DisposableBean,InitializingBean {

    public void destroy() throws Exception {
        System.out.println("DisposableBean-Interface");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean-Interface");
    }

    @PostConstruct
    public void start(){
        System.out.println("First PostConstruct");
    }

    @PostConstruct
    public void start2(){
        System.out.println("Second PostConstruct");
    }

    @PostConstruct
    public void start3(){
        System.out.println("Third PostConstruct");
    }

    @PreDestroy
    public void stop(){
        System.out.println("PreDestroy");
    }

    public void initMethod(){
        System.out.println("initMethod");
    }

    public void destroyMethod(){
        System.out.println("destroyMethod");
    }
}
</pre>
<p>The output is:</p>
<pre class="prettyprint" lang-text>
Third PostConstruct
Second PostConstruct
First PostConstruct
InitializingBean-Interface
initMethod
PreDestroy
DisposableBean-Interface
destroyMethod
</pre>
<p>So we can conclude that the annotations are processed first, followed by the interfaces and at last the methods from the xml-file.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/11/the-lifecycle-of-a-spring-bean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; An introduction</title>
		<link>http://dev.eek.be/2009/11/php-an-introduction/</link>
		<comments>http://dev.eek.be/2009/11/php-an-introduction/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 13:06:08 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=180</guid>
		<description><![CDATA[I had to give a presentation about PHP at my work earlier this year. It was a quick and simple introduction about PHP and his features. If you want to use this presentation by yourself, please leave me a comment. odp-file ppt-file pptx-file pdf-file]]></description>
			<content:encoded><![CDATA[<p>I had to give a presentation about PHP at my work earlier this year.<br />
It was a quick and simple introduction about PHP and his features.<br />
If you want to use this presentation by yourself, please leave me a comment.<br />
<a href="http://dev.eek.be/wp-content/uploads/2009/11/PHP-An-introduction.odp">odp-file</a><br />
<a href="http://dev.eek.be/wp-content/uploads/2010/04/PHP-An-introduction.ppt">ppt-file</a><br />
<a href="http://dev.eek.be/wp-content/uploads/2009/11/PHP-An-introduction.pptx">pptx-file</a><br />
<a href="http://dev.eek.be/wp-content/uploads/2009/11/PHP-An-introduction.pdf">pdf-file</a></p>
<p><img src="http://dev.eek.be/wp-content/uploads/2009/11/img0.png" alt="PHP-An introduction" title="PHP-An introduction" width="512" height="384" class="aligncenter size-full wp-image-186" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img1.png" alt="Agenda" title="Agenda" width="512" height="384" class="aligncenter size-full wp-image-188" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img2.png" alt="History of PHP" title="History of PHP" width="512" height="384" class="aligncenter size-full wp-image-190" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img3.png" alt="What is PHP" title="What is PHP" width="512" height="384" class="aligncenter size-full wp-image-191" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img4.png" alt="img4" title="img4" width="512" height="384" class="aligncenter size-full wp-image-192" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img5.png" alt="types" title="types" width="512" height="384" class="aligncenter size-full wp-image-193" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img6.png" alt="Variables" title="Variables" width="512" height="384" class="aligncenter size-full wp-image-194" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img7.png" alt="Predefined variables" title="Predefined Variables" width="512" height="384" class="aligncenter size-full wp-image-195" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img8.png" alt="Predefined variables" title="Predefined Variables" width="512" height="384" class="aligncenter size-full wp-image-196" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img9.png" alt="Predefined variables example" title="Predefined variables example" width="512" height="384" class="aligncenter size-full wp-image-197" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img10.png" alt="Variables - Scope" title="Variables - Scope" width="512" height="384" class="aligncenter size-full wp-image-198" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img11.png" alt="variable variables" title="variable variables" width="512" height="384" class="aligncenter size-full wp-image-199" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img12.png" alt="operators and control structures" title="operators and control structures" width="512" height="384" class="aligncenter size-full wp-image-200" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img13.png" alt="Functions" title="Functions" width="512" height="384" class="aligncenter size-full wp-image-201" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img14.png" alt="Classes and Objects" title="Classes and Objects" width="512" height="384" class="aligncenter size-full wp-image-202" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img15.png" alt="Constructors and destructors" title="Constructors and destructors" width="512" height="384" class="aligncenter size-full wp-image-203" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img16.png" alt="visibility" title="visibility" width="512" height="384" class="aligncenter size-full wp-image-204" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img17.png" alt="Scope resolution operator" title="Scope resolution operator" width="512" height="384" class="aligncenter size-full wp-image-205" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img18.png" alt="Abstract classes and interfaces" title="Abstract classes and interfaces" width="512" height="384" class="aligncenter size-full wp-image-206" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img19.png" alt="Abstract classes and interfaces" title="Abstract classes and interfaces" width="512" height="384" class="aligncenter size-full wp-image-207" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img20.png" alt="Magic methods" title="Magic methods" width="512" height="384" class="aligncenter size-full wp-image-208" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img21.png" alt="Reflection" title="Reflection" width="512" height="384" class="aligncenter size-full wp-image-209" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img22.png" alt="Popular PHP tools" title="Popular PHP tools" width="512" height="384" class="aligncenter size-full wp-image-210" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img23.png" alt="More info" title="More info" width="512" height="384" class="aligncenter size-full wp-image-211" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img24.png" alt="Questions??" title="Questions??" width="512" height="384" class="aligncenter size-full wp-image-212" /><br />
<img src="http://dev.eek.be/wp-content/uploads/2009/11/img25.png" alt="Contact" title="Contact" width="512" height="384" class="aligncenter size-full wp-image-213" /></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/11/php-an-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>servletconfig vs servletcontext</title>
		<link>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/</link>
		<comments>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 20:15:58 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server management]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=80</guid>
		<description><![CDATA[In this little blogpost, I&#8217;ll try to explain the differences between ServletConfig and ServletContext and when you can use them into a Java Web Application. ServletConfig ServletContext Access deploy-time servlet parameters Access web application parameters One per servlet One per web application ServletConfig ServletConfig is actually very simple. Like stated above, you can retrieve parameters [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.eek.be/wp-content/uploads/2009/10/java2.jpg" alt="java2" title="java2" width="300" height="225" class="aligncenter size-full wp-image-151" />
<p>In this little blogpost, I&#8217;ll try to explain the differences between ServletConfig and ServletContext and when you can use them into a Java Web Application.</p>
<table cellpadding="2" cellspacing="0" border="1">
<tr>
<td><b>ServletConfig</b></td>
<td><b>ServletContext</b></td>
</tr>
<tr>
<td>Access deploy-time servlet parameters</td>
<td>Access web application parameters</td>
</tr>
<tr>
<td>One per servlet</td>
<td>One per web application</td>
</tr>
</table>
<p><b>ServletConfig</b></p>
<p>ServletConfig is actually very simple. Like stated above, you can retrieve parameters for the servlet which are set at deployment time.<br />
I&#8217;ll explain this with a very simple example.
</p>
<p>
If you have created a servlet with the name &#8220;MyServlet&#8221; you can add the following 2 variables into the servlet:</p>
<pre class="prettyprint" lang-xml>
    &lt;servlet&gt;
         &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
         &lt;servlet-class&gt;servlets.MyServlet&lt;/servlet-class&gt;
         &lt;init-param&gt;
             &lt;param-name&gt;variable1&lt;/param-name&gt;
             &lt;param-value&gt;I'm var1&lt;/param-value&gt;
         &lt;/init-param&gt;
         &lt;init-param&gt;
             &lt;param-name&gt;variable2&lt;/param-name&gt;
             &lt;param-value&gt;I'm var2&lt;/param-value&gt;
         &lt;/init-param&gt;
     &lt;/servlet&gt;
     &lt;servlet-mapping&gt;
         &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
         &lt;url-pattern&gt;/MyServlet&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
</pre>
</p>
<p>
You can retrieve variable2 with the following code:</p>
<pre class="prettyprint" lang-java>
getServletConfig().getInitParameter("variable2");
</pre>
</p>
<p>
Retrieve all the variables of the servlet and loop over them:</p>
<pre class="prettyprint" lang-java>
Enumeration allvars = getServletConfig().getInitParameterNames();
while (allvars.hasMoreElements()) {
     String next = (String) allvars.nextElement();
     out.println(next + " has the value " + getServletConfig().getInitParameter(next));
}
</pre>
</p>
<p>
It&#8217;s also possible to retrieve the servlets name through getServletconfig</p>
<pre class="prettyprint" lang-java>
out.println("The name of the servlet is: " + getServletConfig().getServletName());
</pre>
</p>
<p><b>ServletContext</b></p>
<p>
ServletContext can be used to retrieve application-wide parameters, get serverparameters and store/retrieve/remove application-wide attributes.
</p>
<p>It is possible to call the ServletContext right on or through the ServletConfig. Both are the same so you can choose between them.</p>
<pre class="prettyprint" lang-java>
if (getServletContext().equals(getServletConfig().getServletContext())) {
       out.println("both calls retrieve the same Context");
}
</pre>
</p>
<p>
Just like with ServletConfig, you can set parameters in the Deployment Descriptor. They are initialized on compile time. It&#8217;s not possible to remove or add them on runtime.</p>
<pre class="prettyprint" lang-xml>
&lt;context-param&gt;
        &lt;param-name&gt;variable3&lt;/param-name&gt;
        &lt;param-value&gt;Im var 3&lt;/param-value&gt;
&lt;/context-param&gt;
</pre>
<p>And call them in your servlet just like the ServletConfig parameters:</p>
<pre class="prettyprint" lang-java>
getServletContext().getInitParameter("variable3")
</pre>
</p>
<p>
Unlike ServletConfig, ServletContext can work with attributes. These are parameters or objects which you can set into the servlet and retrieve in every other servlet of your Web Application:</p>
<pre class="prettyprint" lang-java>
getServletContext().setAttribute("test", "I'm an application-wide String");
..
out.println(getServletContext().getAttribute("test"));
..
getServletContext().removeAttribute("test");
</pre>
</p>
<p>
I&#8217;ll end this little short tutorial with an example of how you can retrieve serversettings through the Context:</p>
<pre class="prettyprint" lang-java>
out.println(getServletContext().getContextPath());
out.println(getServletContext().getMajorVersion());
out.println(getServletContext().getRealPath(""));
out.println(getServletContext().getServerInfo());
</pre>
</p>
<p>If there are any more questions, just shoot them <img src='http://dev.eek.be/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection with Google Guice by example</title>
		<link>http://dev.eek.be/2009/09/dependency-injection-with-google-guice/</link>
		<comments>http://dev.eek.be/2009/09/dependency-injection-with-google-guice/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:22:20 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[Dependency injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[Inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[providers]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=66</guid>
		<description><![CDATA[In this blogpost, I&#8217;ll explain Dependency Injection with Google Guice through a lot of examples. Hope you like it. Annotation based DI It&#8217;s possible in Guice to inject a class with the help of annotations. In this example, I create a mainclass which creates an order. Inside the order there will be a payment done. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.eek.be/wp-content/uploads/2009/09/guice2.jpg" alt="guice2" title="guice2" width="300" height="171" class="aligncenter size-full wp-image-149" /><br />
In this blogpost, I&#8217;ll explain <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency Injection</a> with <a href="http://code.google.com/p/google-guice/" target="_blank">Google Guice</a> through a lot of examples. Hope you like it.</p>
<p><u>Annotation based DI</u><br />
It&#8217;s possible in Guice to inject a class with the help of annotations. In this example, I create a mainclass which creates an order. Inside the order there will be a payment done. There are 2 types of payments (by card or cash) and they will be injected by Guice.<br />
<b>Payment</b></p>
<pre class="prettyprint" lang-java>
public interface Payment {
    public void pay();
}
</pre>
<p><b>PaymentCash</b></p>
<pre class="prettyprint" lang-java>
public class PaymentCashImpl implements Payment {
    public void pay() {
        System.out.println("I'll pay just plain cash");
    }
}
</pre>
<p><b>PaymentCard</b></p>
<pre class="prettyprint" lang-java>
public class PaymentCardImpl implements Payment{
    public void pay() {
        System.out.println("I'll pay with a credit card");
    }
}
</pre>
<p><b>Order</b></p>
<pre class="prettyprint" lang-java>
public class Order {

    private Payment payment;

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public void finishOrder(){
        this.payment.pay();
    }
}
</pre>
<p><b>Main</b></p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        Order order = new Order();
        Payment payment = new PaymentCardImpl();
        order.setPayment(payment);
        order.finishOrder();
    }
}
</pre>
<p>There isn&#8217;t any Dependency injection in the code above. All the classes are highly coupled into each other. So it&#8217;s time to do some magic:<br />
<b>Payment</b><br />
You can tell through the use of annotations which Payment type is the default implemented class</p>
<pre class="prettyprint" lang-java>
@ImplementedBy(PaymentCardImpl.class)
public interface Payment {
    public void pay();
}
</pre>
<p><b>Order</b><br />
Inject the payment type in the order. What we here say is: Inject the default paymentimplementation, which is set by the @implementedby annotation, into the payment field.</p>
<pre class="prettyprint" lang-java>
public class Order {
    @Inject
    private Payment payment;
....
</pre>
<p><b>Main</b><br />
Lastly, we have to rewrite the main class. In this example, we get an order which uses the PaymentCard as payment type</p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
       Injector injector = Guice.createInjector();
        Order order = injector.getInstance(Order.class);
        order.finishOrder();
    }
}
</pre>
<p>You can run this code. The output should be -I&#8217;ll pay with a credit card-. This code is so cool because if we want to switch to payment with cash at a later point, we just have to change the @implementedBy annotation in the interface and we are done.</p>
<div style="border: 1px solid silver; padding: 8px; background-color: rgb(249, 249, 249); text-align: left;">
<script type="text/javascript"><!--
google_ad_client = "pub-1041875485625119";
/* 160x90, gemaakt 3-6-10 */
google_ad_slot = "3673564135";
google_ad_width = 160;
google_ad_height = 90;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<p><u>Injection types</u><br />
Guice knows different types of injection. The 3 most used are constructor, field and method injection (Examples are based on the above example)<br />
<b>Constructor injection</b></p>
<pre class="prettyprint" lang-java>
import com.google.inject.Inject;

public class Order {
    private Payment payment;

    @Inject
    public Order(Payment payment){
        this.payment=payment;
    }
   ...
}
</pre>
<p><b>Method injection</b></p>
<pre class="prettyprint" lang-java>
import com.google.inject.Inject;

public class Order {
    private Payment payment;

    @Inject
    public void setPayment(Payment payment) {
        this.payment = payment;
    }
   ...
}
</pre>
<p><b>Field injection</b></p>
<pre class="prettyprint" lang-java>
public class Order {
    @Inject
    private Payment payment;
   ...
}
</pre>
<p><u>Module based DI</u><br />
It&#8217;s also possible to use Modules for injecting data. It&#8217;s maybe a little more work but I like it because all the configuration is in the same file. You can compare this with the xml-file in Spring applications.<br />
<b>Payment</b><br />
Remove the @ImplementedBy annotation in the paymentinterface</p>
<pre class="prettyprint" lang-java>
public interface Payment {
    public void pay();
}
</pre>
<p><b>MyModule</b><br />
Next, create the Module class. It has to implement Module, which has one method: configure.<br />
The code speaks for its self. You say nothing more than bind Payment to PaymentCard.</p>
<pre class="prettyprint" lang-java>
public class MyModule implements Module {
    public void configure(Binder arg0) {
        arg0.bind(Payment.class).to(PaymentCardImpl.class);
    }
}
</pre>
<p>Instead of implementing Module, you can also extend AbstractModule. It&#8217;s just a matter of choice.</p>
<pre class="prettyprint" lang-java>
public class MyModule extends AbstractModule {
    public void configure() {
        bind(Payment.class).to(PaymentCardImpl.class);
    }
}
</pre>
<p><b>Main</b><br />
Lastly, we have to put the module into our injector and we are done.</p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        MyModule module = new MyModule();
        Injector injector = Guice.createInjector(module);
        Order order = injector.getInstance(Order.class);
        order.finishOrder();
    }
}
</pre>
<p>The 4 lines in the main class can be written into 1 single line:</p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        Guice.createInjector(new MyModule()).getInstance(Order.class).finishOrder();
    }
}
</pre>
<p><u>Subclassing</u><br />
In Guice, it&#8217;s easy to subclass an implementation.<br />
<b>Payment</b></p>
<pre class="prettyprint" lang-java>
public interface Payment {
    public void pay();
}
</pre>
<p><b>PaymentCardImpl</b></p>
<pre class="prettyprint" lang-java>
public class PaymentCardImpl implements Payment{
    public void pay() {
        System.out.println("I'll pay with a credit card");
    }
}
</pre>
<p><b>PaymentVisaCard</b><br />
extend payment card with paymentvisacard</p>
<pre class="prettyprint" lang-java>
public class PaymentVisaCard extends PaymentCardImpl {
    public void pay() {
        System.out.println("I'll pay with a card from Visa");
    }
}
</pre>
<p>Nothing special in the Main class<br />
<b>Main</b></p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        Guice.createInjector(new MyModule()).getInstance(Payment.class).pay();
    }
}
</pre>
<p><b>MyModule</b><br />
You can specify the hierarchy in the moduleclass</p>
<pre class="prettyprint" lang-java>
public class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Payment.class).to(PaymentCardImpl.class);
        bind(PaymentCardImpl.class).to(PaymentVisaCard.class);
    }
}
</pre>
<p>If you run this code, the Visa Card will be used.<br />
<u>Annotationbindings</u><br />
It&#8217;s also possible to use annotations for injection<br />
<b>Payment</b><br />
There&#8217;s nothing special in the interface and implementations:</p>
<pre class="prettyprint" lang-java>
public interface Payment {
    public void pay();
}
</pre>
<p><b>PaymentCardImpl</b></p>
<pre class="prettyprint" lang-java>
public class PaymentCardImpl implements PaymentCashImpl{
    public void pay() {
        System.out.println("I pay with a card");
    }
}
</pre>
<p><b>PaymentCashImpl</b></p>
<pre class="prettyprint" lang-java>
public class PaymentCashImpl implements Payment {
    public void pay() {
        System.out.println("I pay cash");
    }
}
</pre>
<p>Next, we have to created the annotations @cash and @Card<br />
<b>Cash</b></p>
<pre class="prettyprint" lang-java>
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface Cash { }
</pre>
<p><b>Card</b></p>
<pre class="prettyprint" lang-java>
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface Card { }
</pre>
<p><b>Main</b></p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        Guice.createInjector(new PayModule()).getInstance(Order.class).finishOrder();
    }
}
</pre>
<p><b>Module</b><br />
You have to specify which annotations refers to which implementation in the moduleclass</p>
<pre class="prettyprint" lang-java>
public class PayModule extends AbstractModule{
    @Override
    protected void configure() {
        bind(Payment.class).annotatedWith(Cash.class).to(PaymentCashImpl.class);
        bind(Payment.class).annotatedWith(Card.class).to(PaymentCardImpl.class);
    }
}
</pre>
<p><b>Order</b><br />
Now you can use the annotation in your code to inject the right class</p>
<pre class="prettyprint" lang-java>
public class Order {

    @Inject
    private @Card Payment payment;
   ...
</pre>
<p>If you want to chane Cardpayments to Cashpayments, just change the @Card annotation to @Cash</p>
<p><u>Namedannotationbindings</u><br />
You can also use named annotations. This is a Guice annotation where the value is specified inside the moduleclass.<br />
The paymentinterface, the Main and the Cash and Card implementations are the same as the previous example.<br />
<b>Module</b><br />
Give the annotations a name (&#8220;Cash&#8221; and &#8220;Card&#8221;)</p>
<pre class="prettyprint" lang-java>
public class PayModule extends AbstractModule{
    @Override
    protected void configure() {
        bind(Payment.class).annotatedWith(Names.named("Cash")).to(CashPayment.class);
        bind(Payment.class).annotatedWith(Names.named("Card")).to(CardPayment.class);
    }
}
</pre>
<p><b>Main</b></p>
<pre class="prettyprint" lang-java>
 * @author wim
 */
public class Main {
    public static void main(String[] args) {
        Injector inj = Guice.createInjector(new PayModule());
        Order order = inj.getInstance(Order.class);
        order.getPaymentCard().pay();

        Guice.createInjector(new PayModule()).getInstance(Order.class).getPaymentCash().pay();
    }
}
</pre>
<p><b>Order</b><br />
Now you can use them in your order</p>
<pre class="prettyprint" lang-java>
public class Order {

    private Payment paymentCash;
    private Payment paymentCard;

    @Inject
    public void setPaymentCash(@Named("Cash") Payment payment){
        this.paymentCash=payment;
    }

    public Payment getPaymentCash(){
        return paymentCash;
    }

    @Inject
    public void setPaymentCard(@Named("Card") Payment payment){
        this.paymentCard=payment;
    }

    public Payment getPaymentCard(){
        return paymentCard;
    }
}
</pre>
<p>These annotations are very simple but I don&#8217;t recommend them. Guice doesn&#8217;t check them on spelling or validity so they are kinda error prone.<br />
<u>Instancebinding</u><br />
With instancebinding, you can easily give a value to an instance<br />
<b>Module</b><br />
Give a value to a name inside the module</p>
<pre class="prettyprint" lang-java>
public class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(String.class)
        .annotatedWith(Names.named("PaymentType"))
        .toInstance("...I'm a Visa Card...");
    }
}
</pre>
<p><b>Main</b></p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        String text = Guice.createInjector(new MyModule()).getInstance(Order.class).getText();
        System.out.println(text);
    }
}
</pre>
<p><b>Order</b><br />
Use the annotation.  The String text will be injected with &#8220;&#8230;I&#8217;m a Visa Card&#8230;&#8221;.</p>
<pre class="prettyprint" lang-java>
public class Order {

    @Named("PaymentType")
    @Inject
    private String text;

    public String getText(){
        return text;
    }
}
</pre>
<p><u>Providers</u><br />
The last example is with the use of providers.<br />
A provider can be used when you have to craete an object. You have to annotate it with the @Provides annotation and the return type is the bound type.<br />
<b>Payment</b></p>
<pre class="prettyprint" lang-java>
public interface Payment {
    void pay();
    int getAmount();
}
</pre>
<p><b>CashPayment</b></p>
<pre class="prettyprint" lang-java>
public class CashPayment implements Payment{

    private int amount;

    public void pay() {
        System.out.println("Inside cashpayment");
    }

    public void setAmount(int amount){
        this.amount=amount;
    }

    public int getAmount(){
        return amount;
    }
}
</pre>
<p><b>Main</b></p>
<pre class="prettyprint" lang-java>
public class Main {
    public static void main(String[] args) {
        Payment payment = Guice.createInjector(new MyModule()).getInstance(Payment.class);
        payment.pay();
        System.out.println(payment.getAmount());
    }
}
</pre>
<p><b>MyModule</b><br />
Inside the module class, we can construct the class and set the amount of payment.</p>
<pre class="prettyprint" lang-java>
public class MyModule extends AbstractModule{

    @Override
    protected void configure() {
    }

    @Provides
    Payment providePayment(){
        CashPayment pay = new CashPayment();
        pay.setAmount(100);
        return pay;
    }
}
</pre>
<p><u>Conclusion</u><br />
You can do more with Guice than described above but that&#8217;s up to you to find out.<br />
<b>I love Guice!!</b> It&#8217;s pretty simple and works very good. You don&#8217;t have to use XML to bind your classes and it&#8217;s very lightweight. If you just wan&#8217;t to use Dependency Injection, Guice is in my opinion the number 1.<br />
<script type="text/javascript">var dzone_url = 'http://dev.eek.be/?p=66';</script><br />
<script type="text/javascript">var dzone_title = 'Dependency Injection with Google Guice by example';</script><br />
<script type="text/javascript">var dzone_blurb = 'Google Guice by example';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/09/dependency-injection-with-google-guice/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to flush the contents of Memcached</title>
		<link>http://dev.eek.be/2009/09/memcache/</link>
		<comments>http://dev.eek.be/2009/09/memcache/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 12:48:58 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[telnet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=45</guid>
		<description><![CDATA[I often see people using telnet to flush the entire contents of their Memcached instance. They use the following commands: $ telnet localhost 11211 Trying 127.0.0.1… Connected to localhost. Escape character is ‘^]’. flush_all OK quit Connection to localhost closed by foreign host. $ You have to log in into the appropriate memcached host and [...]]]></description>
			<content:encoded><![CDATA[<p>I often see people using telnet to flush the entire contents of their Memcached instance.<br />
They use the following commands:</p>
<pre class="prettyprint">
$ telnet localhost 11211
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
flush_all
OK
quit
Connection to localhost closed by foreign host.
$
</pre>
<p>You have to log in into the appropriate memcached host and port with telnet. After logged in, you have to use the flush_all command. If this command responses with OK, everything went fine and you can log out of your memcached server using the command quit.</p>
<p>The above command works fine but is a pain in the ass if you have to flush a lot and wants to use your console for other things.<br />
If this is the case, you can run a flush_all in 1 command </p>
<pre class="prettyprint">
echo 'flush_all' | nc localhost 11211
</pre>
<p>By default, nc (or netcat) creates a TCP socket either in listening mode (server socket) or a socket that is used in order to connect to a server (client mode). Actually, netcat does not care whether the socket is meant to be a server or a client. All it does is to take the data from stdin and transfer it to the other end across the network.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/09/memcache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
