<?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>Technology Bits and Bytes &#187; Java</title>
	<atom:link href="http://blogs.circlesource.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.circlesource.com</link>
	<description>CircleSource Technical Talent ShowCase</description>
	<lastBuildDate>Thu, 10 Dec 2009 20:01:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Splitting DICOM file in to HEADER and DATA</title>
		<link>http://blogs.circlesource.com/2009/04/16/splitting-dicom-file-in-to-header-and-data/</link>
		<comments>http://blogs.circlesource.com/2009/04/16/splitting-dicom-file-in-to-header-and-data/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 06:39:23 +0000</pubDate>
		<dc:creator>Anil Kumar</dc:creator>
				<category><![CDATA[DICOM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[DICOM and JAVA]]></category>
		<category><![CDATA[DICOM Blog]]></category>
		<category><![CDATA[DICOM FILE]]></category>
		<category><![CDATA[DICOM IMAGE]]></category>
		<category><![CDATA[DICOM IN TO HEADER AND DATA/IMAGE]]></category>
		<category><![CDATA[DICOM SPLITTING]]></category>
		<category><![CDATA[DICOM with JAVA]]></category>
		<category><![CDATA[DICOM with PACS]]></category>
		<category><![CDATA[extract data/image from DICOM file]]></category>
		<category><![CDATA[extract demographics from DICOM file]]></category>
		<category><![CDATA[PACS]]></category>
		<category><![CDATA[separate dicom file using dcm4chee api]]></category>
		<category><![CDATA[split dicom file using dcm4chee api]]></category>
		<category><![CDATA[SPLITTING DICOM IMAGE/FILE]]></category>
		<category><![CDATA[using DCM4CHEE api in java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=394</guid>
		<description><![CDATA[What is DICOM?
 Digital Imaging and Communications in Medicine (DICOM) is a standard for storing, handling, printing, and transmitting information in medical imaging. DICOM internally defines network communications protocol that uses TCP/IP to communicate between systems. DICOM files can be exchanged between two systems that are capable of receiving image and patient data in DICOM [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="text-align: justify;">What is DICOM?</p>
<p class="MsoNormal" style="text-align: justify;"><strong> D</strong>igital <strong>I</strong>maging and <strong>Co</strong>mmunications in <strong>M</strong>edicine (<span>DICOM</span>) is a standard for storing, handling, printing, and transmitting information in medical imaging. DICOM internally defines network communications protocol that uses TCP/IP to communicate between systems. DICOM files can be exchanged between two systems that are capable of receiving image and patient data in DICOM format. Proposed by National Electrical Manufacturers Association (NEMA) (<a href="http://www.nema.org/">www.nema.org</a>)</p>
<p class="MsoNormal" style="text-align: justify;"><span>A DICOM file contains a header and the image data. The header stores information about the patient&#8217;s name, the type of scan, position and dimension of image and lots of other data. The image data part contains all the image information.</span></p>
<p class="MsoNormal" style="text-align: justify;"><!--[if gte vml 1]&gt;--></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table style="height: 10px;" border="0" cellspacing="0" cellpadding="0" width="900">
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<p><!--[if !vml]--></p>
<table style="height: 13px;" border="0" cellspacing="0" cellpadding="0" width="20" align="left">
<tbody>
<tr>
<td width="66" height="14"></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p><!--[endif]--><strong> </strong></p>
<div id="attachment_396" class="wp-caption alignnone" style="width: 456px"><a rel="attachment wp-att-396" href="http://blogs.circlesource.com/2009/04/16/splitting-dicom-file-in-to-header-and-data/attachment/1/"><img class="size-full wp-image-396" src="http://blogs.circlesource.com/wp-content/uploads/2009/04/1.jpg" alt="DICOM IMAGE HIERARCHY" width="446" height="111" /></a><p class="wp-caption-text">DICOM IMAGE HIERARCHY</p></div>
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span style="text-decoration: underline;">SPLITTING DICOM Image</span></strong><span>:</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> We can split the DICOM image in to header(Demographics) and data(Image) parts using java with the help of dcm4che API.</span></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span style="text-decoration: underline;">HEADER PART</span></strong><span>:</span></p>
<p class="MsoListParagraphCxSpFirst" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span> Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>First we have to open the DICOM image for reading using IOStream.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span> Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>Create a DICOM Object from the stream we opened for reading.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span> Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>Remove the Pixel Data from the DICOM Object. Now this DICOM Object only containing header information.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span> Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>Write the DICOM Object in to a separate file.</span></p>
<div id="attachment_400" class="wp-caption alignnone" style="width: 1034px"><a href="http://blogs.circlesource.com/wp-content/uploads/2009/04/21.jpg"><img class="size-large wp-image-400" src="http://blogs.circlesource.com/wp-content/uploads/2009/04/21-1024x193.jpg" alt="Splitting Header(Demographics) from DICOM file." width="1024" height="193" /></a><p class="wp-caption-text">Splitting Header(Demographics) from DICOM file.</p></div>
<p class="MsoListParagraphCxSpLast" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span><!--[if gte vml 1]&gt;                    &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span style="text-decoration: underline;"><span style="text-decoration: none;"> </span></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span style="text-decoration: underline;"><span style="text-decoration: none;"> </span></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span style="text-decoration: underline;">DATA PART:</span></strong></p>
<p class="MsoListParagraphCxSpFirst" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span>Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>First we have to open the DICOM image for reading using streams.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span>Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>Create a DICOM Object from the stream we opened for reading.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings;"><span>Ø<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><!--[endif]--><span>Read only Pixel Data from the DICOM Object and store in DICOM Element and write it in another file using byte array.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify; text-indent: -0.25in;">
<div id="attachment_403" class="wp-caption alignnone" style="width: 1034px"><a href="http://blogs.circlesource.com/wp-content/uploads/2009/04/3.jpg"><img class="size-large wp-image-403" src="http://blogs.circlesource.com/wp-content/uploads/2009/04/3-1024x215.jpg" alt="Extracting Image from DICOM file." width="1024" height="215" /></a><p class="wp-caption-text">Extracting Image from DICOM file.</p></div>
<p class="MsoListParagraphCxSpMiddle" style="text-align: justify;"><span> </span></p>
<p class="MsoListParagraphCxSpLast" style="margin-left: 0.25in; text-align: justify;"><span><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/04/16/splitting-dicom-file-in-to-header-and-data/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://blogs.circlesource.com/2009/04/02/348/</link>
		<comments>http://blogs.circlesource.com/2009/04/02/348/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 11:19:47 +0000</pubDate>
		<dc:creator>nikhil.kumar</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[TIMEDIFF]]></category>
		<category><![CDATA[TIMESTAMPDIFF]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=348</guid>
		<description><![CDATA[Dealing with MySQL TIMEDIFF() for hours greater than 838
Let&#8217;s have a look at the TIMEDIFF() function in MySQL:
TIMEDIFF(expr1,expr2) - TIMEDIFF() returns expr1 &#8211; expr2 expressed as a time value. expr1 and expr2 are time or date-and-time expressions, but both must be of the same type.
It seems that when TimeDiff is performed on 2 dates that [...]]]></description>
			<content:encoded><![CDATA[<div style="font:12px;"><strong>Dealing with MySQL TIMEDIFF() for hours greater than 838</strong></p>
<p>Let&#8217;s have a look at the TIMEDIFF() function in MySQL:</p>
<p><code><strong>TIMEDIFF(<em>expr1</em>,<em>expr2</em>) -</strong></code> <code>TIMEDIFF()</code> returns <code><em>expr1</em></code> &#8211; <code><em>expr2</em></code> expressed as a time value. <code><em>expr1</em></code> and <code><em>expr2</em></code> are time or date-and-time expressions, but both must be of the same type.</p>
<p>It seems that when TimeDiff is performed on 2 dates that exceed 36 days then a warning is thrown, and the resulting value is always capped to 838.</p>
<p><strong>For example:</strong></p>
<p>select time_format(timediff(&#8216;2009-02-05 00:00:00&#8242;,&#8217;2009-01-01 00:00:00&#8242;),&#8217;%H&#8217;) as TimeDifference</p>
<p>If you execute the above query MySQL will return 838 instead of returning 840 (35 X 24 = 840).</p>
<p>The reason is precisely because TIME type in MySQL has an upper bound of 838:59:59. TIME values may range from &#8216;-838:59:59′ to &#8216;838:59:59′.</p>
<p><code>By using TIMESTAMPDIFF() function you can get around with this problem.</code></p>
<p>Let&#8217;s have a look at the <code>TIMESTAMPDIFF</code>() function in MySQL:</p>
<p><code><strong>TIMESTAMPDIFF(<em>unit</em>,<em>datetime_expr1</em>,<em>datetime_expr2</em>)</strong></code><strong> &#8211; </strong>Returns <code><em>datetime_expr2</em></code><code> - <em>datetime_expr1</em></code>, where <code><em>datetime_expr1</em></code> and <code><em>datetime_expr2</em></code> are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part <code>'00:00:00'</code> where necessary. The unit for the result (an integer) is given by the <code><em>unit</em></code> argument. The legal values for <code><em>unit</em></code> are <code>FRAC_SECOND</code> (microseconds), <code>SECOND</code>, <code>MINUTE</code>, <code>HOUR</code>, <code>DAY</code>, <code>WEEK</code>, <code>MONTH</code>, <code>QUARTER</code>, or <code>YEAR</code>.</p>
<pre>It is bit confusing timediff and timestampdiff expects the times in opposite order.</pre>
<p>Now let&#8217;s try timestampdiff() with same dates</p>
<p>select timestampdiff(hour,&#8217;2009-01-01 00:00:00&#8242;,&#8217;2009-02-05 00:00:00&#8242;) as TimeDifference</p>
<p>If you execute the above query MySQL will return 840. Even if the difference is more than 840 it will not throw any exceptions or any warning. Hence we can use TIMESTAMPDIFF() function where the difference is more than 838 and can get the output in any of <code>FRAC_SECOND</code> (microseconds), <code>SECOND</code>, <code>MINUTE</code>, <code>HOUR</code>, <code>DAY</code>, <code>WEEK</code>, <code>MONTH</code>, <code>QUARTER</code>, or <code>YEAR</code>.</p>
<p>There you have it &#8230; I hope you enjoyed this article. Please leave comments / suggestions / questions if you have.
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/04/02/348/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Battling with java.lang.IllegalStateException: getWriter() has already been called for this response</title>
		<link>http://blogs.circlesource.com/2009/01/08/89/</link>
		<comments>http://blogs.circlesource.com/2009/01/08/89/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 07:04:25 +0000</pubDate>
		<dc:creator>kapil</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=89</guid>
		<description><![CDATA[

 
 
I had a requirement to generate reports as PDF file which will also includes the charts and download them at client&#8217;s system. For creating charts, I was using Chart FX 6.5 (Product of Software FX). 
 
And as a part of framework I created a JAVA class which will create the Charts images [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><strong><span style="font-size: 15pt; font-family: Calibri;"><em><br />
</em></span></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">I had a requirement to generate reports as <strong>PDF </strong>file which will also includes the charts and download them at client&#8217;s system. For creating charts, I was using <strong>Chart FX 6.5</strong> (Product of Software FX). </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">And as a part of framework I created a <strong>JAVA </strong>class which will create the Charts images and return path of the Chart images.(This <strong>JAVA</strong> class was called by controller, a <strong>SERVLET</strong>).</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">There are two constructors for creating ChartServer object. This object is used to design the Chart and assign the values of charts. Lately Chart can be generated by this object.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size: 12pt">1)<span> </span><em>ChartServer chart = new ChartServer();</em></span></strong></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size: 12pt">2)<span> </span><em>ChartServer chart = new ChartServer(servletContext, request, response);</em></span></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Initially I used the first option to create Charts.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">I saved a path of a creating image in a String variable.</span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">path = chart.getHtmlTag(&#8220;1070&#8243;, &#8220;500&#8243;, &#8220;PNG&#8221;);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">and returned this path back to Controller. After all these <strong>CONTROLLER </strong>will call a method which will create the report with charts in <strong>PDF</strong> and send the <strong>PDF </strong>file to the client.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">When application is running and I try to download a <strong>PDF </strong>file, sometimes it was downloading <strong>PDF </strong>and sometimes it was giving following error.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">NullPointerException</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">chart.getHtmlTag(Unknown source)</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">I had a feeling that ChartFX was not finding the context path to generate the Charts. Finally came out know that at many places ChartServer internally uses Application context.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">So I decided to go to second constructor of ChartServer.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">ChartServer chart = new ChartServer(servletContext, request, response);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">For sending <strong>PDF </strong>to client browser I created a method in controller which is the end point of the request.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">private void doDownload(final HttpServletResponse resp,</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>final String filename, final String originalFilename, final String reportType)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>throws IOException {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>File f = new File(filename);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>int length = Constants.ZER0;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>ServletOutputStream op = resp.getOutputStream();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>resp.setContentType(&#8220;application/PDF;charset=UTF-8&#8243;);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>resp.setContentLength((int) f.length());</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>resp.setHeader(&#8220;Content-Disposition&#8221;, &#8220;attachment; filename=&#8221;" + originalFilename + &#8220;&#8221;");</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>byte[] bbuf = new byte[Constants.FIVE_HUNDRED];</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>DataInputStream in = new DataInputStream(new FileInputStream(f));</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>while ((in != null) &amp;&amp; ((length = in.read(bbuf)) != -1)) {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>op.write(bbuf, Constants.ZER0, length);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>}</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>in.close();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>op.flush();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>op.close();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>if (f.exists()) {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>f.delete();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>}</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>}</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">By doing this, I faced another awesome problem.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">java.lang.IllegalStateException: getWriter() has already been called for this response</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>org.apache.catalina.connector.Response.getOutputStream(Response.java:576)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:181)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>javax.servlet.http.HttpServlet.service(HttpServlet.java:627)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>javax.servlet.http.HttpServlet.service(HttpServlet.java:729)</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Oops !! Another exception in developer’s life. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">I debugged it and found that everything is going fine before the following line which responsible for generating the Chart image.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">path = chart.getHtmlTag(&#8220;1070&#8243;, &#8220;500&#8243;, &#8220;PNG&#8221;);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Exception is coming on this line. So, I believed that ChartServer internally calls getWriter() of ServletResponse.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Now I wanted to know whether Response has been committed or not. I used <em>isCommitted</em> method of Servlet Response (Used it in Controller, separate java class and in doDownload method before ServletOutputStream was being opened) to find it out whether response has been committed or not. But no luck every where I got it as false.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">I tried something different this time, I used <strong>JSP </strong>instead of java class, because in <strong>JSP </strong>no need to pass context as a parameter. So, I wrote chart code in <strong>JSP </strong>to create Charts and forwarded the request to another controller which will create the <strong>PDF </strong>file.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">My flow was </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size: 12pt">Controller &#8212;&#8212;&gt; JSP &#8212;&#8212;&#8211;&gt; Controller</span></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">But these time also no luck. Same old exception.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><em><span style="font-family: Calibri;font-size: 12pt"><strong>java.lang.IllegalStateException: getWriter() </strong>has already been called for this response</span></em></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">After a lots of trial and error, I decided to reset buffer of the Servlet Response, Following method I used <em>response.resetBuffer()</em> from Servlet Response before I generate the PDF file.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">But exception was luckier then me and came all over again.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Then I tried to reset the Servlet Response too using <em>reset()</em> method of Servlet Response.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">This time I didn&#8217;t get any error/exception and my <strong>PDF </strong>was generating with appropriate charts and I was able to download it from my Browser too.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">So my final <strong>PDF </strong>creation and downloading method is</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt">public void doDownloadPDF(HttpServletResponse response, </span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-indent: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt">String jasperFilename, String PDFFilename,</span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-indent: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>Map reportParams, JRDataSource dataSource) throws IOException {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>InputStream jasperStream = this.getClass().getClassLoader().getResourceAsStream(</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>jasperFilename + &#8220;.jasper&#8221;);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>response.reset();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>response.resetBuffer(); //This may not required but I feel good to have this.</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal" style="margin-left: 21pt;"><strong><em><span style="font-family: Calibri;font-size: 12pt">response.setContentType(&#8220;application/PDF;charset=UTF-8&#8243;);<span> </span> response.setHeader(&#8220;Content-Disposition&#8221;, &#8220;attachment; filename=&#8221;" + PDFFilename + &#8220;.PDF&#8221; + &#8220;&#8221;");</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span></span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal" style="text-indent: 21pt;"><strong><em><span style="font-family: Calibri;font-size: 12pt">//getting jasperReport by loading jasper file</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>JasperReport jasperReport;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>try {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>//getting jasper Print by filling the report</span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, dataSource);</span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-indent: 2.25pt;"><strong><em><span style="font-family: Calibri;font-size: 12pt">JasperExportManager.exportReportToPDFStream(jasperPrint, response.getOutputStream());</span></em></strong></p>
<p class="MsoNormal" style="text-indent: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt"> </span></em></strong></p>
<p class="MsoNormal" style="text-indent: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>response.getOutputStream().flush();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span><span> </span><span> </span>response.getOutputStream().close();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>} catch (JRException ex) {</span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in;"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>Logger.getLogger(JasperReportController.class.getName()).log(Level.SEVERE, null, ex);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>}</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size: 12pt"><span> </span>}</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">Now my <strong>PDFs </strong>are generating without any problem. I am using <strong>JRE 1.5 and Tomcat 5.5.27</strong>. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size: 12pt">A battle of a Developer has been appreciated by the Exception and Exception is now demolished!! <img src='http://blogs.circlesource.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/01/08/89/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inserting charts into PDF</title>
		<link>http://blogs.circlesource.com/2009/01/07/75/</link>
		<comments>http://blogs.circlesource.com/2009/01/07/75/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 08:00:12 +0000</pubDate>
		<dc:creator>hkishorekumar</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=75</guid>
		<description><![CDATA[

 
JasperReport is report generation tool used to create reports in Java. And also used to include Charts, Images, and Tables into the report. It can produce the reports in multiple formats like PDF, CSV, HTML, XLS etc. In this article, first we discussed about creation of PDF file using jasper report.
 
Tools required to [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="text-align: center;" align="center"><strong><span style="font-size: 15pt; font-family: Calibri;"><br />
</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri; font-size:12pt">JasperReport is report generation tool used to create reports in Java. And also used to include Charts, Images, and Tables into the report. It can produce the reports in multiple formats like <strong>PDF</strong>, <strong>CSV</strong>, <strong>HTML</strong>, <strong>XLS </strong>etc. In this article, first we discussed about creation of <strong>PDF </strong>file using jasper report.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Tools required to generate Jasper reports are JRE 1.5 or above, jasperreport-3.0.0.jar, itext-1.2.3.jar.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Jasper uses <strong>JRXML </strong>files to generate report. For easy to generate <strong>JRXML</strong>, we can use iReport-2.0.4 or later. IReports is a tool which is used to generate <strong>JRXML</strong>. It provides GUI to design the report elements such as titles, fields, variables, parameters, charts etc.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">The generated <strong>JRXML </strong>file contains report design information like title, page footers, page headers, page size, margins etc.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">The <strong>JRXML </strong>file needs to be compiled to generate the JASPER file, which will be used by <strong><em>JasperReport</em></strong> classes to generate the actual report. <strong>JRXML </strong>can be compiled using method <em><strong>compileReport</strong>()</em> of <span class="bold"><em>net.sf.jasperreports.engine.JasperCompileManager</em> class. The compiled binary file will have extension “.<strong>jasper</strong>”. Also we can use iReports to compile the <strong>JRXML </strong>file.</span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;"> </span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;font-size:12pt">For example to create report in form of <strong>PDF </strong>the code is given below.</span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;"> </span></span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperReport jasperReport = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperPrint jasperPrint = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">InputStream jasperStream = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">try {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>jasperStream = this.getClass().getClassLoader().getResourceAsStream(</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>“fileName.jasper&#8221;);&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;(1)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);&#8212;&#8212;&#8212;&#8212;&#8211;(2)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, <span> </span>dataSource);&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;(3)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">//exporting the PDF file</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperExportManager.exportReportToPdfFile(jasperPrint, fileName);&#8212;&#8212;&#8212;&#8211;(4)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">} catch (Exception e) {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>e.printStackTrace();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">}</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">}</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">In above code <em>JasperReport</em> is instance of <em>net.sf.jasperreports.engine.JasperReport</em> and holds the jasper file stream. We are loading the jasper stream at step (1, 2). Here we are using the <strong><em>JRLoader.loadObject()</em></strong> method to load the jasper stream into <em>jasperReport</em>. Now <em>jasperReport</em> instance contains the compiled “.<strong>jasper</strong>”. The “.<strong>jasper</strong>” is nothing but the compiled <strong>JRXML </strong>which holds the design and information about where the different data should fit.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><em><span style="font-family: Calibri;font-size:12pt">JasperFillManager.fillReport(…..)</span></em><span style="font-family: Calibri;font-size:12pt"> method will returns a instance of <em>JasperPrint</em> class and that can be used to export the data into <strong>PDF, CSV, XLS </strong>etc. In above example at step(3) we are creating <em>JasperPrint</em> instance. It takes the parameters </span></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">jasperReport</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>having the compiled .jasper file, </span><!--[endif]--></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">reportParams</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>is actually a <em>java.util.Map</em>. We can set any parameters and their values as key value pairs in map. And then passed as parameter for method <em>fillReport(…..)</em>.<span> </span>This contains the certain static data to be displayed in report.</span><!--[endif]--></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">dataSource</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>is a instance of <em>JRDataSource</em>. Whatever the data we want to show in report can be send as <em>dataSource</em>. This contains the DataSource which will be iterated by JASPER report and shown the data in a table.</span><!--[endif]--></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Now the <em>jasperPrint</em> instance is created, which actually holds the data viewed in PDF page. Finally the <em><strong>exportreportToPdfFile</strong>()</em> method of<span> </span><em>JasperExportManger</em> is used to export the report object into the PDF file and stored with the specified file name. We already know that <em>jasperPrint</em> instance has the report object to export on the page. This object will be sent as a parameter to <em>JasperExportManager.exportReportToPdfFile()</em> method. Finally it returns binary content as bite[] array. This array is nothing but the <strong>PDF </strong>file.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Up to now creation of PDF is completed. Now we discussed about creating charts and insert them into PDF file.</span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">ChartFx for Java 6.5 Development is a tool to develop charts and maps in the form of J<strong>PEG, PNG</strong> etc. This provides APIs which are required to develop charts. These APIs are used to set charts title, labels, size, background color, axis labels, chart type etc. Charts which are developed using <strong>chartFx </strong>are stored in temporary directory. So we can capture them from temp location and display in <strong>PDF</strong>. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example see the given code.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">String path= null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">ChartServer chart1 = new ChartServer(context, request, response);</span></em></strong></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size:12pt">……………………….</span></strong></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size:12pt">……………………….</span></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">path = chart1.getHtmlTag(&#8220;1560&#8243;, “2000”, &#8220;PNG&#8221;);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><em><span style="font-family: Calibri;font-size:12pt">new ChartServer(context, request, response)</span></em><span style="font-family: Calibri;font-size:12pt"> will create chartServer object. This object is the actual chart object which will be used to create a chart image. Here the parameter context is of type </span><code><em><span style="font-size: 10pt; font-family: Calibri;font-size:12pt">javax.servlet.ServletContext</span></em></code><code><span style="font-size: 10pt; font-family: Calibri;">, </span></code><span style="font-family: Calibri;font-size:12pt">request is of type</span><code><span style="font-size: 10pt; font-family: Calibri;font-size:12pt"> <em>javax.servlet.http.HttpServletRequest</em> </span></code><span style="font-family: Calibri;font-size:12pt">and response is of type</span><code><span style="font-size: 10pt; font-family: Calibri;font-size:12pt"> <em>javax.servlet.http.HttpServletResponse.</em> </span></code><span style="font-family: Calibri;font-size:12pt">One more default constructor is also available to create chart server is <em>ChartServer()</em>. There are many more methods are available in this class to set the title of chart, x-axis title, y-axis title, background color, font color, font size, font style etc. In our example we skipped the code of designing part of chart. Any one can design as per their requirement. At the end method <em>getHtmlTag(&#8220;1560&#8243;, “2000”, &#8220;PNG&#8221;)</em>, will return html image tag(<em>&lt;img …. /&gt;</em>) which will have “src” attribute and this attribute has path of chart image file. Also the <em>&lt;img /&gt;</em> element will have the width and height attributes which will hold 1560 and 2000 respectively.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For our example, think that charts are already created and placed in location “C:images flower.jpeg”. Now we display generated charts into <strong>PDF </strong>report. For that, first create parameter in the <strong>JRXML </strong>file using the iReport.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">In iReports goto <strong>File</strong></span><strong><span style="font-family: Wingdings;"><span>&#8211;&gt;</span></span><span style="font-family: Calibri;">view</span><span style="font-family: Wingdings;"><span>&#8211;&gt;</span></span></strong><span style="font-family: Calibri;font-size:12pt">parameters and then select “parameters” tab, and then click “new” button. Give a name for that parameter and type as String. Later we assign value for this parameter from our java code using <em>java.util.Map</em>.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"><!--[if gte vml 1]&gt;                    &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> <img class="alignnone size-full wp-image-78" src="http://blogs.circlesource.com/wp-content/uploads/2009/01/clip_image0021.jpg" alt="clip_image0021" width="330" height="362" /></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">And then add image element in our <strong>JRXML</strong>. Right click on image element and open properties and click on “Image Tab” then add “Image Expression” for that element.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> <img class="alignnone size-full wp-image-77" src="http://blogs.circlesource.com/wp-content/uploads/2009/01/clip_image0022.jpg" alt="clip_image0022" width="380" height="387" /></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">And then design report as per your requirement and compile to generate “.<strong>Jasper</strong>”. Now we have to send path for this image to display in <strong>PDF </strong>from our java code. The java code given below uses the image available at “C:images flower.jpeg” location.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">Map reportParameters = new Hashmap();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">reportParameters.put(“chartPath”,” C:\images <a href="/////flower.jpeg">\flower.jpeg</a>”);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperPrint = JasperFillManager.fillReport(jasperReport, reportParameters,datasource);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Then pass this map as parameter at the time of creating <em>jasperPrint</em> object. Now the path for the image is known for <em>jasperFillManager</em>. So it creates <em>jasperPrint</em> object including this image.<span> </span></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt">Problem when inserting chart in <strong>PDF</strong></span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Some times the inserting chart will have the size that may not be fit into actual size set in <strong>JRXML </strong>file for that image. In this situation while creating the chart, we can divide original chart into number of charts. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example, in <strong>JRXML </strong>we designed the report like it can show chart of 500 pixels height. The chart having 20 bars only perfectly fit in this image element. Now what we have to do is, while creating chart, create two or more charts depending on the result and display them one by one in report. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example, we want to display bar chart for students’ percentages. There are 55 students in class room. As per our design only chart contains bars for 20 students will fit perfectly in image element. Now what we have to do is, while creating chart split it into three charts contains students’ percentage bars of 20, 20, and 15 each. Now display these images into the PDF and they will be displayed perfectly.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Wow! Finally we get the charts into <strong>PDF </strong>with the combination of <strong>ChartFX </strong>and Jasper Reports. I hope this will help anyone to develop reports in the form of <strong>PDF</strong>.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/01/07/75/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Logging with “Java logging API”</title>
		<link>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/</link>
		<comments>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 04:23:20 +0000</pubDate>
		<dc:creator>maqbool</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=20</guid>
		<description><![CDATA[ 
I wonder why you want to use Java logging API when you can use log4j. Well if you want to or have to use Java’s bundled API for logging, it is not that bad. There are several good articles on the web that go it much more details on this topic then what I [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves /> <w:TrackFormatting /> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF /> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:SplitPgBreakAndParaMark /> <w:DontVertAlignCellWithSp /> <w:DontBreakConstrainedForcedTables /> <w:DontVertAlignInTxbx /> <w:Word11KerningPairs /> <w:CachedColBalance /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math" /> <m:brkBin m:val="before" /> <m:brkBinSub m:val="&#45;-" /> <m:smallFrac m:val="off" /> <m:dispDef /> <m:lMargin m:val="0" /> <m:rMargin m:val="0" /> <m:defJc m:val="centerGroup" /> <m:wrapIndent m:val="1440" /> <m:intLim m:val="subSup" /> <m:naryLim m:val="undOvr" /> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"   DefSemiHidden="true" DefQFormat="false" DefPriority="99"   LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Normal" /> <w:LsdException Locked="false" Priority="9" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="heading 1" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9" /> <w:LsdException Locked="false" Priority="39" Name="toc 1" /> <w:LsdException Locked="false" Priority="39" Name="toc 2" /> <w:LsdException Locked="false" Priority="39" Name="toc 3" /> <w:LsdException Locked="false" Priority="39" Name="toc 4" /> <w:LsdException Locked="false" Priority="39" Name="toc 5" /> <w:LsdException Locked="false" Priority="39" Name="toc 6" /> <w:LsdException Locked="false" Priority="39" Name="toc 7" /> <w:LsdException Locked="false" Priority="39" Name="toc 8" /> <w:LsdException Locked="false" Priority="39" Name="toc 9" /> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption" /> <w:LsdException Locked="false" Priority="10" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Title" /> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font" /> <w:LsdException Locked="false" Priority="11" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtitle" /> <w:LsdException Locked="false" Priority="22" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Strong" /> <w:LsdException Locked="false" Priority="20" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Emphasis" /> <w:LsdException Locked="false" Priority="59" SemiHidden="false"    UnhideWhenUsed="false" Name="Table Grid" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text" /> <w:LsdException Locked="false" Priority="1" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="No Spacing" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 1" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 1" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 1" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 1" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision" /> <w:LsdException Locked="false" Priority="34" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="List Paragraph" /> <w:LsdException Locked="false" Priority="29" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Quote" /> <w:LsdException Locked="false" Priority="30" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Quote" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 1" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 1" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 1" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 1" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 1" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 2" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 2" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 2" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 2" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 2" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 2" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 2" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 2" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 3" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 3" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 3" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 3" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 3" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 3" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 3" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 3" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 3" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 4" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 4" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 4" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 4" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 4" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 4" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 4" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 4" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 4" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 5" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 5" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 5" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 5" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 5" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 5" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 5" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 5" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 5" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 6" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 6" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 6" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 6" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 6" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 6" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 6" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 6" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 6" /> <w:LsdException Locked="false" Priority="19" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis" /> <w:LsdException Locked="false" Priority="21" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis" /> <w:LsdException Locked="false" Priority="31" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference" /> <w:LsdException Locked="false" Priority="32" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Reference" /> <w:LsdException Locked="false" Priority="33" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Book Title" /> <w:LsdException Locked="false" Priority="37" Name="Bibliography" /> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading" /> </w:LatentStyles> </xml><![endif]--> <!--[if gte mso 10]></p>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-qformat:yes;
	mso-style-parent:"";
	mso-padding-alt:0in 5.4pt 0in 5.4pt;
	mso-para-margin-top:0in;
	mso-para-margin-right:0in;
	mso-para-margin-bottom:10.0pt;
	mso-para-margin-left:0in;
	line-height:115%;
	mso-pagination:widow-orphan;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;}
</style>
<p><![endif]--></p>
<p class="MsoNormal">I wonder why you want to use Java logging API when you can use log4j. Well if you want to or have to use Java’s bundled API for logging, it is not that bad. There are several good articles on the web that go it much more details on this topic then what I am going to cover her. So, look at the articles at the end of this article for links to some of those detailed articles.</p>
<p class="MsoNormal">So, what am I going to be covering in this article? Just few tricks that I found intriguing and somewhat hard to understand. <span> </span>First of all the <code><span style="font-size: 10pt; line-height: 115%;">logging.properties</span></code> file. This file resides in the JRE directory as <code><span style="font-size: 10pt; line-height: 115%;">lib/logging.properties </span></code><code><span style="font-size: 8pt; line-height: 115%;">(/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib </span></code><span style="font-size: 9pt; line-height: 115%;">for Mac OS users</span>). You can edit this file to control the behavior of the default loggers that get installed when JVM starts. This is definitely is not a good idea. You can copy this file to a user space and then can edit the copy to your liking and load this file as part of your program to control the logging. As I am working on a Mac I will copy the system properties file to my space as <code><span style="font-size: 9pt; line-height: 115%;">/Users/user/LogApi/mylogging.properties</span></code>. Now, I would like to have two types of handlers, the standard Console Handler and a File Handler. I edit the file to install the two handlers. So, make sure that the following line is uncommented in your properties file</p>
<p class="MsoNormal"><code><span style="font-size: 9pt; line-height: 115%;">handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler</span></code></p>
<p class="MsoNormal">I would like to log everything to a file but only the SEVERE and above log entries to my log console. By default, the FileHandler logs everything to the file, so we don’t have to do anything here. To make the ConsoleHandler log only entries higher thatn SEVERE, add line similar to:</p>
<p class="MsoNormal"><code><span style="font-size: 9pt; line-height: 115%;">java.util.logging.ConsoleHandler.level = SEVERE</span></code></p>
<p class="MsoNormal">Configure the FIleHandler handler with the following properties.</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.pattern = /Users/user/LogApi/build/classes/javaapi%u.log</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.limit = 50000</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.count = 1</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java.util.logging.FileHandler.append = true</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">The first line specifies where the logging file is to be placed. Please see (<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html">http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html</a>) for other options and detials. The <code><span style="font-size: 9pt; line-height: 115%;">“%u”</span></code> in the above line basically makes the log file rotate through “<code><span style="font-size: 9pt; line-height: 115%;">javaapi0.log</span></code>” and “<code><span style="font-size: 9pt; line-height: 115%;">javaapi1.log</span></code>”. The “<code><span style="font-size: 9pt; line-height: 115%;">count”</span></code> option controls how many of these files the logger cycles through. Also don’t forget to set the <code><span style="font-size: 9pt; line-height: 115%;">append</span></code> mode to <code><span style="font-size: 9pt; line-height: 115%;">true</span></code> as the default this set to false. You are done. A simple program to exercise this is given below:</p>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">package logapi;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">import java.util.logging.*;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">/**</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">* A simple Java program, to understand the Java logging API</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">*/</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">public class Main {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private Logger theLogger = null;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span></span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public static void main(String[] args) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>Main hello = new Main("Hello world!");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>hello.sayHello();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private String theMessage;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public Main(String message) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theMessage = message;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public void sayHello() {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>try {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//get my logger for this class.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger = Logger.getLogger(Main.class.getName());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at INFO level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.info(“info message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at SEVERE level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.severe("severe message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>} catch(Exception e){</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// caught exception, lets log it</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.log(Level.SEVERE,"Caught excepttion, ",e);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">}</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">Once you compile this program using javac to Main.class, you will execute this program with the following command:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">java -Djava.util.logging.config.file=/Users/user/LogApi/mylogging.properties Main</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">You are asking the jvm to load your own logger.properties file with the “-D” switch. This is the output of this program:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$ java -Djava.util.logging.config.file=/Users/user/LogApi/mylogging.properties Main</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:55:08 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">SEVERE: severe message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">The program after execution also wrote a file called “javaapi0.log” and the contents of this file are:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$ more javaapi0.log </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:59:32 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">INFO: info message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">Dec 21, 2008 8:59:32 PM logapi.Main sayHello</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">SEVERE: severe message...</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 7pt;">localhost:classes user$</span></code></p>
<p class="MsoNormal">
<p class="MsoNormal">Notice that on the console we only saw the “SEVERE” message but the log file has both the info and the sever messages.</p>
<p class="MsoNormal">
<p class="MsoNormal">If you don’t want to mess around with the properties files but rather want to control the logging programmatically, we can do that also. Although I advise not doing this as you have no control over the logging once the program is rolled out to the filed except re-compiling. However, I will show you how we can do this programmatically just for understanding sake. I hope you have read the other detail tutorial on the web and know the hierarchy of the loggers and their default behaviour. The basic behavior of the logger is when the message comes to it for logging, it checks with its filter and if the message passes the filter the message is handed over to the installed handlers and then the message is also passed on to the parent logger. The parent logger does the same thing till we hit the “root” logger. The root logger handlers are installed at the start of the JVM through the properties file sitting under JRE/lib directory. What we will do is install our own Console and File handlers and then disable the use of the parent handlers by calling the method “theLogger.setUseParentHandlers(false)”. That way we make sure that the only handlers the message passes through are the handlers we install. Here is the code for this:</p>
<p class="MsoNormal">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">import java.util.logging.*;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">/**</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">* A simple Java program, to understand the Java logging API</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">*/</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">public class Main {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private Logger theLogger = null;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public static void main(String[] args) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>Main hello = new Main("Hello world!");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>hello.sayHello();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>private String theMessage;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public Main(String message) {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theMessage = message;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>public void sayHello() {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>boolean append = true;</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>try {</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// create my own Handlers for file and console</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>FileHandler fh = new FileHandler("/Users/user/LogApi/build/classes/javaapi%u.log", 50000, 2, append);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//ConsoleHandler only if we need to log to Console</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>ConsoleHandler ch = new ConsoleHandler();</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>//get my logger for this class.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger = Logger.getLogger(Main.class.getName());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// the logging gets bubbled up to the parent and the parent default</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// handlers are used. Lets disable the use of the parent Handlers.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.setUseParentHandlers(false);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// set the formatting for the log file to the simple</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>fh.setFormatter(new java.util.logging.SimpleFormatter());</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// install our own handlers in to the logger.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.addHandler(fh);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// ConsoleHandler Seetings only if we want to log to console.</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>ch.setLevel(Level.SEVERE);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.addHandler(ch);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at INFO level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.info("info message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// log a message at SEVERE level</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.severe("severe message...");</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>} catch(Exception e){</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>// caught exception, lets log it</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>theLogger.log(Level.SEVERE,"Caught excepttion, ",e);</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"><span> </span>}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;">}</span></code></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><code><span style="font-size: 9pt;"> </span></code></p>
<p class="MsoNormal">Compile this to Main.class and execute to get the same effect as the previous program.</p>
<p class="MsoNormal">Finally, the reason I had to deal with this is, we were working on a Java program to parse some files on a regular interval (every 2 minutes). This program got installed under cron. As you know, under cron anything that is written to the stdout or stderr gets emailed to the user under which the program is running. If you start logging a lot of messages to stdout/stderr (the console) you will get a lots of messages flooding your inbox. Worst yet, the /var/mail/user file may continue to grow and fill up the whole space if the program is left running for a long time without cleaning the messages. If we selectively log messages to the console, then you get an e-mail message for the critical messages that you want to see any ways. <span> </span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://www.onjava.com/pub/a/onjava/2002/06/19/log.html">http://www.onjava.com/pub/a/onjava/2002/06/19/log.html</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://www.crazysquirrel.com/computing/java/logging.jspx">http://www.crazysquirrel.com/computing/java/logging.jspx</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><a href="http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html">http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html</a></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;">
<p class="MsoNormal">
<p class="MsoNormal">
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/12/22/logging-with-%e2%80%9cjava-logging-api%e2%80%9d/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generating random Date/Time between 2 different Dates/TimeStamps in Java</title>
		<link>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/</link>
		<comments>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 13:34:10 +0000</pubDate>
		<dc:creator>Nivasini</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Random dates]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=8</guid>
		<description><![CDATA[It is common to find a random number between 2 given numbers&#8230; In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning&#8230; But later figured out a simple solution for the same&#8230; The steps for [...]]]></description>
			<content:encoded><![CDATA[<p>It is common to find a random number between 2 given numbers&#8230; In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning&#8230; But later figured out a simple solution for the same&#8230; The steps for generating a random time between 2 timestamps really turned out to be very simple &amp; this article is about how we achieved that&#8230;</p>
<p>If min and max are the 2 variables that are holding minimum and maximum values, &amp; random is the random number that is to be generated, in java the random number is calculated in this way&#8230;<br />
<strong><code><br />
random = (Math.random() * (max - min) ) + min<br />
</code></strong></p>
<p>Here <strong>Math.random()</strong> is a function in java that generates any random value between 0 &amp; 1&#8230; We multiply that with the difference between max &amp; min values and finally add the obtained result with the min value to get a random number between 2 given numbers.. This idea was set as the basement for our requirement too&#8230; Wondering how?? This is how it goes&#8230;</p>
<p>We use <strong>Calendar</strong> object which defaults to current date and time. We then set Hour, minute &amp; second in the same object(If required we can set the year, month &amp; date too).. Now we have the Calendar object with the date what we set or current date and time. Calendar class has a method named <strong>getTimeInMillis</strong> which will convert this timeStamp to a long value. This long value serves as the first min variable as in the example where we generate a random number between 2 different numbers.</p>
<p>Now in the same way we get the long value of the second timestamp too which is the max variable value. Using a similar calculation as above we get a random long number between 2 different long numbers which are the long time. Finally we construct the date object with the long value that we obtained.. This date object&#8217;s value is the random time stamp between 2 different Calendar objects and hence the we can generate random date/time between two different Calendar objects!!!!   <img src='http://blogs.circlesource.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here goes the code for that..<br />
<strong><code><br />
//code to generate random timestamp between morning 6 to evening 8 pm<br />
public static void main(String args[]){<br />
Calendar cdr = Calendar.getInstance();<br />
cdr.set(Calendar.HOUR_OF_DAY, 6);<br />
cdr.set(Calendar.MINUTE, 0);<br />
cdr.set(Calendar.SECOND, 0);<br />
long val1=cdr.getTimeInMillis();<br />
</code></p>
<p><code><br />
cdr.set(Calendar.HOUR_OF_DAY, 20);<br />
cdr.set(Calendar.MINUTE, 0);<br />
cdr.set(Calendar.SECOND, 0);<br />
long val2=cdr.getTimeInMillis();<br />
</code></p>
<p><code><br />
Random r=new Random();<br />
long randomTS=(long)(r.nextDouble()*(val2-val1))+val1;<br />
Date d=new Date(randomTS);<br />
System.out.println(d.toString());<br />
}<br />
</code></strong><br />
<strong>PS: This will work even for generating random date between two different dates which can be a month/year gap.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
