<?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>lstierneyltd &#187; How to&#8217;s</title>
	<atom:link href="http://lstierneyltd.com/blog/category/development/how-tos/feed/" rel="self" type="application/rss+xml" />
	<link>http://lstierneyltd.com/blog</link>
	<description>Yet another development blog</description>
	<lastBuildDate>Wed, 13 Jul 2011 12:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Read a File as String with Java</title>
		<link>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/</link>
		<comments>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 08:19:36 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[quick tips]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=264</guid>
		<description><![CDATA[Introduction I&#8217;m always Googling for a way to do this. This seems to be the best &#8220;idiomatic&#8221; solution I&#8217;ve found. So without further ado&#8230; Example]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I&#8217;m always Googling for a way to do this. This seems to be the best &#8220;idiomatic&#8221; solution I&#8217;ve found. So without further ado&#8230;</p>
<h3>Example</h3>
<pre class="brush: java; title: ; notranslate">
public String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try{
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    }
    finally {
        stream.close();
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Test Private Java Methods using Reflection</title>
		<link>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/</link>
		<comments>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/#comments</comments>
		<pubDate>Thu, 05 May 2011 17:02:06 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=254</guid>
		<description><![CDATA[Introduction I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments. Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against! Example The class under test The (Reflection Based) [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments.</p>
<p>Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against!</p>
<h3>Example</h3>
<h4>The class under test</h4>
<pre class="brush: java; title: ; notranslate">
public class Product() {
    private String privateMethod(String id) {
    //Do something private
    return &quot;product_&quot; + id;
  }
}
</pre>
<h4>The (Reflection Based) Unit Test</h4>
<pre class="brush: java; title: ; notranslate">
import java.lang.reflect.Method;

import static org.junit.Assert.*;
import org.junit.Test;

public class ProductTest {
    private Product product; // the class under test
    private Method m;
    private static String METHOD_NAME = &quot;privateMethod&quot;;
    private Class[] parameterTypes;
    private Object[] parameters;

    @Before
    public void setUp() throws Exception {
        product = new Product();
        parameterTypes = new Class[1];
        parameterTypes[0] = java.lang.String.class;
        m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
        m.setAccessible(true);
        parameters = new Object[1];
    }

    @Test
    public void testPrivateMethod() throws Exception {
        parameters[0] = &quot;someIdentifier&quot;;
        String result = (String) m.invoke(product, parameters); 

        //Do your assertions
        assertNotNull(result);
    }
}
</pre>
<h3>Update</h3>
<p>I&#8217;ve since been told that if <a href="http://code.google.com/p/dp4j/" target="_blank">dp4j.jar</a> is in the classpath at compile-time, it will inject the necessary reflection to make this work. I haven&#8217;t had time to try this yet so YMMV.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to stop XSLT from escaping XML in output</title>
		<link>http://lstierneyltd.com/blog/development/how-to-stop-xslt-from-escaping-xml-in-output/</link>
		<comments>http://lstierneyltd.com/blog/development/how-to-stop-xslt-from-escaping-xml-in-output/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 14:53:48 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=201</guid>
		<description><![CDATA[I was asked by a colleague today how to prevent XSLT from escaping XML &#8220;special characters&#8221; in its output. i.e. < was being escaped to &#038;lt ;. I didn't know but somebody else did... &#60;xsl:value-of select=&#8221;somethingWhichContainsXml&#8221; /&#62; should be &#60;xsl:value-of select=&#8221;somethingWhichContainsXml&#8221; disable-output-escaping=&#8221;yes&#8221; /&#62;]]></description>
			<content:encoded><![CDATA[<p>I was asked by a colleague today how to prevent XSLT from escaping XML &#8220;special characters&#8221; in its output. i.e. < was being escaped to &#038;lt ;.</p>
<p>I didn't know but somebody else did...</p>
<pre>
&lt;xsl:value-of select=&#8221;somethingWhichContainsXml&#8221; /&gt;

should be 

&lt;xsl:value-of select=&#8221;somethingWhichContainsXml&#8221; disable-output-escaping=&#8221;yes&#8221; /&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/how-to-stop-xslt-from-escaping-xml-in-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display Version in Android Application</title>
		<link>http://lstierneyltd.com/blog/development/display-version-in-android-application/</link>
		<comments>http://lstierneyltd.com/blog/development/display-version-in-android-application/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 10:09:11 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=181</guid>
		<description><![CDATA[I was looking for a way to display the Version on the splash screen of my Android app, more specifically, the &#8220;versionName&#8221; as defined in the application&#8217;s AndroidManifest.xml After A LOT of looking I discovered PackageInfo, which is a Java API to all of the info held in the manifest. Armed with this information is [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking for a way to display the Version on the splash screen of my Android app, more specifically, the &#8220;versionName&#8221; as defined in the application&#8217;s AndroidManifest.xml</p>
<p>After A LOT of looking I discovered <a href="http://developer.android.com/reference/android/content/pm/PackageInfo.html" target="_blank">PackageInfo</a>, which is a Java API to all of the info held in the manifest.</p>
<p>Armed with this information is was easy to produce the following:</p>
<pre class="brush: plain; title: ; notranslate">
private void displayVersionName() {
    String versionName = &quot;&quot;;
    PackageInfo packageInfo;
    try {
        packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    	versionName = &quot;v &quot; + packageInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    TextView tv = (TextView) findViewById(R.id.versionNameTextView);
    tv.setText(versionName);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/display-version-in-android-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Log4j Nested Diagnostic Contexts (NDC)</title>
		<link>http://lstierneyltd.com/blog/development/log4j-nested-diagnostic-contexts-ndc/</link>
		<comments>http://lstierneyltd.com/blog/development/log4j-nested-diagnostic-contexts-ndc/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 18:31:57 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=118</guid>
		<description><![CDATA[Introduction and Problem Log4j is a brilliant set of libraries which provides good &#8220;out of the box&#8221; logging capabilities. Sometimes however, in multi-threaded environments (for example web apps), it can be hard to decipher the interleaved messages from the various clients e.g. Client A logs something Client B logs something Client A logs something Client [...]]]></description>
			<content:encoded><![CDATA[<h4>Introduction and Problem</h4>
<p>Log4j is a brilliant set of libraries which provides good &#8220;out of the box&#8221; logging capabilities.</p>
<p>Sometimes however, in multi-threaded environments (for example web apps), it can be hard to decipher the interleaved messages from the various clients<br />
<code><span id="more-118"></span></code><br />
e.g.</p>
<p>Client A logs something<br />
Client B logs something<br />
Client A logs something<br />
Client C logs something<br />
Client A logs something<br />
Client A logs something<br />
Client B logs something<br />
Client A logs something</p>
<p>Getting hard to read already!</p>
<h4>Solution</h4>
<p>Luckily for us Log4j has a built in solution to this common problem: Nested Diagnostic Contexts.</p>
<p>To quote the API doc &#8220;A Nested Diagnostic Context, or NDC in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously. </p>
<p>Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play&#8230;.&#8221; </p>
<p><a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html">http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html</a></p>
<p>The simple example below applies NDC to a Message Driven Bean (MDB):</p>
<h4>Sample Java Code</h4>
<pre>
package test.controller;

import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;

public class Log4jDiagnosticContextSetter implements MessageListener {

    private static final Logger LOG = Logger.getLogger(Log4jDiagnosticContextSetter.class);

    private MessageListener messageListener;

    public void onMessage(Message message) {
        boolean loggingContextIsSet = addLoggingContextData(message);

        try {
            messageListener.onMessage(message); // the bean does its stuff
        }
        finally {
            if (loggingContextIsSet){
                removeLoggingContextData();
            }
        }
    }

    private boolean addLoggingContextData(Message message) {
        if (!LOG.isInfoEnabled())
            return false;

        try {
            NDC.push(getUniqueRef());
        }
        catch (Exception e) {
            LOG.error("Error pushing identifier onto Log4J NDC stack.", e);

            return false;
        }

        return true;
    }

    private void removeLoggingContextData() {
        try {
            while (NDC.getDepth() != 0) {
                NDC.pop();
            }
        }
        catch (Exception e) {
            LOG.error("Error popping message ID off of Log4J NDC stack.", e);
    }

    private String getUniqueRef() {
        // some way to get a unique reference
    }
}
</pre>
<h4>Sample Log Output</h4>
<p>You can see here in the example log output the result of the NDC push code above (in this case &#8220;98765432&#8243; is the unique identifier)</p>
<pre>
[02/06/2010 12:49:34:883 BST] [MessageListenerThreadPool : 0] DEBUG AuditServiceListener [98765432] - Processing message
[02/06/2010 12:49:34:883 BST] [MessageListenerThreadPool : 0] DEBUG ServiceMsgProcessor  [98765432] - Starting processing of XML request. Request=(<someMessage></someMessage>)
[02/06/2010 12:49:34:883 BST] [MessageListenerThreadPool : 0] DEBUG RequestUnmarshaller  [98765432] - Unmarshalling XML request payload to object.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/log4j-nested-diagnostic-contexts-ndc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Quartz to schedule tasks in Spring</title>
		<link>http://lstierneyltd.com/blog/development/use-quartz-to-schedule-tasks-in-spring/</link>
		<comments>http://lstierneyltd.com/blog/development/use-quartz-to-schedule-tasks-in-spring/#comments</comments>
		<pubDate>Fri, 07 May 2010 16:47:56 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[quartz]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=87</guid>
		<description><![CDATA[Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application. In this simple example (notionally checking Twitter for updates) we integrate it with Spring. Spring Config &#60;bean name="checkTwitterForUpdates" class="org.springframework.scheduling.quartz.JobDetailBean"&#62; &#60;property name="jobClass" value="test.twitterReader" /&#62; &#60;/bean&#62; &#60;bean id="cronTwitterUpdateCheckTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"&#62; &#60;property name="jobDetail" [...]]]></description>
			<content:encoded><![CDATA[<p>Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application.</p>
<p>In this simple example (notionally checking Twitter for updates) we integrate it with Spring.<br />
<code><span id="more-87"></span></code></p>
<h4>Spring Config</h4>
<pre>
&lt;bean name="checkTwitterForUpdates"
  class="org.springframework.scheduling.quartz.JobDetailBean"&gt;

       &lt;property name="jobClass" value="test.twitterReader" /&gt;
&lt;/bean&gt;

&lt;bean id="cronTwitterUpdateCheckTriggerBean"
  class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;

       &lt;property name="jobDetail" ref="checkTwitterForUpdates" /&gt;
       &lt;!-- Run every day at noon --&gt;
       &lt;property name="cronExpression" value="0 0 12 * * ?" /&gt;
&lt;/bean&gt;

&lt;bean id="scheduler"
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;

       &lt;property name="triggers"&gt;
           &lt;list&gt;
               &lt;ref bean="cronTwitterUpdateCheckTriggerBean" /&gt;
           &lt;/list&gt;
       &lt;/property&gt;
&lt;/bean&gt;
</pre>
<p>You may just want to call a method on an existing object (perhaps a business object). We can use MethodInvokingJobDetailFactoryBean to do just this:</p>
<p> Often you just need to invoke a method on a specific object. Using the MethodInvokingJobDetailFactoryBean you can do exactly this:</p>
<pre>
&lt;bean id="jobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;
    &lt;property name="targetObject" ref="someBusinessObject"/&gt;
    &lt;property name="targetMethod" value="someMethod"/&gt;
&lt;/bean&gt;
</pre>
<h4>Java</h4>
<pre>
package twitter;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class twitterReader extends QuartzJobBean {
    protected void executeInternal(JobExecutionContext ctx)
      throws JobExecutionException {

      // Like whatever.......
    }
}
</pre>
<h4>Libraries</h4>
<p>You can get get Quartz <a href="http://www.quartz-scheduler.org/">here</a></p>
<p>You may also need:<br />
<a href="http://www.slf4j.org/">Simple Logging Facade for Java (SLF4J)</a> and</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/use-quartz-to-schedule-tasks-in-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Quickly Setup Subversion Server on Linux (Ubuntu)</title>
		<link>http://lstierneyltd.com/blog/development/how-to-quickly-setup-subversion-server-on-linux-ubuntu/</link>
		<comments>http://lstierneyltd.com/blog/development/how-to-quickly-setup-subversion-server-on-linux-ubuntu/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 19:55:35 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=49</guid>
		<description><![CDATA[Installation First of all you&#8217;ll need to see if you&#8217;ve got Subversion installed. Check this article on how to query installed packages or just try executing: % svn help If you haven&#8217;t got Subversion installed then: % apt-get install subversion Users and Groups % addgroup subversion % usermod -a -G subversion yourUser Create Repository % [...]]]></description>
			<content:encoded><![CDATA[<h4>Installation</h4>
<p>First of all you&#8217;ll need to see if you&#8217;ve got Subversion installed. Check <a href="http://lstierneyltd.com/blog/development/tips/show-and-search-list-of-installed-packages-ubuntu/">this article</a> on how to query installed packages or just try executing:</p>
<pre>% svn help</pre>
<p>If you haven&#8217;t got Subversion installed then:</p>
<pre>% apt-get install subversion</pre>
<h4>Users and Groups</h4>
<pre>% addgroup subversion
% usermod -a -G subversion yourUser</pre>
<h4>Create Repository</h4>
<pre>% svnadmin create /path/to/repository</pre>
<h4>Configure Repository</h4>
<pre>% chgrp -R subversion /path/to/repository
% chmod -R g+rws /path/to/repository
% vi /path/to/repository/conf/svnserve.conf

Edit/uncomment the following lines:

anon-access = none
auth-access = write
password-db = passwd

% vi /path/to/repository/conf/passwd

Add the following line:

yourUser = yourSecretPassword</pre>
<h4>Start Subversion Server as Daemon</h4>
<pre>svnserve -d</pre>
<h4>Troubleshooting</h4>
<p>You will need sudo or root access (unless you want to see a lot of permission denied errors!)<br />
If you see this error (when using Windows SVN client):</p>
<pre>
Can’t move
‘..\..\.svn\entries’ to ‘..\..\.svn\entries’:
The file or directory is corrupted and unreadable.
</pre>
<p>Then try excluding the local directory from the Windows Indexer.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/how-to-quickly-setup-subversion-server-on-linux-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

