Learning Ruby: Knowing.net Exercise #1

I've just finished exercise #1 from: knowing.net. Please feel free to critique this code, I am serious about learning Ruby and getting introduced to members of the community.



Spring Note: PropertyPlaceholderConfigurer

Often there are Spring-related configuration options which don't belong inside the final jar/war for a Java application. Either because the options are different for each deployment environment or because the value of the option is sensitive (passwords). The PropertyPlaceholderConfigurer class attempts to remedy the management and injection of the values for these options. It reads one or more property files, optionally using system properties for defaults, and uses the values found to fill property placeholders of the form ${property name} found in the ApplicationContext in which the configurer is defined.

  • It provides a mechanism for externalizing configuration so that each environment can be configured differently without re-building the application.
  • It provides a mechanism for injecting test properties at test-time without incurring much development overhead.
  • Using the classpath:<file> pseudo-uri enables developers to easily include default properties while still allowing users to over-ride the defaults by adding the location of a properties file to the application's classpath.
  • The PropertyPlaceholderConfigurer is extensible and provides a method, convertPropertyValue(String), which can be over-ridden in a sub-class to handle the encryption of properties.
  • The configurer class does not re-load at a regular interval. Changing the properties managed by the configurer requires a restart of the application using it. This can be overcome in some limited situations through a custom sub-class, but for the most part, is not really supported by the Spring container.
  • Only Spring-managed beans can take advantage of these properties.





Productivity Tools: Launchy

Launchy is a simple application indexer/launcher for Windows/Linux/Mac OS. It is an application which combines some of the features of the Spotlight and Dock tools in Mac OS. You set up Launchy with a set of folders and file extensions to scan, and it will create a catalog of the files found in those folders. Any of the cataloged files can then be opened quickly by pressing alt+space at any time and then typing some, or all, of the file's name. It is important to note that Launchy does not catalog the contents of the files, only the files' names.


Except for the times that Launchy is rebuilding its catalog, you will never see Launchy utilizing your CPU. The index does appear to take up some memory (mine's currently at around 8MB), but that should be expected for this sort of application.

Filesystem Integration

Launchy integrates well with your filesystem. That is, you can start typing C:\Pro, hit tab, and then hit enter to open up an Explorer window for the Program Files directory. You can use this functionality to get to any folder/file on your filesystem, even those not indexed by Launchy.


Launchy includes a plugin system and includes some interesting plugins out of the box:

Allows you to do simple calculations and conversions from within Launchy.
Indexes the applets from the Control Panel.
Create custom commands to run against the command-line.
Integrates Launchy with your bookmarks and allows you to do searches against several online search engines from within Launchy.

I've also found one additional plugin that has been very useful, the PuTTY plugin. This plugin adds all of the saved sessions from your PuTTY installation to Launchy's catalog. This gives you the capability to type ssh, hit tab, and see a list of saved PuTTY sessions.


Productivity Tools: Virtual Desktops

Virtual desktop systems allow me to set up multiple workspaces within my window manager, each of which contain a separate set of application windows which will only be shown when the workspace is activated. This is a powerful expansion of the idea of application switching (alt+tab) as I can now switch easily between groups of applications, instead of just a single application. By creating task-related workspaces, I am able to stay focused on only the one or two tools that matter for the task I am currently working on, while still having other tools open for access later.

While virtual desktops have been around for ages in the *nix world, Microsoft still refuses to include these capabilities out of the box. This has led to an array of 3rd party tools that provide virtual desktops. I've tried several of these tools over the years, and VirtuaWin is the one which I keep returning to. VirtuaWin is a virtual desktop manager for Windows which gives you a set of virtual desktops and global keystrokes for switching between them.

By default, VirutaWin provides you with two actions: move to another workspace (win+←/↑/→/↓) and move to another workspace with the current window (alt+win+←/↑/→/↓). There is also a module, Smart CoolName, which adds the ability to flash, for a few seconds, the name of the current desktop while switching desktops. You can also do some window management by clicking on the system-tray icon: switch to a specific window/desktop, move a specific window to the current desktop, and flag a window so that is is present on every desktop.

I can usually get away with just four desktops while I'm working:

IDE and documentation windows
tools which support my work, like database clients and JIRA windows
my messaging clients and Outlook
all the other application windows, work-related or not


Spring Note: NamedParameterJdbcTemplate

While I've been using Spring in various capabilities for some time now, I've never run across a situation where I've needed to execute a sql query with a duplicated parameter from code utilizing Spring. The following is a simple example of the type of query I ran into:

--Query Version 1
select * from table_1 where ID = ?
select * from table_2 where ID = ?

Using the normal JdbcTemplate.query method with this query would require you to create a two-element array which contains the same value in both elements. Silly DRY violation, right? Luckily, some of the folks behind the Spring JDBC packages decided to include a way to define queries where the values of parameters can be specified by name rather than by index. Here's an example using the named parameter syntax handled by the NamedParameterJdbcTemplate class:

final String query = "select * from table_1 where ID = :id"
        + " union"
        + " select * from table_2 where ID = :id"

final Map<String, String> params = new HashMap<String, String>();
params.put("id", id);

final RowMapper mapper = new ExampleRowMapper();

// jdbcTemplate is an instance of NamedParameterJdbcTemplate which
// has been created in the Spring ApplicationContext using an
// appropriately-created DataSource.
final List results = jdbcTemplate.query(query, params, mapper);

Interesting note:

After digging through the Spring's SVN repository, I found that the NamedParameterJdbcTemplate class does not appear to rely on the CallableStatement class for supporting the named parameter functionality. That means that using this class should be safe for all JDBC drivers(which support PreparedStatements), as the named parameters are handled above the JDBC driver's level.