Caffeine Induced
The Weblog of Lance Hankins
 
Tuesday November 21, 2006
Oracle: Difference between two Timestamps, in seconds...

In oracle, if you subtract two timestamps, the result is of type "interval". Its somewhat cumbersome to get the total number of seconds this interval represents. Here's an Oracle function which will yield the total number of seconds between two timestamps :

Using the above, we can get the toal time in seconds, between two timestamp parameters (effectively converts the "interval" difference of the two timestamps into to the total number of seconds)

Posted by lhankins ( Nov 21 2006, 12:10:44 AM CST ) Permalink
Friday February 24, 2006
Rebuilding our Server...

We had a hard drive crash recently and have now rebuilt our blog server. We also took the opportunity to upgrade to the latest version of Roller. I'm really looking forward to the new features that prevent comment spam :)

Posted by lhankins ( Feb 24 2006, 01:27:20 PM CST ) Permalink Comments [1]
Thursday December 22, 2005
Tip: "right-click on file and send-to cygwin tail"

During a typical "development" day, there are many times when I want to "start a tail" on some arbitrary log file. Wouldn't it be nice if you could just right-click on a file in Windows Explorer and start a tail on it...? Here's how.

First, we create a simple 1 line batch file called "startTailOn.cmd". The batch file will expect a single parameter (the path to the file).

This batch file simply starts a new command prompt with a title of "tail on <path to supplied file param>", and then executes the cygwin tail command on the specified file.

Now we just need to add a shortcut to our batch file to our Windows "send to" menu (shown when you right click on a item in Windows Explorer). To add a new action to your "send to", you just need to add a shortcut to the action in the SendTo folder in your home directory (e.g. mine is at C:\Documents and Settings\lhankins\SendTo).

So in keeping with our earlier example, we'll add a shortcut here to our "startTailOn.cmd" file (as depicted below).

screenshot

Note - you can rename the default shortcut name of "Shortcut to startTailOn.cmd" to just "tail".

That's it. Now suppose we right click on d:\temp\boot.log and select our new "send to tail" action :

screenshot

The only shortcoming here is that the batch file we created will barf if the path parameter has spaces in it (the part that fails is the 'window title' argument to the 'start' command). I generally keep a second batch file on my SendTo menu to handle these cases, e.g. "startTailOnPathWithSpaces.cmd".

That's all.

Posted by lhankins ( Dec 22 2005, 12:00:01 PM CST ) Permalink
Wednesday August 24, 2005
Easy way to test your equals() and hashcode() from Junit...

If you've been doing Java long enough, chances are you've been bitten by an incorrectly written equals() method.

Here's a quick way to increase your test coverage by easily adding junit tests to ensure equals() and hashcode() are consistent for two identical instances of a given concrete class.

Add the following to one of your abstract TestCase classes (in most of my projects, I have an abstract Junit test case per layer - e.g. AbstractDaoTestCase) :

Now, in your concrete testcases, you can utilize the above method in the following way :

The only requirement is that your concrete class ("Person" in this case) must implement Serializable. Note the call to Util.deepCopyBySerialization() - this method simply serializes the supplied object to a ByteArray and then reads a duplicate of it back in (implementation shown below) :

Posted by lhankins ( Aug 24 2005, 12:00:00 AM CDT ) Permalink Comments [2]
Saturday August 13, 2005
On the improved JS Support in IDEA 5.0...

I got <really> excited when I found out that Intellij was adding support for JavaScript and CSS to IDEA 5.0. I was particularly intrigued by the idea that all the refactorings would be available for JavaScript (e.g. rename method, rename class, find-usages, etc).

Alas, the 5.0 support for JavaScript isn't even close to being that sophisticated. You can do a few simple things, but nothing close to what you can do from the Java side when it comes to refactoring.

After overcoming my initial disappointment (and using 5.0 for a few days), I do believe the new version is a step in the right direction, however. Here are a few of the new features that I find helpful :

  • You can do <ctrl-space> completion for local variable names
  • It will let you refactor-rename variables / function parameters (<shift-f6>). It will NOT let you do this for methods.
  • It will flag duplicate variable declarations as errors.
  • In general - its fairly good at highlighting syntax errors for you (an improvement over their previous JS support).
  • They added the block comment hotkeys (<ctrl-shift-/>).
  • They now treat JS that is inlined in a JSP like first class JS (parse it, syntax highlight it, etc).
  • You can do a limited form of "find-usages". Its limited to the class level, however, not the method level.

In summary, I think what they've added is a step in the right direction, but there is certainly room for a ton of improvement in this area. I realize that some of the slick refactorings they have on the Java side are much more difficult in JavaScript, since everything's so loosely typed. Nonetheless, here are a few things I'd like to see in future builds :

  • Find usages on methods
  • Refactor-rename methods
  • Show inheritance chain for classes
  • Typing "this." should do a better job of popping up all the known methods for the current object. For example, if I type this from a class method, it should at least show all available methods from the current class and any superclasses.
  • CTRL-F12 from the JS file should give you a list of methods like it does in Java.
Posted by lhankins ( Aug 13 2005, 12:00:00 AM CDT ) Permalink
Saturday July 30, 2005
More Readable Classpaths for Ant Builds...

Its always a bit painful when you hit the inevitable ClassNotFound exception during development and you end up double checking an Ant classpath variable against the jars you think should be there. The way I typically debug this is to assign the particular ant classpath to a property, then echo the property value, e.g. :

Which will end up producing output that looks something like this (a big, semicolon delimited list of jars) :

That format gets a little painful to scan with the human eye, since all the paths typically begin with the same root. So here's a better way to do this : We'll use the <propertyregex> ant task to replace the semicolons in our debug variable with a newline and some whitespace.

Now when we run the aforementioned Ant snippet, it will produce the following output (much easier on the eyes) :

Posted by lhankins ( Jul 30 2005, 12:00:00 AM CDT ) Permalink
Sunday July 10, 2005
Yahoo Desktop Search - Better than Google Desktop Search...

I recently switched to Yahoo Desktop Search. I find it superior to Google Desktop Search (which I had been using for 4-5 months prior to discovering Yahoo's version). There are several things about the Yahoo version that I find more appealing :

  1. It supports many more filetypes than Google Desktop Search
  2. You can configure it to index additional filetypes (e.g. I've added .java, .jsp, .js, .html, .css, .xml, etc). This is a big advantage for me, since, while doing development, I often have the feeling "OK - I've done something like this before, but which project was it in...?" Being able to index all my development related content is a huge plus for me.
  3. The index files Yahoo creates seem to be much smaller. I used Google Desktop Search for several months before realizing that it had created 3.7 GB worth of index files on my local filesystem (yikes!). Yahoo indexes more content (since it supports so many more filetypes) and I seem to be hovering right around 1.15 GB of index files.
Posted by lhankins ( Jul 10 2005, 12:00:00 AM CDT ) Permalink Comments [1]
Monday July 04, 2005
Subtle Difference Between JavaScript Evaluation in IE vs. Firefox...

I came across an annoying difference between the way IE and Firefox interpret JavaScript array lengths. Consider the following snippet:

So basically the only difference between a1 and a2 is that a2 has a trailing comma at the end of the array initializer (legal in both Java and JavaScript). Anyway, in Firefox, the lengths of these two arrays are the same (as I would expect), the alert evaluates to :

a1.length = [4], a2.length = [4]

In IE, however, the lengths are different (it actually increases the size of the array for the trailing comma - <sigh>)

a1.length = [4], a2.length = [5]

Ggggrrrrr....

Posted by lhankins ( Jul 04 2005, 06:00:17 AM CDT ) Permalink Comments [2]
Sunday February 20, 2005
Great RegEx Test Utility...

Nithy recently pointed me at a great interactive tool for testing and debugging Regular Expressions. Its called "RegEx Coach" (screenshot shown below). I find it very helpful because it lets you put sample text in the bottom pane, and then as you type the regex in the top pane, it highlights what portion of the sample text is matched so far. Thus far its proven very useful :)

Posted by lhankins ( Feb 20 2005, 12:00:00 AM CST ) Permalink
Monday September 27, 2004
"Simplicity is Complexity Resolved..."

A few weekends ago, I had the chance to visit the Nasher Sculpture Center here in Dallas. Like many museums, they give you a set of headphones and a portable media player which you can use to listen to additional information regarding the artists on display (you type in the number on the particular piece you're looking at, and it dials up an audio clip on that particular work / artist).

While wandering through the exhibit, I came across some of Constantin Brancusi's work. Constantin Brancusi was an artist back in the early 1900's. He was famous for his simplistic, yet elegant style. One of his most famous works is entitled "The Kiss" (picture shown below) :

The audio clip for this work struck a chord with me because his commentary on simplicity certainly has relevance when applied to the discipline of software engineering. The clip for this particular sculpture was quoting Brancusi on simplicity :

"Simplicity is not an end in art, but one arrives at simplicity in spite of oneself in drawing near to the reality in things."

And, more succinctly :

"Simplicity is Complexity Resolved..."

I've always felt that good designers should be uncomfortable with complexity (this is not to say that they shouldn't be capable of dealing with it, but rather that they should strive to eliminate it at every turn). This natural discomfort constantly spurs them to "look for a better way".

During the development process, if you find that things are becoming overly complex, perhaps you've missed an abstraction. Maybe its time to stop and refactor... Personally I find it very satisfying when I refactor something that was becoming too complex into something that is dramatically simpler. In some ways, this sort of conceptual reduction seems analagous to reducing a mathematical equation into its simplest form.

Ahh... the joys of our craft ;)

Posted by lhankins ( Sep 27 2004, 12:00:00 AM CDT ) Permalink
Wednesday March 10, 2004
Diffing two files in IDEA...

IDEA has an excellent diff facility, complete with syntax highlighting and merging capability. I often use this for comparing two versions of the same file from CVS, but until yesterday, I didn't realize you could use this same facility to compare ANY two files (not just from source control). Just multi-select two files in the project view, right click and select "Compare Two Files"...

I like to keep a scratch directory in all my IDEA projects which is not committed to source control. It'll have files in it like "scratch1.xml", "scratch2.xml", etc (very easy to get to with ctrl-shift-N). Using these and IDEA's "diff two files" facility - its pretty easy to compare expected vs. actual output (useful for comparing SOAP requests, other generated XML documents, etc).

I guess sometimes it pays to read the directions ;-)

Posted by lhankins ( Mar 10 2004, 12:00:00 AM CST ) Permalink
Sunday February 01, 2004
An Ant Target that every project should have...

An ant target that every project should define is one which allows you to backup your source code (without neccessarily committing it). This is useful for a variety of reasons (lots of work done but not ready to commit, about to do a that big merge you've been dreading for a while, paranoid about the unusual noises coming from your hard drive, etc). Here's the target I've been using on my last several projects.

It basically just creates an archive of all source/config/buildfiles from your project in the directory defined by ${dir.backup.src}. The archive will follow the format : <project-name>-src-yyyyMMdd-hhmm.zip

Posted by lhankins ( Feb 01 2004, 12:00:00 AM CST ) Permalink Comments [1]
Saturday January 31, 2004
Useful Webpage Debugging Tools...

I stumbled across these bookmarklets over on the CSS Discuss Wiki. They are a collection JavaScript utility functions which are useful for debugging webpages during development. The idea is you bookomark them in your browser, then you can use them to access/manipulate/debug the currently viewed webpage.

Some of them are very useful - for example, the generated source bookmarklet will popup a new window with the actual HTML source for the DOM tree of the current webpage (very useful if portions of the page in question were generated by JavaScript - which means there was no "original" source).

There is also a Shell bookmarklet which will pop open a JavaScript shell which has full access to the currently viewed page.

Posted by lhankins ( Jan 31 2004, 12:00:00 AM CST ) Permalink
Monday January 26, 2004
Ant 1.6 import tag (#include one ant file into another)...

Ant 1.6 has a new <import> tag which allows you to "#include" one ant file into another. There was a kludgy way of doing this with older versions of Ant (using the entity includes trick) but I was never really that crazy about it (seemed like it always caused IDEA to freak out).

Here's a discussion of this feature from the Ant Wiki.

I started using this on my current project and have been pretty pleased so far. We have a top level build.xml, and a build.xml file for each subsystem. We pulled out the common properties and paths into separate files (CommonAntProperties.xml and CommonAntPaths.xml) and use the <import> task to import them where needed. I guess my only complaint thus far is that IDEA doesn't seem to support the import tag. It flags all usages of properties which are contained in my CommonAntProperties.xml file as errors in any of the main build.xml files. Oh well - it can't be perfect. On a positive note, I noticed IDEA does have tab completion support for ant build.xml files (for the variables it does recognize). Just hit CTRL-SPACE after you do ${ and it will pop-up a list of know variable names.

Posted by lhankins ( Jan 26 2004, 12:00:00 AM CST ) Permalink Comments [1]
Wednesday June 25, 2003
Automated nightly defrags on Windows XP...

On my machine I have two partitions : C & D. The C partition has the OS, all Programs like Office, IDEA, Oracle, etc on it. The D drive just has data - mostly source code, project directories, personal files, etc.

I’ve noticed that when I’m doing development, the D drive gets fragmented very quickly (even in one days time). After a full day's coding, I usually manage to create 1000 or so fragmented files (even when the machine has been completely defragged the day before). This is after doing full rebuilds, re-installations of the appserver (we have an ant target to completely wipe out our Jboss directory and re-install it), pulls from CVS, etc..

Anyway – you can setup Windows built in defrag tool to run on a recurring basis. Here’s how :

Create a batch file (e.g. c:\bin\DefragLocalDrives.cmd) with the contents similar to the following (customize to your local drive structure):

Now create a scheduled task to run this batch file. Goto Start | Control Panel | Scheduled Tasks and click on “Add Scheduled Task” Point the new scheduled task at the batch file you created in step 1 and set up the recurrence to your preferred frequency.

I set mine up to run every morning at 4:30 AM. At first I thought this would be overkill, but I’ve noticed that when doing development I always have some fragmentation, even after only a day’s use.

That’s it…

Posted by lhankins ( Jun 25 2003, 12:00:00 AM CDT ) Permalink