<?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>Elliott&#039;s Development Blog &#187; Programming</title>
	<atom:link href="http://www.elliottsprehn.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elliottsprehn.com/blog</link>
	<description>Exploring Life Through Math, Algorithms and Code</description>
	<lastBuildDate>Mon, 30 Aug 2010 17:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Trust Your API (Railo Caching Silliness)</title>
		<link>http://www.elliottsprehn.com/blog/2010/08/30/trust-your-api-railo-caching-silliness/</link>
		<comments>http://www.elliottsprehn.com/blog/2010/08/30/trust-your-api-railo-caching-silliness/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 04:46:49 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Railo]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=139</guid>
		<description><![CDATA[One of the biggest problems rampant amongst developers is ignoring the standard library and writing your own solution to the same problem because we think it&#8217;s faster/smarter/better. I know I&#8217;m certainly guilty of this thinking too. Unfortunately this is pretty much always a mistake. I recently stressed this issue quite a lot in my Designing [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest problems rampant amongst developers is ignoring the standard library and writing your own solution to the same problem because we think it&#8217;s faster/smarter/better. I know I&#8217;m certainly guilty of this thinking too. Unfortunately this is pretty much always a mistake. I recently stressed this issue quite a lot in my <a href="blog/presentations/designing-scalable-and-creative-algorithms/">Designing Scalable and Creative Algorithms</a> presentation showing how using built in CF features instead of writing your own algorithm (even if theoretically more efficient) often provides significant performance improvements.</p>
<p>A good example I&#8217;ve found of this problem is the Railo railo.runtime.op.Constants class in the Railo runtime source. This class contains a cache for java.lang.Integer instances and methods for getting them from primitive values. It also contains named constants for the values up to 10 and creates the values manually up to 99 and puts them in an array. Another method is provided that converts boolean primitive values to java.lang.Boolean instances.</p>
<p>The sentiment behind this class makes sense. It reduces the number of Integer instances that need to be created and reduces the amount of garbage being generated by the program.</p>
<p>The problem here is that both of these methods implement a feature that already exists in the Java API. Moreover since the method is part of the API it&#8217;ll likely get compiled to native code much faster.</p>
<p>Had the Railo developers trusted the API (and read the documentation) they would have used Integer.valueOf(int) and Boolean.valueOf(boolean). Both of these standard API methods use a cache, and the Integer cache provided in the standard library caches values from -128 to 127 (as required by the <a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7">Java Language Specification</a>) providing a much more expansive cache than the 0 to 99 values the Railo developers implemented.</p>
<p>So back to what I said in my presentation: <strong>Trust the API: It is Faster!</strong></p>
<p><a href="https://jira.jboss.org/browse/RAILO-934">Railo Bug Ticket 934</a></p>
<p><strong>Note:</strong></p>
<p>It should be noted that Railo supports Java 1.4+ and the cache is only present in 1.5+ so using their own cache in Railo does make some sense, but their use of new Integer() instead of Integer.valueOf() and only caching values of 0-99 means you never get the benefit from Java 1.5+</p>
<p><strong>Second Note:</strong></p>
<p>Apparently this has been fixed in bleeding edge Railo, though the source bundle and the SVN repo haven&#8217;t been updated. For anyone else who wondered, they keep that source on <a href="http://github.com/getrailo/railo">Git Hub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2010/08/30/trust-your-api-railo-caching-silliness/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Prototypal Inheritance in ColdFusion</title>
		<link>http://www.elliottsprehn.com/blog/2010/03/24/using-prototypal-inheritance-in-coldfusion/</link>
		<comments>http://www.elliottsprehn.com/blog/2010/03/24/using-prototypal-inheritance-in-coldfusion/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 04:57:21 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=94</guid>
		<description><![CDATA[Ben Nadel was recently looking at Prototypal Inheritance in ColdFusion, which is incidentally one of the topics I talk about in my I bet you didn&#8217;t know you could do that with ColdFusion presentation, which I gave at BFusion 2008 and will be doing an updated version of at CFUnited 2010. &#60;/shameless-plug&#62; The code samples [...]]]></description>
			<content:encoded><![CDATA[<p>Ben Nadel was recently looking at <a href="http://www.bennadel.com/blog/1881-Faking-Prototypal-Inheritance-In-ColdFusion-Components.htm">Prototypal Inheritance in ColdFusion</a>, which is incidentally one of the topics I talk about in my <a href="http://www.elliottsprehn.com/blog/presentations/i-bet-you-didnt-know-you-could-do-that-with-coldfusion/">I bet you didn&#8217;t know you could do that with ColdFusion</a> presentation, which I gave at BFusion 2008 and will be doing an updated version of at <a href="http://cfunited.com/2010/">CFUnited 2010</a>. &lt;/shameless-plug&gt;</p>
<p>The code samples have a Prototype.cfc and some examples that use complete objects as the prototype like with JS.</p>
<pre class="brush:coldfusion">
user = createObject("component","User").init();
admin = createObject("component","Admin").init();
admin.setPrototype(user);

me = admin.new();
me.setName("Elliott Sprehn");

// Somewhere off in plugin land...
function toXML() {
	return "
		<user id='#this.getId()#'>
			<name>#this.getName()#</name>
		</user>";
}
user.toXML = toXML;

// In the application's code
writeOutput( me.toXML() );
/*
	<user id="1">
		<name>Elliott Sprehn</name>
	</user>
*/
</pre>
<p>One reason I really like this is that it lets you arbitrarily extend the model in plugins without impacting object creation performance.</p>
<p>There&#8217;s a tendency to go crazy with the Factory pattern and then broadcast lots of events (transfer takes this approach with actionAfterNewTransferEvent and friends). Unfortunately it makes object creation slower and slower since each create has to notify more and more listeners.</p>
<p>The Eclipse IDE ran into this problem and called it the &#8220;event storm&#8221; where a single mouse click might fire thousands of events and make the whole IDE seem slow.</p>
<p>Using prototypal inheritance we can pre-compute the entire prototype chain and all the complex object state so that each object create only needs one extra setPrototype().</p>
<p>Thinking outside the traditional class based object approach can solve many future problems and make your life easier. Whether it be by using function pointers creatively or something even less conventional like prototypal inheritance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2010/03/24/using-prototypal-inheritance-in-coldfusion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ColdFusion Mappings and Relative Paths</title>
		<link>http://www.elliottsprehn.com/blog/2010/02/19/coldfusion-mappings-and-relative-paths/</link>
		<comments>http://www.elliottsprehn.com/blog/2010/02/19/coldfusion-mappings-and-relative-paths/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 09:56:57 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=69</guid>
		<description><![CDATA[Recently one of our applications started failing inconsistently with strange errors about certain files not existing. We&#8217;d see error emails about things like the /myapplication/views/pages/survey.cfm not existing which didn&#8217;t make any sense since /myapplication is a mapping created inside the Application.cfc &#60;cfset this.mappings["/myapplication"] = expandPath("../")&#62; See the bug there? ExpandPath() is relative to the requested [...]]]></description>
			<content:encoded><![CDATA[<p>Recently one of our applications started failing inconsistently with strange errors about certain files not existing. We&#8217;d see error emails about things like the <code>/myapplication/views/pages/survey.cfm</code> not existing which didn&#8217;t make any sense since /myapplication is a mapping created inside the Application.cfc</p>
<pre class="brush: coldfusion">
&lt;cfset this.mappings["/myapplication"] = expandPath("../")&gt;
</pre>
<p>See the bug there? ExpandPath() is relative to the requested template. This application had previously had all requests routed through <code>/public/index.cfm</code> so the mapping worked fine.</p>
<p>However, recently we had added some new web services like <code>/public/services/ScheduleService.cfc</code> and now whenever an Ajax request went through the CF mapping would change from pointing to <code>/</code> to <code>/public</code> causing all other concurrent requests to fail with confusing missing file errors.</p>
<p>I fixed this by making sure the path was no longer relative.</p>
<pre class="brush: coldfusion">
&lt;cfset this.mappings["/myapplication"] =
   getDirectoryFromPath(getCurrentTemplatePath())&gt;
</pre>
<p>So the moral of the story is to <strong>NEVER</strong> use expandPath() to create a mapping that&#8217;s relative to the webroot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2010/02/19/coldfusion-mappings-and-relative-paths/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ColdFusion arguments.callee</title>
		<link>http://www.elliottsprehn.com/blog/2009/07/16/coldfusion-argumentscallee/</link>
		<comments>http://www.elliottsprehn.com/blog/2009/07/16/coldfusion-argumentscallee/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 17:33:48 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/2009/07/16/coldfusion-argumentscallee/</guid>
		<description><![CDATA[Last night Ben Nadel sent me an email asking if there was any way to get the currently executing function so you could get the metadata from it. &#60;cffunction name="test" myAttribute="1"&#62; &#60;--- How can we get the myAttribute value? ---&#62; &#60;/cffunction&#62; The first obvious attempt at this is to use getMetaData(test).myAttribute, and that works fine [...]]]></description>
			<content:encoded><![CDATA[<p>Last night Ben Nadel sent me an email asking if there was any way to get the currently executing function so you could get the metadata from it.</p>
<pre class="brush: coldfusion">&lt;cffunction name="test" myAttribute="1"&gt;
  &lt;--- How can we get the myAttribute value? ---&gt;
&lt;/cffunction&gt;</pre>
<p>The first obvious attempt at this is to use <code>getMetaData(test).myAttribute</code>, and that works fine until you pass the function as a pointer and then it&#8217;s not called <em>test</em> anymore. Instead we need a different way to get the current function.</p>
<p>I had looked into this for implementing closures some time ago, and even talked about this at BFusion 2008. My original workaround was to create an API around the function pointer. For example <em>executeFunction</em>(func) that passes the function pointer in as an argument. Unfotunately, this also means you can&#8217;t just pass this function around transparently to anything that expects a function pointer.</p>
<p>Last night, however, I had a eureka moment and figured this one out. To get a reference to the current function we&#8217;re going to harness exceptions and the information we can get from the stack.</p>
<pre class="brush: coldfusion">&lt;cffunction name="getStackFunction" access="public" returntype="any" output="false"&gt;
   &lt;cfargument name="name" type="string" required="true"&gt;
   &lt;cfargument name="depth" type="numeric" required="false" default="1"&gt;

   &lt;cfset var TemplateClassLoader = createObject("java","coldfusion.runtime.TemplateClassLoader")&gt;
   &lt;cfset var servletContext = getPageContext().getServletContext()&gt;
   &lt;cfset var templatePath = ""&gt;
   &lt;cfset var TemplateClass = ""&gt;
   &lt;cfset var field = ""&gt;

   &lt;cftry&gt;
      &lt;cfthrow type="Exception"&gt;

   &lt;cfcatch type="any"&gt;
      &lt;cfset templatePath = cfcatch.tagContext[depth+1].template&gt;
   &lt;/cfcatch&gt;
   &lt;/cftry&gt;

   &lt;cfset TemplateClass = TemplateClassLoader.findClass(servletContext,templatePath)&gt;
   &lt;cfset field = TemplateClass.getDeclaredField(arguments.name)&gt;
   &lt;cfset field.setAccessible(true)&gt;

   &lt;cfreturn field.get(TemplateClass.newInstance())&gt;
&lt;/cffunction&gt;</pre>
<p>We can then use this code with the (<strong>case sensitive</strong>) name of the function in the current stack to get a pointer to that function.</p>
<pre class="brush: coldfusion">&lt;cffunction name="test" myAttribute="1" output="false"&gt;
   &lt;cfargument name="x" type="numeric"&gt;
   &lt;cfset arguments.callee = getStackFunction("test")&gt;
   &lt;cfreturn arguments.x + getMetaData(arguments.callee).myAttribute&gt;
&lt;/cffunction&gt;</pre>
<p>You can use the depth parameter to get functions farther down the stack as well.</p>
<p>I&#8217;ll go over how to build proper closure constructs using this technique, and some other novel ones that don&#8217;t even require runtime magic in an upcoming post.</p>
<p>Oh, and hats off to Ben for sparking my interest in this issue again! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2009/07/16/coldfusion-argumentscallee/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>cfimage and ImageScaleToFit on Large Images Pegs CPU</title>
		<link>http://www.elliottsprehn.com/blog/2009/01/14/cfimage-and-imagescaletofit-on-large-images-pegs-cpu/</link>
		<comments>http://www.elliottsprehn.com/blog/2009/01/14/cfimage-and-imagescaletofit-on-large-images-pegs-cpu/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 11:05:07 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/2009/01/14/cfimage-and-imagescaletofit-on-large-images-pegs-cpu/</guid>
		<description><![CDATA[I was working on the new profile picture feature of the registration and CFUnited 2009 website when I ran into an issue where a user uploaded a 2112 x 2816 (2MB) photo. This seemed to cause file locking issues in cfimage which I was told were addressed by CF 8.0.1 Cumulative Hot Fix 1. I [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on the new profile picture feature of the registration and CFUnited 2009 website when I ran into an issue where a user uploaded a 2112 x 2816 (2MB) photo. </p>
<p>This seemed to cause file locking issues in cfimage which I was told were addressed by CF 8.0.1 Cumulative Hot Fix 1. I applied the hot fix and everything seemed to work, but then when I started testing with the large file again the CPU would jump to 100% and the request would never seem to finish. I tried all kinds of things, and nothing I did seemed to make a difference.</p>
<p>Finally I tried the &#8220;highestPerformance&#8221; interpolation and the requests started finishing hundreds of times faster. Any other interpolation algorithm seems to cause the thread to lock up. </p>
<p>I&#8217;ve fixed this in our code with:</p>
<pre class="brush: coldfusion">&lt;!---
	CF8 will peg the CPU to 100% and this thread will seem
	to hang for minutes on large images if we don't choose
	highestPerformance.
---&gt;
&lt;cfif getSize("kilobytes") gt 375&gt;
	&lt;cfset arguments.interpolation = "highestPerformance"&gt;
&lt;/cfif&gt;
</pre>
<p>What&#8217;s most odd is that the issue only appears on Windows on the production server. On my Macbook I can choose a different algorithm and get a nice resize without hanging the request.</p>
<p>Production server is Java 1.6 u11, CF8.0.1 HF2, Windows Server 2003</p>
<p>Anyone have any ideas?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2009/01/14/cfimage-and-imagescaletofit-on-large-images-pegs-cpu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ColdFusion 8 Exception Handling Breaks HTTP Requests</title>
		<link>http://www.elliottsprehn.com/blog/2007/08/01/coldfusion-8-exception-handling-breaks-http-requests/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/08/01/coldfusion-8-exception-handling-breaks-http-requests/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 06:52:04 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/2007/08/01/coldfusion-8-exception-handling-breaks-http-requests/</guid>
		<description><![CDATA[Just found this bug today&#8230;. So CF8 outputs the cfcatch.message into the Reason-Phrase portion of the HTTP Response, however it does not strip new lines (LF or CR). A web server, however, should never send new lines in the Reason-Phrase [1], and should probably be truncating that error message at a certain length. [1] RFC2616, [...]]]></description>
			<content:encoded><![CDATA[<p>Just found this bug today&#8230;.  </p>
<p>So CF8 outputs the cfcatch.message into the Reason-Phrase portion of the HTTP Response, however it does not strip new lines (LF or CR). A web server, however, should never send new lines in the Reason-Phrase [1], and should probably be truncating that error message at a certain length. </p>
<p>[1] <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html">RFC2616, Section6, HTTP Response</a></p>
<p>It&#8217;s pretty easy to reproduce this bug:</p>
<p><code>&amp;lt;cfthrow message="foo #chr(10)##chr(10)##chr(10)# bar"&amp;gt;</code></p>
<p>Another way to show this is with the new deserializeJSON() function in CF8 when the JSON is not valid. CF outputs the exception message with the JSON into the Reason-Phrase portion of the HTTP response Status-Line without stripping out new lines.</p>
<h3>ColdFusion Code</h3>
<pre class="brush: js">&lt;cfset json = '
{
    "foo": [
        {}
        "",
        {
            "f": {}
        }
    ]
}
'&gt;

&lt;cfset deserializeJSON(json)&gt;</pre>
<p>And the server responds with:</p>
<h3>HTTP Response</h3>
<pre class="brush: plain">HTTP/1.1 500 JSON parsing failure: Expected ',' or ']' at character 20:'"' in {
	"foo": [
		{}
		"",
		{
			"f": {}
		}
	]
}
Date: Wed, 01 Aug 2007 05:31:39 GMT
Server: Apache/1.3.33 (Darwin) mod_fastcgi/2.4.2 PHP/5.2.0 JRun/4.0
server-error: true
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

&lt;!-- " ---&gt;&lt;/TD&gt;&lt;/TD&gt;&lt;/TD&gt;&lt;/TH&gt;&lt;/TH&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;/TR&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;/TABLE&gt;</pre>
<p>As it stands now, if you had <strong>100</strong> lines of JSON and there&#8217;s an error at the end, CF will dump <strong>all previous lines</strong> of JSON into the http Reason-Phrase.</p>
<p>This is particularly apparent in Safari (and WebKit based browsers) where it actually displays the HTTP headers in the body of the page because it sees new lines and assumes the HTTP headers are complete, and worse in Gecko based browsers that render the page as text/plain because the Content-Type header is never processed!</p>
<p>It should also be noted that CF7 output &#8220;Internal Server Error&#8221; for the Reason-Phrase instead of the exception message.</p>
<p>There also seems to be some other random junk thrown into the page when an exception is thrown&#8230;.</p>
<pre class="brush: coldfusion">foo bar baz&lt;cfthrow message="foo #chr(10)##chr(10)#bar"&gt;</pre>
<p>Will generate the following right after the http headers:</p>
<pre class="brush: plain">b
foo bar baz
1f27
</pre>
<p>I hope this saves someone some time trying to figure out what&#8217;s going on on their code! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/08/01/coldfusion-8-exception-handling-breaks-http-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the Expected Results for GetCurrentTemplatePath() in a Custom Tag.</title>
		<link>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 21:43:39 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/</guid>
		<description><![CDATA[While working on the template system used for the conference websites I ran across a problem where I needed the path to the template that called a custom tag. The first thing I tried was getCurrentTemplatePath() thinking that it might return that since the documentation makes no mention of custom tags. Instead, however, the function [...]]]></description>
			<content:encoded><![CDATA[<p>While working on the template system used for the <a href="http://cfunited.com">conference websites</a> I ran across a problem where I needed the path to the template that called a custom tag. The first thing I tried was getCurrentTemplatePath() thinking that it might return that since the documentation makes no mention of custom tags. Instead, however, the function returns the path to the custom tag itself.</p>
<p><a href="http://www.bennadel.com/">Ben Nadel</a> noticed some of this <a href="http://www.bennadel.com/blog/828-Dynamic-And-Unexpected-ColdFusion-GetCurrentTemplatePath-Behavior.htm">odd behavior</a> as well.</p>
<p>I spent a long time trying to figure out how to get the caller template path, including what Ben did which was to add a special function to the caller scope.</p>
<pre class="brush: coldfusion">&lt;cfscript&gt;
function getCallerTemplatePath() {
    return getCurrentTemplatePath();
}
caller.getCallerTemplatePath = getCallerTemplatePath;
path = caller.getCallerTemplatePath();
&lt;/cfscript&gt;</pre>
<p>This doesn&#8217;t work though. Instead I still got the template path of the custom tag!</p>
<p>I dug around in the PageContext (which is returned from getPageContext() if you&#8217;re not familiar) with no luck and finally gave up resorting to this&#8230;</p>
<pre class="brush: js">/**
    Monumental hack, but the only way I could figure out how to do
    a getCurrentTemplatePath() like call that resolves to the page
    that called this custom tag.
*/
function getCallerTemplatePath() {
    try {
       error;
    } catch( any cfcatch ) {
        return cfcatch.tagContext[3].template;
    }
}</pre>
<p>Which worked but really felt like a hack since it means throwing an exception on every request. So I kept an eye out as I dug around in the internals of the CF engine for various other things, and today I was rewarded with an awesome solution.</p>
<pre class="brush: js">/** Gets the path to the page that called this custom tag. */
function getCallerTemplatePath() {
    var field = getMetaData(caller).getDeclaredField("pageContext");
    field.setAccessible(true);
    return field.get(caller).getPage().getCurrentTemplatePath();
}</pre>
<p>Now to get at why and how this kind of thing works&#8230;</p>
<p>Inside the ColdFusion runtime the foundation unit for all scripts, components and tags is the <code>coldfusion.runtime.CFPage</code> object, and the getCurrentTemplatePath() function is really identical to&#8230;</p>
<pre class="brush: js">function getCurrentTemplatePath() {
    return getPageContext().getPage().getCurrentTemplatePath();
}</pre>
<p>After realizing this it dawned on me that the custom tags, cfcs, and pages all have their own PageContext and Page objects, and as such the template path is going to be different, or rather bound, to the page in which the function is called from, not where it&#8217;s defined.</p>
<p>Knowing this I was able to grab the page context out of the caller scope, which is the page context of the caller, and not the current page, and use that to get the current template path of the page for which that page context operates.</p>
<p>Also, for those who aren&#8217;t familiar, the getMetaData() function can be used to return the java.lang.Class instance for most objects you wouldn&#8217;t normally be able to call getClass() on in ColdFusion. For instance you can call <code>getMetaData(variables).getName()</code> and you&#8217;ll get <code>coldfusion.runtime.VariableScope</code>.</p>
<p>Doing this really made my code feel less icky, so I hope this is useful to someone else.</p>
<p>(PS, Tested and works on CF6+ and CF7+, anyone have CF8?)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>I Object!</title>
		<link>http://www.elliottsprehn.com/blog/2007/02/26/i-object/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/02/26/i-object/#comments</comments>
		<pubDate>Mon, 26 Feb 2007 08:15:02 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=3</guid>
		<description><![CDATA[While doing some casual web surfing I came across a rather interesting blog entry about Ruby&#8217;s types and looping. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here: One reason I think methods like this are great is that Ruby is intended to be read! Which [...]]]></description>
			<content:encoded><![CDATA[<p>While doing some casual web surfing I came across a rather interesting blog entry about <a href="http://www.ditchnet.org/wp/2007/02/07/3times-erh-links/">Ruby&#8217;s types and looping</a>. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here:</p>
<p>One reason I think methods like this are great is that Ruby is intended to be <strong>read</strong>! Which actually makes 5.times(&#038;block) make much more sense than a C style 3 part for loop (that&#8217;s where it came from, not Java).</p>
<p><code>5.times do |i|; end</code> can be read as &#8220;5 times do this&#8221; or better &#8220;do this 5 times&#8221;</p>
<p>for on the other hand looks more like: <code>for( i=0; i &amp;lt; 5; i++ ) {}</code> which has no such linear meaning, &#8220;for set i equal to 0, i less than 5, i plus one do this&#8221;, its all out of order. At best you can rearrange it in your head as &#8220;set i to 0, while i is less than 5 do do this, add one to i after each iteration&#8221;</p>
<p>for() is also prone to error with the condition operator, was that supposed to be &lt; or &lt;= ? Plenty of applications have had bugs because of this, and where something loops to isn&#8217;t always clear. If you really don&#8217;t like 5.times, there&#8217;s other methodologies though, in fact you can use a &#8216;for&#8217; loop:</p>
<pre class="brush: ruby">for i in (0...5); end</pre>
<p>Read as &#8220;For i in the range of [0, 5) do this&#8221;</p>
<pre class="brush: ruby">(0...5).each do |i|; end</pre>
<p>Read as &#8220;for each in the range [0, 5) do this&#8221;</p>
<pre class="brush: ruby">0.upto(4) do |i|; end</pre>
<p>Again it reads linearly, from &#8220;from 0 up to 4 do this&#8221;.</p>
<p>I definitely think this makes Ruby more OO; Java suffers quite a lot from the distinction between primitive types and regular types. Want to design a collection in Java and store both objects and primitive types? You can&#8217;t declare:</p>
<pre class="brush: java">
class Collection&lt;T&gt; {
...
     public void add( T elem );
...
}
</pre>
<p>Instead you need to declare methods for each primitive type, and that method. This bloats Java types with many extra methods, or requires object wrappers that are a nasty hit on performance (something that&#8217;s totally unnecessary with a decent compiler). Other languages deal with this much more elegantly; by making numbers objects all you need to declare is the method that accepts the type T.</p>
<p>The Java API is greatly bloated because of this. Look at java.util.Arrays which has 9 methods to join() an array, one for each primitive type, and another for Object because of the Object#toString() and String.valueOf() distinction, even the String.valueOf() method has 9 signatures to deal with this limitation.</p>
<p>In ruby to get a string I can call to_s on *any* object, there is no null that causes exceptions, or special case primitives. I think Ruby unified all these types into a single Object hierarchy quite elegantly. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/02/26/i-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.elliottsprehn.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=34552&rnd=1208783982" /></channel>
</rss>
