<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Vineet Manohar's blog</title>
	
	<link>http://www.vineetmanohar.com</link>
	<description>Java, Web 2.0 and other Tech topics</description>
	<lastBuildDate>Tue, 31 Aug 2010 05:14:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/vineetmanohar" /><feedburner:info uri="vineetmanohar" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>How to copy bean properties with a single line of code</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/AuL1rlX9TTE/</link>
		<comments>http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 05:10:31 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1152</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/';
var dzone_title = 'How to copy bean properties with a single line of code';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
This article shows how to copy multiple properties from one bean to another with a single line of code, even if the property names in the source and target beans are different. Copying properties from one bean is quite common especially if you are working with a lot of POJOs, for example working with JAXB [...]


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/09/jaxb-code-snippets-for-beginners/' rel='bookmark' title='Permanent Link: JAXB code snippets for beginners'>JAXB code snippets for beginners</a></li><li><a href='http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/' rel='bookmark' title='Permanent Link: Calling a static method from EL'>Calling a static method from EL</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/';
var dzone_title = 'How to copy bean properties with a single line of code';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>This article shows how to copy multiple properties from one bean to another with a single line of code, even if the property names in the source and target beans are different.</p>
<p>Copying properties from one bean is quite common especially if you are working with a lot of POJOs, for example working with <a target="_blank" href="http://www.vineetmanohar.com/tag/jaxb/">JAXB</a> objects. Lets walk through the following example where we want to copy all properties from the User object -&gt; SystemUser object</p>
<h3>Example: source and target objects</h3>
<pre name="code" class="java">// source object that we want to copy the properties from
public class User {
    private String first;
    private String last;
    private String address1;
    private String city;
    private String state;
    private String zip;
    private String phone;

    // getters and setters
    // ...
}

// target object that we want to copy the properties to
public class SystemUser {
    private String firstName;
    private String lastName;
    private String phone;
    private String addressLine1;
    private String addressLine2;
    private String city;
    private String state;
    private String zip;

    // getters and setters
    // ...
}
</pre>
<h3>Example continued: Preparing the objects</h3>
<pre name="code" class="java">// initializing the source object with example values
User user = new User();
user.setFirst("John");
user.setLast("Smith");
user.setAddress1("555 Lincoln St");
user.setCity("Washington");
user.setState("DC");
user.setZip("00000");
user.setPhone("555-555-5555");

// creating an empty target object
SystemUser systemUser = new SystemUser();
</pre>
<h3>Approach 1: Traditional code for copying properties</h3>
<pre name="code" class="java">systemUser.setFirstName(user.getFirst());
systemUser.setLastName(user.getLast());
systemUser.setPhone(user.getPhone());
systemUser.setAddressLine1(user.getAddress1());
systemUser.setAddressLine2(user.getAddress2());
systemUser.setCity(user.getCity());
systemUser.setState(user.getState());
systemUser.setZipcode(user.getZip());
</pre>
<h3>Approach 2: Single line code for copying properties</h3>
<pre name="code" class="java">
copyProperties(user, systemUser, "first firstName", "last lastName", "phone",
               "address1 addressLine1", "address2 addressLine2", "city", "state", "zip zipcode");
</pre>
<p><strong>Parameters</strong></p>
<ol>
<li><strong>user</strong> &#8211; the source object</li>
<li><strong>systemUser</strong> &#8211; the target object</li>
<li><strong>first firstName</strong> &#8211; indicates that the &#8220;first&#8221; property of the source object should be copied to the &#8220;firstName&#8221; property of the target object</li>
<li><strong>last lastName</strong> &#8211; indicates that the &#8220;last&#8221;  property of the source object should be copied to the &#8220;lastName&#8221;  property of the target object</li>
<li><strong>phone</strong> &#8211; indicates that the &#8220;phone&#8221;  property of the source object should be copied to the &#8220;phone&#8221;  property of the target object</li>
<li> &#8230;. and so on</li>
</ol>
<p>The underlying code uses Apache Commons BeanUtils code to copy the property value from the source to the destination.</p>
<p>You can download the copyProperties code from <a href="http://code.google.com/p/vineetmanohar/source/browse/trunk/src/main/java/com/vineetmanohar/util/BeanPropertyCopyUtil.java" target="_blank">Google Code</a>. Simply copy the code to your project. The code requires apache BeanUtils. If you are using maven, you can get BeanUtils by adding the following to your pom.xml</p>
<pre name="code" class="xml"> &lt;!-- for maven only, add this to your pom to get BeanUtils, needed by the copyProperties code --&gt;
 &lt;dependency&gt;
   &lt;groupId&gt;commons-beanutils&lt;/groupId&gt;
   &lt;artifactId&gt;commons-beanutils&lt;/artifactId&gt;
   &lt;version&gt;1.8.3&lt;/version&gt;
 &lt;/dependency&gt;</pre>
<h2>References</h2>
<ul>
<li><a href="http://code.google.com/p/vineetmanohar/source/browse/trunk/src/main/java/com/vineetmanohar/util/BeanPropertyCopyUtil.java" target="_blank">copyProperties Java code from Google Code</a></li>
<li><a target="_blank" href="http://commons.apache.org/beanutils/">Apache Commons BeanUtils</li>
</ul>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/09/jaxb-code-snippets-for-beginners/' rel='bookmark' title='Permanent Link: JAXB code snippets for beginners'>JAXB code snippets for beginners</a></li><li><a href='http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/' rel='bookmark' title='Permanent Link: Calling a static method from EL'>Calling a static method from EL</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/AuL1rlX9TTE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/</feedburner:origLink></item>
		<item>
		<title>Calling a static method from EL</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/7y9MiG4vXCk/</link>
		<comments>http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:29:47 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[el]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1189</guid>
		<description><![CDATA[This post shows how to call any static method from EL. You can also pass arguments to the method.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/' rel='bookmark' title='Permanent Link: How to pass parameters in EL methods'>How to pass parameters in EL methods</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/';
var dzone_title = 'Calling a static method from EL';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>In my previous post I showed <a title=" How to pass parameters in EL methods" href="http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/" target="_blank">how to pass parameters in EL methods</a>. In this post, I will describe how to call static methods from EL.</p>
<h3>Step 1: Download Call.java and ELMethod.java</h3>
<p>Download the classes <a href="http://vineetmanohar.googlecode.com/svn/trunk/src/main/java/com/vineetmanohar/util/Call.java">Call.java</a> and <a href="http://vineetmanohar.googlecode.com/svn/trunk/src/main/java/com/vineetmanohar/util/ELMethod.java">ELMethod.java</a>, and copy them to your project. </p>
<h3>Step 2: Add an instance of Call.java as an application scoped bean</h3>
<p>There are many ways to do this, depending on this technology you are using. Here are some examples:</p>
<p><strong>How to set request scope in JSP<br />
</strong></p>
<pre name="code" class="java">request.setAttribute("call", new Call());
</pre>
<p><strong>How to set session scope in JSP<br />
</strong></p>
<pre name="code" class="java">session.setAttribute("call", new Call());
</pre>
<p><strong>How to set application scope from Servlet</strong></p>
<pre name="code" class="java">this.getServletContext().setAttribute("call", new Call())</pre>
<p><strong>How to set application scope from JSP</strong></p>
<pre name="code" class="xml">&lt;jsp:useBean id="call" class="Call" scope="application" /&gt;
</pre>
<p><strong>How to set application scope in Seam</strong></p>
<p>Add these annotations to your class</p>
<pre name="code" class="java">@Name("call")
@Scope(ScopeType.APPLICATION)
</pre>
<h3>Step 3: Call any static method from EL as follows</h3>
<p>Let&#8217;s say you have a static method which formats date. The method takes 2 arguments, the date format string and the Date object to format.</p>
<pre name="code" class="java">
package com.mycompany.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
 public static String formatDate(String format, Date date) {
   return new SimpleDateFormat(format).format(date);
 }
}
</pre>
<p>You can call the above static method from EL as follows. In the example below, &#8220;account&#8221; is a request scoped bean with a Date property called &#8220;creationDate&#8221; which needs to be formatted.</p>
<pre name="code" class="java">
  ${call["com.mycompany.util.DateUtils.formatDate"]["MMM, dd"][account.creationDate]}
</pre>
<p>Just like the above example, you can call any static method (except overloaded methods). Simply pass the full class and method name as the first argument, and any parameters required by the method as subsequent arguments. In general, the format is:</p>
<pre name="code" class="java">
  ${call["full.package.name.MethodName"][arg1][arg2][...]}
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>Use map notation with [square brackets] when passing arguments to ${call}</li>
<li>First argument is the full package name + &#8220;.&#8221; + method name</li>
<li>If the static method you are calling takes arguments, simply pass those arguments using more [square brackets]</li>
<li>Overloaded methods are not supported, i.e. you cannot call methods where two methods with the same name exist in the class</li>
</ul>
<h3>References</h3>
<ul>
<li><a href="http://vineetmanohar.googlecode.com/svn/trunk/src/main/java/com/vineetmanohar/util/Call.java">Call.java</a> source code from my Google code project</li>
<li><a href="http://vineetmanohar.googlecode.com/svn/trunk/src/main/java/com/vineetmanohar/util/ELMethod.java">ELMethod.java</a> source code from my Google code project</li>
<li><a href="http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/">How to pass parameters in EL methods</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/' rel='bookmark' title='Permanent Link: How to pass parameters in EL methods'>How to pass parameters in EL methods</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/7y9MiG4vXCk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/</feedburner:origLink></item>
		<item>
		<title>How to pass parameters in EL methods</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/w8K_JB3f-H8/</link>
		<comments>http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 22:41:43 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[el]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1149</guid>
		<description><![CDATA[How to pass multiple parameters in an EL function, in 3 simple steps.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/' rel='bookmark' title='Permanent Link: Calling a static method from EL'>Calling a static method from EL</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/';
var dzone_title = 'How to pass parameters in EL methods';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>This article describes how to pass any number of parameters in EL functions in 3 simple steps.</p>
<h2><strong>Step 1</strong>: Extend the class ELMethod</h2>
<p>Extend the class <a href="http://vineetmanohar.googlecode.com/svn/trunk/src/main/java/com/vineetmanohar/util/ELMethod.java" target="_blank">ELMethod.java</a>, and implement the following method:</p>
<pre name="code" class="java">public abstract Object result(Object[] args);
</pre>
<p>For example, say we want to create a <strong>formatDate</strong> method which takes 2 arguments</p>
<ol>
<li>the format String</li>
<li>the Date to be formatted</li>
</ol>
<p>All you need to do is extend the ELMethod class and implement the  result(Object[] args) method.</p>
<h3>Example</h3>
<pre name="code" class="java">import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatDate extends ELMethod {
 private static final long serialVersionUID = 1L;

 public FormatDate() {
   // number of arguments to the result method
   super(2);
 }

 public Object result(Object[] args) {
   String pattern = (String) args[0];
   Date date = (Date) args[1];

   return new SimpleDateFormat(pattern).format(date);
 }
}
</pre>
<h2>Step 2: Add an instance of you class as an application scoped bean</h2>
<p>There are many ways to do this, depending on this technology you are using. Here are some examples:</p>
<p><strong>Set request scope in JSP<br />
</strong></p>
<pre name="code" class="java">request.setAttribute("formatDate", new FormatDate());
</pre>
<p><strong>Set session scope in JSP<br />
</strong></p>
<pre name="code" class="java">session.setAttribute("formatDate", new FormatDate());
</pre>
<p><strong>Set application scope from Servlet</strong></p>
<pre name="code" class="java">this.getServletContext().setAttribute("formatDate", new FormatDate())</pre>
<p><strong>Set application scope from JSP</strong></p>
<pre name="code" class="xml">&lt;jsp:useBean id="formatDate" class="FormatDate" scope="application" /&gt;
</pre>
<p><strong>Set application scope in Seam</strong></p>
<p>Add these annotations to your class</p>
<pre name="code" class="java">@Name("formatDate")
@Scope(ScopeType.APPLICATION)
</pre>
<h2>Step 3: Call the function via map syntax</h2>
<p>Simply pass each argument using the map syntax [] as follows</p>
<p><strong>EL code</strong></p>
<pre name="code" class="java">${formatDate["MMM, dd"][account.creationDate]}
</pre>
<p>All arguments passed via EL are sent to the result(Object[] args) method of the FormatDate class.</p>
<h2>How does it work</h2>
<p>Each argument is collected via the get(Object key) method and is added to an array. When all the arguments have been specified the result method is invoked and the resulting value is returned.</p>
<h2>Reference</h2>
<ul>
<li>Source code of<a href="http://code.google.com/p/vineetmanohar/source/browse/trunk/src/main/java/com/vineetmanohar/util/ELMethod.java" target="_blank"> ELMethod.java</a> on my Google Code project. This class is thread safe and can be application scoped.</li>
<li><a href="http://code.google.com/p/vineetmanohar/source/browse/trunk/src/main/java/com/vineetmanohar/util/FormatDate.java" target="_blank">FormatDate.java</a> example on my Google Code project</li>
<li><a href="http://code.google.com/p/vineetmanohar/source/browse/trunk/src/main/java/com/vineetmanohar/util/Add.java" target="_blank">Add.java</a> example, which adds 2 numbers in EL (e.g. ${add[1][2]})</li>
</ul>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/' rel='bookmark' title='Permanent Link: Calling a static method from EL'>Calling a static method from EL</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/' rel='bookmark' title='Permanent Link: 2 ways to convert Java Map to String'>2 ways to convert Java Map to String</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/w8K_JB3f-H8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/07/how-to-pass-parameters-in-el-methods/</feedburner:origLink></item>
		<item>
		<title>How to automatically recover Tomcat from crashes</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/cCYwA9-z4X0/</link>
		<comments>http://www.vineetmanohar.com/2010/06/howto-auto-recover-tomcat-crashes/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 03:09:19 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1108</guid>
		<description><![CDATA[A script which automatically detects that tomcat has crashed and restarts it.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/03/howto-rotate-tomcat-catalina-out/' rel='bookmark' title='Permanent Link: How to rotate Tomcat catalina.out'>How to rotate Tomcat catalina.out</a></li><li><a href='http://www.vineetmanohar.com/2009/10/how-to-automate-project-versioning-and-release-with-maven/' rel='bookmark' title='Permanent Link: How to automate project versioning and release with Maven'>How to automate project versioning and release with Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/03/cache-java-webapps-with-squid-reverse-proxy/' rel='bookmark' title='Permanent Link: Cache Java webapps with Squid Reverse Proxy'>Cache Java webapps with Squid Reverse Proxy</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/06/howto-auto-recover-tomcat-crashes/';
var dzone_title = 'How to automatically recover Tomcat from crashes';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>Tomcat occasionally crashes if you do frequent hot-deploys or if you are running it on a machine with low memory. Every time tomcat crashes someone has to manually restart it, so I wrote a script which automatically detects that tomcat has crashed and restarts it.</p>
<p>Here&#8217;s the pseudo logic:</p>
<pre name="code" class="css">every few minutes {
  check tomcat status;

  if (status is "not running") {
    start tomcat;
  }
}
</pre>
<p>Here&#8217;s a shell script to implement the above logic. It assumes that you are running on a unix/linux system and have /etc/init.d/tomcat* script setup to manage tomcat.</p>
<p>Adjust the path to &#8220;/etc/init.d/tomcat&#8221; in the script below to reflect the correct path on your computer. Sometimes it is called /etc/init.d/tomcat5 or /etc/init.d/tomcat6 depending on your tomcat version. Also make sure that the message &#8220;Tomcat Servlet Container is not running.&#8221; matches with the message that you get when you run the script when tomcat is stopped.</p>
<pre name="code" class="css">#! /bin/sh
SERVICE=/etc/init.d/tomcat
STOPPED_MESSAGE="Tomcat Servlet Container is not running."

if [ "`$SERVICE status`" == "$STOPPED_MESSAGE"];
then
{
  $SERVICE start
}
fi
</pre>
<p><strong>To run the script every 10 minutes: </strong></p>
<p>1. Save the above script to &#8220;/root/bin/recover-tomcat.sh&#8221;.</p>
<p>2. Add execute permission:</p>
<pre name="code" class="css">chmod +x /root/bin/recover-tomcat.sh</pre>
<p>3. Add this to root&#8217;s crontab, type the following as root:</p>
<pre name="code" class="css">crontab -e</pre>
<p>4. Add the following lines to the crontab:</p>
<pre name="code" class="css"># monitor tomcat every 10 minutes
*/10 * * * * /root/bin/recover-tomcat.sh</pre>
<h3>What if I don&#8217;t have /etc/init.d/tomcat* script on my computer?</h3>
<p>Tomcat creates a pid file, typically in the TOMCAT_HOME/bin directory. This file contains the process id of the tomcat process running on the machine. The pseudo logic in that case would be:</p>
<pre name="code" class="css">if (the PID file does not exist) {
  // conclude that tomcat is not running
  start tomcat
}
else {
  read the process id from the PID file
  if (no process that id is running) {
    // conclude that tomcat has crashed
    start tomcat
  }
}
</pre>
<p>You can implement the above logic as follows. The following is experimental and is merely a suggested way, test it on your computer before using it.</p>
<pre name="code" class="css"># adjust this to reflect tomcat home on your computer
TOMCAT_HOME=/opt/tomcat5

if [ -f $TOMCAT_HOME/bin/tomcat.pid ]
then
  echo "PID file exists"
  pid="`cat $TOMCAT_HOME/bin/tomcat.pid`"
  if [ "X`ps -p $pid | awk '{print $1}' | tail -1`" = "X"]
  then
    echo "Tomcat is running"
  else
    echo "Tomcat had crashed"
    $TOMCAT_HOME/bin/startup.sh
  fi
else
  echo "PID file does not exist. Restarting..."
  $TOMCAT_HOME/bin/startup.sh
fi
</pre>
<h3>Why would tomcat crash?</h3>
<p>The most common reason is low memory. For example, if you have allocated 1024MB of max memory to tomcat and enough memory is not available on that machine. Other reasons may involve repeated hot-deploys causing memory leaks, rare JVM bugs causing the JVM to crash.</p>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/03/howto-rotate-tomcat-catalina-out/' rel='bookmark' title='Permanent Link: How to rotate Tomcat catalina.out'>How to rotate Tomcat catalina.out</a></li><li><a href='http://www.vineetmanohar.com/2009/10/how-to-automate-project-versioning-and-release-with-maven/' rel='bookmark' title='Permanent Link: How to automate project versioning and release with Maven'>How to automate project versioning and release with Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/03/cache-java-webapps-with-squid-reverse-proxy/' rel='bookmark' title='Permanent Link: Cache Java webapps with Squid Reverse Proxy'>Cache Java webapps with Squid Reverse Proxy</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/cCYwA9-z4X0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/06/howto-auto-recover-tomcat-crashes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/06/howto-auto-recover-tomcat-crashes/</feedburner:origLink></item>
		<item>
		<title>Getting started with Nexus Maven Repo Manager</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/K-Z-hk6s-Cs/</link>
		<comments>http://www.vineetmanohar.com/2010/06/getting-started-with-nexus-maven-repo-manager/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 01:32:20 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[nexus]]></category>
		<category><![CDATA[repo]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1076</guid>
		<description><![CDATA[This tutorial outlines steps required to install Nexus (Maven Repository Manager) under Tomcat, or another webapp container. It shows you practical configuration and includes code snippets that go in your pom.xml and settings.xml in order to read and publish artifacts to Nexus server.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/12/publishing-to-maven-central-repo-in-5-steps/' rel='bookmark' title='Permanent Link: Publishing to Maven Central Repo in 5 steps'>Publishing to Maven Central Repo in 5 steps</a></li><li><a href='http://www.vineetmanohar.com/2009/10/how-to-automate-project-versioning-and-release-with-maven/' rel='bookmark' title='Permanent Link: How to automate project versioning and release with Maven'>How to automate project versioning and release with Maven</a></li><li><a href='http://www.vineetmanohar.com/2009/11/tweet-your-builds-with-maven-twitter-plugin/' rel='bookmark' title='Permanent Link: Tweet your builds with Maven Twitter Plugin'>Tweet your builds with Maven Twitter Plugin</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/06/getting-started-with-nexus-maven-repo-manager/';
var dzone_title = 'Getting started with Nexus Maven Repo Manager';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<h2>Overview</h2>
<p>This tutorial outlines steps required to install <a href="http://nexus.sonatype.org/">Nexus</a> (Maven Repository Manager) under Tomcat, or another webapp container. It shows you practical configuration and includes code snippets that go in your pom.xml and settings.xml in order to read and publish artifacts to your Nexus server.</p>
<h3>Step 1: Download</h3>
<p>Download Nexus from <a href="http://nexus.sonatype.org/downloads" target="_blank">here</a> (at the time of writing, latest is <a href="http://nexus.sonatype.org/downloads/nexus-webapp-1.6.0.war" target="_blank">1.6.0</a>)</p>
<h3>Step 2: Install</h3>
<p>Copy the war to TOMCAT_HOME/webapps/nexus.war</p>
<p>Though not required, it is a generally good idea to restart tomcat after installing a new war.</p>
<pre name="code" class="xml">/etc/init.d/tomcat restart</pre>
<h3>Step 3: Configure security</h3>
<p><strong>a) </strong><strong>Change default admin password:</strong> The default admin username/password is admin/admin123. Login as admin and change the password to a secure password.</p>
<p>Login -&gt; [admin, admin123] -&gt; Left Menu -&gt; Security -&gt; Change Password -&gt; click &#8220;Change Password&#8221;</p>
<p><strong>b) </strong><strong>Anonymous Access:</strong> By default Nexus is open to the public. If you want to secure access to nexus, disable &#8216;Nexus anonymous user&#8217;</p>
<p>Admin -&gt; Left Menu -&gt; Users -&gt; &#8216;Nexus anonymous user&#8217; -&gt; Status=Disabled</p>
<p><strong>c) Deployment user:</strong> Change password for deployment user</p>
<p>Admin -&gt; Left menu -&gt; Users -&gt; Deployment user -&gt; Change email address</p>
<p>Admin -&gt; Left menu -&gt; Users -&gt; Right click on &#8216;Deployment user&#8217; in the user list -&gt; Set Password -&gt; click &#8216;Set password&#8217; to finish</p>
<h3>Step 4: Set SMTP server</h3>
<p>It is a good idea to configure SMTP server, so that you can receive emails from Nexus.</p>
<p>Admin login -&gt; Left menu -&gt; Administration -&gt; Server -&gt;SMTP Settings -&gt; (host localhost, port 25, no login, no password mostly works on a linux machine)</p>
<h3><strong>Step 5: Change Base Url<br />
</strong></h3>
<p>If you are running Nexus behind Apache using mod_jk or mod_proxy, change your base url here.</p>
<p>Admin login -&gt; Left menu -&gt; Administration -&gt; Server -&gt; Application Server Settings -&gt; Base url</p>
<h3>Step 6: Add a task to periodically remove old snapshots</h3>
<p>If you or your CI server publishes snapshots to Nexus several times a day, then you should consider adding a task to delete duplicate/old snapshots for the same GAV (group, artifact, version). If you don&#8217;t do this, you will notice that the Nexus disk usage will increase with time.</p>
<p>Admin login -&gt; Left menu -&gt; Administration -&gt; Scheduled tasks -&gt; Add&#8230; -&gt; name=&#8221;Remove old snapshots&#8221;, Repository/Group=Snapshots (Repo), Minimum Snapshot Count=1, Snapshot Retention(days)=3, Recurrence=Daily, Recurring time=2:00 -&gt; click &#8216;Save&#8217;</p>
<h3>Step 7: Using Nexus: reading and publishing artifacts</h3>
<p>If you want to deploy your artifacts to your Nexus, you need to configure 2 files: pom.xml and settings.xml</p>
<p><strong>a) pom.xml &#8211; </strong>for each project which wishes to publish to Nexus, add your repo to the pom.xml</p>
<pre name="code" class="xml">&lt;distributionManagement&gt;
 &lt;!-- Publish the versioned releases here --&gt;
 &lt;repository&gt;
  &lt;id&gt;vineetmanohar-nexus&lt;/id&gt;
  &lt;name&gt;vineetmanohar nexus&lt;/name&gt;
  &lt;url&gt;dav:http://nexus.vineetmanohar.com/nexus/content/repositories/releases&lt;/url&gt;
 &lt;/repository&gt;

 &lt;!-- Publish the versioned releases here --&gt;
 &lt;snapshotRepository&gt;
  &lt;id&gt;vineetmanohar-nexus&lt;/id&gt;
  &lt;name&gt;vineetmanohar nexus&lt;/name&gt;
  &lt;url&gt;dav:http://nexus.vineetmanohar.com/nexus/content/repositories/snapshots&lt;/url&gt;
 &lt;/snapshotRepository&gt;
&lt;/distributionManagement&gt;

&lt;!-- download artifacts from this repo --&gt;
&lt;repositories&gt;
 &lt;repository&gt;
  &lt;id&gt;vineetmanohar-nexus&lt;/id&gt;
  &lt;name&gt;vineetmanohar&lt;/name&gt;
  &lt;url&gt;http://nexus.vineetmanohar.com/nexus/content/groups/public&lt;/url&gt;
  &lt;releases&gt;
   &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/releases&gt;

  &lt;snapshots&gt;
   &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/snapshots&gt;
 &lt;/repository&gt;
&lt;/repositories&gt;

&lt;!-- download plugins from this repo --&gt;
&lt;pluginRepositories&gt;
 &lt;pluginRepository&gt;
  &lt;id&gt;vineetmanohar-nexus&lt;/id&gt;
  &lt;name&gt;vineetmanohar&lt;/name&gt;
  &lt;url&gt;http://nexus.vineetmanohar.com/nexus/content/groups/public&lt;/url&gt;
  &lt;releases&gt;
   &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/releases&gt;
  &lt;snapshots&gt;
   &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/snapshots&gt;
 &lt;/pluginRepository&gt;
&lt;/pluginRepositories&gt;</pre>
<p><strong>b) settings.xml</strong> &#8211; If you have disabled anonymous access to Nexus, add the deployment password to your <strong>~/.m2/repository/settings.xml</strong> file</p>
<pre name="code" class="xml">&lt;settings&gt;
 &lt;servers&gt;
  &lt;server&gt;
   &lt;!-- this id should match the id of the repo server in pom.xml --&gt;
   &lt;id&gt;vineetmanohar-nexus&lt;/id&gt;
   &lt;username&gt;deployment&lt;/username&gt;
   &lt;password&gt;password_goes_here&lt;/password&gt;
  &lt;/server&gt;
 &lt;/servers&gt;
&lt;/settings&gt;</pre>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/12/publishing-to-maven-central-repo-in-5-steps/' rel='bookmark' title='Permanent Link: Publishing to Maven Central Repo in 5 steps'>Publishing to Maven Central Repo in 5 steps</a></li><li><a href='http://www.vineetmanohar.com/2009/10/how-to-automate-project-versioning-and-release-with-maven/' rel='bookmark' title='Permanent Link: How to automate project versioning and release with Maven'>How to automate project versioning and release with Maven</a></li><li><a href='http://www.vineetmanohar.com/2009/11/tweet-your-builds-with-maven-twitter-plugin/' rel='bookmark' title='Permanent Link: Tweet your builds with Maven Twitter Plugin'>Tweet your builds with Maven Twitter Plugin</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/K-Z-hk6s-Cs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/06/getting-started-with-nexus-maven-repo-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/06/getting-started-with-nexus-maven-repo-manager/</feedburner:origLink></item>
		<item>
		<title>What is Google TV … in 2 minutes</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/zpYdgAMciO8/</link>
		<comments>http://www.vineetmanohar.com/2010/05/what-is-google-tv-in-2-minutes/#comments</comments>
		<pubDate>Fri, 21 May 2010 21:57:03 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1063</guid>
		<description><![CDATA[Google TV is a Smart TV (or set-top box) which aims to seamlessly combine your Internet and TV experience.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/06/what-is-google-wave-in-plain-simple-english-in-2-minutes/' rel='bookmark' title='Permanent Link: What is Google Wave: in plain simple English in 2 minutes'>What is Google Wave: in plain simple English in 2 minutes</a></li><li><a href='http://www.vineetmanohar.com/2010/02/what-is-google-buzz/' rel='bookmark' title='Permanent Link: What is Google Buzz?'>What is Google Buzz?</a></li><li><a href='http://www.vineetmanohar.com/2009/06/google-introduces-page-speed/' rel='bookmark' title='Permanent Link: Google introduces Page Speed'>Google introduces Page Speed</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/05/what-is-google-tv-in-2-minutes/';
var dzone_title = 'What is Google TV &#8230; in 2 minutes';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p><a href="http://www.google.com/tv/" target="_blank">Google TV</a> is a Smart TV (or set-top box) which aims to seamlessly combine your Internet and TV experience. It runs Android OS, Chrome browser and Flash 10. Third party developers can create interesting applications and make them available to end users via an App marketplace.</p>
<p>Google TV aims to take TV viewing experience to the next level, similar to what smart phones (Android, iPhone) did for phones. The key here, in my opinion, is creating a framework and open up the TV-internet application space to third-party developers.</p>
<h3>Summary</h3>
<ul>
<li>Google TV was <a href="http://www.youtube.com/googledevelopers#p/p/B09682344C2F233B/0/ASZbArr7vdI" target="_blank">announced</a> at <a href="http://code.google.com/events/io/2010/" target="_blank">2010 Google I/O</a>, in stores Fall 2010</li>
<li>Google has partnered with Sony (TVs), Intel (atom processor), Best Buy (sales to end users), Dish Network (Set-top box), Logitech (peripherals) and Adobe (Flash player)</li>
<li>Google TV units will be available (in form of TVs and/or Set-top boxes) this fall, well in time for the Christmas season</li>
<li>Runs Android OS, Chrome Browser, Flash 10</li>
<li>Third party developers can write apps and users can download apps via a Marketplace</li>
<li>End users can surf channels on TV as they normally do, and also access relevant internet related apps like Youtube, Picasa and discover apps written by third party developers</li>
<li>Sign up <a href="https://services.google.com/fb/forms/googletvinfo/" target="_blank">here</a> to receive updates about Google TV</li>
</ul>
<p><object width="425" height="355" type="application/x-shockwave-flash" data="http://www.youtube.com/v/diTpeYoqAhc"><param name="movie" value="http://www.youtube.com/v/diTpeYoqAhc" />This video was embedded using the YouTuber plugin by <a href="http://www.roytanck.com">Roy Tanck</a>. Adobe Flash Player is required to view the video.</object></p>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/06/what-is-google-wave-in-plain-simple-english-in-2-minutes/' rel='bookmark' title='Permanent Link: What is Google Wave: in plain simple English in 2 minutes'>What is Google Wave: in plain simple English in 2 minutes</a></li><li><a href='http://www.vineetmanohar.com/2010/02/what-is-google-buzz/' rel='bookmark' title='Permanent Link: What is Google Buzz?'>What is Google Buzz?</a></li><li><a href='http://www.vineetmanohar.com/2009/06/google-introduces-page-speed/' rel='bookmark' title='Permanent Link: Google introduces Page Speed'>Google introduces Page Speed</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/zpYdgAMciO8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/05/what-is-google-tv-in-2-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/05/what-is-google-tv-in-2-minutes/</feedburner:origLink></item>
		<item>
		<title>2 ways to convert Java Map to String</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/cSBYncutWx0/</link>
		<comments>http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/#comments</comments>
		<pubDate>Sat, 08 May 2010 02:59:26 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[deserialize]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=1003</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/';
var dzone_title = '2 ways to convert Java Map to String';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
This article shows 2 ways to convert Java Map to String. Approach 1: simple, lightweight &#8211; produces query string like output, but restrictive. Approach 2: uses Java XML bean serialization, more robust but produces overly verbose output. Approach 1: Map to query string format Approach 1 converts a map to a query-string like output. Here&#8217;s [...]


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/01/3-ways-to-serialize-java-enums/' rel='bookmark' title='Permanent Link: 3 ways to serialize Java Enums'>3 ways to serialize Java Enums</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/' rel='bookmark' title='Permanent Link: How to copy bean properties with a single line of code'>How to copy bean properties with a single line of code</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/';
var dzone_title = '2 ways to convert Java Map to String';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>This article shows 2 ways to convert Java Map to String. </p>
<ul>
<li>Approach 1: simple, lightweight &#8211; produces query string like output, but restrictive. </li>
<li>Approach 2: uses Java XML bean serialization, more robust but produces overly verbose output.</li>
</ul>
<h3>Approach 1: Map to query string format</h3>
<p>Approach 1 converts a map to a query-string like output. Here&#8217;s what an output looks like:</p>
<pre name="code" class="java">
name1=value1&amp;name2=value2
</pre>
<p>Full code:</p>
<pre name="code" class="java">import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {
 public static String mapToString(Map&lt;String, String&gt; map) {
   StringBuilder stringBuilder = new StringBuilder();

   for (String key : map.keySet()) {
    if (stringBuilder.length() &gt; 0) {
     stringBuilder.append("&amp;");
    }
    String value = map.get(key);
    try {
     stringBuilder.append((key != null ? URLEncoder.encode(key, "UTF-8") : ""));
     stringBuilder.append("=");
     stringBuilder.append(value != null ? URLEncoder.encode(value, "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return stringBuilder.toString();
  }

  public static Map&lt;String, String&gt; stringToMap(String input) {
   Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();

   String[] nameValuePairs = input.split("&amp;");
   for (String nameValuePair : nameValuePairs) {
    String[] nameValue = nameValuePair.split("=");
    try {
     map.put(URLDecoder.decode(nameValue[0], "UTF-8"), nameValue.length &gt; 1 ? URLDecoder.decode(
     nameValue[1], "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return map;
  }
}
</pre>
<h4>Example usage code</h4>
<pre name="code" class="java">
 Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
 map.put("color", "red");
 map.put("symbols", "{,=&amp;*?}");
 map.put("empty", "");
 String output = MapUtil.mapToString(map);
 Map&lt;String, String&gt; parsedMap = MapUtil.stringToMap(output);
 for (String key : map.keySet()) {
  Assert.assertEquals(parsedMap.get(key), map.get(key));
 }
</pre>
<h4>Output with Approach 1</h4>
<pre name="code" class="java">
symbols=%7B%2C%3D%26*%3F%7D&amp;color=red&amp;empty=
</pre>
<h4>Caveat</h4>
<ul>
<li>Only supports String keys and values.</li>
<li>Due to the nature of serialization, null keys and values are not supported. Null will be converted to an empty String. This is because there is no way to distinguish between a null and an empty String in the serialized form. If you need support for null keys and values, use java.beans.XMLEncoder as shown below.</li>
</ul>
<h3>Approach 2: Java Bean XMLEncoder: Map to String</h4>
<p>Java provides XMLEncoder and XMLDecoder classes as part of the java.beans package as a standard way to serialize and deserialize objects. This</p>
<pre name="code" class="java">
 Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
 map.put("color", "red");
 map.put("symbols", "{,=&amp;*?}");
 map.put("empty", "");
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 XMLEncoder xmlEncoder = new XMLEncoder(bos);
 xmlEncoder.writeObject(map);
 xmlEncoder.flush();

 String serializedMap = bos.toString()
 System.output.println(serializedMap);
</pre>
<h4>Output with Approach 2</h4>
<p>The serialized value is shown below. As you can see this is more verbose, but can accommodate different data types and null keys and values.</p>
<pre name="code" class="java">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;java version="1.5.0_17"&gt;
 &lt;object&gt;
  &lt;void method="put"&gt;
   &lt;string&gt;symbols&lt;/string&gt;
   &lt;string&gt;{,=&amp;amp;*?}&lt;/string&gt;
  &lt;/void&gt;
  &lt;void method="put"&gt;
   &lt;string&gt;color&lt;/string&gt;
   &lt;string&gt;red&lt;/string&gt;
  &lt;/void&gt;
  &lt;void method="put"&gt;
   &lt;string&gt;empty&lt;/string&gt;
   &lt;string&gt;&lt;/string&gt;
  &lt;/void&gt;
 &lt;/object&gt;
</pre>
<h3>Java Bean XMLDecoder: String to Map</h3>
<pre name="code" class="java"> XMLDecoder xmlDecoder = new XMLDecoder(new ByteArrayInputStream(serializedMap.getBytes()));
 Map&lt;String, String&gt; parsedMap = (Map&lt;String, String&gt;) xmlDecoder.readObject();

 for (String key : map.keySet()) {
  Assert.assertEquals(parsedMap.get(key), map.get(key));
 }
</pre>
<h3>Summary</h3>
<p>While Java provides a standard (and overly verbose) way to serialize and deserialize objects, this articles discusses an alternative lightweight way to convert a Java Map to String and back. If you are serializing a map with non-null String keys and values, then you should be able to use this alternative way, otherwise use the Java bean serialization.</p>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/01/3-ways-to-serialize-java-enums/' rel='bookmark' title='Permanent Link: 3 ways to serialize Java Enums'>3 ways to serialize Java Enums</a></li><li><a href='http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/' rel='bookmark' title='Permanent Link: 3 ways to run Java main from Maven'>3 ways to run Java main from Maven</a></li><li><a href='http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/' rel='bookmark' title='Permanent Link: How to copy bean properties with a single line of code'>How to copy bean properties with a single line of code</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/cSBYncutWx0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/05/2-ways-to-convert-java-map-to-string/</feedburner:origLink></item>
		<item>
		<title>How to configure multiple page.xml files in Seam 2.2</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/8DB3JvPQEqY/</link>
		<comments>http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 00:04:43 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[seam]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=979</guid>
		<description><![CDATA[Seam lets you specify navigation rules in the pages.xml file which can become very large for apps with many pages and transitions. Seam allows you to break this file into multiple small files. This allows you to easily manage large applications by creating multiple pages.xml files, typically one per page. This article describes how to create multiple pages.xml files in Seam 2.2.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/03/a-common-configuration-mistake-which-makes-seam-slow/' rel='bookmark' title='Permanent Link: A common configuration mistake which makes Seam slow'>A common configuration mistake which makes Seam slow</a></li><li><a href='http://www.vineetmanohar.com/2009/04/seam-asynchronous-email/' rel='bookmark' title='Permanent Link: Seam Asynchronous Email'>Seam Asynchronous Email</a></li><li><a href='http://www.vineetmanohar.com/2009/03/implementing-context-senstive-permissions-and-authorization-in-jsf-seam/' rel='bookmark' title='Permanent Link: Implementing context senstive permissions and authorization in JSF Seam'>Implementing context senstive permissions and authorization in JSF Seam</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/';
var dzone_title = 'How to configure multiple page.xml files in Seam 2.2';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<h3>Overview</h3>
<p>Seam lets you specify navigation rules in the <strong>pages.xml</strong> file which can become very large for apps with many pages and transitions. Seam allows you to break this file into multiple small files. This allows you to easily manage large applications by creating multiple pages.xml files, typically one per page. This article describes how to create multiple pages.xml files in Seam 2.2.</p>
<h3>Step 1: Configure /WEB-INF/components.xml</h3>
<p><strong>Make sure you declare the </strong><strong>navigation namespace at the top of components.xml and each file under &lt;navigation:pages&gt; as follows</strong></p>
<pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;components xmlns="http://jboss.com/products/seam/components"
 xmlns:core="http://jboss.com/products/seam/core" xmlns:persistence="http://jboss.com/products/seam/persistence"
 xmlns:drools="http://jboss.com/products/seam/drools" xmlns:bpm="http://jboss.com/products/seam/bpm"
 xmlns:security="http://jboss.com/products/seam/security" xmlns:mail="http://jboss.com/products/seam/mail"
 xmlns:web="http://jboss.com/products/seam/web" xmlns:navigation="http://jboss.com/products/seam/navigation"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
 http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
 http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
 http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
 http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
 http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
 http://jboss.com/products/seam/navigation http://jboss.com/products/seam/navigation-2.2.xsd
 http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd"&gt;

 &lt;mail:mail-session host="localhost" tls="false" ssl="false" port="25" /&gt;

 &lt;component&gt;
  &lt;property name="createTempFiles"&gt;true&lt;/property&gt;
  &lt;property name="maxRequestSize"&gt;1000000000&lt;/property&gt;
 &lt;/component&gt;

 &lt;component name="org.jboss.seam.core.init"&gt;
  &lt;!-- enabling this slows down Seam significantly --&gt;
  &lt;property name="debug"&gt;false&lt;/property&gt;
 &lt;/component&gt;

 &lt;core:init transaction-management-enabled="false" /&gt;

 &lt;security:identity authenticate-method="#{loginController.authenticate}"
  remember-me="true" /&gt;

 &lt;event type="org.jboss.seam.security.notLoggedIn"&gt;
  &lt;action execute="#{redirect.captureCurrentView}" /&gt;
 &lt;/event&gt;

 &lt;event type="org.jboss.seam.security.postAuthenticate"&gt;
  &lt;action execute="#{redirect.returnToCapturedView}" /&gt;
 &lt;/event&gt;

 &lt;navigation:pages&gt;
  &lt;navigation:resources&gt;
   &lt;!-- global --&gt;
   &lt;value&gt;/WEB-INF/pages.xml&lt;/value&gt;

   &lt;!-- one per page --&gt;
   &lt;value&gt;/WEB-INF/createAccount.page.xml&lt;/value&gt;
   &lt;value&gt;/WEB-INF/login.page.xml&lt;/value&gt;
  &lt;/navigation:resources&gt;
 &lt;/navigation:pages&gt;
&lt;/components&gt;</pre>
<h3>Step 2. Save all .page.xml files in WEB-INF directory</h3>
<pre name="code" class="xml">WEB-INF/createAccount.page.xml
WEB-INF/login.page.xml</pre>
<h3>Step 3. Create the .page.xml files.</h3>
<p>See the sample file: <strong>createAccount.page.xml</strong> below. Note that the root tag is <strong>&lt;pages&gt;</strong>. Make sure that the same page is not repeated in another file.</p>
<pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;pages xmlns="http://jboss.com/products/seam/pages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.2.xsd"&gt;

 &lt;page view-id="/createAccount.xhtml" login-required="false"&gt;
 &lt;!-- Create account --&gt;
 &lt;navigation
  from-action="#{createAccountController.createAccountFormCreateAccount}"&gt;

   &lt;!-- Account created --&gt;
   &lt;rule if-outcome="accountCreated"&gt;
    &lt;redirect view-id="/login.xhtml"&gt;
     &lt;message&gt;Your account has been created. Please log in.&lt;/message&gt;
    &lt;/redirect&gt;
   &lt;/rule&gt;
  &lt;/navigation&gt;
 &lt;/page&gt;
&lt;/pages&gt;</pre>
<h3>FAQ</h3>
<h4>Can I use Seam 2.0 style &lt;view id&gt;.page.xml files?</h4>
<p>You should be able to use <strong>viewid.page.xml</strong> xml files according to the examples that ship with Seam 2.2.0. However, some users have <a href="http://seamframework.org/Community/Seam210SP1PagexmlNavigationIsNotWorking" target="_blank">complained</a> that it doesn&#8217;t work in Seam 2.1. I could not get it to work with Seam 2.2.0 as well.</p>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2009/03/a-common-configuration-mistake-which-makes-seam-slow/' rel='bookmark' title='Permanent Link: A common configuration mistake which makes Seam slow'>A common configuration mistake which makes Seam slow</a></li><li><a href='http://www.vineetmanohar.com/2009/04/seam-asynchronous-email/' rel='bookmark' title='Permanent Link: Seam Asynchronous Email'>Seam Asynchronous Email</a></li><li><a href='http://www.vineetmanohar.com/2009/03/implementing-context-senstive-permissions-and-authorization-in-jsf-seam/' rel='bookmark' title='Permanent Link: Implementing context senstive permissions and authorization in JSF Seam'>Implementing context senstive permissions and authorization in JSF Seam</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/8DB3JvPQEqY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/</feedburner:origLink></item>
		<item>
		<title>Spring classpath scan breaks when migrating from JBoss4 to JBoss5</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/S5rlVX1lKI4/</link>
		<comments>http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 00:57:28 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=967</guid>
		<description><![CDATA[Spring 2.5 classpath scan does not work on JBoss5 due to internal architecture changes in JBoss5. You may face this issue when migrating from JBoss4 to JBoss5. This article walks you through a simple fix, with code snippets.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/' rel='bookmark' title='Permanent Link: EJB3 JPA error when migrating from JBoss version 4 to 5'>EJB3 JPA error when migrating from JBoss version 4 to 5</a></li><li><a href='http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/' rel='bookmark' title='Permanent Link: How to configure multiple page.xml files in Seam 2.2'>How to configure multiple page.xml files in Seam 2.2</a></li><li><a href='http://www.vineetmanohar.com/2009/04/seam-asynchronous-email/' rel='bookmark' title='Permanent Link: Seam Asynchronous Email'>Seam Asynchronous Email</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/';
var dzone_title = 'Spring classpath scan breaks when migrating from JBoss4 to JBoss5';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>Spring 2.5 classpath scan does not work on JBoss5 due to internal architecture changes in JBoss5. You may face this issue when migrating from JBoss4 to JBoss5. This article walks you through a simple fix, with code snippets.</p>
<h3>Classpath scan issue</h3>
<p>In JBoss 4, your application was probably using Spring 2.5.6 with classpath scan to load spring beans (as shown below). When migrating your code to JBoss5, you will notice that component-scan does not work with JBoss5. Symptoms may include null Spring components or if you were using Spring Seam integration and the Spring beans were loaded through classpath scan then you may be the following error:</p>
<p><strong>Seam Code</strong></p>
<pre name="code" class="java">public class LoginController extends Controller {
    @In("#{userService}")
    protected demo.entity.service.UserService userService;</pre>
<p><strong>Stacktrace</strong></p>
<pre name="code" class="java">Caused by: org.jboss.seam.RequiredException: @In attribute requires non-null value: loginController.#{userService}
 at org.jboss.seam.Component.getValueToInject(Component.java:2335)
 at org.jboss.seam.Component.injectAttributes(Component.java:1736)
 at org.jboss.seam.Component.inject(Component.java:1554)
 at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
 at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)</pre>
<p><strong>Spring 2.5.6 dependency in pom.xml</strong></p>
<pre name="code" class="xml"> &lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring&lt;/artifactId&gt;
 &lt;version&gt;2.5.6&lt;/version&gt;
 &lt;/dependency</pre>
<p><strong>Old spring config XML</strong></p>
<pre name="code" class="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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

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

 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
 default-init-method="init" default-destroy-method="shutdown"&gt;
 &lt;context:annotation-config /&gt;
 &lt;context:component-scan base-package="demo.dao.jpa" /&gt;
&lt;/beans&gt;</pre>
<p>If you run the above code in JBoss5, component-scan does not work. See this <a href="http://jira.springframework.org/browse/SPR-5120?focusedCommentId=41972&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_41972">JIRA issue on Spring&#8217;s website</a>.</p>
<h3>Solution</h3>
<p>You need to migrate your spring version from 2.5.6 to 3.0.0.RELEASE. Remove your old spring dependency for 2.5.6 and add the following. Add snippets for the components that your application needs. Here&#8217;s a list of common spring components and their maven dependencies.</p>
<p><strong>New dependency</strong></p>
<pre name="code" class="xml"> &lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
 &lt;version&gt;3.0.0.RELEASE&lt;/version&gt;
 &lt;/dependency&gt;

 &lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
 &lt;version&gt;3.0.0.RELEASE&lt;/version&gt;
 &lt;/dependency&gt;

 &lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
 &lt;version&gt;3.0.0.RELEASE&lt;/version&gt;
 &lt;/dependency&gt;</pre>
<pre name="code" class="xml"> &lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
 &lt;version&gt;3.0.0.RELEASE&lt;/version&gt;
 &lt;scope&gt;test&lt;/scope&gt;
 &lt;/dependency&gt;</pre>
<p>Complete list is available <a href="http://blog.springsource.com/2009/12/02/obtaining-spring-3-artifacts-with-maven/" target="_blank">here</a>. After making this change, build your project with the new dependencies. Your old code should work with Spring 3 on JBoss 5.1.0.GA.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">public abstract class GeneratedLoginController extends Controller {<br />
@In(&#8220;#{userDao}&#8221;)<br />
protected demo.dao.UserDao userDao;</p>
<p>@In(&#8220;#{userService}&#8221;)<br />
protected demo.entity.service.UserService userService;</p>
</div>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/' rel='bookmark' title='Permanent Link: EJB3 JPA error when migrating from JBoss version 4 to 5'>EJB3 JPA error when migrating from JBoss version 4 to 5</a></li><li><a href='http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/' rel='bookmark' title='Permanent Link: How to configure multiple page.xml files in Seam 2.2'>How to configure multiple page.xml files in Seam 2.2</a></li><li><a href='http://www.vineetmanohar.com/2009/04/seam-asynchronous-email/' rel='bookmark' title='Permanent Link: Seam Asynchronous Email'>Seam Asynchronous Email</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/S5rlVX1lKI4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/</feedburner:origLink></item>
		<item>
		<title>EJB3 JPA error when migrating from JBoss version 4 to 5</title>
		<link>http://feedproxy.google.com/~r/vineetmanohar/~3/ISgqSBw7P20/</link>
		<comments>http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 00:03:54 +0000</pubDate>
		<dc:creator>vineet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ejb3]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[jta]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.vineetmanohar.com/?p=955</guid>
		<description><![CDATA[When I tried migrating my JBoss 4.2 application which uses Spring to manage transactions, to JBoss 5.1, I got the following error message. 

Specification violation [EJB3 JPA 6.2.1.2] - You have not defined a jta-data-source for a JTA enabled persistence context

This is a known issue and there are two known workarounds for this problem. See article for details.


Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/' rel='bookmark' title='Permanent Link: Spring classpath scan breaks when migrating from JBoss4 to JBoss5'>Spring classpath scan breaks when migrating from JBoss4 to JBoss5</a></li><li><a href='http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/' rel='bookmark' title='Permanent Link: How to configure multiple page.xml files in Seam 2.2'>How to configure multiple page.xml files in Seam 2.2</a></li><li><a href='http://www.vineetmanohar.com/2009/05/maven-cargo-jboss/' rel='bookmark' title='Permanent Link: Maven Cargo JBoss'>Maven Cargo JBoss</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
var dzone_url = 'http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/';
var dzone_title = 'EJB3 JPA error when migrating from JBoss version 4 to 5';
var dzone_blurb = '';
var dzone_style = '1';
//-->
</script>
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> 
</div>
<p>When I tried migrating my JBoss 4.2 [war] application which uses Spring to manage transactions, to JBoss 5.1, I got the following error message.</p>
<h3>The error message</h3>
<pre name="code" class="xml">11:18:55,683 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=#demo state=Create
java.lang.RuntimeException: Specification violation [EJB3 JPA 6.2.1.2] - You have not defined a jta-data-source for a JTA enabled persistence context named: demo
	at org.jboss.jpa.deployment.PersistenceUnitInfoImpl.&lt;init&gt;(PersistenceUnitInfoImpl.java:115)
	at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:275)</pre>
<h3>Here&#8217;s the persistence.xml file</h3>
<pre name="code" class="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 version="1.0"&gt;
 &lt;persistence-unit name="demo"&gt;
 &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
 &lt;class&gt;demo.jpa.User&lt;/class&gt;
 &lt;class&gt;demo.jpa.Issue&lt;/class&gt;
 &lt;class&gt;demo.jpa.UploadedFile&lt;/class&gt;
 &lt;properties&gt;
   &lt;property name="hibernate.hbm2ddl.auto" value="create" /&gt;
   &lt;property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /&gt;
   &lt;property name="hibernate.show_sql" value="true" /&gt;
   &lt;property name="hibernate.format_sql" value="true" /&gt;
   &lt;property name="hibernate.use_sql_comments" value="true" /&gt;
 &lt;/properties&gt;
 &lt;/persistence-unit&gt;
&lt;/persistence&gt;</pre>
<h2>Solution/workaround</h2>
<p>JBoss loads file named &#8220;persistence.xml&#8221; through this file:</p>
<pre name="code" class="xml">jboss-5.1.0.GA/server/default/deployers/ejb3.deployer/META-INF/jpa-deployers-jboss-beans.xml</pre>
<p>&#8230; via this code snippet (line 21-95)</p>
<pre name="code" class="xml">   &lt;bean name="PersistenceParsingDeployer" class="org.jboss.jpa.deployers.PersistenceParsingDeployer"/&gt;
   &lt;bean name="PersistenceDeployer" class="org.jboss.jpa.deployers.PersistenceDeployer"/&gt;

   &lt;bean name="PersistenceUnitDeployer" class="org.jboss.jpa.deployers.PersistenceUnitDeployer"&gt;
    ...
   &lt;/bean&gt;</pre>
<p><strong>Solution 1:</strong></p>
<p>Comment out the above lines. This solution is a server configuration modification and therefore must be done on every server (dev/qa/production) that you want to run your app on. You may not be able to use this approach if you don&#8217;t have control over your [production] server configuration. See Solution 2 for an application oriented solution.</p>
<p><strong>Solution 2:</strong></p>
<ol>
<li>Rename the <strong><em>persistence.xml</em></strong> file to something else, for example, <em><strong>spring-persistence.xml</strong></em></li>
<li>Change the config for the spring <em><strong>entityManagerFactory</strong></em> bean to explicitly refer to this new file. See below</li>
</ol>
<p style="padding-left: 60px;"><strong>Spring configuration &#8211; before:</strong></p>
<pre name="code" class="xml" style="padding-left: 60px;">&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
 &lt;property name="dataSource" ref="dataSource" /&gt;
 &lt;property name="jpaVendorAdapter"&gt;
   &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
 &lt;/property&gt;
&lt;/bean&gt;</pre>
<p style="padding-left: 60px;"><strong>Spring configuration &#8211; after:</strong></p>
<pre name="code" class="xml" style="padding-left: 60px;">&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
 &lt;property name="dataSource" ref="dataSource" /&gt;
 &lt;property name="jpaVendorAdapter"&gt;
   &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
 &lt;/property&gt;
 &lt;!-- ADD THE FOLLOWING PROPERTY --&gt;
 &lt;!-- provide the location of new persistence.xml --&gt;
 &lt;property name="persistenceXmlLocation" value="classpath:META-INF/spring-persistence.xml" /&gt;
&lt;/bean&gt;</pre>
<h3>Reference</h3>
<p>See more details of the corresponding <a title="JIRA issue at jboss.org" href="https://jira.jboss.org/jira/browse/EJBTHREE-1730?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&amp;focusedCommentId=12453872#action_12453872" target="_blank">JIRA issue at jboss.org</a></p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 198px; width: 1px; height: 1px;">&lt;bean name=&#8221;PersistenceParsingDeployer&#8221; class=&#8221;org.jboss.jpa.deployers.PersistenceParsingDeployer&#8221;/&gt;</p>
<p>&lt;bean name=&#8221;PersistenceDeployer&#8221; class=&#8221;org.jboss.jpa.deployers.PersistenceDeployer&#8221;/&gt;<br />
&lt;bean name=&#8221;PersistenceUnitDeployer&#8221; class=&#8221;org.jboss.jpa.deployers.PersistenceUnitDeployer&#8221;&gt;</p>
</div>


<p>Related posts:<ol><li><a href='http://www.vineetmanohar.com/2010/04/spring-classpath-scan-issue-jboss5/' rel='bookmark' title='Permanent Link: Spring classpath scan breaks when migrating from JBoss4 to JBoss5'>Spring classpath scan breaks when migrating from JBoss4 to JBoss5</a></li><li><a href='http://www.vineetmanohar.com/2010/04/how-to-configure-multiple-page-xml-files-in-seam-2-2/' rel='bookmark' title='Permanent Link: How to configure multiple page.xml files in Seam 2.2'>How to configure multiple page.xml files in Seam 2.2</a></li><li><a href='http://www.vineetmanohar.com/2009/05/maven-cargo-jboss/' rel='bookmark' title='Permanent Link: Maven Cargo JBoss'>Maven Cargo JBoss</a></li></ol></p><img src="http://feeds.feedburner.com/~r/vineetmanohar/~4/ISgqSBw7P20" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vineetmanohar.com/2010/04/ejb3-jpa-error-when-migrating-from-jboss-version-4-to-5/</feedburner:origLink></item>
	</channel>
</rss>
