<?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>Paul Codding&#039;s Weblog &#187; Java</title>
	<atom:link href="http://www.paulcodding.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulcodding.com/blog</link>
	<description>My miscellaneous ramblings and how-to&#039;s</description>
	<lastBuildDate>Sat, 17 Jul 2010 02:59:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Building a Jabber Bot</title>
		<link>http://www.paulcodding.com/blog/2009/02/28/jabber_bot/</link>
		<comments>http://www.paulcodding.com/blog/2009/02/28/jabber_bot/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 17:50:05 +0000</pubDate>
		<dc:creator>Paul Codding</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.paulcodding.com/blog/?p=44</guid>
		<description><![CDATA[The Idea After a great suggestion from a colleague I was inspired to create what I think is an under-used utility.  That utility is an Instant Messaging Bot.  I know they seem dated, and harken back to the days of AIM bots, but in this world of rapid publishing and communications I think they&#8217;re an [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Idea</strong></p>
<p>After a great suggestion from a colleague I was inspired to create what I think is an under-used utility.  That utility is an Instant Messaging Bot.  I know they seem dated, and harken back to the days of AIM bots, but in this world of rapid publishing and communications I think they&#8217;re an under utilized tool.  This post is meant to highlight the use of two technologies: Java, and <a title="Openfire" href="http://www.igniterealtime.org/projects/openfire/index.jsp" target="_blank">Openfire</a>.</p>
<p><strong>The Technology</strong></p>
<p>Openfire is an open-source Jabber(XMPP) server.  The installation is quite simple, the only thing that is required is a JRE, and if you would like, a MySQL database.  In my installation I have installed Openfire on Ubuntu JeOS that has the sun-java6-jdk and mysqld-server packages installed.  Openfire is distributed as a debian package, so installation is a breeze.  You can read more <a title="here" href="http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html" target="_blank">here</a> on how to install, this post will assume you have a working server and a Java development environment&#8230;with maven.</p>
<p><strong>The Application</strong></p>
<p>We want our application to login to the Jabber server, and listen for messages bound for the bot user.  Once a message is received it should be checked for a command, and a proper response should be sent back to the user.  In order to accomplish this we need three classes.  One class will handle logging into the Jabber server and beginning to listen for chat messages.  The other class will be invoked when a chat is initiated, and the other will handle messages sent in that chat session.</p>
<p>Smack is the client library that we will use for this project and can be download from the same site that you downloaded Openfire from.  Smack includes the two interfaces we will use for the class that listens for chat requests, and handles chat messages.  These interfaces are <a title="ChatManagerListener" href="http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/ChatManagerListener.html" target="_blank">ChatManagerListener</a>, and <a title="MessageListener" href="http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/MessageListener.html" target="_blank">MessageListener</a>.</p>
<p><strong>The Code</strong></p>
<p>We&#8217;ll start by adding our dependency for the Smack client library:</p>
<pre name="code" class="xml">&lt;dependency&gt;
  &lt;groupId&gt;jivesoftware&lt;/groupId&gt;
  &lt;artifactId&gt;smack&lt;/artifactId&gt;
  &lt;version&gt;3.0.4&lt;/version&gt;
&lt;/dependency&gt;</pre>
<p>Now that we have the necessary dependency we can focus on our three classes.  We will start with our App class.  This class as stated before is responsible for connecting to the Openfire server.  Here is the App.java class.</p>
<pre name="code" class="java">public class App {
  public static void main(String[] args) {
    // Assumes you have the SRV records setup in DNS
    XMPPConnection conn = new XMPPConnection("yourdomain.com");
    try {
      conn.connect();
      conn.login("IM_BOT", "Your Bot Password");
      ChatManager chatManager = conn.getChatManager();
      chatManager.addChatListener(new BotChatListener());

      System.out.println("Connected");
      while (conn.isConnected()) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      conn.disconnect();
    } catch (XMPPException e) {
      e.printStackTrace();
    }
  }
}</pre>
<p>Our next class is the class responsible for listening and handling incoming chat requests and adding a message listener to the new chat.  Here is the BotChatListener.java class.</p>
<pre name="code" class="java">public class BotChatListener implements ChatManagerListener {

  public void chatCreated(Chat chat, boolean local) {
	  chat.addMessageListener(new BotMessageListener());
  }
}</pre>
<p>The MessageListener referred to in the BotChatListener is responsible for handling commands passed to the bot and responding to them appropriately.  Here is the BotMessageListener.java class.</p>
<pre name="code" class="java">public class BotMessageListener implements MessageListener {

  public void processMessage(Chat chat, Message message) {
    System.out.println(chat.getParticipant() + " said -&gt; "
        + message.getBody());
    Message botMessage = null;

    try {
      botMessage = new Message();
      if (message.getBody().equals("nothing")) {
        botMessage.setBody("Fine...");
      } else if (message.getBody().equals("make me toast")) {
        botMessage.setBody("Sure thing");
      } else {
        botMessage.setBody("What do you need?");
      }
      chat.sendMessage(botMessage);
    } catch (XMPPException e) {
      e.printStackTrace();
    }
  }
}</pre>
<p>And that&#8217;s it&#8230;the next step is to extend the BotMessageListener class to do whatever it is you want to accomplish with your bot.  If you wish, you can make a Ferris Bueller type bot, and use it to log into Google Talk with your Gmail account and respond to your co-workers messages while you&#8217;re out doing whatever it is that you do for fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulcodding.com/blog/2009/02/28/jabber_bot/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using the OpenSessionInViewInterceptor for Spring + Hibernate3</title>
		<link>http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/</link>
		<comments>http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 19:35:38 +0000</pubDate>
		<dc:creator>Paul Codding</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/</guid>
		<description><![CDATA[If you&#8217;re like me and have written Spring web applications using Hibernate for persistence, you&#8217;ve probably seen this error when using FetchType.LAZY in your @OneToMany annotation: &#8220;failed to lazily initialize a collection of role: your.Class.assocation no session or session was closed.&#8221; If you are using lazy loading rather than eager loading for obvious reasons, and [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me and have written Spring web applications using Hibernate for persistence, you&#8217;ve probably seen this error when using <code>FetchType.LAZY</code> in your <code>@OneToMany</code> annotation: &#8220;<em>failed to lazily initialize a collection of role: your.Class.assocation no session or session was closed</em>.&#8221;</p>
<p>If you are using lazy loading rather than eager loading for obvious reasons, and you&#8217;re trying to access associated objects in your view code, this error can be a bit difficult to resolve.  Luckily there is the <a href="http://static.springframework.org/spring/docs/2.5.1/api/org/springframework/orm/hibernate3/support/OpenSessionInViewInterceptor.html">OpenSessionInViewInterceptor</a>, or the <a href="http://static.springframework.org/spring/docs/2.5.1/api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html">OpenSessionInViewFilter</a>.  These solutions will keep the hibernate session open long enough for the view to render what is needed before it is closed, thus allowing you to access your lazy loaded associations without raising an exception.  The detail of what this functionality provides can be found on the Hibernate website <a href="http://www.hibernate.org/43.html">here</a>.</p>
<p>Now, how does one get this to work in their application?  Well, I&#8217;ve chosen to illustrate the basic steps necessary to implement the  OpenSessionInViewInterceptor.  You will need to modify 2 files.  These being your <code>applicationContext.xml</code>, and <code>YourApplication-servlet.xml</code> files.  We&#8217;ll start with the <code>applicationContext.xml</code> file.  You will need to place the following snippet after you&#8217;ve defined your <code>sessionFactory</code> bean.</p>
<pre name="code" class="xml">&lt;bean id="openSessionInViewInterceptor"
    class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"&gt;
    &lt;property name="sessionFactory"&gt;
        &lt;ref local="sessionFactory"/&gt;
    &lt;/property&gt;
    &lt;property&gt; name="flushModeName"&gt;
        &lt;value&gt;FLUSH_AUTO&lt;/value&gt;
    &lt;/property&gt;
&lt;/bean&gt;</pre>
<p>What does the <code>FLUSH_AUTO</code> do?  Here is a quote from the javadoc for the interceptor:</p>
<blockquote><p>&#8220;This interceptor will by default not flush the Hibernate Session, with the flush mode being set to FlushMode.NEVER. It assumes that it will be used in combination with service layer transactions that handle the flushing: the active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction.&#8221;</p></blockquote>
<p>We use the <code>FLUSH_AUTO</code> to automatically flush the session in this case because I am not using transactions.<br />
Now comes the edit to your <code>YourApplication-servlet.xml</code> file, or whatever file you&#8217;ve used to define your URL Mappings.  Add the following snippet to your URL mapping bean.</p>
<pre name="code" class="xml">&lt;property name="interceptors"&gt;
  &lt;list&gt;
    &lt;ref bean="openSessionInViewInterceptor" /&gt;
  &lt;/list&gt;
&lt;/property&gt;</pre>
<p>And that&#8217;s that, redeploy and you will no longer see that dreaded exception.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Spring, Hibernate, and Sitemesh</title>
		<link>http://www.paulcodding.com/blog/2008/01/11/spring-hibernate-and-sitemesh/</link>
		<comments>http://www.paulcodding.com/blog/2008/01/11/spring-hibernate-and-sitemesh/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 04:48:37 +0000</pubDate>
		<dc:creator>Paul Codding</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Sitemesh]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.paulcodding.com/blog/2008/01/11/spring-hibernate-and-sitemesh/</guid>
		<description><![CDATA[This week at work I&#8217;ve had the luxury of getting to sit down and write a sample application using a few of my favorite tools: Spring, and Hibernate-Annotations. New to this equation has been Sitemesh. I&#8217;ve been using Tiles for quite a while and have felt very comfortable with it, but at the same time [...]]]></description>
			<content:encoded><![CDATA[<p>This week at work I&#8217;ve had the luxury of getting to sit down and write a sample application using a few of my favorite tools: Spring, and Hibernate-Annotations.  New to this equation has been Sitemesh.  I&#8217;ve been using <a href="http://tiles.apache.org/">Tiles</a> for quite a while and have felt very comfortable with it, but at the same time a little annoyed with the amount of work and time it takes to use it.  As I sat down to pour out as many best practices as possible into this sample application I figured I should use the opportunity to take the time necessary to explore a new tool.  At first I had a hard time finding out just what it takes to use Sitemesh with Spring. I found some helpful hints here and there, but because this blog is meant to try to aggregate my searches into something useful for you (the reader), I will outline just what it takes to integrate Sitemesh into an application.</p>
<h4>Sitemesh Integration</h4>
<p>What do you have to do to integrate Sitemesh with your newly created Spring application?  Well, here is the list of prerequisites:</p>
<ul>
<li>Download Sitemesh from <a href="http://www.opensymphony.com/sitemesh/">OpenSymphony</a>.</li>
<li>Build the sitemesh-2.x.jar using ant.</li>
<li>Copy the sitemesh-2.x.jar to the <code>WEB-INF/lib</code> directory of your web application</li>
</ul>
<p>
Now we can move on to integrating Sitemesh into your web application.  To use sitemesh with your application only 1 file needs to be edited amazingly enough.  Open up your <code>web.xml</code> file and add this snippet:</p>
<pre>
&lt;!-- Sitemesh --&gt;
&lt;filter&gt;
	&lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
	&lt;filter-class&gt;
		com.opensymphony.module.sitemesh.filter.PageFilter
	&lt;/filter-class&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
	&lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
	&lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
<p>So, now what?  The next step is to setup the directory structure and files necessary to make things work.  Refer to this <a href="http://www.opensymphony.com/sitemesh/decorators.html">page</a> on the OpenSymphony site detailing the creation of your first decorator, and creating the <code>WEB-INF/decorators.xml</code> file.  I&#8217;ve included a copy of my <code>main.jsp</code> file for reference:</p>
<pre>
&lt;%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
	prefix="decorator"%&gt;
&lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
	&lt;head&gt;
		&lt;title&gt;&lt;spring:message code="application.title" /&gt;&lt;/title&gt;
		&lt;decorator:head /&gt;
		&lt;%@ include file="/WEB-INF/view/includes/style.jsp"%&gt;
		&lt;%@ include file="/WEB-INF/view/includes/js.jsp"%&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div id="container"&gt;
			&lt;%@ include file="/WEB-INF/view/includes/header.jsp"%&gt;
			&lt;div id="internalWrapper"&gt;
				&lt;%@ include file="/WEB-INF/view/includes/links.jsp"%&gt;
				&lt;div id="content"&gt;
					&lt;decorator:body /&gt;
				&lt;/div&gt;
			&lt;/div&gt;
			&lt;%@ include file="/WEB-INF/view/includes/footer.jsp"%&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>So, how do you get your Controllers to spit out decorated pages?  Easy, just create a jsp that will be used as the content of the <code>&lt;decorator:body /&gt;</code> tag in the <code>main.jsp</code> file.  When you&#8217;re returning your <code>ModelAndView</code> just refer to that jsp like the example below:</p>
<pre>
public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	List&lt;DomainObject&gt; userList = userService.findAll();
	request.setAttribute("userList", userList);
	return new ModelAndView("list.jsp", null);
}
</pre>
<p>Here is an example snippet of my url mapping for reference as to where the jsp is located:</p>
<pre>
&lt;prop key="/users/*.html"&gt;userController&lt;/prop&gt;
</pre>
<p>In this case my <code>list.jsp</code> is located in the <code>users</code> folder under my web root.</p>
<p>So, isn&#8217;t this easier than Tiles?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulcodding.com/blog/2008/01/11/spring-hibernate-and-sitemesh/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Utilizing Ruby on Rails Web Services with Java</title>
		<link>http://www.paulcodding.com/blog/2007/03/31/utilizing-ruby-on-rails-web-services-with-java/</link>
		<comments>http://www.paulcodding.com/blog/2007/03/31/utilizing-ruby-on-rails-web-services-with-java/#comments</comments>
		<pubDate>Sun, 01 Apr 2007 03:29:24 +0000</pubDate>
		<dc:creator>Paul Codding</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[RoR]]></category>

		<guid isPermaLink="false">http://www.paulcodding.com/blog/?p=4</guid>
		<description><![CDATA[As part of a project I&#8217;m currently working on, I&#8217;ve had to integrate a RoR application with another developer&#8217;s Java code. At first glance, things looked to be fairly simple as I envisioned a simple Web Service communication between Java and Ruby would be a breeze&#8230;not so fast. Getting the two to play nice with [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a project I&#8217;m currently working on, I&#8217;ve had to integrate a RoR application with another developer&#8217;s Java code.  At first glance, things looked to be fairly simple as I envisioned a simple Web Service communication between Java and Ruby would be a breeze&#8230;not so fast.  Getting the two to play nice with each other took some time as I was unaware that RoR only supports the RPC/encoded style/use and not Document/literal.  After getting a prototyped client written read using <a href="http://www.springframework.org/spring-ws">Spring-ws</a> (does not support RPC/encoded) I quickly found out that it was going to go nowhere fast.  I ended up choosing to use <a href="http://ws.apache.org/axis/">Apache Axis</a> as I&#8217;ve used it for a previous project, and I knew for sure it supported RPC/encoded.</p>
<p>Part of the reason for this blog is to share information from developer to developer issues that I&#8217;ve encountered that have either been unable or too busy to find solutions to.  In this case my solution is to let the reader know that the best bet client to use when consuming RoR web services in Java is <a href="http://ws.apache.org/axis/">Apache Axis</a>.  If anyone would enjoy a code example or two I&#8217;d be glad to oblige&#8230;although the artifacts generated by Axis 1.4 are fairly involved, the process is fairly easy especially when using the <a href="http://www.soapui.org/">SoapUI tool</a> to generate the artifacts based on the RoR WSDL e.g. http://localhost:3000/web_service_name/service.wsdl .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulcodding.com/blog/2007/03/31/utilizing-ruby-on-rails-web-services-with-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

