Friday, March 07, 2014

Mutli-line String Literal in Java

Eclipse Preferences: Java > Editor > Typing, check "Escape text when pasting into a string literal". Once turned on, it automatically add "\r\n" when I am pasting multi-line string into a string literal. Very nice.

Wednesday, March 05, 2014

Windows PowerShell Script to Delete Old Files

Just starting to dab into PowerShell script after it has been in the wild for 8 years. Turns out to be surprisingly good. First you have a nice editor (PowerShell ISE), second is the vast amount of cmdlets that make coding go fast.

Here is a script to delete old files that are 30 days or older. It used to take at least 40 lines of VBS to achieve.

Before you get too excited, a few things to be aware of:
  • Availability of PowerShell: WindowsXP may not have it, and Windows 7 may only have it up to version 2. So, if you found some exciting cmdlets, chances are they are not available on your OS by default
  • Executing a PowerShell file takes extra effort. It's forbidden by default. So you have to do either of the following:
    • Open PowerShell, and execute this command: 
         Set-ExecutionPolicy RemoteSigned
    • Or, when running a file use this command line (notice full path to the ps1 file has to be provided here): 
         PowerShell.exe -ExecutionPolicy ByPass c:\scripts\myScript.ps1

Tuesday, February 11, 2014

Read MS Access File into CFQuery

Here is code snippet that will read arbitrary Microsoft Access file, and return the result in CFQuery. No Coldfusion data source is required.

- Coldfusion 9, 64-bit

Notice, in the snippet, there is a dump of all DB drivers loaded by Coldfusion. So, if you ever need access to other dynamic data connection, just swap out the connection string in the sample.

Friday, February 07, 2014

TaskKill vs. PSKill

I've been using PSKill for at least 7 years now. Just today, I found out TASKKILL is actually better than PSKILL in all fronts. I should finally stop using PSKILL. My finding is that TASKKILL in Windows 7 has all the features that PSKILL provides, plus the following advantages:

  • No download needed
  • No user agreement needed for the first time user (which is a big plus for server side automated execution)
  • Can kill *ALL* processes with the image name (compare to PSKILL only kill one of them. So with PSKILL, I will need to repeatedly invoke it until all the processes are gone. This is no fun when you are trying to run in automatically on a server.)
  • Richer command line options

Thursday, January 16, 2014

ExtJS Grid Cell Rendered with Mixed Image and Text


To render mixed image and text in ExtJS grid, I found the following two approaches

  • Implement custom renderer, return a value like this 
      <img src="..."/>you text content
  • Use CSS to add background image to the cell (see Reference)
Problem: misaligned columns in the first approach
However, when I examine a grid with "locked" columns, I starting to see problem in the first implementation. Often times, the locked columns and floating columns are misaligned. Turns out image tag in the cell will cause grid to adjust row hight. However, ExtJS does not adjust fixed columns and floating columns together. So, if there is image in fixed column only, it will frequently make it taller than the floating columns, thus causing misalignment. To solve this problem, I added the following style to the image tag:

   margin-top: -4px;
   margin-bottom: -4px;

Event Handling

The image in the cell is usually an active component. In order to intercept the click event, I found two good approaches, again.
  • In JS code, hookup event handlers to the image component, and fire event from there. Refer to implementation of ActionColumn (ExtJS source grid\column\Action.js)
  • Change the selType of grid to 'cellmodel', and add select event listener


Displaying an Image Inside an Ext JS GridPanel Cell

Friday, January 10, 2014

New Years Resolution: do not update to .NET 4.5.1

Ok, just kidding. Should not be a new year's resolution. But serious, it was causing me nightmares over the holiday season. All of a sudden, several clients starting to have problem using an ActiveX control in IE (to be precise, ChartFx 6/7 ActiveX controls). After some really boring days researching, installing/uninstalling/reinstalling and testing, I finally found out .NET 4.5.1 was causing the problem.

I have no idea why it's a problem, but I know if I uninstall 4.5.1 and rollback to 4.5.0, then all the problems are gone. So, it's official, .NET 4.5.1 update is banned.

Problem with Intranet Web Site Content?

Did your Intranet web application users complain about your website not working at all? Here is one of the root cause: IE running in emulation mode of older browsers. It will mess up some JavaScript functions that relies on relative new features.


  • Your clients use IE
  • Only have problem when using Intranet like URLs
  • JavaScript console shows weried error messages like: 'JSON' is undefined
  • IE 11 Emulation (or IE10 document mode) is showing IE 7 or older or even quirk mode (however, if you see Edge then the solution I provide below won't help you)
  • DOM view showing your nice HTML5 doc type being commented out
IE commented out doctype

Root Cause

IE option "Display intranet sites in Compatibility View" forced it to emulate behavior of older browser for Intranet web applications, regardless what is in the web page's header.


This worked for me: change IIS HTTP header
  • In IIS admin, go to the folder or file that you want to enable IE "Edge" mode
  • Click "HTTP Response Headers"
  • Click "Add", and type Name: X-UA-Compatible, Value: IE=edge

HTTP Header that will fix the problem

Saturday, November 16, 2013

Javascript JSON Trailing Comma and IE

Seems like Google Chrome and Firefox are both pretty tolerant when it comes to trailing commas in JSON. However IE can be really pissed off by those extra characters.

Two symptoms I typically see:

  • Syntax Error: IE will simply report a syntax error like this: SCRIPT1028: Expected identifier, string or number
JSON string that can cause above error message:

var x = {
    a: 1,
    b: 2,};

  • Undefined object: when the trailing comma is in an array declaration, IE will add a undefined object at the end of the array (Firefox, and Chrome does not do this). For example, for the code below, my test results are: IE -> 4, Firefox -> 3, Google Chrome -> 3

var x = [1, 2, 3,];

       This usually will cause unexpected error in the code down below, which may loop through the array and try to access the undefined last element.

It is the second kind of problem that causes the most headache. Because in the first case, IE will pinpoint line number so that we can fix the problem immediately. In the second case, you are likely to get an obscure error message from a code that is trying to access the undefined last object.

Make Coldfusion 1000 Times Faster

Here is a well kept secret to make Coldfusion runs 1000 times faster: make your initial implementation 1000 time SLOWER.

Kidding aside, here are some performance tips:

  • Clean up debug code, especially CFDUMP. I have a production server that runs a query for 14 minutes, turns out a CFDUMP for debugging purpose is left behind. After it's removed, the query only took less than 5 seconds to finish;
  • JSON serialization is not as fast as you wished. I have a query that took 1 second to get result from SQL server, but then it took about 10 seconds to serialize the result to JSON string;
  • Similarly WDDX, SOAP could take significant amount of time to serialize large object;
  • Use AMF binary communication if possible (at least if you use Flex, or Coldfusion to Coldfusion communication, do give AMF some serious consideration). Moving from SOAP webservice call to AMF have make our response time order of magnitude faster;
  • Other usual suspects that are applicable to many other programming languages: loop, nested loop, multiply vs. divide, ...
  • Profiling: I was not able to find a good profiling tool for Coldfusion, so end up roll my own hand coded profiler. It's a little cumbersome, requires manually insert check points to collect performance statistics. Never the less, it helps in quickly identify bottlenecks;
  • Caching: turn on caching in Coldfusion is very easy. So, whenever it make sense, do turn on query caching, page caching, ... This often can be a 10 second job that improves performance by 100 times;
  • Coldfusion Admin: trusted cache, disable debugging
  • Always use fully qualified variable names to save scope lookup time;
  • Application.cfc: double check, triple check this file, because any performance issue here will affect ALL the pages of your website;
  • use cfqueryparam instead of text in cfquery;
  • Fine tune JVM parameters
  • Hardware: CPU, RAM, network, and Disk (again measure and identify bottleneck, before purchase any expensive upgrades)
  • Scale out: use load-balancer to distribute work load
  • Last but not least: make sure your SQL queries are all in good shape. Often times, Coldfusion server itself is not the bottleneck, your SQL server is;