Skip to content

By Programmer For Programmer

Here I lay down the useful tips, tricks and utilities for programmers like myself.

We spin up and down a lot of EC2 instances at my job, and I wanted to share my SSH Config that makes life easier:

$ cat ~/.ssh/config
Host *amazonaws.com
  IdentityFile ~/.ssh/organization.pem
  User ec2-user
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Here’s what it does:

  • IdentityFile: Makes it so I don’t have to use the -i switch every time. The identity file is from the EC2 security group we use, so it’s always the same.
  • User: The AMI we use always has the user “ec2-user”
  • StrictHostKeyChecking: By setting this to “no”, you won’t get warnings about how the ip address has changed. This is an issue with EC2 since they re-use ips, and we use scripts to bring up and down machines, so we can’t have the scripts expecting any input
  • UserKnownHostsFile: This actually prevents the previous bullet point from happening. This says don’t every try to remember the host

So what is the result of this? Well, given an EC2 instance I just spun up, all I do to log in is:

$ ssh ec2-54-245-6-48.us-west-2.compute.amazonaws.com

Much easier!

We use Chef Server at work, but many times I want to use Chef Solo for some local Vagrant testing.

I created this little handy function that will sync roles, data bags, or environments to your current directory:

# Sync "role", "data bag", or "environment" using knife in the current dir
function knifesync() {
  rm -f *.json
  knife $@ list | while read line
  do
    if [ "$1" == "data" ]; then
      rm -Rf $line
      mkdir $line
      knife data bag show $line | while read item
      do
        echo "data bag: $line.$item"
        knife data bag show $line $item --format=json > $line/$item.json
      done
    else
      echo "$@: $line"
      knife $@ show $line --format=json > $line.json
    fi
  done
}

Put this in your .bash_profile, then use it like so:

$ knifesync role          #sync roles to the current directory
$ knifesync data bag      #sync databags to the current directory
$ knifesync environment   #sync environments to the current directory

This function deletes any .json files already in the directory during sync, so just be careful.

Preamble

I shared this with the DWR mailing list, and the guys thought it was useful, so I’m posting here to share with the world.

Feel free to use this code under the Open Software License v. 3.0 (OSL-3.0).

Background

I wanted to have a “show technical details” link on our error popup that said what happened when an error occurred.

Environment:

  • DWR 2.0.5
  • ExtJS 3.3.1
Three classes:
  • BatchMapObtainer - This class retrieves the “batch map” from the current error handler callstack
  • BatchMapWrapper - Wraps the “batch map” in order to wrap DWR-specific details
  • ErrorHelper - Uses the above in order to construct a message for the user

The Code

FILE: BatchMapObtainer.js
Ext.ns('jx.core.dwr');

/**
 * @class jx.core.dwr.BatchMapObtainer
 * @static
 */
jx.core.dwr.BatchMapObtainer = function() {

	var isArgDwrBatch = function(arg) {
		return Ext.isDefined(arg.httpMethod) && Ext.isDefined(arg.map);
	};

	var getDwrBatchFromArguments = function(args) {
		for (var i=0; i<args.length; i++) {
			if (isArgDwrBatch(args[i])) {
				return args[i];
			}
		}
		return null;
	};

	var getBatchMapFromCallStack = function() {
		var currentFn = arguments.callee;
		var dwrBatch;
		var visitedFunctions = [];
		while (currentFn && !dwrBatch) {
			dwrBatch = getDwrBatchFromArguments((currentFn.caller && currentFn.caller.arguments) || []);
			if (visitedFunctions.indexOf(currentFn) !== -1) {
				currentFn = null;
			}
			else {
				visitedFunctions.push(currentFn);
				currentFn = currentFn.caller;
			}
		}
		return dwrBatch && dwrBatch.map;
	};

	return {

		/**
                 * @method buildFromCallStack 
                 * @static
		 * Tries to obtain a DWR batch map from the current call stack.
		 */
		obtainFromCallStack: function() {
			return getBatchMapFromCallStack();
		}

	};

}();
FILE: BatchMapWrapper.js
Ext.ns('jx.core.dwr');

/**
 * @class jx.core.dwr.BatchMapWrapper
 * @extends Object
 * Use to parse parameters from the DWR batch map.
 */
jx.core.dwr.BatchMapWrapper = Ext.extend(Object, {

	batchMap: null,

	constructor: function(batchMap) {
		this.batchMap = batchMap;
	},

	getBatchId: function() {
		return this.batchMap.batchId;
	},

	getBatchMap: function() {
		return this.batchMap;
	},

	/**
	 * @return Array of call details.  Each detail has "method" and "params" attributes
	 */
	getDwrCallDetails: function() {
		var callDetails = [];
		for (var i=0; this.hasCallDetailsFor(i); i++) {
			var params = [];
			for (var j=0; this.isParamDefined(i, j); j++) {
				params.push(this.getParamAt(i, j));
			}
			callDetails.push({
				method: this.getDwrCallNameFor(i),
				params: params
			});
		}
		return callDetails;
	},

	// private
	hasCallDetailsFor: function(callIndex) {
		return Ext.isDefined(this.batchMap['c' + callIndex + '-id']);
	},

	// private
	isParamDefined: function(callIndex, paramIndex) {
		return Ext.isDefined(this.getParamAt(callIndex, paramIndex));
	},

	// private
	getParamAt: function(callIndex, paramIndex) {
		return this.batchMap['c' + callIndex + '-param' + paramIndex];
	},

	// private
	getDwrCallNameFor: function(callIndex) {
		return this.getScripNameFor(callIndex) + '.' + this.getMethodNameFor(callIndex);
	},

	// private
	getScripNameFor: function(callIndex) {
		return this.batchMap['c' + callIndex + '-scriptName'];
	},

	// private
	getMethodNameFor: function(callIndex) {
		return this.batchMap['c' + callIndex + '-methodName'];
	}

});

/**
 * @method buildFromCallStack 
 * @static
 * Tries to obtain a BatchMapWrapper wrapped around the "batch map" for the current call stack
 */
jx.core.dwr.BatchMapWrapper.buildFromCallStack = function() {
	var batchMap = jx.core.dwr.BatchMapObtainer.obtainFromCallStack();
	return batchMap ? new jx.core.dwr.BatchMapWrapper(batchMap) : null;
};
FILE: ErrorHelper.js
Ext.ns('jx.core.dwr');

/**
 * @class jx.core.dwr.ErrorHelper
 * @static
 * Helper to gain information about DWR calls.
 */
jx.core.dwr.ErrorHelper = function() {

	var getBaseDwrCallMessage = function(batchMapWrapper) {
		var message = '';
		var dwrCallDetails = batchMapWrapper.getDwrCallDetails();
		if (Ext.isArray(dwrCallDetails)) {
			for (var i=0; i<dwrCallDetails.length; i++) {
				var details = dwrCallDetails[i];
				message += "DWR Call: " + details.method + "\nParams: " + Ext.util.JSON.encode(details.params);
			}
		}
		return message;
	};

	var getFullBatchMapMessage = function(batchMapWrapper) {
		var message = "Batch Map:";
		var batchMap = batchMapWrapper.getBatchMap();
		for (var key in batchMap) {
			if (!Ext.isFunction(batchMap[key])) {
				message += "\n   " + key + ": " + batchMap[key];
			}
		}
		return message;
	};

	return {

		getDwrCallDetailsFromCallStack: function() {
			var batchMapWrapper = jx.core.dwr.BatchMapWrapper.buildFromCallStack();
			return batchMapWrapper && batchMapWrapper.getDwrCallDetails();
		},

		buildDwrCallDetailsMessageFromCallStack: function() {
			var message = '';
			var batchMapWrapper = jx.core.dwr.BatchMapWrapper.buildFromCallStack();
			if (batchMapWrapper) {
				message += getBaseDwrCallMessage(batchMapWrapper);
				message += "\n\n";
				message += getFullBatchMapMessage(batchMapWrapper);
				message += "\n\n";
			}
			return message;
		}

	};

}();
Example Usage:

In DWR errorHandler, simply:

var msg = jx.core.dwr.ErrorHelper.buildDwrCallDetailsMessageFromCallStack();
var id = Ext.id();
var html = '<a href="" onclick="document.getElementById(\'' + id + '\').style.display=\'block\'; return false;">' +
	'Show Technical Details</a><br />' +
	'<div id="' + id + '" style="display: none;"><textarea style="width: 100%; height: 150px;">' + msg +
	'</textarea></div>';
Ext.Msg.alert("Error", html);

Good luck!

If you are like me, you work on a web application, and every time you edit Java classes, you need to restart your server to see the changes.  Sometimes it takes awhile to get back to where you were in the app to test, which makes this process especially painful and time-consuming.

Well I just stumbled across this amazing tool the other day called JRebel.  Basically it let’s you setup your Eclipse environment so that when you save Java files (or more specifically, when it picks up on new compiled classes), it redeploys the changes to your environment, and they take affect immediately.

I was especially shocked when I changed one of my Spring XML files, and it rebuilt and redeployed my Spring config!

This has already saved me tons of time in the short while I’ve been using it.  You should really check it out.

http://www.zeroturnaround.com/jrebel/

(PS: I don’t get anything for recommending JRebel… I was just really impressed by it)

 

 

 

Background

So at my current job at jamasoftware.com, our Contour application supports the three major databases MySQL, SQL Server, and Oracle (sorry PostgreSQL fans… maybe soon).  Recently I needed to run UPDATE statements with JOINs, and had to learn the different syntax for each of these databases.  I thought I would share all of them side-by-side, so that you can easily see the differences:

Our Simplified Schema:
Table: ORGANIZATION
id int primary key

Table: PROJECT
id int primary key
organizationId int foreign key ORGANIZATION(id)

Table: DOCUMENT
id int primary key
projectId int foreign key ORGANIZATION(id)
organizationId int null
The Task

Update all DOCUMENT records with the organizationId of their corresponding project

The Solution
MySQL:
UPDATE DOCUMENT d
    INNER JOIN PROJECT p ON d.projectId = p.id 
SET d.organizationId = p.organizationId 
WHERE d.organizationId IS NULL;

SQL Server:
UPDATE d
SET d.organizationId = p.organizationId 
FROM DOCUMENT d
    INNER JOIN PROJECT p ON d.projectId = p.id 
WHERE d.organizationId IS NULL

Oracle:
UPDATE (SELECT d.organizationId AS docOrgId, p.organizationId AS projectOrgId 
        FROM DOCUMENT d
            INNER JOIN PROJECT p ON d.projectId = p.id 
        WHERE d.organizationId IS NULL) v 
set v.docOrgId = v.projectOrgId

We had an issue at work recently where our MySQL transactions weren’t being properly rolled back or committed, and we ended up with transactions that would last several hours, locking tables, until we killed them manually. After fixing the true issue, I wrote this script to check MySQL InnoDB status every 15 minutes (with cron) and email us if there are any transactions lasting longer than 15 seconds.

For testing, you can also pass in as the first argument a file name that contains some example “show innodb status” output to make sure it works.

#!/usr/bin/sh
DB_USER=root
DB_PASS=password
STATUS_FILE=/tmp/innodb_status.out
TMP_FILE=/tmp/long_tx_check.out
MAIL_TO="email1@asdf.com;email2@asdf.com"
TX_THRESH=15

if [ -z $1 ]
then
	mysql -u $DB_USER -p$DB_PASS -A -e "show innodb status\G" > $STATUS_FILE
else
	STATUS_FILE=$1
fi

cat $STATUS_FILE | awk "/TRANSACTIONS/,/FILE I/ { t=1 } /ACTIVE/ { if (\$5 > $TX_THRESH && t == 1) { on=1 } else { on=0 } } /thread id/ { if (on==1) { print \$0 } }" > $TMP_FILE

FIRSTLINE=""
read -r FIRSTLINE < "$TMP_FILE"
if [ "$FIRSTLINE" != "" ]
then
	echo "LONG RUNNING TRANSACTION FOUND... SENDING EMAIL"
	mail -s "LONG RUNNING TRANSACTION!!" "$MAIL_TO" < $TMP_FILE
else
	echo "No long running transactions found"
fi

This code was based off code taken from here. Always give credit where credit is due :)

Here’s the situation:

  • Auto-flush is OFF (either FlushMode.COMMIT or FlushMode.MANUAL)
  • You save() or saveOrUpdate() an object with Hibernate
  • Your object is not using IDENTITY for its id
  • Later (possible unrelated) code tries to get the saved object back by id

If you found this post, you probably know that no object is returned with the given id.  This is because the session has not been flushed.

So just flush the session before retrieval, right?  Well yes, but not so fast.  Most people, including myself, don’t want to have a big “session.flush()” in the middle of service layer code.

My solution was to register a “save” and “save-update” event listener with Hibernate, and process the insert immediately after.

Register lisenters (using Spring):

<bean id="sessionFactory">
    <property name="eventListeners">
        <map>
            <entry key="save">
                <bean class="com.jamasoftware.contour.util.hibernate.ExecuteImmediateSaveEventListener"/>
            </entry>
            <entry key="save-update">
                <bean class="com.jamasoftware.contour.util.hibernate.ExecuteImmediateSaveOrUpdateEventListener"/>
            </entry>
        </map>
    </property>
</bean>

Listeners:

public class ExecuteImmediateSaveEventListener extends DefaultSaveEventListener {

	private static final long serialVersionUID = 1L;
	
	protected Serializable performSaveOrReplicate(
			Object entity,
			EntityKey key,
			EntityPersister persister,
			boolean useIdentityColumn,
			Object anything,
			EventSource source,
			boolean requiresImmediateIdAccess) {
		Serializable id = super.performSaveOrReplicate(
				entity, 
				key, 
				persister, 
				useIdentityColumn, 
				anything, 
				source, 
				requiresImmediateIdAccess);
		if (!useIdentityColumn) {
			immediatelyInsert(source);
		}
		return id;
	}

	private void immediatelyInsert(EventSource source) {
		source.getActionQueue().executeInserts();
	}
	
}
public class ExecuteImmediateSaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {

	private static final long serialVersionUID = 1L;
	
	protected Serializable performSaveOrReplicate(
			Object entity,
			EntityKey key,
			EntityPersister persister,
			boolean useIdentityColumn,
			Object anything,
			EventSource source,
			boolean requiresImmediateIdAccess) {
		Serializable id = super.performSaveOrReplicate(
				entity, 
				key, 
				persister, 
				useIdentityColumn, 
				anything, 
				source, 
				requiresImmediateIdAccess);
		if (!useIdentityColumn) {
			immediatelyInsert(source);
		}
		return id;
	}

	private void immediatelyInsert(EventSource source) {
		source.getActionQueue().executeInserts();
	}
	
}

This code was tested with Hibernate 3.3.1.

This problem was especially annoying to me because I work with an application that can be deployed on many databases. MySQL and SQL Server, for example, allow IDENTITY columns, which Hibernate will immediately execute INSERT statements for since it needs to in order to get their id. But Oracle doesn’t work this way, and the execution of the INSERT statements goes onto the ActionQueue, since it uses a SEQUENCE. What happens then, is that you end up with code that works for one database that doesn’t work for another.

Using the above listeners, my code now acts the same whether using IDENTITY columns or not.

Just wanted to give props real quick to the awesome profiler JProfiler (http://www.ej-technologies.com/products/jprofiler/overview.html) for providing me with an open-source license to profile my open-source project HTML4Java (https://sourceforge.net/projects/html4java/). The open-source community is so friendly :) .

Ever wanted to continue from a doubly-nested for loop? How about break from a switch statement more than one level deep?

Simple! First you have to label your loop, and then put that label name after the break or continue keyword.

“continue” example:

fooloop: for(foo: foos) {
    ...
    barloop: for(bar : bars) {
        ...
        for (blotto : blottos) {
            ...
            if (next_innermost_loop)
                continue; //normal
            if (next_middle_loop)
                continue barloop; //goes to the next iteration of "barloop"
            if (next_outer_loop)
                continue fooloop; //goes to the next iteration of the outermost loop, "fooloop"
        }
    }
}

“break” example:

fooswitch: switch(foo) {
    case 1:
        ...
        break;
    case 2:
        ...
       switch(bar) {
            ...
            if (break_this_switch)
                break; //normal
            if (break_outer_switch)
                break fooswitch; // breaks out of the out switch, "fooswitch"
        }
}

While most might consider this bad practice, when writing some scratch code to do some one-off task that only you will ever use, sometimes it’s nice to use a shortcut :) .

Every time I feel like I know all the Java syntax there is, I learn something new :)

Let’s say you have a class with a static constructor that returns a map, and that map is typed and does fancy things, but you still want to use generics from wherever you are using the Map.

public class MyClass {

    public static final <K,V> Map<K,V> createMyMap() {
        ...
        return map;
    }
}

If you are like me, you hate hate hate casting to generics, but you are forced to do that or have a big ugly @SuppressWarnings("unchecked") on the method that you access this method.

@SuppressWarnings("unchecked")
function Map<String, Object> getMyMap() {
    return MyClass.createMyMap();
}

Well not anymore!. Today I just learned (by looking through some Java source code) that you can assign generics on your static calls by using the following syntax.

function Map<String, Object> getMyMap() {
    return MyClass.<String, Object>createMyMap();
}

Just put the generics right after the period and before the method name. Easy.

I learned this while trying to figure out why java.util.Collections has static fields EMPTY_LIST, EMPTY_SET, and EMPTY_MAP, and also static methods emptyList(), emptySet, and emptyMap. It says right in the code, “Unlike this method, the field does not provide type safety”.