Archive for August, 2004

del.icio.us plugin for Mozilla Firefox

Wednesday, August 25th, 2004

I just wrote a plugin to search my del.icio.us bookmarks using Mozilla Firefox’s search engine functionality.

For those of you know don’t know, Mozilla Firefox is a great, light-weight, Web-browser that I have found to be a very nice replacement for Internet Explorer. Firefox has a search box that you can use to search any search engine you want, just by adding a plugin.

del.icio.us (pronounced “delicious”) is a free social-bookmarking Website. It allows you to keep your bookmarks online and share them with other people. del.icio.us (only) allows you to search your own bookmarks if you are logged into the system.

You can install the plugin by clicking on: http://www.3drweb.com/mozilla

I’ve also submitted the plugin to be posted on the main Mozilla search engine Webpage, so hopefully it will be available to the general public soon.

Class references and static initializers

Tuesday, August 17th, 2004

Let’s say that you are using a type-safe enumeration pattern to reference a list of protocols. Your protocol object might have a reference to a ProtocolHandler class which a client would use to load on demand using reflection.

Look at the following code see if you can identify a possible trouble spot.

public class Protocol {
  private Class protocolHandlerClass;
  public static final Protocol TIBCO = new Protocol(
          TibcoProtocolHandler.class);
  .
  .
  .
  private Protocol(Class protocolHandlerClass) {
      this.protocolHandlerClass = protocolHandlerClass;
  }
  public Class getProtocolHandlerClass() {
      return protocolHandlerClass;
  }
}

public class TibcoProtocolHandler {
  public static final String SEND_SUBJECT =
	loadSendSubjectFromDB();

  private static String loadSendSubjectFromDB() {
      …
  }
}

The first time you reference a class, it will initialize all of it’s static fields. In the case of this example, the TIBCO field would be initialized. The gotcha is that the first time you reference anything on the Protocol class, it will also initialize all the static methods on all of the class objects that it refers to in its static initializers.



When the Protocol object is referenced, it loads all of TibcoProtocolHandler’s static variables (because they are part of the class object). Thus, in this case, the SEND_SUBJECT field would be initialized, which would call the loadSendSubjectFromDB() method. So, the first time you make any reference to Protocol, it will hit the database. Not entirely what you expect when you glance at the Protocol class.

To remedy this, either:

  • Don’t use statics in classes that are referenced by their class objects in other classes statics (in this case, don’t put statics in the ProtocolHandler classes)

  • Don’t reference class objects in statics, (try making Protocol store strings that represent the classes rather than the class objects themselves)
  • Don’t use a static enums (make protocol use a factory method instead of a typesafe enum thus pushing the responsibility of providing the class object out to the client code)

Spring: Creating Objects So You Don’t Have To

Sunday, August 15th, 2004

I have published a new article that introduces the Spring framework. Here are some excerpts from the introduction to the article:

“One of the more exciting open-source Java frameworks that has gathered steam in the last year is Spring. Spring aims to minimize dependency of application components by providing a plug-in architecture. Because Spring links objects together instead of the objects linking themselves together, it is categorized as a ‘dependency injection’ or ‘inversion of control’ framework.”

“Though dependency injection is the basis of the Spring framework, Spring also provides a rich set of tools built on top of its core dependency injection functionality. Spring provides an MVC framework, support for several presentation frameworks, a transaction management framework, DAO support, support for several O/R mapping tools, and more.”