Thursday, July 31, 2008

Multiple applet instances problem solved (?)

We have had some problems with our applet getting instanciated several times. Seems like it is not only started twice but there are also several instances created by the browser. We have spend quite a lot of time on troubleshooting this and trying to find a solution, and now it seems like we have found a solution:

We changed the APPLET tag in our HTML page to a OBJECT tag. This has to be made specifically for Microsoft Internet Explorer, since it is not supported in other browsers like Firefox and Safari. So in our HTML file we check which browser the user has, and use the OBJECT tag if it is Internet Explorer, the APPLET tag otherwise, something like this:

var html = "<applet archive='MyJar.jar' code='MyApplet.class' height='700' width='1000'>";
if (navigator.appName == 'Microsoft Internet Explorer'){
// Use object tag for MS Internet Explorer
html = "<object classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93'" +
" width='1000' height='700'>";
html += "<param name='code' value='MyApplet.class'/>";
html += "<param name='archive' value='MyJar.jar'/>";
}

It seems like this helps, but we do need more testing. Possibly we need to change to using the EMBED tag for other browsers but for now we stick to the APPLET tag.

There is a discussion on this in the Java developer forums here.

Thursday, July 03, 2008

Formatting java log records

While configuring logging levels for different parts of your software is flexible in java logging, formatting log records are not. In terms of what is delivered with the jre you have two alternatives:
  1. SimpleFormatter
  2. XMLFormatter
None of these give you any choice on what to include. SimpleFormatter tells you todays date in every record, but not what thread is printing. XMLFormatter dumps everything, but since the xml file is not valid (at least not on mac) it is not easy to process, you have to edit it to make it valid first. So we decided to write our own formatter, which is not difficult. Here it comes:

public class ThreadFormatter extends Formatter {

public String format(LogRecord record) {
return "Thread-" +
record.getThreadID() + " " +
record.getMillis() % 10000 + " [" +
record.getSourceClassName() + ":" +
record.getSourceMethodName() + "] " +
record.getMessage() + "\n";
}
}

Most important change (besides printing it all on one line in a more compact format) is that the Thread ID is included. The date is actually not very interesting for us, we just display part of the timestamp to give a rough idea of what takes time in the system.

This works very well, when we run our application standalone or in NetBeans applet viewer. But when we run the application as an applet it will not (for security reasons) load the log formatter class. Feels like the guys at Sun didn't think about logging from applets when the designed java logging. What we would need is a standard log formatter where you can configure the log format, without needing to load your own class. Perhaps we will get that in a later version....

Wednesday, July 02, 2008

Mac Java logging

Continued with the java logging work, specially on the mac. Found the following:
  1. logging to console is not possible, since java intercepts all console ouput and writes it to the logg... Creates a loop that fills th console. I have not found any way to turn this off, please drop me a line if you find one..
  2. logging to file with the standard XMLFormatter (the default if you dont change the configuration) produces (almost) an xml file.
  3. The 'XML' file has encoding "MacRoman" , which I have never heard of, but it seems like it is almost standard. Safari (Apple's own browser) does not know it, and complains 'Unsupported encoding MacRoman'. Firefox does not complain about the encoding however.
  4. The 'XML' file has a root tag of <log>. There is no closing tag, so it's an invalid xml. If you want to open the file in Firefox you have to add </log> by hand.
  5. If you print xml strings to your log (which we do) the XMLFormatter tries to escape them, but does not succeed, so you might have to remove them.
Seems like there are a few bugs here....