18. February 2010 13:26 by Markus Wollny

The real Y2K problem: LongInt Unix-Timestamps

18
Feb/10
0

On January 19th 2038 I’ll be 63 years, 9 months and 23 days old. So unfortunately there are still a couple of days until I can think about retirement. What’s wrong with this date?

The Unix timestamp of 2038-01-19 03:14:07 is 2147483647. This is the maximum number that fits into the int4 data type. One second later we’ll be getting integer overflow for any operations on Unix timestamps. Like getting the actual date from that Unix timestamp via dateAdd() in ColdFusion.

17. February 2010 10:27 by Markus Wollny

Death to isDefined()

17
Feb/10
0

Ever come across a couple of lengthy stack traces in your ColdFusion logfile that start with something like “Error in blah – java.net.SocketException: Broken pipe”? Here’s what that’s about:

While isDefined() may be a convenient way to check the existence of a certain variable, you just should avoid it at all cost and use structKeyExists([SCOPE],”[variableName]“) instead.

Not only will structKeyExists() be much faster than isDefined(), as you’ve told ColdFusion where to look for your variable, so it doesn’t have to check through all available scopes. You’ll also avoid a lot of useless chatter clogging up your server’s log files.

Say you’re looking for a variable which at this point is in fact not defined in any of the available scopes and you’re using evil isDefined() to do the job. ColdFusion will scan all the available scopes and eventually it would check the CGI-scope, too. And here’s our problem: If the original connection from browser to webserver has already been closed in the meantime, which may happen for various reasons, the JRun connector (i.e. the bit that does the communication between webserver and the Coldfusion Application Server) will obviously fail to read the HTTP connection header and tell that tale in the log file in quite excessive detail, full stack trace and everything, although you really couldn’t care less about it.

So in essence: Just pretend you never heard of isDefined(). structKeyExists() ist so much nicer and will give you a warm fuzzy feeling all over every time you use it.

4. February 2010 18:51 by Markus Wollny

A little ReReplace CFConfusion (solved)

4
Feb/10
0

The following two snippets are only different in regard to the iSomeVal string which is used in a Regex replacement string. The first is a string that starts with a character (‘fortytwo’) the second is a string that starts with a number (‘42′). The first example is working fine, whereas the second is somewhat lacking something quite essential:

<cfscript>
 variables.strSource = 'The answer is: Hmm.';
 variables.iSomeVal = 'fortytwo';
 variables.strTarget = ReReplace(variables.strSource,'^(.*:\s).*$','\1'&variables.iSomeVal,'ALL');
 writeOutput('~~' & variables.strTarget & '~~');
</cfscript>

Output: ~~The answer is: fortytwo~~

<cfscript>
 variables.strSource = 'The answer is: Hmm.';
 variables.iSomeVal = '42';
 variables.strTarget = ReReplace(variables.strSource,'^(.*:\s).*$','\1'&variables.iSomeVal,'ALL');
 writeOutput('~~' & variables.strTarget & '~~');
</cfscript>

Output: ~~The answer is:~~

WTF?

A few brain processor cycles later you realize what’s going on: The replacement string now reads ‘\142′ – and there’s no back reference with that number.

So we need a little ugly trick because slash-escaping won’t help us here either. We’ll use the \u operator which usually would indicate to uppercase the following character (which in our case is an integer, so this doesn’t harm us):

<cfscript>
 variables.strSource = 'The answer is: Hmm.';
 variables.iSomeVal = '42';
 variables.strTarget = ReReplace(variables.strSource,'^(.*:\s).*$','\1\u'&variables.iSomeVal,'ALL');
 writeOutput('~~' & variables.strTarget & '~~');
</cfscript>

Output: ~~The answer is: 42~~

Now in this case we’re lucky. Any suggestions on what could be done if we don’t actually know if the variable replacement bit after the first back reference would begin with a character or a number? I don’t want to hack around it with a separating space or something similar – which could of course be removed in a second pass, but this doesn’t seem elegant… Feels like I’m missing something extremely obvious here…

Update: Seems like I’m not missing anything ColdFusion-wise. There’s actually a RegEx feature described on regular-expression.info as ‘$10 through $99 treated as $1 through $9 (and a literal digit) if fewer than 10 groups’. There’s no clue as to the implementation in ColdFusion here, but judging from what I’ve seen in my example code, I’d say it’s fair to assume that CF does not deliver the desired result in this category. But alas, all is not lost as we’re running on top of Java, which I cannot ever shout out happily quite often enough under such circumstances. For behold:

<cfscript>
 variables.objRegex = createObject('component','JavaRegExp');
 variables.strSource = 'The answer is: Hmm.';
 variables.iSomeVal = '42';
 variables.strTarget = variables.objRegex.regExpReplace('^(.*:\s).*$',variables.strSource,'$1'&variables.iSomeVal,true);
 writeOutput('~~' & variables.strTarget & '~~');
</cfscript>

Output: ~~The answer is: 42~~

Yay! This snipped doesn’t use ReReplace but the Java RegEx Component by massimocorner.com I mentioned in an earlier post UDF to strip certain chars, but leave UBB tags alone.

21. January 2010 16:02 by Markus Wollny

Using client-side caching in ColdFusion

21
Jan/10
0

The cheapest request you can think of is the one where you actually don’t need to provide an answer – because you have answered it before and you know that facts haven’t changed. Now server-side caching is one approach, but in that case you just save yourselves the actual computing, you still have to retrieve your answer from the cache and deliver it to the client. Client-side caching is much more elegant.

11. September 2009 17:13 by Markus Wollny

ColdFusion UDF to test if a Java Class implements a method

11
Sep/09
0

I recently started implementing a couple of our full text search requirements using Sphinx. I am extremely happy with this search engine, as it’s lightning fast and provides some quite easy integration with the data we store in our PostgreSQL databases, is highly scalable and fairly easy to implement in ColdFusion via the Sphinx Client API. 

9. September 2009 9:40 by Marc

FYI: Function returning Void kills Variables

9
Sep/09
1

I recently discovered some interesting behaviour in Coldfusion. If you ask for a result of a function in a variable and you set returntype void, the variable is destroyed and cant be found after this.

Example:

<cffunction name="getSomething" returntype="void">
 
     Do something here, like updating a database 
     or increasing a counter value
 
</cffunction>
 
<cfset VARIABLES.RecieveSomething = "">
 
<cfset VARIABLES.RecieveSomething= getSomeThing()>
 
<cfif IsDefined("VARIABLES.RecieveSomething")>
     still there
<cfelse>
     cant found this variable anymore
</cfif>

Returntype Void is kinda tricky, so use it well-considered.

4. September 2009 11:59 by Markus Wollny

UDF to grab a frame from an FLV to JPG

4
Sep/09
0

This requires FFMPEG to be installed on your server. Here’s the UDF:

<cffunction name="flvgrabber" access="public" output="no" returntype="void" hint="grabs a frame from an FLV at a specified second and renders it as a JPG">
	<cfargument name="strPathToFLV" type="string" required="yes" hint="absolute path to the source flv">
	<cfargument name="strPathToJPG" type="string" required="yes" hint="absolute path to the target JPG; if file exists and is writeable, it will be overwritten">
	<cfargument name="strFrameAtTime" type="string" required="no" default="00:00:05" hint="time at which frame should be grabbed in format hh:mm:ss">
	<cfscript>
		var strTMPPath = '/tmp/';
		var strUniqueFname = CreateUUID();
		var strPathToFFMPEG = '/usr/bin/ffmpeg';
		var strArguments = '';
		var qTempFile = '';
	</cfscript>
 
	<cfif not DirectoryExists(getDirectoryFromPath(arguments.strPathToJPG))>
		<cfthrow message="target directory does not exist">
	</cfif>
	<cfif not FileExists(arguments.strPathToFLV)>
		<cfthrow message="source FLV does not exist">
	</cfif>
	<cfif not RefindNoCase('\.flv$',arguments.strPathToFLV)>
		<cfthrow message="source file must be an .flv">
	</cfif>
	<cfif not RefindNoCase('\.jpg$',arguments.strPathToJPG)>
		<cfthrow message="target file must be a .jpg">
	</cfif>		
	<cfif not RefindNoCase('^\d\d:\d\d:\d\d$',arguments.strFrameAtTime)>
		<cfthrow message="time must be set as hh:mm:ss">
	</cfif>
	<cfset strArguments = "-i ""#arguments.strPathToFLV#"" -an -ss #arguments.strFrameAtTime# -an -r 1 -vframes 1 -y #strTMPPath##strUniqueFname#-%d.jpg">		
 
	<cfexecute name="#strPathToFFMPEG#" arguments="#strArguments#" timeout="30"></cfexecute>
	<cfdirectory name="qTempFile" action="list" directory="#strTMPPath#" filter="#strUniqueFname#-*.jpg" listinfo="name" recurse="no" type="file">
	<cffile action="move" source="#strTMPPath##qTempFile.name#" destination="#arguments.strPathToJPG#">		
</cffunction>

And here’s how to use it:

<cfscript>
flvgrabber(strPathToFLV='/some/path/some.flv',strPathToJPG='/some/path/some.jpg',strFrameAtTime='00:00:03');
</cfscript>

Have fun!

Tagged as: , , , ,
31. August 2009 15:08 by Markus Wollny

Useful RegEx: Check if list contains only integers

31
Aug/09
0

Trivial, but useful… For 0 and positive integers only:

^[0-9]+(?:\,[0-9]+)*$

If you wish to include negative integers:

^(?:-?[0-9]+)(?:\,(?:-?[0-9]+))*$

So this might be a useful validity checker function:

<cffunction name="lstContainsIntegersOnly" output="no" returntype="boolean" access="private">
	<cfargument name="lstI" required="yes" type="string">
	<cfargument name="bNegativeAllowed" required="no" type="boolean" default="false">
	<cfscript>
		var bIntegersOnly = FALSE;
		if (arguments.bNegativeAllowed) {
			if (ReFind('^(?:-?[0-9]+)(?:\,(?:-?[0-9]+))*$',arguments.lstI)) { bIntegersOnly = TRUE; }
		} else {
			if (ReFind('^[0-9]+(?:\,[0-9]+)*$',arguments.lstI)) { bIntegersOnly = TRUE; }
		}
		return bIntegersOnly;
	</cfscript>
</cffunction>
21. July 2009 14:06 by Markus Wollny

UDF to strip certain chars, but leave UBB tags alone

21
Jul/09
1

We are developing a commenting system which is supposed to discourage comment spam by making comments more or less unreadable when they crossed a certain threshold of negative ratings. We decided that we’d like to strip all vowels from the text, though we’d like to keep the UBB-style tags inside the comment unchanged.
You’ll find that this last bit makes the whole task a little more complicated than just a simple Regex-Replace. We’ll need to use a negative lookbehind, then mark the characters we do not wish to strip, then remove any “unmarked” characters and finally remove our marker.

12:43 by Markus Wollny

CF Instance Hangs, “too many open files” in Log

21
Jul/09
0

This is really quite trivial, but may be of some interest to people who don’t know too much about Linux OS tweaking. Today our dev-server instance was hanging – again. Only this time I took the liberty to make our developers wait a little longer before I restarted it, so I could actually diagnose the issue and resolve it for good. As only one of our two instances was hanging, I took a peek into its logfiles first – but couldn’t find anything out of the ordinary. And the Enterprise Manager reported the instance to be running, too.