Friday, November 30, 2007

IWDatacapture.callServer call fails / caveat

problem:

when i build teamsite DCTs i always find a new and wonderful way to break something.

Today i was building a DCT that used IWDatacapture.callServer to call a cgi script that would get some stuff from the database and then out put it into a hidden frame and do a javascript callback to the parent window.

everything was going well until i made the mistake of making 2 calls to different IWDatacapture.callServer cgi scripts one right after the other. one of them would always fail.

solution:

The problem stems from the fact that CGI scripts that output html make use of a single blank.html file to write out their contents. If you call 2 of them "almost at the same time" one of them will overwrite the other's blank.html file and cause it to fail. The only reasonable solution is to combine both CGIs into one file and let it write to one blank.html file, you can also try to make sure that CGIs dont get called at the same time, but that would be difficult to control.

So it seems that its probably a reasonable requirement to only allow ONE IWDatacapture.callServer call per DCR, otherwise things might break. How sad.

Thursday, November 29, 2007

Java: Understanding Weak References

The other day i was reading programming at reddit and stumbled on this article:
Understanding Weak References which was a good read but i still had some questions, so i turned to Java Developer Forums for help. I didnt get flamed! (much). What i got was a very good response from a user named kdgregory which is note-to-dogself worthy.

My questions are in blue boxes:
If weak references are great at stopping leaks, why not use them all the time?

They're great at stopping leaks in the situation where you're attaching data to an object that (should) have a limited lifetime. If you used a regular map (most likely an IdentityHashMap), you'd be creating a strong reference to the object via the map, so it would never be collected, even when there are no other references to it (I call this a leak, other people don't).

For example, ThreadLocal uses weak references, because they'll be cleared when the thread ends.

ObjectOutputStream should use weak references (and a changed protocol) so that it can be used in long-running conversations without constantly calling reset(). But it doesn't.

"WeakHashMap works exactly like HashMap, except that the keys (not the values!) are referred to using weak references."
  • does this imply that hashmap stores values in weak refs, if so, why?
  • should i use WeakHashMap for tasks where i will have another reference to objects i put into the map. ie i dont put my only ref into this map?

No, HashMap doesn't use weak references for either key or value. The author's wording (and for that matter, the JDK JavaDoc wording) is a little misleading.

One typical use for weak references is in a canonicalizing map to reduce memory consumption with duplicated data. With such a map, you would store an object as both key and value, and retrieve the canonical value when presented with an arbitrary key -- think String.intern(). However, you can't use WeakHashMap to do this without wrapping the value in a weak reference also.

When do i not use weak references ?

Most of the time. Any reference object means that you have to access your original object in two steps (have to call get() on the reference) rather than just one. And normally, you'd know the lifetime of your objects.

Monday, November 19, 2007

music for november

Using the google mp3 search i find some good stuff, im going to put all the november music into this post.

this place has a player built in, very nice:
http://media.soundfixrecords.com/player/xspf_player.swf

lots of full albums here - seems like dance / dnb:
http://danav.org/audio/

Friday, November 16, 2007

parse xml with JDOM and Xpath

every now and again its good to have your config in an xml file, but i am usually to lazy to remember to how to do it properly and just use java.util.Properties. Well no more of that!

This is a short self contained code sample on how to parse some xml using jdom and jaxen

you will need:
jdom lib
jaxen lib (which may be old now)

Monday, November 12, 2007

wait in windows bat script - good way

i recently wrote about using ping to pause for a number of seconds in a batch script. It was brought to my attention that you can use /wait to actually wait for a process to complete like so:

start /wait /i "unimportant title put whatever here" "C:\path\to\some\executable.exe"

or if you have an msi installer you can do this:

msiexec /i "C:\path\to\msiinstaller.msi" /qr

Friday, November 09, 2007

extract links using htmlparser

i have come to realize that even though i love to do everything in regex, extracting links with regex is not really the best idea.

the best idea is to use htmlparser to extract the links.

This code sniplet will extract links from a page body, and given the link where the body was found it will resolve the relative links. This is tailored to be used by a crawler.

Thursday, November 08, 2007

wait or pause in windows batch script

problem:
i am told that windows .bat files do not have a sleep or wait command.
this is pretty sucky for when you want to wait or sleep, isnt it?

solution:
do this terrible hack
ping 1.0.0.0 -n 1 -w 5000 >NUL

that will block for 5 seconds. yey!

also, if you need to wait for a process to finish, i blogged about that here

also, if you have the ability to install something on the box where you will run your script, you should get the Windows Server 2003 Resource Kit tools from Microsoft: which works with XP. This gives you a real sleep command! (Thanks to Tricky for pointing this out in the comments)