IE7 cancel onbeforeunload

I was using the onbeforeunload event to confirm the user wants to leave the page with changes, something similar to when you create a new mail in Gmail. Supposedly, setting the event.returnValue to a String will activate a browser prompt when you leave the page. Setting it null makes it not prompt, after I decided that there were no changes made.

But that only worked in FF2. IE showed a “null” as the message. No matter what I set for the event.returnValue in IE, it prompted. I even tried setting the returnValue to false, and event.cancelBubble = true, but nothing worked.

Finally by trial and error, the only way to make IE not prompt is to NOT set it at all. Don’t call event.returnValue = something. This works for FF2 too, so I’m done.

Loop antipattern

Consider the following snippet:

boolean left = true;
int m = 0;
while (m < 2400) {
  System.out.println(left ? "left" : "right");
  left = !left;
  m++;
}

could be improved as:

for (int m=0;m<2400;m++) {
  System.out.println(m%2==0 ? "left" : "right");
}

The antipattern to be recognized here are:

  1. The number of iterations was known at compile-time. This is a hint to use for loops. Whiles are used when the number of iterations are unknown. Although it is possible to write it as a while loop, a for loop makes your intention concise, and less error-prone (such as forgetting to increment).
  2. Modulus can be used for "cycles". In this case it was a 2-item cycle, therefore a ternary operator was used. A cycle with more items can also be used by using an array to reference the items, then use array[m%array.length] to index the cycle item.

Disclaimer: Although the code was actually written (not by me), it is not meant to shame or blame. It is to be taken as a lesson for sharing and improvement.

Rails logger rotation

The mongrel rails log file grows rather quickly, even in production mode since every request is logged. Log rotating becomes a basic necessity. After googling there appears to be 2 standard ways of rotating logs: use the built in rails logger parameters or use an external rotator such as logrotate. Many have also expressed problems with clustering using the built-in logger rotation.

I couldn’t find a ready Windows implementation of logrotate (I was avoiding having to build it myself) and therefore I settled for the built-in rails logger. Clustering wasn’t really an option for me due to some internal reasons. I set up the test rotation for 20 files having 10kb each, and I thought I was done when I saw backup log files of 10kb created. But when it hit the 20files, it didn’t erase the last file. Instead, it continued to increment the latest file beyond its 10kb limit!

Well, that wasn’t what I thought about rotation. After various searches in vain I took a plunge into the source pool. There, I found the implementation performs a rename of the previous log files to “shift” them. A-ha. I did a quick test with the rename function using Ruby and JRuby and found that JRuby doesn’t do rename the same as Ruby. It was supposed to overwrite it but it didn’t, nor did it throw an error.

I did a quick patch to delete the file to be rename if it exist, so that it would rename successfully. Following that, I filed a JRuby bug. Not sure if it’s really a bug, but well. Let them decide.

Internationalized domain names

ICANN is testing out URLs in 11 languages other than those that supports the roman characters, such as Chinese, Tamil, Arabic, Korean and Japanese. To do this, 11 new TLDs (top level domains) and secondary domains representing http://example.test have been created and inserted into the DNS servers. Interestingly, as the Arabic and Hebrew languages are read left-to-right, the URL will be reversed.

Try http://例子.测试. It can actually be resolved.

You can find the plan at http://www.icann.org/topics/idn/idn-evaluation-plan-v2-9-2-14aug07.pdf

Internet Explorer (IE) limits CSS imports to 32

Although the link is talking about a vulnerability when more than 32 imports are used, I’m actually having issues with a web page that actually needs more than 32 css imports. The page works fine in Firefox 2 but fails in IE 7. The article also didn’t explicitly mention IE 7, but rather older MS browsers. Even if the vulnerability may not be there, the limit of 32 stylesheet imports may still be imposed.

http://www.juniper.net/security/auto/vulnerabilities/vuln3394.html

I’m still trying to find formal documentation to support this fact, I can’t seem to find it in MSDN (as usual). Don’t ask me why I have > 32 imports. Some things are beyond my control.

Apache James

Needed a SMTP server for testing our application today, and after evaluating a few options (company smtp, various free/open source solutions) it turned out that James worked best.

James was able to run as a standalone SMTP/POP3/NNTP server or Windows Service all at the same time, and highly configurable with its XML file. I was able to get it running quickly with its installation instructions. However the site documentation wasn’t so explicit on SSL configuration; its instructions assumed a lot about the user knowing about JSSE, keystores, SSL and the such. (Well, they probably are thinking someone who didn’t know these shouldn’t be setting up a secure mail server)

http://james.apache.org/server/2.3.0/usingTLS.html
http://wiki.apache.org/james/UsingSSL

These are two good references to getting it setup. In short, do the following:

  1. Use java keytool to create a keystore containing the key pair for SSL.
  2. Edit {james}\apps\james\SAR-INF\config.xml
  3. Uncomment the ssl section of the section, replacing the values of file, password, and key-password with those of your keystore.
  4. Edit your SMTP server section and uncomment the useTLS element. Change the port number if preferred.
  5. Copy {jre}\lib\ext\sunjce_provider.jar file into the {james}\lib directory.
  6. Restart James.
  7. Test using a mail client. For a self-signed cert you may need to configure your mail client to accept the certificate.

Remember always to check your log file for error messages. It saved me loads of time from guessing what’s actually wrong. Also important is to copy {jre}\lib\ext\sunjce_provider.jar file into the {james}\lib directory. Yes, this is the step I missed that was causing a NoSuchAlgorithmException which I found in the log file.

Irritating Checkbox margin

The IE checkbox has a margin/padding around it that cannot be removed even by setting both of them to zero. You can observe it by checking it and looking at the dotted line surrounding it. Firefox doesn’t have this issue.

http://www.webmasterworld.com/css/3282586.htm

This forum gives the solution as setting its height and width to 13. I tried 12, and the checkbox was shrunk…

Linux Antivirus

Quote of the Day:

With Linux you actually get to use your system’s resources for doing what you want to do, not for running a whole bunch of utilities designed to protect it. Linux is protected by its design, not by third party products.

– simongrahamuk (http://www.certforums.co.uk/forums/thread8164.html)

Was helping to look for Linux antivirus and came across the above comment. Come to think of it, it sounds dumb to run Windows plus a suite of programs on top just to protect Windows. Then again if Windows tries to build more security into its OS will it become another anti-competition saga?

Escaping

I’ve always felt that networking problems and concepts are useful in application programming as well, as the problems will appear again at a higher level. Escaping is just another one of them.

In networking, signals/packets have special sequences to indicate special conditions such as the end of a frame. However the same special sequence may appear in data. By escaping and the sequence in the data, the receiver will be able to interpret the packet and the data correctly.

At the application programming level, the simplest example is with a quoted String: “This String has “some” quotes.” The some is being quoted in a quoted String. Escaping typically defines an escape character, such as a backslash (\). Therefore when the quote is prefixed by a backslash, it is interpreted as a data quote. All other special characters can now be prefixed with a backslash to indicate it is a special character.

Using an escape character causes another problem: the backslash now cannot be represented, and requires escaping as well. Using yet another escape character may solve the current problem, but it causes the same problem again. The usual solution to this is to use the escape character itself to escape itself. Therefore a backslash is represented as a double backslash (\\).

Other common escaping examples include VB quotes (” becomes “”) and HTML entities (< becomes &lt;). Thus the next time you encounter problems with data containing control sequences, think of this solution! 1. Escape all control sequences using an escape sequence. 2. Escape the escape sequence using itself.

The geek is alive

There’s a lack of new posts here. On the contrary, it doesn’t mean I’m not hitting nice problems to post, but its overwhelming that I’ve no time to post all of ’em. In the meantime here’s a few links to keep you occupied (if there were really any visitors to this site…)

http://ask-leo.com/what_is_thumbsdb_and_can_i_delete_it.html

Explains layman tech stuff to layman in layman terms. Great when you have laymen asking you tech questions.

http://www.computerworld.com/action/article.do?command=viewArticleTOC&specialReportId=9000342&articleId=9024364

Computerworld’s “100 Best Places to Work in IT 2007”. I happen to be in one of them, but I’ve yet to feel that its a good place yet. Maybe a few more years.