<?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>ColdFused? &#187; java</title>
	<atom:link href="http://www.rupeshk.org/blog/index.php/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rupeshk.org/blog</link>
	<description>My Views on ColdFusion, Java and related technologies</description>
	<lastBuildDate>Mon, 21 Jun 2010 12:40:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ColdFusion ORM : How to log SQL</title>
		<link>http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-orm-how-to-log-sql/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-orm-how-to-log-sql/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 14:47:33 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[ORM]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-orm-how-to-log-sql/</guid>
		<description><![CDATA[When you use ORM for developing an application, SQLs are generated and executed by the underlying ORM engine (i.e Hibernate for ColdFusion ORM). However, for both troubleshooting and performance optimization, it is crucial to monitor what queries are getting generated. It can help you find out if there is any error in mapping that you [...]]]></description>
			<content:encoded><![CDATA[<p>When you use ORM for developing an application, SQLs are generated and executed by the underlying ORM engine (i.e Hibernate for ColdFusion ORM). However, for both troubleshooting and performance optimization, it is crucial to monitor what queries are getting generated. It can help you find out if there is any error in mapping that you have provided as well as it can help you decide various tuning strategies. </p>
<p>ColdFusion can log the SQLs generated by ORM either onto the console or a file. At the same time it leaves enough hook for you to log it anywhere you want.</p>
<p>ColdFusion ORM provides two ways to log the SQLs.</p>
<ol>
<li>Using application setting to log to console : This is a quick and simple way to log the sql to console. This is enabled by setting &#8220;logsql&#8221; in ormsettings.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;">   <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> this.ormsettings.logsql<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>This setting is self sufficient and it will log all the sqls executed by hibernate to the console (or a file where the server output goes). However this is not a very flexible option. The sqls are always written to the console and it will be combined with any other output that goes to console. Also this option will not show the DDL queries used for creating or updating tables. It only logs the SQL for the entity operations. </p>
<li>Using log4J.properties: Hibernate uses <a href="http://logging.apache.org/log4j/">log4j</a> for its logging and you can completely control its logging (including SQL) by modifying the log4j.properties. log4j.properties is present under &lt;cf_home&gt;/lib directory. Please note that you don&#8217;t need to do any application specific setting for this. </li>
</ol>
<p>&nbsp;</p>
<p>I will go in details about using log4j.properties for SQL logging. Here is a snippet from log4j.properties file that is shipped with ColdFusion.</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">###--------------- Hibernate Log Settings ------ </span>
<span style="color: #808080; font-style: italic;">### Set Hibernate log </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">ERROR, HIBERNATECONSOLE </span>
&nbsp;
<span style="color: #808080; font-style: italic;">### log just the SQL </span>
<span style="color: #808080; font-style: italic;">#log4j.logger.org.hibernate.SQL=DEBUG, HIBERNATECONSOLE </span>
<span style="color: #808080; font-style: italic;">#log4j.additivity.org.hibernate.SQL=false </span>
<span style="color: #808080; font-style: italic;">### Also log the parameter binding to the prepared statements. </span>
<span style="color: #808080; font-style: italic;">#log4j.logger.org.hibernate.type=DEBUG </span>
<span style="color: #808080; font-style: italic;">### log schema export/update ### </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate.tool.hbm2ddl</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">DEBUG, HIBERNATECONSOLE </span>
<span style="color: #808080; font-style: italic;">### log cache activity ### </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate.cache</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">ERROR, HIBERNATECONSOLE </span>
&nbsp;
<span style="color: #808080; font-style: italic;"># HibernateConsole is set to be a ColsoleAppender for Hibernate message  using a PatternLayout. </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.ConsoleAppender </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.PatternLayout </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">%d<span style="">&#123;</span>MM/dd HH:mm:ss<span style="">&#125;</span> <span style="">&#91;</span>%t<span style="">&#93;</span> HIBERNATE %-5p - %m%n%n </span>
<span style="color: #808080; font-style: italic;">#---------------------------------------------</span></pre></div></div>

<p>First we will see the relevant settings for SQL logging:</p>
<ul>
<li>log4j.logger.org.hibernate.SQL : Defines whether the SQL executed for entity operations will be logged and where it will be logged. The second value for this i.e &#8216;HIBERNATECONSOLE&#8217; is a appender that controls <strong>where</strong> the SQLs will be logged. In the above example HIBERNATECONSOLE is a &#8216;console&#8217; appender which means it will log the sql to console.
<li>log4j.logger.org.hibernate.type : Defines whether parameter values used for parametrized query will be logged.
<li>log4j.logger.org.hibernate.tool.hbm2ddl : Defines whether DDL sql statements will be logged. </li>
</ul>
<p>To enable the SQL logging for console, we just need to uncomment the settings mentioned above. Here is how the hibernate log settings in log4j.properties file would look like</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">###--------------- Hibernate Log Settings ------ </span>
<span style="color: #808080; font-style: italic;">### Set Hibernate log </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">ERROR, HIBERNATECONSOLE </span>
&nbsp;
<span style="color: #808080; font-style: italic;">### log just the SQL </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate.SQL</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">DEBUG, HIBERNATECONSOLE </span>
<span style="color: #000080; font-weight:bold;">log4j.additivity.org.hibernate.SQL</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">false </span>
<span style="color: #808080; font-style: italic;">### Also log the parameter binding to the prepared statements. </span>
<span style="color: #808080; font-style: italic;">#log4j.logger.org.hibernate.type=DEBUG </span>
<span style="color: #808080; font-style: italic;">### log schema export/update ### </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate.tool.hbm2ddl</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">DEBUG, HIBERNATECONSOLE </span>
<span style="color: #808080; font-style: italic;">### log cache activity ### </span>
<span style="color: #000080; font-weight:bold;">log4j.logger.org.hibernate.cache</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">ERROR, HIBERNATECONSOLE </span>
&nbsp;
<span style="color: #808080; font-style: italic;"># HibernateConsole is set to be a ColsoleAppender for Hibernate message  using a PatternLayout. </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.ConsoleAppender </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.PatternLayout </span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">%d<span style="">&#123;</span>MM/dd HH:mm:ss<span style="">&#125;</span> <span style="">&#91;</span>%t<span style="">&#93;</span> HIBERNATE %-5p - %m%n%n </span>
<span style="color: #808080; font-style: italic;">#---------------------------------------------</span></pre></div></div>

<p><a href="http://www.rupeshk.org/misc/2507/1/log4j.txt">Here is the complete log4j.properties</a> for logging SQL for console. Ofcourse after changing this you need to restart the server. If you need to log the parameter values used for queries, you need to uncomment &#8216;#log4j.logger.org.hibernate.type=DEBUG&#8217; as well. </p>
<p>What if you want to log the SQL to a file and not to console? That is pretty easy. You just need to change the &#8216;Appender&#8217; used here (HIBERNATECONSOLE) to point to a &#8216;FileAppender&#8217; instead of a ConsoleAppender. Here is how the configuration for HIBERNATECONSOLE should look like after you point it to a File Appender. </p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.FileAppender</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.File</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">../hibernatesql.log</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.Append</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">true</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.PatternLayout</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">%d<span style="">&#123;</span>MM/dd HH:mm:ss<span style="">&#125;</span> <span style="">&#91;</span>%t<span style="">&#93;</span> HIBERNATE %-5p - %m%n%n</span></pre></div></div>

<p>For standalone ColdFusion installation, the file &#8216;hibernatesql.log&#8217; will be created in the <cfhome>/logs directory.You can also specify a full path of the file for property &#8216;log4j.appender.HIBERNATECONSOLE.File&#8217; and the log will be written to that.</p>
<p>That was easy. Isn&#8217;t it? What if you want a rolling log file where you dont want the log file size to grow infinitely. That is fairly easy too. All you need to do is to use an appropriate appender. The appender definition for that will look like</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.RollingFileAppender</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.File</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">../hibernatesql.log</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.Append</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">true</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.MaxFileSize</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">500KB</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.MaxBackupIndex</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">3</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">org.apache.log4j.PatternLayout</span>
<span style="color: #000080; font-weight:bold;">log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">%d<span style="">&#123;</span>MM/dd HH:mm:ss<span style="">&#125;</span> <span style="">&#91;</span>%t<span style="">&#93;</span> HIBERNATE %-5p - %m%n%n</span></pre></div></div>

<pre></pre>
<p><a href="http://www.rupeshk.org/misc/2507/2/log4j.txt">Here is the complete log4j.properties</a> for logging SQL to a file that will be rolled automatically once it reaches 500KB.</p>
<p>Now that you have seen how easy it is to change one &#8216;Appender&#8217; to another, you can pretty much log it anywhich way you want. Here are some of the interesting &#8216;Appender&#8217;s that come with log4j which you can easily use.</p>
<p><a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html">DailyRollingFileAppender</a>, <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/varia/ExternallyRolledFileAppender.html">ExternallyRolledFileAppender</a>, <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/JMSAppender.html">JMSAppender</a>, <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SMTPAppender.html">SMTPAppender</a>, <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SocketAppender.html">SocketAppender</a>, <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SyslogAppender.html">SyslogAppender</a></p>
<p>See <a href="http://logging.apache.org/log4j/1.2/index.html">log4J</a> for more details on log4j settings.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-orm-how-to-log-sql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>DNS lookup Caching in ColdFusion/Java</title>
		<link>http://www.rupeshk.org/blog/index.php/2008/01/dns-lookup-caching-in-coldfusionjava/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2008/01/dns-lookup-caching-in-coldfusionjava/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 02:22:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2008/01/10/dns-lookup-caching-in-coldfusionjava/</guid>
		<description><![CDATA[Two days back, I got an interesting question from our support team. The Customer in this case was using CFLdap which connected to a particular LDAP server and things were working fine. The problem came when they replaced this LDAP server with another LDAP server and assigned the same dns name to the new ldap [...]]]></description>
			<content:encoded><![CDATA[<p>Two days back, I got an interesting question from our support team. The Customer in this case was using CFLdap which connected to a particular LDAP server and things were working fine. The problem came when they replaced this LDAP server with another LDAP server and assigned the same dns name to the new ldap server. Ideally any connection made henceforth should have worked with the new ldap server but actually that did not happen. ColdFusion started throwing error and it did not work until the ColdFusion server is restarted. Ever seen something similar?</p>
<p>It is obvious that it happend because the IP address was being cached for the host name as a result of which ColdFusion was still trying to connect to the old IP address even though the host name now pointed to a different IP address. This caching also applies for all other network protocol tags such as CFHTTP, CFFTP, CFFEED etc and is not limited to CFLDAP. It is actually the JVM that does this caching. When JVM is requested to resolve a host name, it does the host name to IP address resolution and caches the result. This result will be used for all subsequent lookups. It also caches the negative results. By that I mean, if the dns reolution fails, it caches the failed result for a certain period so that any lookup for that hostname in that period will not result into another resolution on network and will immediately return the failed result.</p>
<p>For more detail on this caching, check out the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html">Javadoc for InetAddress</a> class.</p>
<p>As per this doc, there are two parameters that control this caching</p>
<blockquote><p>
<strong>networkaddress.cache.ttl</strong> (default: -1) : Indicates the caching policy for successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the successful lookup.A value of -1 indicates &#8220;cache forever&#8221;.</p>
</blockquote>
<p></p>
<blockquote><p>
<strong>networkaddress.cache.negative.ttl</strong> (default: 10) : Indicates the caching policy for un-successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the failure for un-successful lookups.A value of 0 indicates &#8220;never cache&#8221;. A value of -1 indicates &#8220;cache forever&#8221;.
</p>
</blockquote>
<p>Now where do you specify this settying? You can specify this setting in <strong>&lt;Java_home&gt;/jre/lib/security/java.security</strong> file. For standalone ColdFusion server it will be in &lt;ColdFusion _dir&gt;/runtime/jre/lib/security/java.security file.</p>
<p>As you see, by default <strong>networkaddress.cache.ttl</strong> caches the result for ever and hence it is configured for best performance. Any change to this mean drop in performance. If you don&#8217;t want to cache the resolved IP address for ever, as is the case here, you would need to change <strong>networkaddress.cache.ttl</strong> value to 60 seconds or 300 seconds or any value you feel suitable. You would not want to set it to 0 as that would mean &#8220;never cache&#8221; the result which might affect the performance significantly.</p>
<p>In which case you would want to change the value for <strong>networkaddress.cache.negative.ttl</strong>? That would be mostly in case when you want to cache the negative result for a longer time and in turn improving the performance. For example, if you are trying to connect to a hostname which can not be resolved to any ip address, and that happens very frequently, each of the call (as long as they are not in the same 10 sec window) would become very slow. Increasing this value would increase the performance but again you would not want to cache the negative result for ever.</p>
<p>After you change this setting, you will have to restart the ColdFusion server for this change to take effect.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2008/01/dns-lookup-caching-in-coldfusionjava/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ColdFusion and WebService : &#8220;class file has wrong version&#8221;</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/08/coldfusion-and-webservice-class-file-has-wrong-version/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/08/coldfusion-and-webservice-class-file-has-wrong-version/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 02:39:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/08/08/coldfusion-and-webservice-class-file-has-wrong-version/</guid>
		<description><![CDATA[I can&#8217;t even count the number of times I have seen this error while QA folks were running webservices regressions for ColdFusion 8. Everytime, someone was running webservices tests on a J2EE configuration they used to get an error :
coldfusion.jsp.CompilationFailedException: Errors reported by Java compiler:&#8230;.class file has wrong version 49.0, should be 48.0
I saw the [...]]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t even count the number of times I have seen this error while QA folks were running webservices regressions for ColdFusion 8. Everytime, someone was running webservices tests on a J2EE configuration they used to get an error :</p>
<p><em>coldfusion.jsp.CompilationFailedException: Errors reported by Java compiler:<br />&#8230;.<br />class file has wrong version 49.0, should be 48.0</em></p>
<p>I saw the same error posted in the public forum where the user had deployed ColdFusion 8 on JRun with JDK1.5 and was trying to invoke webservice. And hence this blog entry <img src='http://www.rupeshk.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>This error comes because of incompatibility between the tools.jar and the JVM version. ColdFusion 8 ships Java 1.6&#8217;s tools.jar (you will find it in cfusion/lib) which is of course incompatible with any other JDK/JRE version. You will see the same behavior with ColdFusion 7 as well, if you run it on a JVM other than 1.4. So the rule of thumb is &#8211; if ColdFusion is running on a JVM version other than the shipped one, you must ensure that the tools.jar is in synch. In short, you will need to copy JDK&#8217;s tools.jar to cfusion/lib.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/08/coldfusion-and-webservice-class-file-has-wrong-version/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using CFFeed with URL sending compressed content</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/08/using-cffeed-with-url-sending-compressed-content/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/08/using-cffeed-with-url-sending-compressed-content/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 21:49:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[cffeed]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/08/03/using-cffeed-with-url-sending-compressed-content/</guid>
		<description><![CDATA[Today someone posted on ColdFusion forum regarding this problem where cffeed was not able to handle a particular URL and it was throwing an error. The URL which he tried was http://movies.msn.com/rss/topcelebs and it failed with an error 
&#8220;Unable to read the source URL.unknown compression method&#8221;  
The reason it happens is &#8211; The URL [...]]]></description>
			<content:encoded><![CDATA[<p>Today someone posted on ColdFusion forum regarding this problem where cffeed was not able to handle a particular URL and it was throwing an error. The URL which he tried was <a href="http://movies.msn.com/rss/topcelebs">http://movies.msn.com/rss/topcelebs</a> and it failed with an error </p>
<p>&#8220;<em>Unable to read the source URL.<br />unknown compression method</em>&#8221;  </p>
<p>The reason it happens is &#8211; The URL returns the response in gzip compressed format only. So when ColdFusion sent a request to this URL and asked for uncompressed data, it could not get anything and hence it was unable to read it. A simpe workaround for this is to use cfhttp to fetch the content, write to a temporary file and then use the cffeed tag to read this file. Important thing to keep in mind here is to set an additional header in the cfhttp tag using cfhttpparam to indicate that it can accept compressed data as well.</p>
<p>Here is the modified code where it first tries cffeed with the URL. If that fails, then it tries to use cfhttp to fetch the content and writes to a temporary file and then uses it in cffeed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> tempDir<span style="color: #0000FF;">=</span><span style="color: #800080; font-weight: bold;">GetTempDirectory</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> tempFile <span style="color: #0000FF;">=</span> <span style="color: #800080; font-weight: bold;">GetTempFile</span><span style="color: #0000FF;">&#40;</span>tempDir, <span style="color: #009900;">&quot;myfeed&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> tempFileName <span style="color: #0000FF;">=</span> <span style="color: #800080; font-weight: bold;">GetFileInfo</span><span style="color: #0000FF;">&#40;</span>tempFile<span style="color: #0000FF;">&#41;</span>.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cftry</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffeed</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;read&quot;</span> <span style="color: #0000FF;">source</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;http://movies.msn.com/rss/topcelebs&quot;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;feedInStruct&quot;</span> <span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfcatch</span> any<span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfhttp</span> <span style="color: #0000FF;">url</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;http://movies.msn.com/rss/topcelebs&quot;</span> <span style="color: #0000FF;">path</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#tempDir#&quot;</span> <span style="color: #0000FF;">file</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#tempFileName#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
        <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfhttpparam</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;header&quot;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;Accept-Encoding&quot;</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;compress,gzip,deflate&quot;</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfhttp</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffeed</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;read&quot;</span> <span style="color: #0000FF;">source</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#tempFile#&quot;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;feedInStruct&quot;</span> <span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfcatch</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cftry</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #800080; font-weight: bold;">FileExists</span><span style="color: #0000FF;">&#40;</span>tempFile<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #800080; font-weight: bold;">FileDelete</span><span style="color: #0000FF;">&#40;</span>tempFile<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#feedInStruct#&quot;</span><span style="color: #0000FF;">&gt;</span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/08/using-cffeed-with-url-sending-compressed-content/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Finding Image Type for a file</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/06/finding-image-type-for-a-file/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/06/finding-image-type-for-a-file/#comments</comments>
		<pubDate>Fri, 08 Jun 2007 22:22:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/06/08/finding-image-type-for-a-file/</guid>
		<description><![CDATA[This is a follow up on my previous post on URLConnection APIsWe will use this API to find out the image type of a given file. Here is the udf.

&#38;lt;cffunction name=&#34;getMimeType&#34;&#38;gt;
    &#38;lt;cfargument name=&#34;filepath&#34;&#38;gt;
&#160;
    &#38;lt;cfset var urlConn = createObject(&#34;java&#34;, &#34;java.net.URLConnection&#34;)&#38;gt;
    &#38;lt;cfset var fileobj = fileopen(filepath, &#34;readbinary&#34;)&#38;gt;
  [...]]]></description>
			<content:encoded><![CDATA[<p>This is a follow up on my previous post on <a href="http://www.rupeshk.org/blog/index.php/2007/06/finding-mime-type-of-any-file">URLConnection APIs</a><br />We will use this API to find out the image type of a given file. Here is the udf.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;">&amp;lt;cffunction name=&quot;getMimeType&quot;&amp;gt;
    &amp;lt;cfargument name=&quot;filepath&quot;&amp;gt;
&nbsp;
    &amp;lt;cfset var urlConn = createObject(&quot;java&quot;, &quot;java.net.URLConnection&quot;)&amp;gt;
    &amp;lt;cfset var fileobj = fileopen(filepath, &quot;readbinary&quot;)&amp;gt;
    &amp;lt;!--- just read the first 20 characters of the file as thats sufficient ---&amp;gt;
    &amp;lt;cfset var bytes = fileread(fileobj, 20)&amp;gt;
    &amp;lt;cfset var istream = createObject(&quot;java&quot;, &quot;java.io.ByteArrayInputStream&quot;).init(bytes)&amp;gt;
    &amp;lt;cfset fileobj.close()&amp;gt;
    &amp;lt;cfreturn urlConn.guessContentTypeFromStream(istream)&amp;gt;
&amp;lt;/cffunction&amp;gt;
&amp;lt;cffunction name=&quot;GetImageType&quot;&amp;gt;
    &amp;lt;cfargument name=&quot;filepath&quot;&amp;gt;
    &amp;lt;cfset var mimetype = getMimeType(filepath)&amp;gt;
    &amp;lt;cfset var imagetype=&quot;&quot;&amp;gt;
    &amp;lt;cfif not isDefined(&quot;mimetype&quot;)&amp;gt;
        &amp;lt;cfthrow message=&quot;Not an Image file&quot;&amp;gt;
    &amp;lt;/cfif&amp;gt;
    &amp;lt;cfswitch expression=&quot;<span style="color: #0000FF;">#mimetype#</span>&quot;&amp;gt;
        &amp;lt;cfcase Value=&quot;image/gif&quot;&amp;gt;
            &amp;lt;cfset imagetype=&quot;gif&quot;&amp;gt;
        &amp;lt;/cfcase&amp;gt;
        &amp;lt;cfcase Value=&quot;image/x-bitmap&quot;&amp;gt;
            &amp;lt;cfset imagetype=&quot;bmp&quot;&amp;gt;
        &amp;lt;/cfcase&amp;gt;
        &amp;lt;cfcase Value=&quot;image/png&quot;&amp;gt;
            &amp;lt;cfset imagetype=&quot;png&quot;&amp;gt;
        &amp;lt;/cfcase&amp;gt;
        &amp;lt;cfcase Value=&quot;image/jpeg&quot;&amp;gt;
            &amp;lt;cfset imagetype=&quot;jpeg&quot;&amp;gt;
        &amp;lt;/cfcase&amp;gt;
        &amp;lt;cfcase Value=&quot;image/jpg&quot;&amp;gt;
            &amp;lt;cfset imagetype=&quot;jpeg&quot;&amp;gt;
        &amp;lt;/cfcase&amp;gt;
        &amp;lt;cfdefaultcase&amp;gt;&amp;lt;cfthrow message=&quot;Not an Image file&quot;&amp;gt;
        &amp;lt;/cfdefaultcase&amp;gt;
        &amp;lt;/cfswitch&amp;gt;
        &amp;lt;cfreturn imagetype&amp;gt;
&amp;lt;/cffunction&amp;gt;</pre></div></div>

<p>If I run the code below</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;">&amp;lt;cfset filepath = &quot;C:\temp\test.jpg&quot;&amp;gt;
&amp;lt;cfoutput&amp;gt;<span style="color: #0000FF;">#GetImageType<span style="color: #0000FF;">&#40;</span>filepath<span style="color: #0000FF;">&#41;</span>#</span>&amp;lt;/cfoutput&amp;gt;</pre></div></div>

<p>It nicely prints out &#8220;jpeg&#8221;.
<p>You should note that I used the new File IO function added in ColdFusion 8 using which I can read as many no of bytes from the file as I want. No more reading the entire file into memory. </p>
<p>More extensive post on File IO functions coming next !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/06/finding-image-type-for-a-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finding mime type of any file</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/06/finding-mime-type-of-any-file/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/06/finding-mime-type-of-any-file/#comments</comments>
		<pubDate>Fri, 08 Jun 2007 21:51:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/06/08/finding-mime-type-of-any-file/</guid>
		<description><![CDATA[Hemant recently told me about this cool API in java.net.URLConnection which can tell you the mime type for common file types.
guessContentTypeFromName(String name) &#8211; that simply checks the extension of the file name specified and gets the mime type from a static map it maintains. This can be very useful in many scenarios. The map it [...]]]></description>
			<content:encoded><![CDATA[<p>Hemant recently told me about this cool API in java.net.URLConnection which can tell you the mime type for common file types.
<p><b>guessContentTypeFromName(String name)</b> &#8211; that simply checks the extension of the file name specified and gets the mime type from a static map it maintains. This can be very useful in many scenarios. The map it uses is quite extensive and contains almost all the mime types.</p>
<p>But this has a problem. What if some one has renamed a gif to jpg? This method will say that the file is a jpg image whereas it was a gif.</p>
<p>Thankfully there is another method in the same class which can address this problem. </p>
<p><b>guessContentTypeFromStream(InputStream stream)</b> &#8211; which reads the stream, takes a look at the initial bits of the file and uses that to determine the file type.<br />This method does a check for the following mime types :-
<pre>- application/java-vm- application/x-java-serialized-object- text/html- application/xml- image/gif- image/x-bitmap- image/x-pixmap- image/png- image/jpeg- image/jpg- image/vnd.fpx- audio/basic- audio/x-wav</pre>
<p>Though this method leaves out many of other common types such as mp3, it is still very useful. This makes it so easy to find out the type of an image.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/06/finding-mime-type-of-any-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Use Java 5 feature in Jdk1.4</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/03/use-java-5-feature-in-jdk1-4/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/03/use-java-5-feature-in-jdk1-4/#comments</comments>
		<pubDate>Fri, 02 Mar 2007 19:50:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/03/02/use-java-5-feature-in-jdk1-4/</guid>
		<description><![CDATA[I have been getting the itch for a long tome to use Java 5 features like generics, new concurrency (java.util.concurrent), autoboxing, &#8220;for each&#8221; loop and many more. But sadly I can not do it at work because ColdFusion has to be supported on all application servers. Since many of the application servers are still on [...]]]></description>
			<content:encoded><![CDATA[<p>I have been getting the itch for a long tome to use Java 5 features like generics, new concurrency (java.util.concurrent), autoboxing, &#8220;for each&#8221; loop and many more. But sadly I can not do it at work because ColdFusion has to be supported on all application servers. Since many of the application servers are still on J2EE 1.4 and not all of them are compatible with Java 5, we have to write JDK1.4 compliant code.  Which means that all those wonderful features of Java5 can not be used for some more time to come. Or so I thought until yesterday when I read Brian Goetz&#8217;s (of &#8220;Java concurrency in practice&#8221; fame) <a href="http://www-128.ibm.com/developerworks/java/library/j-jtp02277.html?ca=drs-">article</a> which says that we can use some of the Java features in JDK1.4 also.<br />At first I was kind of puzzled as in how could that be possible? Did they had these features and they just did not mention about it in JDK1.4 or probably they just sneaked it in 1.4 in last moment but not talk about it because of some reasons.</p>
<p>It dawned to me when I went halfway through the article. It looks like most of these new syntaxes did not need any change in the bytecode instruction set which means that the compiler would generate the same bytecodes or instruction set for it as it would have for equivalent code without using these features. So it should be possible to use a class created by java5 compiler in 1.4. But don&#8217;t we get UnsupportedClassVersionError when we try to do that? </p>
<p>Here is the catch. Due to some reasons they had to change the class version and that is why this UnsupportedClassVersionError. However these guys added a flag to java compiler (which ofcourse is undocumented) that can allow this. You need to invoke javac with &#8220;-source 1.5 -target jsr14&#8243; option and this will generate 1.4 compatible bytecodes for java5 syntax. This means I can happily write my code using generics, autoboxing, for..each loop, compile it with java5 compiler with this flag and be assured that this can run on 1.4 VM also.</p>
<p>That leaves one more set of features i.e; new concurreny, which I really really like in java5 and want to use. Thankfully some guys at <a href="http://dcl.mathcs.emory.edu/">Distibuted Computing Lab at Emory University</a> have <a href="http://dcl.mathcs.emory.edu/util/backport-util-concurrent/">backported java5 concurrency</a> (java.uti.concurrent) features that can be used in java 1.3+. I have infact used it and it is awesome !</p>
<p>So most of the juicy stuffs that were added in java5 can run on 1.4. Off to checking it out little more. <img src='http://www.rupeshk.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/03/use-java-5-feature-in-jdk1-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java.net article on ColdFusion and JavaEE</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/03/java-net-article-on-coldfusion-and-javaee/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/03/java-net-article-on-coldfusion-and-javaee/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 15:12:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/03/01/java-net-article-on-coldfusion-and-javaee/</guid>
		<description><![CDATA[I found this interesting article by Kola Oyedeji on java.net. He talks about integration between ColdFusion and JavaEE and how this could be of interest to JSP developers.We in our team here have discussed it so many times that why can not ColdFusion be used by J2EE/JSP developers or enterprises that standardize on J2EE. ColdFusion [...]]]></description>
			<content:encoded><![CDATA[<p>I found this <a href="http://today.java.net/pub/a/today/2007/02/27/coldfusion-for-jsp-developers.html">interesting article</a> by Kola Oyedeji on <a href="http://java.net">java.net</a>. He talks about integration between ColdFusion and JavaEE and how this could be of interest to JSP developers.<br />We in our team here have discussed it so many times that why can not ColdFusion be used by J2EE/JSP developers or enterprises that standardize on J2EE. ColdFusion is a pure J2EE solution, runs on all the major applications servers, is so feature rich, so productive that it should be a no brainer for everyone to adapt this. And to this when I add the features that we are adding in Scorpio, it makes even more sense. I don&#8217;t think there is any other platform which can come even close to CF in terms of features and productivity.<br />At MAX, I had a conversation with one of our customers and he said something very interesting. His client wanted a pure J2EE solution and his competitors were asking for nearly 6 months to implement this. So this guy did everything in CF, bundled it as an ear (that can be deployed on application server) and shipped in flat one month !! And his client never ever thought that this was a CF application. <img src='http://www.rupeshk.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Good to see this kind of article on a java website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/03/java-net-article-on-coldfusion-and-javaee/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Accessing password protected URL</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/02/accessing-password-protected-url/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/02/accessing-password-protected-url/#comments</comments>
		<pubDate>Sun, 25 Feb 2007 20:12:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[cfdocument]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/02/25/accessing-password-protected-url/</guid>
		<description><![CDATA[How do you access a password protected URL (requiring basic authentication) programmatically? well, there are two ways

If you know that the URL is protected : This is the simple one. Since you know the URL is protected, add the required authorization header in the request and you are done. What if you are not making [...]]]></description>
			<content:encoded><![CDATA[<p>How do you access a password protected URL (requiring basic authentication) programmatically? well, there are two ways</p>
<ol>
<li>If you know that the URL is protected : This is the simple one. Since you know the URL is protected, add the required authorization header in the request and you are done. What if you are not making the socket connection but instead using java.net.URL or java.net.URLConnection to retrieve the content of url? You can use setRequestProperty() method of URLConnection to add any request header you want. So the code would look like

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>someurlstring<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">URLConnection</span> conn <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> encoding <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sun.<span style="color: #006633;">misc</span>.<span style="color: #006633;">BASE64Encoder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username:password&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
conn.<span style="color: #006633;">setRequestProperty</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Authorization&quot;</span>, <span style="color: #0000ff;">&quot;Basic &quot;</span> <span style="color: #339933;">+</span> encoding<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">InputStream</span> in <span style="color: #339933;">=</span> conn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
...</pre></div></div>

</li>
<li>What if you dont know in advance whether the URL is protected or not? You can not arbitrarily add the authorization heder for all the URLs. The answer to this is &#8220;java.net.Authenticator&#8221;. You need to install an instance of java.net.Authenticator using setDefault() method of Authenticator. Whenever URLConnection sees that the url is protected, it will use this installed authenticator to get the username and password and set the required authorization header automatically.<br />
Here is how the code snippet for doing this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Authenticator</span>.<span style="color: #006633;">setDefault</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyAuthenticator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
....
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAuthenticator <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Authenticator</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">PasswordAuthentication</span> getPasswordAuthentication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PasswordAuthentication</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span>, <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This particular technique is particularly useful for people who need a workaround for retrieving password protected images in cfdocument. Here is what you need to do.</p>
<ul>
<li>Use the source below to create Authenicator class

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAuthenticator <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Authenticator</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> user<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> passwd<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MyAuthenticator<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> user, <span style="color: #003399;">String</span> passwd<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">user</span> <span style="color: #339933;">=</span> user<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">passwd</span> <span style="color: #339933;">=</span> passwd<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">PasswordAuthentication</span> getPasswordAuthentication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PasswordAuthentication</span><span style="color: #009900;">&#40;</span>user, passwd.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Compile this and put MyAuthenticator.class in wwwroot/WEB-INF/classes.</li>
<li>Put the following code in your application.cfm or application.cfc

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> not <span style="color: #800080; font-weight: bold;">IsDefined</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;Application.authenticator&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
      <span style="color: #808080; font-style: italic;">&lt;!--- Do initializations ---&gt;</span>
      <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> authenticator<span style="color: #0000FF;">=</span> <span style="color: #800080; font-weight: bold;">createObject</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;java&quot;</span>, <span style="color: #009900;">&quot;java.net.Authenticator&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
      <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> myauthenticator <span style="color: #0000FF;">=</span> <span style="color: #800080; font-weight: bold;">createObject</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;java&quot;</span>, <span style="color: #009900;">&quot;MyAuthenticator&quot;</span><span style="color: #0000FF;">&#41;</span>.init<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;username&quot;</span>, <span style="color: #009900;">&quot;password&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
      <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> authenticator.setDefault<span style="color: #0000FF;">&#40;</span>myauthenticator<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
      <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #0000FF;">Application</span>.authenticator<span style="color: #0000FF;">=</span>myauthenticator<span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>replace &#8220;username&#8221; and &#8220;password&#8221; with actual username and password.</li>
<li>give the request for the cfdocument page for which you were getting the red-x for image. The images should appear this time.</li>
</ul>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/02/accessing-password-protected-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Mystery of &quot;Too many open files&quot;</title>
		<link>http://www.rupeshk.org/blog/index.php/2007/02/the-mystery-of-too-many-open-files/</link>
		<comments>http://www.rupeshk.org/blog/index.php/2007/02/the-mystery-of-too-many-open-files/#comments</comments>
		<pubDate>Mon, 05 Feb 2007 18:26:00 +0000</pubDate>
		<dc:creator>Rupesh Kumar</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rupesh106.wordpress.com/2007/02/05/the-mystery-of-too-many-open-files/</guid>
		<description><![CDATA[Last week we saw an interesting problem while running the regression tests on Linux. Immediately after the tests were started, the VM started throwing error &#8220;FileNotFoundException : Too many open files&#8221;. Ofcourse the files were there but the VM was trying to say that there are too many file descriptors open which were not closed. [...]]]></description>
			<content:encoded><![CDATA[<p>Last week we saw an interesting problem while running the regression tests on Linux. Immediately after the tests were started, the VM started throwing error <b>&#8220;FileNotFoundException : Too many open files&#8221;</b>. Ofcourse the files were there but the VM was trying to say that there are too many file descriptors open which were not closed. It was hitting the open file limit set by the OS. This was kind of absurd because we always close all the files that we open. And moreover we had never seen this problem before. So we started suspecting 1.6 VM on which we were running it.</p>
<p>Immediately after this, it got worse. The exception changed into <b>&#8220;SocketException: Too many open files&#8221;</b> and all the socket connection started getting rejected. So merely after serving 100 requests, the server was down to its knees. </p>
<p>A quick google search suggested to increase the open file descriptor limit on the machine. </p>
<p>&#8220;ulimit -aH&#8221; that gives the max limit for number of open files returned 1024.</p>
<p>I added every possible way to increase it. Here are few</p>
<p>1. In /etc/security/limits.conf <br />*       soft    nofile  1024<br />*       hard    nofile  65535<br />2. Increase ulimit by &#8220;ulimit -n 65535&#8243;<br />3. echo 65535 &gt; /proc/sys/fs/file-max<br />4. In /etc/sysctl.conf <br />fs.file-max=65535</p>
<p>Increasing file descriptor limit did not help much either. Even after increasing this limit, we were still getting this error.And then <b>Sanjeev</b> (another brilliant CF engineer with an amazing sense of humour) cracked it !!<br />Just before we started getting these errors, there was another error which I had overlooked assuming it was test problem which infact was the clue.<br />The error was something like<br />coldfusion.jsp.CompilationFailedException: Errors reported by Java compiler: error: error reading /opt/coldfusionscorpio/lib/./././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././././././././././.<br />/././././././././././././././././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././././././././././.<br />/././././././././././././././././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././././././././././.<br />/././././././././././././././././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././././././././././.<br />/././././././././././././././././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././././././././././.<br />/././././././././././././././././././././././././././././././././././././././././././<br />././././././././././././././././././././././././././././././././././&#8230;.<br /> at coldfusion.jsp.JavaCompiler.compileClass(JavaCompiler.java:138)</p>
<p>Sanjeev ran it through the debugger and he nailed the culprit. It was something which no one had even suspected. It was javac. <img src='http://www.rupeshk.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />We had hit upon a Sun&#8217;s bug in javac where if the classpath contains a jar which has a manifest and manifest contains classpath entry with relative paths of other jars as well as path to itself, javac goes in an infinite loop. (Sun&#8217;s bug no  <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6400872">6400872</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6446657">6446657</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6456960">6456960</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6485027">6485027</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206485">6206485</a>)</p>
<p>I dont have the source for javac to pin point what exactly it did, but probably it kept opening all the entries in the classpath and because of the stack overflow it could not close those files &#8211; hence reaching the max limit of open file descriptors. This was a third party jar that had this manifest entry. Once we found the problem, the solution was simple &#8211; just remove the classpath entry from the manifest! Whew !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rupeshk.org/blog/index.php/2007/02/the-mystery-of-too-many-open-files/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
