<?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>Software Passion &#187; Code Snippets</title>
	<atom:link href="http://www.softwarepassion.com/category/code_snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.softwarepassion.com</link>
	<description>by Krzysztof Grajek</description>
	<lastBuildDate>Fri, 23 Dec 2011 07:48:47 +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>Apache Camel &#8211; JDBC with Spring Transaction support</title>
		<link>http://www.softwarepassion.com/apache-camel-jdbc-with-spring-transaction-support/</link>
		<comments>http://www.softwarepassion.com/apache-camel-jdbc-with-spring-transaction-support/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 13:17:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[ESB]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=726</guid>
		<description><![CDATA[Continuing my journey with Fuse ESB (ServiceMix) and Apache Camel I will present a small example on how to route your messages in a transacted way using Spring Transaction Manager. For the purpose of this tutorial we are going to define a datasource to in memory Hsqldb instance and configure it with &#8216;org.springframework.jdbc.datasource.DataSourceTransactionManager&#8217;. Our camel [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Continuing my journey with Fuse ESB (ServiceMix) and Apache Camel I will present a small example on how to route your messages in a transacted way using Spring Transaction Manager.<br />
For the purpose of this tutorial we are going to define a datasource to in memory Hsqldb instance and configure it with &#8216;org.springframework.jdbc.datasource.DataSourceTransactionManager&#8217;. Our camel example will be based on the tutorial/document from the FuseSource website itself which could be found at : <a href="http://fusesource.com/docs/router/2.5/transactions/index.html">http://fusesource.com/docs/router/2.5/transactions/index.html</a>.<br />
Our application will do the following:</p>
<ol>
<li>Create database table in the in-memory hsqldb database and populate it with same data &#8211; we are going to use JdbcTemplate for this purpose</li>
<li>The route itself will pick up a messages from the specified directory, one by one, one of the messages will contain data causing an exception and rolling back our transaction and database commit.</li>
<li>After each message is processed sucessfully the dump of our database will be printed to the console</li>
<li>If the exception occurs and the transaction will be rolled back, the appropriate log would be displayed stating the current database content</li>
</ol>
<p>The full source code for this example, packaged as a maven based project can be downloaded from the <a href="https://github.com/softberries/camel_jdbc_transactions">Github</a> website.</p>
<p>1. We are going to use 2 spring beans for our example, one with injected datasource in a constructor which will populate our database data:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">tutorial.jdbc</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.sql.DataSource</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.jdbc.core.JdbcTemplate</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CreateTable <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>CreateTable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> DataSource dataSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> JdbcTemplate jdbc<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> DataSource getDataSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> dataSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDataSource<span style="color: #009900;">&#40;</span>DataSource dataSource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dataSource</span> <span style="color: #339933;">=</span> dataSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> CreateTable<span style="color: #009900;">&#40;</span>DataSource ds<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CreateTable constructor called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setDataSource<span style="color: #009900;">&#40;</span>ds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setUpTable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUpTable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;About to set up table...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JdbcTemplate<span style="color: #009900;">&#40;</span>dataSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;create table accounts (name varchar(50), amount int)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;insert into accounts (name,amount) values (?,?)&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Major Clanger&quot;</span>, <span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;insert into accounts (name,amount) values (?,?)&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Tiny Clanger&quot;</span>, <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Table created&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>another bean will be responsible for our route logic, performing database operations. Datasource for this bean is injected using a property (see the camel-context.xml file below for more details):</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">tutorial.jdbc</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.sql.DataSource</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.camel.Exchange</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.camel.language.XPath</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.jdbc.core.JdbcTemplate</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AccountService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>AccountService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> JdbcTemplate jdbc<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> AccountService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDataSource<span style="color: #009900;">&#40;</span>DataSource ds<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JdbcTemplate<span style="color: #009900;">&#40;</span>ds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Adds a specific amount of money to a named account<br />
&nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp;* @param name - account name<br />
&nbsp; &nbsp; &nbsp;* @param amount - amount to add<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> credit<span style="color: #009900;">&#40;</span>@XPath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/transaction/transfer/receiver/text()&quot;</span><span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XPath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/transaction/transfer/amount/text()&quot;</span><span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> amount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;credit() called with args name = &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and amount = &quot;</span> <span style="color: #339933;">+</span> amount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> origAmount <span style="color: #339933;">=</span> jdbc.<span style="color: #006633;">queryForInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select amount from accounts where name = ?&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> name <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newAmount <span style="color: #339933;">=</span> origAmount <span style="color: #339933;">+</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>amount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;update accounts set amount = ? where name = ?&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> newAmount, name <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Subtract a specific amount of money from a named account.<br />
&nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp;* @param name - account name<br />
&nbsp; &nbsp; &nbsp;* @param amount - amount to add<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> debit<span style="color: #009900;">&#40;</span>@XPath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/transaction/transfer/sender/text()&quot;</span><span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name, @XPath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/transaction/transfer/amount/text()&quot;</span><span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> amount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;debit() called with args name = &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and amount = &quot;</span> <span style="color: #339933;">+</span> amount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> iamount <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>amount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>iamount <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Debit limit is 100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> origAmount <span style="color: #339933;">=</span> jdbc.<span style="color: #006633;">queryForInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select amount from accounts where name = ?&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> name <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newAmount <span style="color: #339933;">=</span> origAmount <span style="color: #339933;">-</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>amount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newAmount <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Not enough in account&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; jdbc.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;update accounts set amount = ? where name = ?&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> newAmount, name <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dumpTable<span style="color: #009900;">&#40;</span>Exchange ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dump() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;?&gt;</span> dump <span style="color: #339933;">=</span> jdbc.<span style="color: #006633;">queryForList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from accounts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ex.<span style="color: #006633;">getIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setBody</span><span style="color: #009900;">&#40;</span>dump.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Our datasource, transaction manager and injection points for our beans are defined in camel-context.xml file located in &#8216;resources/META-INF/spring&#8217; directory:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- Configures the Camel Context--&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;camelContext</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://camel.apache.org/schema/spring&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tutorial<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/camelContext<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <br />
&nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- spring transaction manager --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;txManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- datasource to the database --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.jdbc.datasource.SimpleDriverDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;driverClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hsqldb.jdbcDriver&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jdbc:hsqldb:mem:camel&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;sa&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- &nbsp;Bean to initialize table in the DB --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;createTable&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;tutorial.jdbc.CreateTable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Bean for account service --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;accountService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;tutorial.jdbc.AccountService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>2 and 3. When our configuration and the spring beans are ready we can start coding our route:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//our route definition</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//noop option - if true, the file is not moved or deleted in any way</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file:src/data?noop=true&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//mark the route as transacted</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">transacted</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//execute spring bean methods</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;credit&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;debit&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;dumpTable&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//log the result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log:ExampleRouter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>4. For simple error handling mechanism I have decided to use &#8216;onException&#8217; which will be executed every time an exception will be thrown in our route and transaction will be rolled back</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//handle exceptions and log them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//handle exceptions and log them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onException<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006633;">maximumRedeliveries</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">handled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>, <span style="color: #0000ff;">&quot;dumpTable&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file:target/messages?fileName=deadLetters.xml&amp;fileExist=Append&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">markRollbackOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Remember to use SpringRouteBuilder for our transacted route instead of simple RouteBuilder class, below is the full source of our route configuration using Java DSL:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">tutorial</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.camel.spring.SpringRouteBuilder</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A Camel Router<br />
&nbsp;* <br />
&nbsp;* @version $<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyRouteBuilder <span style="color: #000000; font-weight: bold;">extends</span> SpringRouteBuilder <span style="color: #009900;">&#123;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//handle exceptions and log them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onException<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006633;">maximumRedeliveries</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">handled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>, <span style="color: #0000ff;">&quot;dumpTable&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file:target/messages?fileName=deadLetters.xml&amp;fileExist=Append&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">markRollbackOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//our route definition</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//noop option - if true, the file is not moved or deleted in any way</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file:src/data?noop=true&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//mark the route as transacted</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">transacted</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//execute spring bean methods</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;credit&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;debit&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">beanRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountService&quot;</span>,<span style="color: #0000ff;">&quot;dumpTable&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//log the result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log:ExampleRouter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>To run an example, navigate into the project folder and execute the following command:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;mvn camel:run</div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/apache-camel-jdbc-with-spring-transaction-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse RCP &#8211; Auto-complete Text and Combo widgets</title>
		<link>http://www.softwarepassion.com/eclipse-rcp-auto-complete-text-and-combo-widgets/</link>
		<comments>http://www.softwarepassion.com/eclipse-rcp-auto-complete-text-and-combo-widgets/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 12:34:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Eclipse RCP]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=638</guid>
		<description><![CDATA[Today I will describe a simple way to add auto-completion to you Eclipse RCP/SWT based application using some resources I&#8217;ve found browsing the web. To not reinvent the wheel, I have found a project on sourceforge which does just that, and which does the job well enough. First of all go to the sourceforge website [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will describe a simple way to add auto-completion to you Eclipse RCP/SWT based application using some resources I&#8217;ve found browsing the web. To not reinvent the wheel, I have found a project on sourceforge which does just that, and which does the job well enough.</p>
<p>First of all go to the sourceforge website and download the sources, you don&#8217;t have to download a full eclipse plugin project, just download the packages we need from <a href="http://swtaddons.cvs.sourceforge.net/viewvc/swtaddons/net.sf.swtaddons/src/">here</a> by clicking &#8216;Download GNU Tarball&#8217;.<br />
Copy and paste the whole downloaded folder into your &#8216;src&#8217; directory visible in eclipse &#8216;package explorer&#8217;.</p>
<p>You can use this classes in the following way:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//eg for text fields</span><br />
<span style="color: #000000; font-weight: bold;">new</span> AutocompleteTextInput<span style="color: #009900;">&#40;</span>fieldTxt, options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Where &#8216;fieldTxt&#8217; is your textfield object (already created) and &#8216;options&#8217; is a String array (String[]) with possible options to display.</p>
<p>Thanks to <a href="http://sourceforge.net/users/derekhunter4">derekhunter4</a> for this project. You can find more info on sourceforge: <a href="http://sourceforge.net/projects/swtaddons/">swtaddons</a></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/eclipse-rcp-auto-complete-text-and-combo-widgets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse RCP &#8211; Simple Logging mechanism</title>
		<link>http://www.softwarepassion.com/eclipse-rcp-simple-logging-mechanism/</link>
		<comments>http://www.softwarepassion.com/eclipse-rcp-simple-logging-mechanism/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 13:54:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Eclipse RCP]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=634</guid>
		<description><![CDATA[This is a short snippet to solve you logging needs in Eclipse RCP application by using build in logging functionality. I have created 3 simple static utility methods but you can create your LogUtil utility class as you wish based on this example: import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; public class LogUtil &#123; &#160; &#160; public static [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>This is a short snippet to solve you logging needs in Eclipse RCP application by using build in logging functionality.<br />
I have created 3 simple static utility methods but you can create your LogUtil utility class as you wish based on this example:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.IStatus</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.eclipse.core.runtime.Status</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LogUtil <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> logError<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IStatus st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Status<span style="color: #009900;">&#40;</span>IStatus.<span style="color: #006633;">ERROR</span>,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">PLUGIN_ID</span>, msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>st<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> logInfo<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IStatus st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Status<span style="color: #009900;">&#40;</span>IStatus.<span style="color: #006633;">INFO</span>,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">PLUGIN_ID</span>, msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>st<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> logWarning<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IStatus st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Status<span style="color: #009900;">&#40;</span>IStatus.<span style="color: #006633;">WARNING</span>,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">PLUGIN_ID</span>, msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aactivator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Activator</span></a>.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>st<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Hope that helps someone who was forced to use System.out.println to debug simple RCP plugin applications.</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/eclipse-rcp-simple-logging-mechanism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java EE 6 &#8211; JSF 2.0 Internationalization with session scoped language switcher bean</title>
		<link>http://www.softwarepassion.com/java-ee-6-jsf-2-0-internationalization/</link>
		<comments>http://www.softwarepassion.com/java-ee-6-jsf-2-0-internationalization/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 19:31:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=620</guid>
		<description><![CDATA[Today I will present a short tutorial on how to implement internationalization feature in your JSF 2.0 web application. This tutorial is based on the previous one called Java EE 6 – Getting Started (maven, CDI and persistence) ready project. We will continue from that point and add simple i18n feature with language switching capabilities. [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will present a short tutorial on how to implement internationalization feature in your JSF 2.0 web application. This tutorial is based on the previous one called <a href="http://www.softwarepassion.com/java-ee-6-getting-started-maven-cdi-and-persistence-ready-project/">Java EE 6 – Getting Started (maven, CDI and persistence) ready project</a>. </p>
<p>We will continue from that point and add simple i18n feature with language switching capabilities.<br />
First of all you need to define available languages for your application in faces-config.xml found in (src/main/webapp/WEB-INF/faces-config.xml</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- This file is not required if you don't need any extra configuration. --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;faces-config</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;http://java.sun.com/xml/ns/javaee</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">&lt;!-- Write your navigation rules here. You are encouraged to use CDI for creating @Named managed beans. --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;locale-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default-locale<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>en<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default-locale<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;supported-locale<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/supported-locale<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/locale-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-bundle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;base-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.softwarepassion.j2ee6tutorial.messages<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/base-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;var<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>msg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/var<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-bundle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faces-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>As you can see, I have defined support for two languages here, &#8216;pl&#8217; &#8211; Polish and English which is the default one.<br />
In order to use your language specific messages you need to define &#8216;.properties&#8217; file for each single language.<br />
Place your files in /src/main/resources/name/of/your/package/*.properties&#8217; We will start with the english version first (messages.properties):</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_english1.png" rel="lightbox[620]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_english1.png" alt="" title="sp_english" width="800" height="500" class="aligncenter size-full wp-image-625" /></a></p>
<p>I have added just one single property for the string key called &#8216;Title&#8217;</p>
<p>Do the same for the second properties file called &#8216;messages_pl_PL.properties&#8217; and place the file in the same location. As you have probably noticed already, the file for each language must follow simple naming convention like messages_+your language code+.properties</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_polish1.png" rel="lightbox[620]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_polish1.png" alt="" title="sp_polish" width="800" height="500" class="aligncenter size-full wp-image-626" /></a></p>
<p>Now its time to actually implement i18n handling logic. To save selected language for each user we will use the most natural option which is a session scoped JSF managed bean.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Locale</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ManagedBean</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.SessionScoped</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span><br />
<br />
<br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Used for managing i18n in application for each user<br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;*<br />
&nbsp;*/</span><br />
@ManagedBean<br />
@SessionScoped<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LanguageSwitcher <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Serializable</span></a> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 2756934361134603857L<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOG <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>LanguageSwitcher.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Locale</span></a> locale <span style="color: #339933;">=</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getViewRoot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLocale</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Locale</span></a> getLocale<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> locale<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> locale.<span style="color: #006633;">getLanguage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Sets the current {@code Locale} for each user session<br />
&nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp;* @param languageCode - ISO-639 language code<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> changeLanguage<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> language<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; locale <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Locale</span></a><span style="color: #009900;">&#40;</span>language<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getViewRoot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setLocale</span><span style="color: #009900;">&#40;</span>locale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Once we have properties files defined, created our session scoped managed JSF bean, its time to wire it up in your xhtml documents.<br />
I will add all language switching code inside my xhtml template which is used throughout the project as the template for all xhtml documents.<br />
Locate your template file called &#8216;default.xhtml&#8217; (src/main/webapp/WEB-INF/templates/default.xhtml) and add change it to the following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span><br />
<span style="color: #00bbdd;"> &nbsp; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;#{languageSwitcher.language}&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:f</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/core&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:view</span> <span style="color: #000066;">locale</span>=<span style="color: #ff0000;">&quot;#{languageSwitcher.locale}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java EE 6 Starter Application<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputStylesheet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;css/screen.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sidebar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Language switcher:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;langauge_form&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandLink</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{languageSwitcher.changeLanguage('pl')}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Polski&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> | <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandLink</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{languageSwitcher.changeLanguage('en')}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;English&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:insert</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[Template content will be inserted here]<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ui:insert<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:graphicImage</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/resources/gfx/weld.png&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Weld logo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This project was generated from a Maven archetype maintained by the Weld team.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Weld is the reference implementation of CDI, released under the Apache License, Version 2.0.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:view<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Final webpage with language switcher:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_web_page_final.png" rel="lightbox[620]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_web_page_final.png" alt="" title="sp_web_page_final" width="800" height="500" class="aligncenter size-full wp-image-627" /></a></p>
<p>You can download the project files from here: <a href='http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_J2EE6Tutorial.zip'>sp_J2EE6Tutorial</a></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/java-ee-6-jsf-2-0-internationalization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Java EE 6 &#8211; Getting Started (maven, CDI and persistence) ready project</title>
		<link>http://www.softwarepassion.com/java-ee-6-getting-started-maven-cdi-and-persistence-ready-project/</link>
		<comments>http://www.softwarepassion.com/java-ee-6-getting-started-maven-cdi-and-persistence-ready-project/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 09:19:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=609</guid>
		<description><![CDATA[This short tutorial describes how to get started easily and quickly with Java EE 6 development. For the purpose of this tutorial you would need to have installed the following tools: 1. Maven 3 2. JBoss AS 6.0 3. Eclipse (I will use Helios release) 4. Eclipse m2eclipse plugin (for managing Maven based projects) If [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>This short tutorial describes how to get started easily and quickly with Java EE 6 development. For the purpose of this tutorial you would need to have installed the following tools:</p>
<p>1. Maven 3<br />
2. JBoss AS 6.0<br />
3. Eclipse (I will use Helios release)<br />
4. Eclipse m2eclipse plugin (for managing Maven based projects)</p>
<p>If you have all this tools ready, you can quickly create an maven J2EE archetype provided by JBoss.</p>
<p>1. Open up the Eclipse IDE and select &#8216;File -> New -> Project&#8217;<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_maven_project.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_maven_project.png" alt="" title="sp_maven_project" width="523" height="476" class="aligncenter size-full wp-image-610" /></a></p>
<p>2. One the next screen just click &#8216;Next&#8217;<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_maven_project2.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_maven_project2.png" alt="" title="sp_maven_project2" width="630" height="508" class="aligncenter size-full wp-image-611" /></a></p>
<p>3. One the archetype selection screen, type &#8216;weld&#8217; in a filter box and select weld.jsf.jee archetype 1.0.0-BETA1<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_weld_project.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_weld_project.png" alt="" title="sp_weld_project" width="711" height="505" class="aligncenter size-full wp-image-612" /></a></p>
<p>4. Fill up the details for your project<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_weld_details.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_weld_details.png" alt="" title="sp_weld_details" width="711" height="505" class="aligncenter size-full wp-image-614" /></a></p>
<p>5. Click &#8216;Finish&#8217;</p>
<p>6. Right click on the newly created project and select &#8216;Run As.. -> Run on Server&#8217; and select Jboss 6.0 server to run your project. If you haven&#8217;t configured your jboss server with eclipse you can do it here.<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_jboss_Server.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_jboss_Server.png" alt="" title="sp_jboss_Server" width="523" height="617" class="aligncenter size-full wp-image-615" /></a></p>
<p>7. Go to the page at: http://localhost:8080/J2EE6Tutorial where J2EE6Tutorial  is the name of my project and 8080 is the port I had installed JBoss application server.<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_browser1.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_browser1.png" alt="" title="sp_browser1" width="1060" height="475" class="aligncenter size-full wp-image-616" /></a></p>
<p>What you will see is probably the error saying:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">ServletException</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span>home.<span style="color: #006633;">xhtml</span><span style="color: #339933;">:</span> The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">'java.lang.String'</span> does not have the property <span style="color: #0000ff;">'id'</span>.<br />
&nbsp; &nbsp; <span style="color: #006633;">javax</span>.<span style="color: #006633;">faces</span>.<span style="color: #006633;">webapp</span>.<span style="color: #006633;">FacesServlet</span>.<span style="color: #006633;">service</span><span style="color: #009900;">&#40;</span>FacesServlet.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">321</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; org.<span style="color: #006633;">jboss</span>.<span style="color: #006633;">weld</span>.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">ConversationPropagationFilter</span>.<span style="color: #006633;">doFilter</span><span style="color: #009900;">&#40;</span>ConversationPropagationFilter.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">67</span><span style="color: #009900;">&#41;</span></div></div>
<p>This is due to the bug in the j2ee weld artifact. </p>
<p>8. To solve the mentioned bug, open up home.xhtml file located in &#8216;scr/main/webapp/&#8217; folder and change the following line:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #009900;">&lt;h:dataTable var<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_widget&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#widgets&quot;</span>&gt;</span></div></div>
<p>to:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #009900;">&lt;h:dataTable var<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_widget&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#{widgets}&quot;</span>&gt;</span></div></div>
<p>9. Redeploy the application on the server. Go to the &#8216;Servers&#8217; tab, select our application, right click on it, and select &#8216;Full Publish&#8217;.<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_server_select.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_server_select.png" alt="" title="sp_server_select" width="620" height="290" class="aligncenter size-full wp-image-617" /></a></p>
<p>10. Now you can refresh the application in the browser.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_browser_ready.png" rel="lightbox[609]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/sp_browser_ready.png" alt="" title="sp_browser_ready" width="1066" height="743" class="aligncenter size-full wp-image-618" /></a></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/java-ee-6-getting-started-maven-cdi-and-persistence-ready-project/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Getting started with Drools Flow</title>
		<link>http://www.softwarepassion.com/getting-started-with-drools-flow/</link>
		<comments>http://www.softwarepassion.com/getting-started-with-drools-flow/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 12:07:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Jboss Drools]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=548</guid>
		<description><![CDATA[Today I will present my latest discovery called JBoss Drools and Drools Flow in particular. I am completely new to the drools framework and because I couldn&#8217;t find myself a quick and simple intro to the framework I will post it myself. Presented example is completely useless from the business perspective and it doesn&#8217;t describe [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will present my latest discovery called JBoss Drools and Drools Flow in particular. I am completely new to the drools framework and because I couldn&#8217;t find myself a quick and simple intro to the framework I will post it myself. Presented example is completely useless from the business perspective and it doesn&#8217;t describe any real world scenario.</p>
<p>According to the Drools Flow website itself:</p>
<blockquote><p>Drools Flow provides workflow or (business) process capabilities to the Drools platform. A business process or workflow describes the order in which a series of steps need to be executed, using a flow chart. This makes it much easier to describe a complex composition of various tasks. Processes are especially useful in describing state-based, long-running processes. Drools Flow allows end users to specify, execute and monitor (a part of) their business logic using these processes. The Drools Flow process framework is easily embeddable into any Java application (as a simple Java component) or can run standalone in a server environment.</p></blockquote>
<p>First of all you need to prepare your development environment. You will need the following:</p>
<ul>
<li>Eclipse IDE</li>
<li><a href="http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-bin.zip">Drools Binaries v.5.1</a></li>
<li><a href="http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-eclipse-all.zip">Drools Eclipse 3.5 Workbench</a></li>
</ul>
<p>For the purpose of this tutorial that&#8217;s enough, you can try to download full Drools stack using the installer (including JBoss AS, Eclipse IDE, Birt reporting, Guvnor etc) but I didn&#8217;t have any luck with this installer and I guess you will be better off once you do install everything by hand as you learn.</p>
<p>Unpack drools binaries somewhere on your machine and Drools Eclipse workbench into eclipse folder. Restart your IDE and you should be able to create new Drools projects by now.</p>
<p>Select File -> New Project from the Eclipse file menu:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_1.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_1-261x300.png" alt="" title="drools_1" width="261" height="300" class="aligncenter size-medium wp-image-551" /></a></p>
<p>Provide descriptive name for your project:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_2.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_2-287x300.png" alt="" title="drools_2" width="287" height="300" class="aligncenter size-medium wp-image-552" /></a></p>
<p>Un-click all the proposed examples which eclipse plugin wants to generate for you, you can do that later if you want to learn more on drools.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_3.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_3-300x291.png" alt="" title="drools_3" width="300" height="291" class="aligncenter size-medium wp-image-553" /></a></p>
<p>Select Drools runtime, if you have no runtime configured, click &#8216;Configure Workspace settings&#8217; and point to the directory where you have downloaded your binaries.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_4.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_4-300x291.png" alt="" title="drools_4" width="300" height="291" class="aligncenter size-medium wp-image-554" /></a></p>
<p>After you click finish, you should have a basic Drools project structure created for you:<br />
<span id="more-548"></span><br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_5.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_5-300x97.png" alt="" title="drools_5" width="300" height="97" class="aligncenter size-medium wp-image-555" /></a></p>
<p>Let&#8217;s create our application entry point with the main method we can execute to test our flow.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_6.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_6-256x300.png" alt="" title="drools_6" width="256" height="300" class="aligncenter size-medium wp-image-556" /></a></p>
<p>You should end up with a simple java class and single main method:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SPDroolsFlowExample <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now lets create our Flow file for the Drools Flow engine, select New -> Other on the project node and under the Drools folder select &#8216;Flow File&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_7.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_7-300x287.png" alt="" title="drools_7" width="300" height="287" class="aligncenter size-medium wp-image-557" /></a></p>
<p>Select the name for your flow file:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_8.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_8-270x300.png" alt="" title="drools_8" width="270" height="300" class="aligncenter size-medium wp-image-558" /></a></p>
<p>The last step when creating your flow file is to select the runtime compatibility with the drools engine:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_9.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_9-286x300.png" alt="" title="drools_9" width="286" height="300" class="aligncenter size-medium wp-image-559" /></a></p>
<p>Once your flow file is created make sure its under src/main/rules while in the package explorer window, this will ensure that we can find it easily on our class-path while executing our test application.<br />
Make sure you are in the &#8216;Drools Perspective&#8217;, open the rules file (*.rf) and drop the following onto the designer window:<br />
Assuming you have a start event already placed in your flow (green cirle)</p>
<ol>
<li>1 Gateway [diverge]</li>
<li>3 Logs (found under &#8216;service tasks&#8217;)</li>
<li>1 Gateway [converge]</li>
<li>1 End event</li>
</ol>
<p>Once you do that, place them on the screen similary to this screenshot below and connect all together using &#8216;Sequence Flow&#8217; arrow found at the top of the flow toolbox.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_11.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_11-300x159.png" alt="" title="drools_11" width="300" height="159" class="aligncenter size-medium wp-image-560" /></a></p>
<p>When you have your diagram ready, click on the whitespace anywhere on the diagram designer and under the properties tab provide the ID attribute, for this example we use &#8216;com.softwarepassion.droolsflowexample&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_12_new.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_12_new-300x87.png" alt="" title="drools_12_new" width="300" height="87" class="aligncenter size-medium wp-image-572" /></a></p>
<p>Now on the designer select the first Gateway (diverge) and under properties tab set its type to &#8216;OR&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_13.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_13-300x152.png" alt="" title="drools_13" width="300" height="152" class="aligncenter size-medium wp-image-562" /></a></p>
<p>For the second Gateway provide the type &#8216;XOR&#8217;, this ensures that we have to get the flow only from one of the nodes to proceed, selecting &#8216;AND&#8217; for the second gateway would require two branches to be executed but we want to execute just one, either top or bottom log.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_14.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_14-300x235.png" alt="" title="drools_14" width="300" height="235" class="aligncenter size-medium wp-image-563" /></a></p>
<p>Once you select OR for the first gateway you can set constraints describing what exactly has to happen to go to the top branch or the bottom branch.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_15.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_15-300x103.png" alt="" title="drools_15" width="300" height="103" class="aligncenter size-medium wp-image-564" /></a></p>
<p>Click &#8216;&#8230;&#8217; on the Constraints row (inside properties tab):</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_16.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_16-300x158.png" alt="" title="drools_16" width="300" height="158" class="aligncenter size-medium wp-image-565" /></a></p>
<p>Select &#8216;Edit&#8217; button for the top branch (&#8216;To node Log Top&#8217;), change its type to &#8216;java&#8217;, dialect to &#8216;java&#8217; and type in:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></div>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_17.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_17-300x223.png" alt="" title="drools_17" width="300" height="223" class="aligncenter size-medium wp-image-566" /></a></p>
<p>Similarly, for the other branch set the condition to return false:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_18.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_18-300x227.png" alt="" title="drools_18" width="300" height="227" class="aligncenter size-medium wp-image-567" /></a></p>
<p>Hardcoding &#8216;true&#8217; and &#8216;false&#8217; values is just for now, we will learn later on, how to insert some object into the flow and read its state inside a flow task.</p>
<p>Just for debugging purposes provide descriptive messages to our Log events, you can do that either from the Properties tab or by double clicking the node on the diagram itself.<br />
Because we are using Log events all of them have the name &#8216;Log&#8217;.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_19.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_19-300x201.png" alt="" title="drools_19" width="300" height="201" class="aligncenter size-medium wp-image-568" /></a></p>
<p>Now lets do some wiring. Create a simple handler for our work items, &#8216;executeWorkItem&#8217; method will be called every time the &#8216;Log&#8217; task is executed within our workflow.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItem</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItemHandler</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItemManager</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleWorkItemHandler <span style="color: #000000; font-weight: bold;">implements</span> WorkItemHandler<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> executeWorkItem<span style="color: #009900;">&#40;</span>WorkItem workItem, WorkItemManager manager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Executing work item &quot;</span> <span style="color: #339933;">+</span> workItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; manager.<span style="color: #006633;">completeWorkItem</span><span style="color: #009900;">&#40;</span>workItem.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> abortWorkItem<span style="color: #009900;">&#40;</span>WorkItem workItem, WorkItemManager manager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Do nothing here</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Wiring it all together, we end up with the following code in our main class:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.KnowledgeBase</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.KnowledgeBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.KnowledgeBuilderFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.ResourceType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.io.ResourceFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.StatefulKnowledgeSession</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SPDroolsFlowExample <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp;* @throws Exception <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase knowledgeBase <span style="color: #339933;">=</span> readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StatefulKnowledgeSession ksession <span style="color: #339933;">=</span> knowledgeBase.<span style="color: #006633;">newStatefulKnowledgeSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleWorkItemHandler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleWorkItemHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">getWorkItemManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerWorkItemHandler</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log&quot;</span>, handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">startProcess</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">fireAllRules</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> KnowledgeBase readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBuilder kbuilder <span style="color: #339933;">=</span> KnowledgeBuilderFactory.<span style="color: #006633;">newKnowledgeBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; kbuilder.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ResourceFactory.<span style="color: #006633;">newClassPathResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;droolsflowexample.rf&quot;</span><span style="color: #009900;">&#41;</span>, ResourceType.<span style="color: #006633;">DRF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> kbuilder.<span style="color: #006633;">newKnowledgeBase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Click on the class and select &#8216;Run  as..&#8217; -> &#8216;Java Application&#8217;, this will should print something similar to the following messages:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_20.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_20-300x53.png" alt="" title="drools_20" width="300" height="53" class="aligncenter size-medium wp-image-569" /></a></p>
<p>Source for the flow file at this stage of the tutorial is listed below:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span> <br />
<span style="color: #339933;">&lt;</span>process xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://drools.org/drools-5.0/process&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns<span style="color: #339933;">:</span>xs<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xs<span style="color: #339933;">:</span>schemaLocation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://drools.org/drools-5.0/process drools-processes-5.0.xsd&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;RuleFlow&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;flow&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span> package<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.softwarepassion&quot;</span> <span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>header<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>header<span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>nodes<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>start id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Start&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;72&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;206&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>end id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;End&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;802&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;196&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>split id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Gateway&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;203&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;205&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraints<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraint toNodeId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> toType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DROOLS_DEFAULT&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;constraint&quot;</span> priority<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;code&quot;</span> dialect<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> <span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;&lt;/</span>constraint<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraint toNodeId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> toType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DROOLS_DEFAULT&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;constraint&quot;</span> priority<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;code&quot;</span> dialect<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> <span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;&lt;/</span>constraint<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>constraints<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>split<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>join id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Gateway&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;537&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;197&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log Top&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;352&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;157&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log top...<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log Bottom&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;354&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;251&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log bottom..<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log End&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;657&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;196&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log end<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>nodes<span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>connections<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>connections<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #339933;">&lt;/</span>process<span style="color: #339933;">&gt;</span></div></div>
<p>If all is fine and working we can continue to the next part of our tutorial showing you how to add data to our workflow which can be read/modified by the workflow tasks.<br />
For this example we will create a very simple POJO which we will pass from one task to the other and based on the properties contained within that POJO we will direct our flow to one of the two branches created earlier in the tutorial.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleTO <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> msg<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMsg<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As you can see this is really simple java class <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Now let&#8217;s create an instance of it and add it to the workflow parameters, inside our class with the main method add the following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase knowledgeBase <span style="color: #339933;">=</span> readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StatefulKnowledgeSession ksession <span style="color: #339933;">=</span> knowledgeBase.<span style="color: #006633;">newStatefulKnowledgeSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, Object<span style="color: #339933;">&gt;</span> parameterMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleTO st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleTO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; st.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BOTTOM&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;st&quot;</span>, st<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleWorkItemHandler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleWorkItemHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">getWorkItemManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerWorkItemHandler</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log&quot;</span>, handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">startProcess</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span>, parameterMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">fireAllRules</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>Setting the &#8216;msg&#8217; field to &#8216;TOP&#8217; will cause to execute our top branch and &#8216;BOTTOM&#8217; our bottom branch respectively.</p>
<p>For all this to work we need to add a variable to our workflow and change our constraints in the first gateway.</p>
<p>Click somwhere on the workflow to get the properties, select the &#8216;variables&#8217; row and add a single variable called &#8216;st&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_22.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_22-300x180.png" alt="" title="drools_22" width="300" height="180" class="aligncenter size-medium wp-image-577" /></a></p>
<p>Note that we have added our object using the key with the same name as the variable we are defining above. Once our variable is ready to be used in the workflow we can reference it in the Constraints dialogs of our diverging gateway:</p>
<p>Top constraint:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_23.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_23-300x225.png" alt="" title="drools_23" width="300" height="225" class="aligncenter size-medium wp-image-578" /></a></p>
<p>Bottom constraint:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_24.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_24-300x225.png" alt="" title="drools_24" width="300" height="225" class="aligncenter size-medium wp-image-579" /></a></p>
<p>Now you can test the application, changing the property of our injected object will cause going into one of the branches. </p>
<p>I hope it helps anyone trying to get started with the drools flow.</p>
<p>Enjoy!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/getting-started-with-drools-flow/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Android Series: Display device CPU and Memory info</title>
		<link>http://www.softwarepassion.com/android-series-display-device-cpu-and-memory-info/</link>
		<comments>http://www.softwarepassion.com/android-series-display-device-cpu-and-memory-info/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 07:10:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Mobile Development]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=533</guid>
		<description><![CDATA[Today I will post a short snippet on how to display cpu and memory info of our andorid device. The program uses external utilities to get the device info by calling cat on &#8216;/proc/meminfo&#8217; and &#8216;/proc/cpuinfo&#8217;. The activity displaying the data from the &#8216;cat&#8217; output is displayed in a TextView contained withing &#8216;ScrollView&#8217;. This is [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will post a short snippet on how to display cpu and memory info of our andorid device. The program uses external utilities to get the device info by calling cat on &#8216;/proc/meminfo&#8217; and &#8216;/proc/cpuinfo&#8217;.<br />
The activity displaying the data from the &#8216;cat&#8217; output is displayed in a TextView contained withing &#8216;ScrollView&#8217;.</p>
<p>This is the output of the cat command running in emulator on my machine:</p>
<p style="text-align: center;"><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/android_mem_info.png" rel="lightbox[533]"><img class="size-full wp-image-534  aligncenter" title="android_mem_info" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/android_mem_info.png" alt="" width="320" height="480" /></a></p>
<p>Layout file:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&gt;</span><br />
<span style="color: #339933;">&lt;</span>ScrollView xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/ScrollView01&quot;</span><br />
&nbsp; &nbsp; &nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; &nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>LinearLayout <br />
&nbsp;android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;vertical&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><br />
&nbsp;<span style="color: #339933;">&gt;</span><br />
&nbsp;<span style="color: #339933;">&lt;</span>TextView<br />
&nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>textColor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#FF0000&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CPU Info:&quot;</span><br />
&nbsp;<span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;</span>TextView<br />
&nbsp;android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/cpuinfo&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><br />
&nbsp;<span style="color: #339933;">/&gt;</span><br />
&nbsp;<span style="color: #339933;">&lt;</span>TextView<br />
&nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>textColor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#0000FF&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Memory Info:&quot;</span><br />
&nbsp;<span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;</span>TextView<br />
&nbsp;android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/memoryinfo&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp;android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><br />
&nbsp;<span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;/</span>LinearLayout<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>ScrollView<span style="color: #339933;">&gt;</span></div></div>
<p>Main activity:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CpuSpeed <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> TextView cpuInfo<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> TextView memoryInfo<span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
cpuInfo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cpuinfo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
cpuInfo.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>getCPUinfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
memoryInfo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">memoryinfo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
memoryInfo.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>getMemoryInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMemoryInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
ProcessBuilder cmd<span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;/system/bin/cat&quot;</span>, <span style="color: #0000ff;">&quot;/proc/meminfo&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
cmd <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcessBuilder<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aprocess+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Process</span></a> process <span style="color: #339933;">=</span> cmd.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> process.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> re <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
result <span style="color: #339933;">=</span> result <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
in.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getCPUinfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
ProcessBuilder cmd<span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> result<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;/system/bin/cat&quot;</span>, <span style="color: #0000ff;">&quot;/proc/cpuinfo&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
cmd <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcessBuilder<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aprocess+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Process</span></a> process <span style="color: #339933;">=</span> cmd.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> process.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> re <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
result <span style="color: #339933;">=</span> result <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
in.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You don&#8217;t need to modify anything inside AndroidManifest.xml file.</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/android-series-display-device-cpu-and-memory-info/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android Series: Download files with Progress Dialog</title>
		<link>http://www.softwarepassion.com/android-series-download-files-with-progress-dialog/</link>
		<comments>http://www.softwarepassion.com/android-series-download-files-with-progress-dialog/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 13:08:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Mobile Development]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=525</guid>
		<description><![CDATA[Today, I will present a short tutorial on how to download files in android displaying at the same time download progress based on the bytes downloaded. For the purpose of this tutorial we will use build in AsyncTask mechanism together with ProgressDialog class. The whole application consists of one activity displaying a &#8216;Start Download&#8217; button. [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today, I will present a short tutorial on how to download files in android displaying at the same time download progress based on the bytes downloaded. For the purpose of this tutorial we will use build in AsyncTask mechanism together with ProgressDialog class.<br />
The whole application consists of one activity displaying a &#8216;Start Download&#8217; button. Clicking our start button we initialize file download passing to the asynctask the url or the resource we want to download.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/andro_async1.png" rel="lightbox[525]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/andro_async1.png" alt="" title="andro_async1" width="640" height="480" class="aligncenter size-full wp-image-528" /></a><br />
AndroAsync.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedInputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLConnection</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Dialog</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ProgressDialog</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.AsyncTask</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AndroAsync <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> DIALOG_DOWNLOAD_PROGRESS <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> startBtn<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ProgressDialog mProgressDialog<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; startBtn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">startBtn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; startBtn.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startDownload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> startDownload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> DownloadFileAsync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adialog+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Dialog</span></a> onCreateDialog<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> DIALOG_DOWNLOAD_PROGRESS<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgressDialog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProgressDialog<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgressDialog.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Downloading file..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgressDialog.<span style="color: #006633;">setProgressStyle</span><span style="color: #009900;">&#40;</span>ProgressDialog.<span style="color: #006633;">STYLE_HORIZONTAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgressDialog.<span style="color: #006633;">setCancelable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgressDialog.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> mProgressDialog<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">class</span> DownloadFileAsync <span style="color: #000000; font-weight: bold;">extends</span> AsyncTask<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, String<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPreExecute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPreExecute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showDialog<span style="color: #009900;">&#40;</span>DIALOG_DOWNLOAD_PROGRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> doInBackground<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>... <span style="color: #006633;">aurl</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> count<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#40;</span>aurl<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLConnection</span></a> conexion <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conexion.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> lenghtOfFile <span style="color: #339933;">=</span> conexion.<span style="color: #006633;">getContentLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ANDRO_ASYNC&quot;</span>, <span style="color: #0000ff;">&quot;Lenght of file: &quot;</span> <span style="color: #339933;">+</span> lenghtOfFile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedInputStream</span></a><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">openStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">OutputStream</span></a> output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afileoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileOutputStream</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/sdcard/some_photo_from_gdansk_poland.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span> data<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> total <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">=</span> input.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total <span style="color: #339933;">+=</span> count<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; publishProgress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>total<span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>lenghtOfFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>data, <span style="color: #cc66cc;">0</span>, count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onProgressUpdate<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>... <span style="color: #006633;">progress</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ANDRO_ASYNC&quot;</span>,progress<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mProgressDialog.<span style="color: #006633;">setProgress</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>progress<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPostExecute<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> unused<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dismissDialog<span style="color: #009900;">&#40;</span>DIALOG_DOWNLOAD_PROGRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>layout file (main.xml) :</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> &nbsp;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/hello&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Start long running task..&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/startBtn&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>While downloadin a file from the internet remember to add INTERNET permission in AndroidManifest.xml</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.INTERNET&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>Check your sd card folder for what was downloaded and enjoy!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/android-series-download-files-with-progress-dialog/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Design Patterns Series &#8211; Part Five: Adapter pattern</title>
		<link>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/</link>
		<comments>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 09:42:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=511</guid>
		<description><![CDATA[According to wikipedia: In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter pattern is commonly used when there is a need to access an object through the interface which doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>According to <a href="http://en.wikipedia.org/wiki/Adapter_pattern">wikipedia</a>:</p>
<blockquote><p>In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface.</p></blockquote>
<p>The adapter pattern is commonly used when there is a need to access an object through the interface which doesn&#8217;t match the one you need. This pattern is especially useful if you have a set of existing classes and you need to use them together with other ones, exposing different interface.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/adapterpattern2.png" rel="lightbox[511]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/adapterpattern2.png" alt="" title="adapterpattern" width="917" height="320" class="aligncenter size-full wp-image-521" /></a><br />
The purpose of this pattern is  to make some objects look like something they are not. Normally, you wouldn&#8217;t use this pattern at the design stage but rather while extending an existing application, working with 3rd party libraries or tools.</p>
<p>Simple example using adapter pattern:<br />
We have an existing class called ThirdPartyShape with a method &#8216;paintShape&#8217; we want to move into a common interface.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThirdPartyShape <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paintShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Painting 3rd party shape&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Common interface in our new application:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ShinyShapeIfc <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Example of standard shape class in our application:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyNewShinyShape <span style="color: #000000; font-weight: bold;">implements</span> ShinyShapeIfc<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Painting brand new and shiny custom shape!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Adapter class used to utilize functionality of 3rd party class into our interface for painting shapes</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShinyShape3rdpartyAdapter <span style="color: #000000; font-weight: bold;">implements</span> ShinyShapeIfc<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ThirdPartyShape thirdPartyShape<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ShinyShape3rdpartyAdapter<span style="color: #009900;">&#40;</span>ThirdPartyShape tps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thirdPartyShape</span> <span style="color: #339933;">=</span> tps<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Method adapting 3rd party functionality into a common<br />
&nbsp; &nbsp; &nbsp;* interface<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thirdPartyShape</span>.<span style="color: #006633;">paintShape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Simple proof of concept, using our shapes with existing 3rd party shapes by implementing and adapter pattern:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args the command line arguments<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ThirdPartyShape thirdPartyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ThirdPartyShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ShinyShapeIfc myOwnShinyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyNewShinyShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//adapting 3rd party class into a common interface using an adapter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ShinyShapeIfc adapted3rdPartyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShinyShape3rdpartyAdapter<span style="color: #009900;">&#40;</span>thirdPartyShape<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//object below implement common interface </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myOwnShinyShape.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; adapted3rdPartyShape.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android Series: Creating custom dialogs</title>
		<link>http://www.softwarepassion.com/android-series-creating-custom-dialogs/</link>
		<comments>http://www.softwarepassion.com/android-series-creating-custom-dialogs/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 11:48:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Mobile Development]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=499</guid>
		<description><![CDATA[Short tutorial on how to implement custom dialogs on the Android platform]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Every android developer will face one day a need to create a custom dialog, where the user has to input some data for processing or any other purposes. This post is a short tutorial on how to create a custom dialog in android, dimming the background activity and asking user to input a string for a search query.<br />
Custom dialog is implemented as another activity and treated as dialog by using special option in AndroidManifest.xml (listed at the end of this post).<br />
The whole application consists of two activities (main + custom dialog), two small layout xml files and a modified AndroidManifest.xml.</p>
<p style="text-align: center;"><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/device_dialog.png" rel="lightbox[499]"><img class="size-medium wp-image-505  aligncenter" title="device_dialog" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/device_dialog-200x300.png" alt="" width="200" height="300" /></a><br />
AndroDialog (main activity)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AndroDialog <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> startDialogBtn<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MY_CUSTOM_DIALOG <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
startDialogBtn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">startdialog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
startDialogBtn.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
startCustomDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Starts an Activity which in this case is a custom<br />
* dialog, as specified in AndroidManifest.xml<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> startCustomDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,SearchDialog.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
startActivityForResult<span style="color: #009900;">&#40;</span>intent, MY_CUSTOM_DIALOG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onActivityResult<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> requestCode, <span style="color: #000066; font-weight: bold;">int</span> resultCode, Intent data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onActivityResult</span><span style="color: #009900;">&#40;</span>requestCode, resultCode, data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>requestCode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #009900;">&#40;</span>MY_CUSTOM_DIALOG<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resultCode <span style="color: #339933;">==</span> Activity.<span style="color: #006633;">RESULT_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ANDRO_DIALOG&quot;</span>,<span style="color: #0000ff;">&quot;Coming back from the search dialog..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> searchQuery <span style="color: #339933;">=</span> data.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span>SearchDialog.<span style="color: #006633;">SEARCH_QUERY_RESULT_FROM_DIALOG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ANDRO_DIALOG&quot;</span>, <span style="color: #0000ff;">&quot;Search query result: &quot;</span> <span style="color: #339933;">+</span> searchQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>main.xml</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&gt;</span><br />
<span style="color: #339933;">&lt;</span>LinearLayout xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;vertical&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>TextView &nbsp;<br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span> <br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span> <br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/hello&quot;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Show Custom Dialog&quot;</span> <br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/startdialog&quot;</span> <br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span> <br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>Button<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>LinearLayout<span style="color: #339933;">&gt;</span></div></div>
<p>SearchDialog.java (custom dialog activity)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.WindowManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchDialog <span style="color: #000000; font-weight: bold;">extends</span> Activity<span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> SEARCH_QUERY_RESULT_FROM_DIALOG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEARCH_DIALOG&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> searchBtn<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> cancelBtn<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> EditText searchEdit<span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">custom_dialog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
searchBtn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
searchBtn.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
returnSearchQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
cancelBtn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cancel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
cancelBtn.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
cancelDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
searchEdit <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">search_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
searchEdit.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
getWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setFlags</span><span style="color: #009900;">&#40;</span>WindowManager.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FLAG_BLUR_BEHIND</span>,<br />
WindowManager.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FLAG_BLUR_BEHIND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> returnSearchQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
Intent resultIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, SearchDialog.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
resultIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>SEARCH_QUERY_RESULT_FROM_DIALOG, searchEdit.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setResult<span style="color: #009900;">&#40;</span>Activity.<span style="color: #006633;">RESULT_OK</span>, resultIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
finish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> cancelDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
finish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>custom dialog xml layout</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>LinearLayout xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/layout_root&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;vertical&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>padding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10dp&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>EditText android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/search_query&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>LinearLayout android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;horizontal&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>background<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@android:drawable/bottom_bar&quot;</span> android<span style="color: #339933;">:</span>paddingLeft<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;4.0dip&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>paddingTop<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;5.0dip&quot;</span> android<span style="color: #339933;">:</span>paddingRight<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;4.0dip&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>paddingBottom<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0dip&quot;</span> android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><br />
&nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/search&quot;</span> android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0.0dip&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span> android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Search&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_weight<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/cancel&quot;</span> android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0.0dip&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span> android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Cancel&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>layout_weight<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> <span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;/</span>LinearLayout<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>LinearLayout<span style="color: #339933;">&gt;</span></div></div>
<p>AndroidManifest.xml</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&gt;</span><br />
<span style="color: #339933;">&lt;</span>manifest xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">package</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.softwarepassion&quot;</span><br />
&nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>versionCode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><br />
&nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>versionName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>application android<span style="color: #339933;">:</span>icon<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@drawable/icon&quot;</span> android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/app_name&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>activity android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;.AndroDialog&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/app_name&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>action android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.intent.action.MAIN&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>category android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.intent.category.LAUNCHER&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>activity<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>activity android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SearchDialog&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Search Dialog&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android<span style="color: #339933;">:</span>theme<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@android:style/Theme.Dialog&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>application<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>manifest<span style="color: #339933;">&gt;</span></div></div>
<p>Hope this helps anyone needing to write a custom dialog for the android platform.</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/android-series-creating-custom-dialogs/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

