Email to Instant Message Gateway

It’s wicked cold this weekend in Massachusetts so I stayed inside and had some fun with Apache James and Smack (an open source library for communicating with XMPP servers from Jive Software). See, I was looking over this page of Mailet ideas on the Apache James wiki and I saw two different people wanting instant message notification of email delivery. Having spent some time writing a custom mailet or two recently and having been recently reminded of the very cool Smack library, it turned out to be relatively easy to snap the two together to make an email to instant messaging gateway. I don’t want to bore you with the details, so I posted a full description, including installation / configuration instructions, source code and binaries over on my projects page. Enjoy!

The cost of Calendar object creation

I spent some quality time with Joshua Bloch this week and by that I mean his book Effective Java. In item number four (the book is organized as a series of fifty items, a format he borrowed from the Effective C++ book by Scott Meyers) he discusses the practice of reusing a single object (instead of creating duplicate objects every time) and uses a Calendar class as an example of an object that you may want to reuse. Near the end of the item, he says “… Calendar instances are particularly expensive to create”, which I’ve heard before, but what does it really mean? Why is it expensive? The JavaDoc for the Calendar class doesn’t attempt to explain why creating instances might be expensive, so I tested it out, just to see how expensive a Calendar object was to create:

long start = System.currentTimeMillis();
for (int i=0; i
and then compared that to the creation of 100,000 Date objects:

long start = System.currentTimeMillis();
for (int i=0; i
On average on my system, the creation of 100,000 Date objects is anywhere from 10 to 30x's as fast as the creation of 100,000 Calendar objects. So while I never doubted Josh, it's obvious he's right. But that doesn't answer the question, why? The only place to go is the Java souce code, which you can download from sun.com. I'll save you the 48MB download and loosely unwrap the creation of a single Calendar object:

// Calendar c = Calendar.getInstance();
* createCalendar(TimeZone.getDefault(), Locale.getDefault());
  * new GregorianCalendar(zone, aLocale)
    * super(zone, aLocale);
      * setTimeInMillis(System.currentTimeMillis());
        * computeFields();
          * computeFieldsImpl()
            * timeToFields
              * internalSet x 6
            * internalSet x 8

and a single Date object:

// Date d = new Date();
* this(System.currentTimeMillis());
  * long fastTime = date;

So now it should be pretty obvious why it a Calendar object is "... particularly expensive to create." The Calender getInstance() method gets the current time in milliseconds and then unzips that value into the current year, month, date, day of the week, day of the year, milliseconds, seconds, minutes, hour of the day, AM / PM, hour, time zone and daylight savings time offset. The Date object? The only thing it does is store the value of the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC in a member variable.

Tune in next week for more exciting object creation action.

Links: 2-17-2006

Links: 2-13-2006

java.lang.IllegalArgumentException: Illegal group reference, replaceAll and dollar signs

This weblog is officially about inane things I run into while trying to do my job at work. Let’s say you have a String object like this:

String mystring = "Your password: #PASSWORD";

and at runtime you need to replace the value of #PASSWORD with a password that a user typed in. You’d write something like this:

String password = "$Jslwe"
mystring = mystring.replaceAll("#PASSWORD", password);

What would happen? You’d expect that the key #PASSWORD would get replaced with the value of the variable ‘password’ (which is “$Jslwe”) and then you’d move happily on your way to something much more interesting. But no, Java throws you an error:

java.lang.IllegalArgumentException: Illegal group reference

which is extremely helpful. Turns out that the second argument to the String replaceAll method “may” have some issues with dollar signs and backslashes which you only find out about if you dig into the Matcher class that backs the replaceAll method or if you’re lucky and you read about the whole thing on a site devoted to regular expressions. In short:

myString.replaceAll(“regex”, “replacement”) replaces all regex matches inside the string with the replacement string you specified. No surprises here. All parts of the string that match the regex are replaced. You can use the contents of capturing parentheses in the replacement text via $1, $2, $3, etc. $0 (dollar zero) inserts the entire regex match. $12 is replaced with the 12th backreference if it exists, or with the 1st backreference followed by the literal “2” if there are less than 12 backreferences. If there are 12 or more backreferences, it is not possible to insert the first backreference immediately followed by the literal “2” in the replacement text.

In the replacement text, a dollar sign not followed by a digit causes an IllegalArgumentException to be thrown. If there are less than 9 backreferences, a dollar sign followed by a digit greater than the number of backreferences throws an IndexOutOfBoundsException. So be careful if the replacement string is a user-specified string. To insert a dollar sign as literal text, use \$ in the replacement text. When coding the replacement text as a literal string in your source code, remember that the backslash itself must be escaped too: “\\$”.