January 16th, 2010

Oracle Delete Taking a Long Time

It may look like your delete statement is hung, but ff your table has foreign keys in another table without indexes on that column Oracle will full table scan that other table for every row that you’re deleting. Hopefully this saves someone some time.

October 6th, 2009

Luhn Digit Generation Oracle PL/SQL Implementation

Here’s how to generate a Luhn Mod-10 Check Digit. It’s implemented as a function in oracle PL/SQL. I spent 2 hours looking for this online, then decided to implement it myself since it’s not that hard. Anyway, I thought I’d post it here for the next person trolling google for it. This is useful for generating credit card numbers or any other place you need a check digit.

This algorithm works by doing the same thing as the validation algorithm, but shifted over one character (the equivalent of adding a zero to a number, generating the sum & subtracting 10).

*EDIT – there was a bug in this code, i just fixed it on 12/4/2009, the old code returned 10 instead of zero. It also was only working on even numbers, sorry about that!

CREATE OR REPLACE FUNCTION generate_luhn (togen in varchar)
RETURN number
curval NUMBER := 0;
total NUMBER := 0;
everyother NUMBER := 1;

togen IS NULL
) then
end IF;

FOR i IN reverse 1 .. LENGTH(togen) loop
curval:=SUBSTR(togen, i, 1);
IF everyother = 1 then
everyother := 0;
curval := curval * 2;
IF(curval> 9) then
end IF;
everyother := 1;
end IF;

total := total + curval;
end loop;
if MOD(total, 10) = 0 THEN
RETURN 10 – MOD(total, 10);
END generate_luhn;

May 13th, 2009

Capturing Uncaught Java Exceptions in a Bash Shell Script

I was burned one to many times by chaining dependent java programs together in a bash script.

By default the java executable will return zero if the program exited manually, and 1 if there was an uncaught RuntimeException.

Let’s say test.sh runs a java program, we want to run it 3 times, but if it fails during any of the runs we want to abort our run.

java Test
echo “exit val: ${exitval}”
exit $exitval

Then in our shell script to run test.sh each time we’d do something like this.


if [ $step1 -eq “0” ] ; then
if [ $step2 -eq “0” ] ; then
if [ $step3 -eq “0” ] ; then
echo “Finished successfully.”
echo “Step 3 failed.”
echo “Step 2 failed with ${step2}”
echo “Step 1 Failed. with ${step1}”

To test my script I made a little java class that will error out half the time that looks like this:
import java.util.Random;
public class Test

public static void main(String[] args)
Random gen = new Random();
if(gen.nextInt(2) > 0)
System.out.println(“I Win!”);
throw new RuntimeException(“I Fail!”);


February 19th, 2009

Mylyn + Trac + wsgi

I am using mod_wsgi instead of mod_python for my trac .11 install.

I’m also using the AccountManager plugin for authentication, and don’t have anonymous access setup.

Anyway, authentication wasn’t working (through XML RPC) hence mylyn didn’t have the perms to do anything, and was silently defaulting to anon mode. even though I had the XML RPC plugin installed and permissions set up.

Here’s the steps to get them all playing nice (this was harder than it looked to figure out):

  1. Install/Enable the XML RPC Plugin for trac
  2. Give your authenticated group the XML_RPC permission
  3. Install/Enable the HttpAuth Plugin
  4. Add this line to your apache config for your site: WSGIPassAuthorization On

January 21st, 2009

Easily Creating Fixed Length Flat Files With Groovy

Here’s an (I think elegant) way to create flat files with Groovy.

I want this script to be easy to read by future developers. So if someone wants to know what the format of the file is they can see it just by opening the script.

Classes We’ll Use


  • SQL: We’ll use this library to access the database, it has some nice closure methods like eachRow which make iterating over results easy.
  • String: Groovy includes a string class that has some nice methods like padRight and padLeft, handy for making fixed length files.


  • File Writer: The easiest way to create files in java.

The Plan

Define our file in a map at the top of the file, it will be SQL Alias Name : Length.

Execute our SQL, for each row iterate through our file definition map and pad each value by the appropriate length.

The Code

Pretty self explanatory — a few neat groovy things we use in this script:

  • Triple quotes for a multiline string (our sql)
  • The format method to easily format a date (bye bye simple date format) for the file name
  • The elvis operator ?: makes the results null safe so we can call padRight.
  • The each method on Groovy’s Map that accepts a closure, we use that to iterate through our file definition.

import groovy.sql.Sql

import java.io.File;
import java.io.FileWriter;

def fileName = “FLATFILE” + String.format(‘%tY%
fileDef.each{ k,v-> writer.append ((row[k] ?: ” “).padRight(v))}
writer.append “\n”


January 14th, 2009

Writing Portable Groovy Scripts with A Magic Runnable Jar

I’ve found myself using Java as a “scripting” language lately due to the necessity of working in mixed *nix and windows environments where I can’t count on a standard scripting language being installed like python,perl, or ruby. What’s more I either don’t have the rights to install a binary of said scripting languages or don’t want to go through the bureaucracy to request the installation.

The benefits of using Java in these environments is I can package my program as a Runnable Jar easily through Eclipse’s Export functionality. I also have a wonderful wealth of libraries for doing almost anything I want — from FTP libraries, to any Database driver I can dream of. Anyone who’s had to deal with the freetds library in perl to connect to a MS SQL database knows what a huge boon being able to use JDBC is.

The cons of using Java is my code is not as terse as it could be, and for common scripting tasks like reading and writing files and database records a simple task now becomes tons of boiler plate exception handling and JDBC calls. I also have to re-compile and re-package my entire program if I want to make a simple change.

What if we could harness the power of any Java Library we wanted, no hassle deployment, but marry the simplicity of a scripting language and flexibility of an interpreted language?

Enter Groovy

Keep reading →

December 29th, 2008

Regular Expression to Replace System.out.println with Log4j

This is nothing fancy, but may help some of you log4j newbies.

I found myself replacing a project’s hideous System.out.println statements with log4j log calls.

Just do a File Search in eclipse , tick the regular expression box and do a Replace:

Find: System\.out\.println\(“(.+?)”\);
Replace: log.info(“\1″);

Now you just have to go in and add your imports and your log field variable to your classes.

October 16th, 2008

Reusing Annotation Based Controllers in Spring MVC

I was a late comer to the wonderful world of Spring. Seeing that I hate XML, I am very keen on using annotations everywhere! Everything I read said that using annotation based controllers wasn’t a good idea because they were hard to re-use due to their very fluid rules for method signatures.

The first thing I did in Spring MVC was to write the perennial CRUD application. After I was done the code was fairly clean but still contained plenty of boilerplate java code. So I wanted to make it generic so I could churn out CRUD’s for maintenance tables.


  1. Only write code that’s specific to the Entity I am dealing with in the controller.
  2. Make use of <Generics> so I don’t have to cast stuff like a caveman from 1997.
  3. Have spring figure out URI mapping based on controller names.
  4. Write zero bean specific xml.

Keep reading →

August 27th, 2008

Eclipselink in J2SE RCP Applications

In this article I’ll go over integrating EclipseLink into an RCP application. I will not explain every detail of the JPA specification however. The source code this article is partially based off of is currently pending IP Review for inclusion in the official EclipseLink project, however you may download it now from here. You can follow the inclusion status on the official bug report. If you just want to browse the source, check out the org.eclipse.* projects in our anon SVN. This example requires the OSGi branch of EclipseLink, which you can get here.

It requires a derby database which you can download from here, simply run the startNetworkServer.bat in the bin directory to start it.


Eclipselink is a part of the Eclipse Project. It’s an ORM that implements the JPA 1.0 specification, the main commiters on the project are Oracle employees, and Eclipselink is based on the mature Toplink library, in fact Toplink is being renamed Eclipselink for future releases.

Why use JPA?

JPA is the official persistence specification for EJB 3, it’s not going anywhere and you’re going to have an easier time finding other developers who are familiar with the JPA specification. What’s even better is the EclipseLink implementation is the JPA 2.0 reference specification, so there’s a good chance some features you enjoy in the EclipseLink implementation will be present in the JPA 2 specification as well.

Why bother with an ORM?

When you’re coding an RCP application, chances are you’re already passing around Java Beans that represent your data model. If you could have the mapping take place automatically from your database to java beans, it just makes your life that much easier. Once you add the power of libraries like Jface Databinding it’s hard to go back to any other method of data access.

Death to XML

Thankfully EclipseLink does not require much XML, the only xml file I create is a persistence.xml that simply lists all my entity classes. A nice editor is included with the JPA Tools plugin that makes managing it simple. All other configuration can be done via annotations.

Tons more after the Jump!
Keep reading →

April 29th, 2008

Easily Add PropertyChangeSupport to Beans in Eclipse

If you plan on using JFace Databinding in SWT you will have to implement Property Change Support on all your Java Beans, (barf). I wasn’t able to find a plugin that would do this in one click to a class so I came up with two ways to do it.

Here’s a code template you can use based on the cool trick I learned at Stuff That Happens

firePropertyChange("${enclosing_method_arguments}", this.${enclosing_method_arguments}, ${line_selection});

Just select the assignment part of your setter and use this template.

If you’re really lazy you can use a regex find and replace (With the File Search Dialog)

Your Find string will be:
(this.+) = (\w+);

And your replace string will be:
firePropertyChange("$2", $1, $1 = $2);

Both of these solutions assume you use an Abstract BaseClass to set up your firePropertyChange stuff, then extend for each bean.

Mine looks like this:

package com.weheartcode;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public abstract class BaseEntity {
private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(

* Adds the property change listener.
* @param listener the listener
public void addPropertyChangeListener(PropertyChangeListener listener) {

* Adds the property change listener.
* @param propertyName the property name
* @param listener the listener
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);

* Removes the property change listener.
* @param listener the listener
public void removePropertyChangeListener(PropertyChangeListener listener) {

* Removes the property change listener.
* @param propertyName the property name
* @param listener the listener
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {

* Fire property change.
* @param propertyName the property name
* @param oldValue the old value
* @param newValue the new value
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,

April 11th, 2008

Automatically Add JavaDoc Comments in Eclipse

Recently while preparing to contribute some code to Eclipse I had to add a copyright comment to the top of every class. Enter JAutodoc it will go through and add Javadoc comments and other automated comments, the update site is:


JAutodoc Preferences

February 15th, 2008

Limiting log4j SMTPAppender

I wanted to set a maximum amount of e-mails that SMTPAppender would send after filling up a mailbox with 30,000 damn e-mails.

Turns out it was pretty damn easy, I set up my appender in a java class, and the SMTP appender has a method called setEvaluator that takes an instance of TriggeringEventEvaluator, so I just created a quick inner class like so.

class LimitingEvaluator implements TriggeringEventEvaluator
private int sentEvents = 0;
private int MAX_EVENTS = 20;
public boolean isTriggeringEvent(LoggingEvent arg0) {
if(sentEvents >= MAX_EVENTS)
return false;
return true;


and when i set up my appender I do this
appender.setEvaluator(new LimitingEvaluator());

January 17th, 2008

Mass E-mailing, worth paying for a service

Most of us have written little mass e-mailing scripts in the past. No big deal, connect to SNMP, loop through our subscriber list, do our thang. But then the requirements come in: ”

“Hey, I need to know what person is clicking on that link, and what time they opened it.”

“How does it look in a hotmail account?”

“Does it pass every spam filter?”

Sure, we could code a little link pass through tool, encoded with the person’s e-mail address we sent it to, we could create test accounts at all the major e-mail services, we can make all sorts of intricate unsubscribe reports. But why would you want to do that? That’s Amateur Hour! You’ve got cool shit to build. The solution? CampaignMonitor.com. They charge $5 base fee for every “Campaign” you send, plus 1 cent per recipient. So to send to 45,000 people that costs you $50. Big woop.

Plus you get all sorts of intricate reports

Campaign Monitor Reports

They allow you to send full HTML with no size limit, you’re not limited to their templates. You also get a nice list manager with unsubscribe and subscribe features built right in.

For a $10 fee you can preview your design in like 10 different e-mail clients & services as well as test that your e-mail passes spam filter (hint: don’t put the word viagra in your e-mail).

CampaignMonitor also has relationships with major ISP’s so you know your e-mail will not be denied and your servers won’t be blacklisted. Plus the office/marketing drones love the reports and the flexibilitiy to send an e-mail campaign themselves.

So don’t waste time re-inventing the wheel dummy, pony up your whopper money and do it the easy way.

And no — they didn’t pay me anything for this post, though they can if they want!

January 2nd, 2008

Postgres select replace with regular expression

Postgres seriously kicks ass, here’s a quick way to do a substring based on a regular expression. A Co-worker wanted to get the first word out of a column that contained a sentence. If we were using something lame like oracle we’d have to do gymnastics to do that.

It’s a one-liner in postgres.

select substring(‘this is a sentence’ from ‘^(.+?)\\s.+$’)

So we’re selecting out the group that is a series of characters at the start of a line (column) before a space. Epic win.

October 18th, 2007

Upgrade to Gutsy Gibbon Fast

Everyone is excited that Ubuntu 7.10 is finally out. (Have a look at the new feature tour here.)

But the servers are getting hammered, so most people are waiting on the upgrade (voluntarily or otherwise).

No need to wait! The more people who download from Bittorent, the faster your speeds! Grab it while it’s hot!
Download a copy of the alternative i386 CD here via Bittorent.
Mac and 64 bit torrents can be obtained here.

Once you have the alternative CD, burn it, boot from it, select the upgrade option, and away you go.

Want to be environmentally friendly and save a CD? Try this:

  1. Mount the ISO to /cdrom:
    sudo mount -t iso9660 ubuntu-7.10-alternate-i386.iso /cdrom -o loop

    Using the full path and proper name for the ISO image.
  2. Open a terminal and type:
    sudo /cdrom/cdromupgrade

    Then the upgrade utility should launch and run from the CD.

Make sure you have the alternative CD and NOT the standard desktop CD.

Good luck and happy upgrading!