Tuesday, August 29, 2006

Java Brainteaser #1

I was reading some blogs tonight and came across some interesting Java brainteasers. Here's one of them.

In certain circumstances the following Java code will compile and run. What are those circumstances?

if(this) {
System.out.println("this is, inded, true!");
}

I'll give you a hint, they require a Java 5 compiler.

UPDATE: Answer in the comments.

Monday, August 28, 2006

Interesting technologies...

A couple of interesting technologies (in no particular order) that I haven't gotten a chance to check out yet but have been meaning to...

JXPath - Allows you to access JavaBeans via XPath

XMLBeans - XML to JavaBean binding library, it's supposed to have some XPath/XQuery support in it

Subversion - Seems to be the most popular revision control system nowadays

Buildix - Subversion, Trac, and CruiseControl all bundled together into a single LiveCD

Genshi - As the author says, "A toolkit for stream-based generation of markup for the web." It seems like a pretty decent and modern templating tool for python, appears to be XML only though.

Janino - An embedded Java compiler. Doesn't even seem to need source code to reside on the disk.

What other interesting technologies are out there that people are interested in but haven't yet gotten a chance to explore?

Tuesday, August 22, 2006

Mono running on OS X

The other day I installed Mono on OS X 10.4.7. The install was painless, I downloaded a .dmg, opened it up, and double clicked the install package. 20 seconds later I had Mono installed and was writing my first "Hello, World!" program. That's how installing all programming environments ought to be.

Now all I need is an idea of a test program to try it out on. :-)

Wednesday, August 16, 2006

Python Plug-ins

Years ago I was writing an IRC bot in python and had the need to create a plug-in architecture so that I could both abstract specific pieces of functionality away from the core of the bot as well as dynamically load and unload bot behaviors at runtime. At that point in time the ability to do this type of thing was present in python but there wasn't a standardized paradigm for how to do it. Since then things have become a bit more standardized with the addition of standardized import hooks and my code is surely outdated.

There was however one particular aspect of my implementation that proved to be nice I wanted to share -- the ability to reload and reinitialize a plug-in at runtime. This ability was accomplished mostly as a natural side-effect of using the imp standard library module. Here's how my code looked:

class PluginManager:
def __init__(self):
self.plugins = {}

def load_plugin(self, name, config):
from imp import find_module, load_module
fh, fn, desc = find_module(name, ['./plugins'])
self.plugins[name] = load_module(name, fh, fn, desc)

if hasattr(plugin, '__loadplugin__'):
plugin.__loadplugin__(self, config)

def unload_plugin(self, name):
try:
plugin = self.plugins[name]

if hasattr(plugin, '__unloadplugin__'):
plugin.__unloadplugin__(self)

del self.plugins[name]
except KeyError:
raise PluginException("Plugin %s not loaded." % name)

Because I manage the set of loaded plug-ins myself in the plugins member variable I can control the lifetime of a plug-in. This is also coupled with the fact that imp.load_module() always reloads a module, even if it had been previously loaded. Also note the use of the __loadplugin__ and __unloadplugin__ module level methods. If a plug-in module defined either one of these then it would be called during (de)initialization. In essence it's a very simple plug-in protocol that's being defined here.

Hopefully this will be of some use to someone in the future. By the way I don't advocate writing your own IRC bot nowadays. There are plenty of high quality, extensible, already built options out there. My personal favorite is Supybot.

UPDATE: In browsing around I found a library that does something very similar to what I described here, Sprinkles. At first glance it doesn't seem to have equivalents for __loadplugin__ or __unloadplugin__ but they may be hidden in there somewhere.

Monday, August 14, 2006

Powerbook Freezes

I've had a 15" high resolution (the one with the 1440x960 resolution) PowerBook G4 for about 10 months now, and up until a few days ago I've been dealing with hard freezes every couple of days or so. Very annoying to say the least. Well, recently I came across a useful thread on the Apple discussion forums that appears to have completely solved my problem.

The fix goes something like this:

  1. Download and install Apple's Computer Hardware Understanding Development Tools (link)
  2. Restart the PowerBook
  3. Go into System Preferences and select the new Processor preference pane
  4. Uncheck the "Allow Nap" option
It's important to note that this fix is only temporary. The next time your PowerBook is rebooted this setting will be lost. For a permanent fix just make your /etc/rc.local contain:

#!/bin/sh
# Disable CPU NAP
/usr/bin/hwprefs cpu_nap=0

There is a drawback to this however. The CPU NAP setting is what allows your processor to automatically adjust its speed downward when it's not being used heavily. This has the advantage of conserving power and lessening the amount of heat generated. So with the CPU NAP disabled, you're looking at more heat and a shorter battery life. Let's all hope for a better solution from Apple (not likely to come now that they're focused on Intel).

Hopefully this will save someone the months of agony that it caused me.

UPDATE: 27 days and counting (as of September 6th) without a crash!!! Everything seems to be fixed, although things are much hotter and my battery life is significantly shorter. It's better than crashing though.

[~]$ uptime
10:38 up 27 days, 12:57, 3 users, load averages: 1.57 1.20 0.95

Hello World

Hello everyone,

Welcome to my blog. I'm still not entirely sure what I plan on discussing here, but it will most likely be a mixture of posts about programming, technology, and anything else I find interesting that I think would be worthwhile sharing with the world. :-)