<?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>Thread.currentThread().join() &#187; Application Programming</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/category/application-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 23 Aug 2011 03:53:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Updating Google Calendar with MS Project Tasks &#8211; 2 &#8211; Revisited</title>
		<link>http://www.ashishpaliwal.com/blog/2011/08/updating-google-calendar-with-ms-project-tasks-2-revisited/</link>
		<comments>http://www.ashishpaliwal.com/blog/2011/08/updating-google-calendar-with-ms-project-tasks-2-revisited/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 03:53:36 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Google Calendar]]></category>
		<category><![CDATA[MS Project]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=648</guid>
		<description><![CDATA[This is the 2nd part in the series. Please refer to Updating Google Calendar with MS Project Tasks - Revisited for Part 1, including how to build the code. In the previous post we saw, how to list all the Calendar's. In this post we shall build upon the code to list all the calendar's [...]]]></description>
			<content:encoded><![CDATA[<p>This is the 2nd part in the series. Please refer to <a href="http://www.ashishpaliwal.com/blog/2011/07/updating-google-calendar-with-ms-project-revisited/">Updating Google Calendar with MS Project Tasks - Revisited</a> for Part 1, including how to build the code.</p>
<p>In the previous post we saw, how to list all the Calendar's. In this post we shall build upon the code to list all the calendar's for a given user account, and update the MS Project tasks to the selected calendar.</p>
<p>We shall need to following three steps:</p>
<ul>
<li>List Calendars and select one</li>
<li>Parse MS Project tasks and convert them to Calendar events</li>
<li>Update Google Calendar with Events</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Step 1: List All calendar's</h3>
<p>We have already seen the code for this in previous <a href="http://www.ashishpaliwal.com/blog/2011/07/updating-google-calendar-with-ms-project-revisited">post</a></p>
<h3> Step 2: Parse MS Project task and convert to Calendar Event</h3>
<p>Lets look at the code for the conversion</p>
<pre class="brush: java">
protected CalendarEventEntry convertTaskToCalenderEntry(Task task) {
    System.out.println(&quot;Task &quot;+task);
    CalendarEventEntry eventEntry = new CalendarEventEntry();
    eventEntry.setTitle(new PlainTextConstruct(task.getName()));

    When date = new When();
    date.setStartTime(new DateTime(task.getStart()));
    eventEntry.addTime(date);
    return eventEntry;
}
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6961884887741817";
/* 468x15, created 8/2/10 */
google_ad_slot = "9956401459";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
The code takes Start and End date of task, as well as title, and creates an CalendarEventEntry for the same. There can be additional logic built into this, like filtering events based on some rules etc.</p>
<h3> Step 3: Updating entries into Google Calendar </h3>
<pre class="brush: java">
public void updateCalenderWithEntry(List&lt;Task&gt; tasks, CalendarService calendarService, URL url) {
    for (Task task : tasks) {
        CalendarEventEntry entry = convertTaskToCalenderEntry(task);
        try {
            calendarService.insert(url, entry);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
}
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6961884887741817";
/* 468x15, created 8/2/10 */
google_ad_slot = "9956401459";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
Here we just iterate through the tasks and update the events using CalendarService. The Calendar URL can be retrieved from the CalendarEntry class.</p>
<p>A complete working example is part of com.ashishpaliwal.mpputils.examples.MppToGoogleCalendar</p>
<h4>Using the sample program</h4>
<p>>com.ashishpaliwal.mpputils.examples.MppToGoogleCalendar [MPP File] [Google User Name] [password]</p>
<h3>What's Next?</h3>
<p>Eager to hear from user, what they would like to see. Adding a UI and out-of-box working package is something that would be good. However, please do try using this and I shall try my best to improve this.</p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2011/08/updating-google-calendar-with-ms-project-tasks-2-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with Terracotta</title>
		<link>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:51:40 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Terracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=340</guid>
		<description><![CDATA[Abstract In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose [...]]]></description>
			<content:encoded><![CDATA[<h2><strong>Abstract</strong></h2>
<p>In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose this example was coz of a very similar requirement that I had to implement in Clustered J2EE application.<br />
Terracotta is well known and needs no introduction <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2><strong>About problem Statement</strong></h2>
<p>Well, I needed a simple solution to have<br />
•	Cluster wide unique id’s<br />
•	Less frequent access to these id’s<br />
•	Optional Persistence</p>
<p><strong>NOTE:</strong> Please note that the current example is slightly modified version of example from terracotta.org.</p>
<h2>Pre-requisite</h2>
<p>To run this example, you need to have following installed<br />
•	Terracotta<br />
•	And JDK offcourse <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Let’s take a look at the sample code first</p>
<pre class="brush: java">
import java.util.concurrent.atomic.*;

public class IdGenerator
{

private AtomicInteger masterCounter = new AtomicInteger(0);

private AtomicInteger slaveCounter = new AtomicInteger(1024);

public int getNewMasterId()
{
int newId = masterCounter.getAndIncrement();
System.out.println(&quot;Master Id is: &quot; + newId);
return newId;
}

public int getNewSlaveId()
{
int slaveId = slaveCounter.getAndIncrement();
System.out.println(&quot;Slave Id is: &quot; + slaveId);
return slaveId;
}

public static void main(String[] args)
{
new IdGenerator().getNewMasterId();
}
}
</pre>
<p>The code has two fields ids master and slave, of which master id need to be unique cluster wide. The code is pretty straight forward, and there is nothing to explain. The id’s are generated by simple getAndIncrement() API.</p>
<p>Lets see the tc-config.xml</p>
<pre class="brush: xml">

&lt;tc:tc-config xmlns:tc=&quot;http://www.terracotta.org/config&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://www.terracotta.org/schema/terracotta-4.xsd&quot;&gt;

&lt;application&gt;
&lt;dso&gt;
&lt;roots&gt;
&lt;root&gt;
&lt;field-name&gt;IdGenerator.masterCounter&lt;/field-name&gt;
&lt;/root&gt;
&lt;/roots&gt;
&lt;/dso&gt;
&lt;/application&gt;
&lt;/tc:tc-config&gt;
</pre>
<p>Since only master Id needed to be unique in this case, hence the configuration</p>
<p>Let’s look at the deployment architecture</p>
<div id="attachment_343" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-343" title="nodes" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2009/11/nodees-300x134.png" alt="Deployment Architecture" width="300" height="134" /><p class="wp-caption-text">Deployment Architecture</p></div>
<p>The application was to run on 3 nodes (3 JVM’s on same machine) and one Terracotta server running.<br />
Before we get into running this application, lets take a look at some useful scripts provided by Terracotta<br />
•	<strong>dso-java (bat|sh)</strong> – startup script that bootstraps Terracotta libraries to your application<br />
•	<strong>start-tc-server (bat|sh)</strong> – Script to start Terracotta Server. It is mandatory to start Terracotta Server before running Clients</p>
<p>Let’s run the application<br />
1.	Compile the application. There is no dependency on TC libraries.<br />
2.	First Start the terracotta Server using script start-tc-server<br />
3.	Run 3 separate JVM with command “dso-java IdGenerator”<br />
That’s it. Our first application is running. If we observe the console output, you can see that we have unique id’s for each invocation.</p>
<h2>References:</h2>
<p>Source code for Terracotta examples - http://svn.terracotta.org/svn/forge/cookbook</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[MPXJ Series] Writing MS Project File using MPXJ</title>
		<link>http://www.ashishpaliwal.com/blog/2009/01/mpxj-series-writing-ms-project-file-using-mpxj/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/01/mpxj-series-writing-ms-project-file-using-mpxj/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 09:14:42 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[MPXJ]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=239</guid>
		<description><![CDATA[In the first part, we shall explore what are the different ways of writing a MS Project file using MPXJ. Pre-requisite The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download   MPXJ - http://sourceforge.net/project/showfiles.php?group_id=70649 Dependencies - http://mpxj.sourceforge.net/dependencies.html Now let’s get back to the original [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part, we shall explore what are the different ways of writing a MS Project file using MPXJ.</p>
<p class="MsoNormal"><strong>Pre-requisite</strong></p>
<p class="MsoNormal">The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download</p>
<p class="MsoListParagraphCxSpFirst"> </p>
<ul>
<li>MPXJ - <a href="http://sourceforge.net/project/showfiles.php?group_id=70649">http://sourceforge.net/project/showfiles.php?group_id=70649</a></li>
<li>Dependencies - <a href="http://mpxj.sourceforge.net/dependencies.html">http://mpxj.sourceforge.net/dependencies.html</a></li>
</ul>
<p class="MsoNormal">Now let’s get back to the original intent of reading MPP files</p>
<p class="MsoNormal">Lets see the simplest form of reading an MPP file</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
    ProjectWriter writer = ProjectWriterUtility.<span style="color: #006600;">getProjectWriter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;test.mpx&quot;</span><span style="color: #66cc66;">&#41;</span>;
    ProjectFile project = <span style="color: #000000; font-weight: bold;">new</span> ProjectFile<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Operations on Project File</span>
    writer.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span>project, <span style="color: #ff0000;">&quot;test.mpx&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/InstantiationException.html"><span style="color: #aaaadd; font-weight: bold;">InstantiationException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IllegalAccessException.html"><span style="color: #aaaadd; font-weight: bold;">IllegalAccessException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> ioEx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    ioEx.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<div>
<p class="MsoNormal">ProjectWriterUtility.getProjectWriter(fileName), provides an appropriate Reader class, based on the version of MS Project file. This is the simplest way to create a writer for an Project file. ProjectFile is the class that contains details about the Schedule, we can iterate and extract information from it. We shall discuss the details on ProjectFile in subsequent posts. ProjectWriterUtility class checks for the file extension and returns an appropriate writer.</p>
<p class="MsoNormal"><strong>Alternative ways</strong></p>
<ul type="disc">
<li class="MsoNormal">For writing MPX file, you      can use net.sf.mpxj.mpx.MPXWriter</li>
<li class="MsoNormal">For writing XML Project file, use net.sf.mpxj.mspdi. MSPDIWriter</li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/01/mpxj-series-writing-ms-project-file-using-mpxj/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>[MPXJ Series] Reading MS Project Files using MPXJ</title>
		<link>http://www.ashishpaliwal.com/blog/2008/12/mpxj-series-reading-ms-project-files-using-mpxj/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/12/mpxj-series-reading-ms-project-files-using-mpxj/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 09:06:43 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[MPXJ Read MPP]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=235</guid>
		<description><![CDATA[It’s been a while since my last post on using MPXJ. You can find it here. Since then have been observing the search items and have found that need to have some simple posts related to MPXJ. So starting a series on MPXJ, which I shall keep updating as and when I learn more about [...]]]></description>
			<content:encoded><![CDATA[<p>It’s been a while since my last post on using MPXJ. You can find it <a href="http://www.ashishpaliwal.com/blog/2008/10/updating-google-calendar-with-ms-project-tasks/">here</a>. Since then have been observing the search items and have found that need to have some simple posts related to MPXJ. So starting a series on MPXJ, which I shall keep updating as and when I learn more about it.</p>
<p>In the first part, let’s explore what are the different ways of reading a MPP file in MPXJ.</p>
<p><strong>Pre-requisite</strong></p>
<p>The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download</p>
<ul>
<li>MPXJ - http://sourceforge.net/project/showfiles.php?group_id=70649</li>
<li>Dependencies - http://mpxj.sourceforge.net/dependencies.html</li>
</ul>
<p>Now let’s get back to the original intent of reading MPP files</p>
<p>Lets see the simplest form of reading an MPP file</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
     ProjectReader reader = ProjectReaderUtility.<span style="color: #006600;">getProjectReader</span><span style="color: #66cc66;">&#40;</span>fileName<span style="color: #66cc66;">&#41;</span>;
     ProjectFile projectFile = reader.<span style="color: #006600;">read</span><span style="color: #66cc66;">&#40;</span>fileName<span style="color: #66cc66;">&#41;</span>;</pre>
<pre class="java5">     <span style="color: #808080; font-style: italic;">// ... process Project file here</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
     e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>ProjectReaderUtility.getProjectReader(), provides an appropriate Reader class, based on the version of MS Project file. This is the simplest way to read an MPP file. ProjectFile is the class that contains details about the Schedule, we can iterate and extract information from it.</p>
<p><strong>Alternative ways</strong></p>
<ul>
<li>For reading MPX file, you can use net.sf.mpxj.mpx.MPXReader</li>
<li>For reading MPP,MPT file, use net.sf.mpxj.mpp.MPPReader</li>
<li>For reading MSPDI, use net.sf.mpxj.mspdi.MSPDIReader</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/12/mpxj-series-reading-ms-project-files-using-mpxj/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Trap Sender using SNMP4J</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-sender-using-snmp4j/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-sender-using-snmp4j/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 06:56:06 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[SMP]]></category>
		<category><![CDATA[SNMP4J]]></category>
		<category><![CDATA[Trap Sender]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=176</guid>
		<description><![CDATA[In this post, we shall implement a Trap Sender using SNMP4J. We may choose to use Apache MINA for sending Traps or can resort to using DatagramSocket class directly. This shall be the logical flow of the implementation Get the encoded Trap Data Send the Trap  Lets look at the first component, on getting the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we shall implement a Trap Sender using SNMP4J. We may choose to use Apache MINA for sending Traps or can resort to using DatagramSocket class directly.</p>
<p>This shall be the logical flow of the implementation</p>
<ul>
<li>Get the encoded Trap Data</li>
<li>Send the Trap </li>
</ul>
<div>Lets look at the first component, on getting the encoded Trap data</div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/trapdata.png"><img class="aligncenter size-full wp-image-178" title="trapdata" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/trapdata.png" alt="" width="500" height="224" /></a></div>
<p>The code snippet above shows a simple way of creating and encoding a Trap PDU. Essentially, we create an instance of PDU class and sets the type as Trap. This is important, else SNMP4J shall throw an exception. Thereafter, we can set the trap parameters. Here, we have hardcoded the parameters, there can be custom implementations that can take these from config files or from UI. After setting the parameters, we just call the encode function passing the Output stream and collect the byte array to be sent.</p>
<div>Sending part is even simpler     </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/sendtrap.png"><img class="aligncenter size-full wp-image-179" title="sendtrap" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/sendtrap.png" alt="" width="500" height="83" /></a></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
// --></script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></p>
<p>The send code is preety straight forward. Here we have used Datagram Socket, we can use Apache MINA UDP Client implementation to send the trap as well.</p>
<div>References</div>
<div>
<ul>
<li><a href="http://www.snmp4j.org/">http://www.snmp4j.org/</a></li>
<li><a href="http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/">Implementing UDP Client using Apache MINA</a> </li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-sender-using-snmp4j/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing Trap Receiver in 30 minutes using Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-receiver-in-30-minutes-using-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-receiver-in-30-minutes-using-apache-mina/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 10:24:26 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Trap Receiver]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=157</guid>
		<description><![CDATA[Will continues to post articles about MINA, to be updated,  Subscribe in a reader Yes, we are going to implement a SNMP trap receiver in less than 30 minutes. If you have been following my post on Apache MINA, this would be a natural extension to it. In this article, we shall bring together all [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
// --></script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></p>
<p>Will continues to post articles about MINA, to be updated, <a rel="alternate" type="application/rss+xml" href="http://feedproxy.google.com/AshishsTechBlog"><img style="vertical-align:middle;border:0" src="http://www.feedburner.com/fb/images/pub/feed-icon16x16.png" alt="" /></a> <a rel="alternate" type="application/rss+xml" href="http://feedproxy.google.com/AshishsTechBlog">Subscribe in a reader</a></p>
<p>Yes, we are going to implement a SNMP trap receiver in less than 30 minutes. If you have been following my post on Apache MINA, this would be a natural extension to it. In this article, we shall bring together all our components to build the system.</p>
<p> <strong>Pre-requisites:</strong></p>
<p><strong> <span style="font-weight: normal;">Please read through these articles. You can find all these posts on <a href="http://www.ashishpaliwal.com/blog/apache-mina/">Apache MINA</a></span></strong></p>
<ul type="disc">
<li>What is Apache MINA</li>
<li>Apache MINA based Server Application Architecture</li>
<li>Implementing UDP Server using Apache MINA</li>
<li>Implementing SNMP4J Decoder for Apache MINA</li>
</ul>
<p> To execute the code, you need following jars</p>
<ul type="disc">
<li>mina-core-2.0.0-M1.jar</li>
<li>slf4j-api-1.5.0.jar</li>
<li>slf4j-log4j12-1.5.0.jar</li>
<li>log4j-1.2.15.jar</li>
<li>SNMP4J.jar</li>
</ul>
<p> I am assuming that you have read my previous posts and jumping straight to the implementations. The high level architecture is explained in the post Apache MINA based Server Application Architecture</p>
<p><strong>Architecture</strong> </p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/traprcvrarchitecture1.png"><img class="aligncenter size-full wp-image-164" title="traprcvrarchitecture1" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/traprcvrarchitecture1.png" alt="" width="500" height="238" /></a></p>
<p>Let’s understand the flow here and see what all we need to do to process a trap</p>
<ul type="disc">
<li class="MsoNormal">Receive      the trap over UDP</li>
<li class="MsoNormal">Decode      the Trap</li>
<li class="MsoNormal">Dump      the Trap</li>
</ul>
<p>Lets see how each of the these maps to the code we have already written</p>
<p><a style="text-decoration: none;" href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/traprcvrcomponents1.png"><span style="color: #000000;"><br />
 </span><img class="aligncenter size-full wp-image-160" style="text-decoration: underline;" title="traprcvrcomponents1" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/traprcvrcomponents1.png" alt="" width="500" height="248" /></a></p>
<p>It's a now simple to understand that we have reused all the components to create the Server. Let me not write too much, and jump straight to the code <span><span>J</span></span></p>
<p><strong> The Server Code</strong></p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/trapserver.png"><img class="aligncenter size-full wp-image-161" title="trapserver" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/trapserver.png" alt="" width="500" height="339" /></a></p>
<p>The only change that we did to the UDP Server code was to add a Protocol Codec. At line 36, we added our custom ProtocolCodecFiler. The SNMPCodecFactory return the instance of our SNMP4J codec. There is not much code and this construct is fairly simple, and explained very well in MINA’s documentation. From the main method, we just need to call the method initialize() and our trap receiver starts.</p>
<p> </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
// --></script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><br />
 <strong>References</strong></p>
<ul type="disc">
<li><a href="http://mina.apache.org/">http://mina.apache.org/</a></li>
<li><a href="http://www.snmp4j.org/">http://www.snmp4j.org/</a></li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
// --></script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-receiver-in-30-minutes-using-apache-mina/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing UDP Server using Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 07:07:28 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UDP]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=151</guid>
		<description><![CDATA[In my last post we created a UDP client using Apache MINA. Lets turn the table and implement the Server side. Let's see how using Apache MINA reduces the effort to create a UDP Server. Steps to create a UDP Server using java.net API's Create a Socket and listen for incoming connection Process each packet [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post we created a UDP client using Apache MINA. Lets turn the table and implement the Server side. Let's see how using Apache MINA reduces the effort to create a UDP Server.</p>
<p>Steps to create a UDP Server using java.net API's</p>
<ol>
<li>Create a Socket and listen for incoming connection</li>
<li>Process each packet in a separate thread <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  (I hate this, unfortunately need this to have high processing rate)</li>
<li>Parse and process the request and optionally send response (Lets omit this to keep things simple)</li>
</ol>
<div>Lets see how to achieve the same using Apache MINA</div>
<div>
<ol>
<li>Create a NioDatagramAcceptor</li>
<li>Add an IoHandler</li>
<li>Bind and make application ready to receive</li>
</ol>
</div>
<div>That's it <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </div>
<div>Before we dive into the code, lets see some assumption made to run this</div>
<div>
<ul>
<li>Our protocol is carrying Strings in UDP packet</li>
<li>We shall not do any transformation on the packets received. We shall just dump the content</li>
</ul>
<div><strong>The Handler</strong></div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/udpsrvhandler.png"><img class="aligncenter size-full wp-image-142" title="udpsrvhandler" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/udpsrvhandler.png" alt="" width="500" height="192" /></a></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>The Handler is in its basic form. Our only method of interest is messageReceived. Since we know that we are getting "String" message without any transformation, we could easily create a new string from the bytes received. A wonderful thing to note about this API is the parameter "<em>message</em>". This makes the API generic enough to cater to any kind of objects. If we had used a ProtocolCodec in the chain and had transformed the byte[] into a custom Object, we would have type-casted <em>message </em>to that object.</p>
<div>That's all in the handler.</div>
<p> </p>
<div><strong>The Server</strong></div>
<div>Lets see the main Server code</div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpsrv.png"><img class="aligncenter size-full wp-image-143" title="minaudpsrv" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpsrv.png" alt="" width="500" height="379" /></a></div>
<p> <script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<div>The Server is even simpler than client <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </div>
<div>We create an instance of NioDatagramAcceptor and add our custom handler to it. We then bind to the port desired. Here I have made it bind to the default port, code can be customized to bind to any port desired.</div>
<p>So what does 31 does. There is wonderful description of this option at <a href="http://www.unixguide.net/network/socketfaq/4.5.shtml">http://www.unixguide.net/network/socketfaq/4.5.shtml</a><br />
The main function is pretty simple. The Server is ready. You can run it, using the Client from the post <a href="http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/">Implementing UDP Client using Apache MINA</a></p>
<p><strong>References</strong></p>
<div>
<ul>
<li><a href="http://mina.apache.org/udp-tutorial.html">http://mina.apache.org/udp-tutorial.html</a></li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing UDP Client using Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 10:47:45 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[UDP Client]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=110</guid>
		<description><![CDATA[Its been a while that I wrote on Apache MINA. Please refer to my post collection on Apache MINA. In this post, I have tried to capture briefly on how to implement a UDP Client using Apache MINA. We shall concentrate on keeping our scope limited to sending data over UDP. In Subsequent posts, we [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while that I wrote on Apache MINA. Please refer to my post collection on <a href="http://www.ashishpaliwal.com/blog/apache-mina/">Apache MINA</a>.</p>
<p>In this post, I have tried to capture briefly on how to implement a UDP Client using Apache MINA. We shall concentrate on keeping our scope limited to sending data over UDP. In Subsequent posts, we shall see how we can enhance this UDP Client to send SNMP Traps.</p>
<p>In brief, these are the steps we need to perform to send a UDP packet</p>
<ol>
<li>Create a Datagram Socket</li>
<li>Create a Datagram Packet</li>
<li>Send the packet</li>
</ol>
<div>This is how we used to do when using java.net API's. The logical flow remains same, but lets see how it maps to Apache MINA</div>
<div>
<ol>
<li>Create a NioDatagramConnector instance</li>
<li>Add an IoHandler (we can use an IoHandlerAdapter, as for this example we don't need to use all the API's)</li>
<li>Connect the NioDatagramConnector</li>
<li>Get the session</li>
<li>Write the data onto the session</li>
</ol>
<div>There is a slight difference in which the MINA API's work. The API's work asynchronously. A call to connect() on NioDatagramConnector, return a reference to ConnectFuture and the operation is kicked off in a new thread. Lets see how the Code looks like and see it in details.</div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient.png"><img class="alignleft size-full wp-image-113" title="minaudpclient" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient.png" alt="" width="500" height="270" /></a></div>
<div> </div>
<p>Lets see the Code in detail now. We have maintained the Session at the class level, assuming that we may reuse the session for sending packets, if session is still active. At line 25, we check if the session is active or not. If inactive we initiate the connection. </p>
<p>Line 28, creates an instance of NioDatagramConnector</p>
<p>Line 31, add an IoHandler to the NioDatagramConnector on Line 28. In MINA terminology, IoHandler contains the code where Business logic of an Application resides. In our case its a pure Adapter, with only log statements inside the functions.</p>
<p>Line 32, a call to connect(), returns an reference to ConnectFuture. The call, starts the connection operation, in a separate thread and returns to the caller. To cater to this asynchronous behaviour, we add an IoFutureListener, to returned ConnectFuture reference. Out there (Line 38-43), we just check if the connection is OK, we get the session from it. Line 39, signals that connection is connected and its safe to get the session for writing data.</p>
<p>The connect code is complete. Lets see how to send the data.</p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient_send.png"><img class="aligncenter size-full wp-image-116" title="minaudpclient_send" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient_send.png" alt="" width="500" height="286" /></a></p>
<p>The send function is pretty simple. We just create an IoBuffer and call write on the session. We have called connect() on Line 59, just to ensure that the connection is alive. If it isn't, it shall create a connection and return a session back.</p>
<p>NOTE: This code is in its very simple form. There can be various mechanism to handle Session (based on destination etc). Also, please be aware of the asynchronous behaviour of the NioDatagramConnector connect() API. If the connection is not complete in time, you might see a null pointer at Line 67 <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> . But this is usually the case. For production code, it is advisable to ensure that session is never null.</p>
<p>That's it, out UDP client is ready. To create an SNMP Trap sender, we just need to pass Trap byte array to send() and we are done. Will try to post that article soon.</p>
<p>References</p>
<ul>
<li><a href="http://mina.apache.org/udp-tutorial.html">http://mina.apache.org/udp-tutorial.html</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Apache MINA based Server Application Architecture</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/apache-mina-based-server-application-architecture/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/apache-mina-based-server-application-architecture/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 11:53:58 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[Application Architecture]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=45</guid>
		<description><![CDATA[Apache MINA is one of the best available NIO Frameworks for creating scalable Server applications in Java. Its fun to create applications using MINA.  Lets have a look how the application architecture looks like Lets briefly see what the Major components are IO service - The API's at this level are responsible for performing actual [...]]]></description>
			<content:encoded><![CDATA[<p>Apache MINA is one of the best available NIO Frameworks for creating scalable Server applications in Java. Its fun to create applications using MINA. </p>
<p>Lets have a look how the application architecture looks like</p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaappl.png"><img class="aligncenter size-full wp-image-92" title="minaappl" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaappl.png" alt="" width="500" height="274" /></a>Lets briefly see what the Major components are</p>
<p><strong>IO service</strong> - The API's at this level are responsible for performing actual IO operation over the network</p>
<p><strong>IO Filters</strong> - Filters are the mechanism by which MINA lets a User plugin its code to manipulate the byte stream from the network. Filters are arranged in Filter chain and are called by MINA in the order in which they are present. Filters are mainly used for transforming byte stream into high level objects.</p>
<p><strong>IO Handler</strong> - IO Handler is the heart of application logic. Here is where the whole application logic is concentrated.</p>
<p>MINA is JMX enabled, hence Filters can be dynamically added/removed from the Filter chain.</p>
<p>The framework comes with samples. Please feel free to try them out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/apache-mina-based-server-application-architecture/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Mocking &#8211; the interpreted way</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/mocking-the-interpreted-way/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/mocking-the-interpreted-way/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 10:49:03 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=82</guid>
		<description><![CDATA[Mocking is a subject extensively covered and used heavily in Unit Testing. There are number of Mocking frameworks available today, ranging from Static Mocking to Dynamic Mocking, and the latest additions are AOP based Mocking Frameworks. Had been Coding for a few years now and at that time, JUnit has just made its entry. Wow! [...]]]></description>
			<content:encoded><![CDATA[<p>Mocking is a subject extensively covered and used heavily in Unit Testing. There are number of Mocking frameworks available today, ranging from Static Mocking to Dynamic Mocking, and the latest additions are AOP based Mocking Frameworks. Had been Coding for a few years now and at that time, JUnit has just made its entry. Wow! now I can automate my Unit Testing, but the pain was had to use static mock, objects. Each time I needed a new Object State, had to recompile and test again.</p>
<p>This is how a static mock code looks like</p>
<div id="attachment_85" class="wp-caption aligncenter" style="width: 479px"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/staticmock.png"><img class="size-full wp-image-85" title="Static Mock" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/staticmock.png" alt="Static Mock" width="469" height="178" /></a><p class="wp-caption-text">Static Mock</p></div>
<p>Even when using a mocking framework, we have to define the state of the object we want to return, which means compiling and redploying especially when testing deployed applications. Can frameworks be smart enough to take care of all data variation? unfortunately, the answer is no. Frameworks provide various ways of reducing the effort, but can't make it zero. Hope its always true, else Develoeprs like me shall loose their jobs.</p>
<p>Coming back to the topic. With a little overhead we can make this mock dynamic, so that we can change the state of the object without compiling and redeplying <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Let me introduce beanshell. Beanshell is a framework that supports lightweight scripting in Java and it now JSR274. It allows to write scripts in Java syntax, with type relaxation. Now lets see how can we make out Status mock, a dynamic mock by using beanshell script. many IDE's support beanshell scripting as well. Other options available are JRuby and Groovy.</p>
<p>Lets change the Mocked Object code to support beanshell</p>
<p><span style="text-decoration: underline;"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/interpretedmock1.png"><img class="aligncenter size-full wp-image-87" title="interpretedmock1" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/interpretedmock1.png" alt="" width="500" height="163" /></a></span></p>
<p>We create an instance of BSH Interpreter and call the file containing the implementation of the function. I prefer seperate file for seperate functions, to have exercise more control. The difference here is, we have moved the main logic from compiled code to interpreted code. This way, we can change the logic at runtime. We just change the bsh file and the interpreter executes the new version.</p>
<p>Lets look at bsh file</p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/bsh.png"><img class="aligncenter size-full wp-image-88" title="bsh" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/bsh.png" alt="" width="499" height="188" /></a></p>
<p>This post displays the cocnept in a very simplistic manner. There can be many variations like implementing the interfaces completely in scripting. Scripting can used as an add-on to existing mocking frameworks taking the Unit Testing to next level.</p>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.beanshell.org/">http://www.beanshell.org/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/mocking-the-interpreted-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

