Skip to content

Pimp My Emacs Post 1

I’ve been moving back to emacs, the editor of my youth.

I’m not a vehement emacs evangelical, and I don’t really hate eclipse. But I’m moving back for rational reasons. Basically, I just don’t like to wait to write code.

At my current client, I have a slow machine. This isn’t uncommon for consultants. It’s absolutely the dumbest thing in the world for a company to do- give their developers underpowered machines. It’s amazing how often it happens, but it’s a fact of life. My system has 2 GB of RAM. Once I have Eclipse, Hsqldb, the Rest server and web server all running I’m basically out of memory. Add all the utility apps and it’s completely spent.

Of course the number one use of memory on my system is Eclipse.

To really switch from eclipse though, emacs will have to do a lot.

For right now, I just wanted to add a mention of my new favorite mode in emacs- org mode.

I’m still a long way from using this effectively- I don’t have the keys memorized, and I still mostly use my dry erase board and notebook instead of jumping to org-mode right away. In short, it’s not ingrained as a habit yet.

One thing I’ve done with it that I am happy with, is that I save my org files to my DropBox, so I can keep my org files synchronized across computers. DropBox is a really slick app- and has clients for all platforms!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Upgrading to Hsqldb 2rc8, part 1, Maven Integration

At my current client engagement, we make full use of hsqldb both in our unit tests and for local development. It’s a great tool, and the changes made since the last release 4 long years ago will make it an even better tool.

My project has hundreds of tests making use of hsqldb, and getting the new version up and running is no small task. I’ve broken this post into two. The first will be the steps I took to get hsqldb integrated into our maven repository. The second will cover getting hsqldb up and running with our legion of tests.

The first important thing to know when upgrading hsqldb, is that the pre-compiled jar files were built with jdk6. This is probably going to be fine for many, but there are many others, such as myself, who would like to be on jdk6 but work at a client site still using jdk5. It sucks to be me, but oh well.

So the first step for me will be to compile the jars myself with jdk15.

To do this, it’s necessary to have 3 environment variables set, JAVA_HOME, ANT_HOME, and ANT_OPTS. Pretty standard. If you’re on windows, there is a setenv.cmd file in the distributed build directory that you can edit and execute in the cmd shell.

At present, the build.xml contains a small bug for users trying to build the jar with jdk15. The “store” target contains a list of depends attributes:

depends=”switchtojdk11,switchtojdk12,switchtojdk14,switchtojdk16″

To build with jdk15 this must be changed to:

depends=”switchtojdk11,switchtojdk12,switchtojdk14,switchtojdk15″

(I’ve added a bug for this on sf)

Next run the following ant tasks from the hsqldb/build directory:

ant hsqldb;
ant sqltool

This will build the jars to the hsqldb/lib directory.

Next step is to add the jars to my local maven repo.

mvn install:install-file -DgroupId=hsqldb -DartifactId=hsqldb -Dversion=2.0.0 -Dpackaging=jar -Dfile="/Documents and Settings/ujxh744/My Documents/Downloads/hsqldb-2.0.0-rc8/hsqldb/lib/hsqldb.jar"
mvn install:install-file -DgroupId=hsqldb -DartifactId=sqltool -Dversion=2.0.0 -Dpackaging=jar -Dfile="/Documents and Settings/ujxh744/My Documents/Downloads/hsqldb-2.0.0-rc8/hsqldb/lib/sqltool.jar"

and update our main pom.xml with the new version of hsqldb and with a new entry for the sqltool:

<dependency>
<groupId>hsqldb</group>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>sqltool</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>

And that’s it. You should be off to the races. Next up I’ll write about some of the sql changes.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Dreamhost Django Install, Revised Edition

I recently went back to doing some Django development after a long lapse, and decided to start by eating my own dogfood…

I started by upgrading Python for my Dreamhost user. I followed my own post, except that I used Python 2.6 instead of 2.5. I missed one step that was caught by a pingback post:

In addition to the steps I outlined before:

$ mkdir opt
$ mkdir downloads
$ cd downloads
$ wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz
$ tar xvzf Python-2.6.4.tgz
$ cd Python-2.6.4
$ ./configure --prefix=$HOME/opt/ --enable-unicode=ucs4
$ make
$ make install

Next append

export PATH=$HOME/opt/bin/:$PATH

to your .bash_profile. Then test (from your home dir):

$ source .bash_profile
$ python -V
Python 2.6.4

If you don’t see Python 2.6.4 after typing python -V then you’ve already done something wrong.

I had to install the Python setuptools:

$ wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg
$ sh setuptools-0.6c9-py2.6.egg

before installing Mysql-Python as before:

$ wget http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz
$ tar xvzf MySQL-python-1.2.2.tar.gz
$ cd MySQL-python-1.2.2
$ python setup.py install

So this brought me to the point where I had an updated version of Python on my Dreamhost account.

Next to set up Django.

I looked at setting up Django with the Dreamhost script, which didn’t seem to work for me but I don’t remember why not. In the end I found SOASI’s post the most useful, although I did it a little differently.

First up, configure your domain.

In Dreamhost panel -> Domains -> Manage Domains click on the domain you want to put your app on (in the example we will set domain.com), and activate “Ruby on Rails Passenger (mod_rails)” option.

Create a public directory in that domain.

Next make sure you have your database setup. You don’t have to have a database for Django now, but if you plan to use one now is a good time to get that set up.

Next I downloaded and installed Django.

I liked the directory structure suggested by Dreamhost. This will work well for having one Django directory and use it for multiple projects on multiple domains.

 /home/username/django/
   source (your django svn checkout, or skip it and use Dreamhost's site-package of Django)
   projects
   applications (any reusable applications shared between projects)

Finally, I added the media and admin media, and also the passenger_wsgi.py

import sys, os
if sys.version &lt; &quot;2.6&quot;: os.execl(&quot;/home/user/opt/bin/python2.6&quot;, &quot;python2.6&quot;, *sys.ar
gv)
sys.path.insert(1, &quot;/home/user/django/source&quot;)
sys.path.insert(1, &quot;/home/user/django/applications&quot;)
sys.path.insert(1, &quot;/home/user/django/projects&quot;)
os.environ['DJANGO_SETTINGS_MODULE'] = &quot;project.settings&quot;
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

On /home/user/django/projects/myproject/settings.py file check settings:

MEDIA_ROOT = '/home/user/domain.com/public/media/'
MEDIA_URL = 'http://www.domain.com/media/'
ADMIN_MEDIA_PREFIX = '/admin_media/'

Create a symbolic link for admin_media:

cd /home/user/domain.com/public/
ln -s home/user/django/source/contrib/admin/media/ admin_media

Django and Python are still new to me, so you get what you pay for with this setup.

Note:
I found this today, and it looks like it could be a good setup too.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Accessing Drools 5 rules via a restful API using Drools Server and Groovy RestClient

If you haven’t heard about drools, you should have a look at it now. It provides multiple ways to write rules: DSL (drl files), groovy, java, excel spreadsheets or via a web interface (Guvnor). I recommend the book, Drools – JBoss Rules 5.0 by Michael Bali as its the only comprehensive documentation available on drools.

One of the lesser known features but extremely powerful is the Drools server that is provides a restful api to access and invoke a rules knowledge base. This is a war available in the Drools binary distribution. You would place the exploded version of this in your server (tomcat 6, in my case) and
— add the following jars in the war: drools-decisiontables.jar, drools-templates.jar, jxl.jar, commons-lang.jar, joda-time.jar

I’m using the same example used in the book. For the source code download here. Copy the following into Drools Server classpath retaining the package structure

— model classes from decision_tables project (droolsbook/decisiontables/bank/model/*)
— model classes from bankingcore project (droolsbook/bank/model/*) retaining the package structure
— add interestcalculation.properties in the classpath (WEB-INF/classes). Add the following
— newInstance = true
— file = -- copy interestcalculation.xls from decision_tables project (drools-book/decision_tables/src/main/resources)

Accessing a restful url with a standard request object that looks like:

<knowledgebase-request>
  <globals>
    <named-fact>
      <id>myglobal</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>42</carPrice>
        <carType>Saab</carType>
      </fact>
    </named-fact>
  </globals>
  <inOutFacts>
    <named-fact>
      <id>myfact</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>50</carPrice>
        <carType>BMW</carType>
      </fact>
    </named-fact>
  </inOutFacts>
  <inFacts>
    <anon-fact>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>55</carPrice>
        <carType>Audi</carType>
      </fact>
    </anon-fact>
    <anon-fact>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>65</carPrice>
        <carType>Mercedes</carType>
      </fact>
    </anon-fact>
  </inFacts>
  <queries>
    <query-type>
      <args>
        <string>one</string>
        <string>two</string>
      </args>
      <factNames>
        <string>ninsf</string>
      </factNames>
      <queryName>Get named inserted fact</queryName>
    </query-type>
  </queries>
</knowledgebase-request>

You would get a standard response back that looks like this:

<knowledgebase-response>
  <globals>
    <named-fact>
      <id>myglobal</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>42</carPrice>
        <carType>Saab</carType>
      </fact>
    </named-fact>
  </globals>
  <outFacts>
    <named-fact>
      <id>ninsf</id>
      <fact class="org.drools.server.InsertedFact">
        <name>one</name>
      </fact>
    </named-fact>
    <named-fact>
      <id>ninsf</id>
      <fact class="org.drools.server.InsertedFact">
        <name>two</name>
      </fact>
    </named-fact>
  </outFacts>
  <inOutFacts>
    <named-fact>
      <id>myfact</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>50</carPrice>
        <carType>BMW</carType>
      </fact>
    </named-fact>
  </inOutFacts>
</knowledgebase-response>

Chapter 11 of Michael Bali's book explains an example using a Ruby client. I'm going to explain with a Groovy client, instead.

import groovyx.net.http.*
import groovyx.net.http.ContentType
import groovy.xml.MarkupBuilder

droolsClient = new RESTClient( 'http://localhost:8080/drools-server-5.0.1/' )

def accountStr = new StringWriter()
def account = new groovy.xml.MarkupBuilder(accountStr)
account.'knowledgebase-request'(){
	inOutFacts() {
		'named-fact'() {
			id("account")
			fact(class:'droolsbook.decisiontables.bank.model.Account') {
				type("STUDENT")
				balance("1000")
				currency("EUR")
			}
		}
	}
}

println 'printing xml ... '
println accountStr

resp = droolsClient.post( path : 'knowledgebase/interestcalculation',
						  body : accountStr.toString(),
						  contentType: ContentType.XML,
						  requestContentType: ContentType.XML)

println "printing response ..."
println resp.data

The above code connects to drools server running on tomcat and expects a interestcalculation.properties rulebase, compiles the knowledgebase and fires all rules returning a response. In this example a student with a balance of 1000 EUR is returned with a 1.00 interest rate from the rule.

I ran into several problems while writing this simple groovy class (thanks to Groovy's horrible exception handling). For instance if I use '/knowledgebase/interestcalculation' instead of 'knowledgebase/interestcalculation' I get a strage error:
org.xml.sax.SAXParseException: The element type "HR" must be terminated by the matching end-tag "".

And if you don't provide contentType: ContentType.XML or requestContentType: ContentType.XML, you get a:
java.lang.NullPointerExceptionat groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setBody

Finally, once you get everything sorted out, the combination of a drools restful client and drools server seems to me an ideal solution to develop, deploy and release drools independently from any application. In fact the application need not know about rules or any of its configuration apart from the restful contract. Quite remarkable.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

an interesting class loading issue with quartz scheduler timing (NoClassDefFoundError)

My quartz job schedulers had a startDelay of 0 ms and a repeatInterval of 10 mins and 5 mins each. On my local environment (win/mac) and sandbox (unix) the scheduler works just fine. But in our production environment (unix), we were getting spurious java.lang.NoClassDefFoundError: Could not initialize class Foo. Now Foo was one of the many classes being called through the scheduler. In this case, I was calling a Foo.someStaticMethod.

After a whole day of debugging, this is what I found: What could be happening was that the job kicked off immediately at startup while the servlet class loader was still loading all classes and thus causing NoClassDefFoundError. Why is the scheduler unable to run after 10 mins, even though the classes should be loaded by then? I’m not sure but it could be that the failed Scheduler initialization causes repeated failed attempts with the same failed class, rather than looking up for a newly loaded class (jvm implementation?).

I added a startDelay of 6000 (1 minute) and it magically works now in all environments.

What’s surprising still is why are local envs and sandbox not having the same issues. It could be that the timing of classloader loading on local and sandbox env is a tad bit faster? I still have to figure that out …

My job schedulers had a startDelay of 0 ms and a repeatInterval of 10 mins (scheduleCertifiedAndRetryFaxes)and 5 mins (scheduleQueuedFaxes).

What could be happening was that the job kicked off immediately at startup while the servlet class loader was still loading all classes and thus causing ClassNotFoundException.  Why is the scheduler

unable to run after 10 mins, even though the classes should be loaded by then? I’m not sure but it could be that the failed Scheduler initialization causes repeated failed attempts with the same failed class,

rather than looking up for a newly loaded class (jvm implementation?).

I added a startDelay of 6000 (1 minute) and it works; fax jobs are fired correctly without any issues. (local, stage)

What’s surprising still is why are local envs and sandbox not having the same issues. It could be that the timing of classloader loading on local and sandbox env is a tad bit faster? I could be wrong.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Linux Banging

This is a great list of bang command usage.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Mcabber Install on Dreamhost

I’m reposting this, it’s what I used to install mcabber on dreamhost:

From your home directory:

mkdir bin lib tmp
chmod 777 tmp
chmod 755 bin lib
echo &amp;amp;quot;export PATH=$PATH:$HOME/bin&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
echo &amp;amp;quot;export PKG_CONFIG_PATH=$HOME/lib/pkgconfig&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
echo &amp;amp;quot;export LD_LIBRARY_PATH=$HOME/lib:/usr/local/lib:$LD_LIBRARY_PATH&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
source ~/.bash_profile
cd ~/tmp
wget ftp://ftp.gtk.org/pub/glib/2.19/glib-2.19.8.tar.gz
tar -xvzf glib-2.19.8.tar.gz
cd glib-2.19.8
./configure --prefix=$HOME
make
make install

glib should now be installed. If you ran into any errors fix those before moving on.

cd ~/tmp
wget http://mcabber.com/files/mcabber-0.9.9.tar.bz2
bunzip2 mcabber-0.9.9.tar.bz2
cd mcabber-0.9.9
./configure --prefix=$HOME
make
make install

Then follow these directions to set up.

Per the comments, remember to delete your tmp dir when you’re done. Or else don’t chmod 777 this directory.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

JavaScript Null Checks and Type Coercion

There are many discussions of JavaScripts type coercion at work, and many examples of the distinction between undefined and null. This becomes very important when performing checks for null and undefined.

Here’s an easy summary:

JavaScript distinguishes between null, which is an object of type ‘object’ that indicates a deliberate non-value, and undefined, which is an object of type ‘undefined’ that indicates an uninitialized value.

While this distinction is made, undefined == null. This is due to JavaScripts type coercion. The two operands do NOT have to be of the same type to yield true for equality, as JavaScript automatically performs conversion on a differing operand.

So:

  • 1==true  // evaluates to true
  • “1″==true // evaluates to true
  • 45==”45″ // evaluates to true
  • null==undefined // evaluates to true

If the last bullet doesn’t seem to make as much sense, you’re not alone.

In a lot of cases, you’re not going to want to evaluate objects with this coercion. For those cases, strict evaluation is allowed with ===. No coercion is performed in this case.

So:

  • true===1  // evaluates to false
  • NaN===NaN // evaluates to false
  • var x=[1,2]
  • var y=[1,2]
  • x===y // evaluates to false
  • undefined === null // evaluates to false

var healthCare = {
exists: true
};

healthCare.exists // will be true
healthCare.isExpensive // will be undefined

Lastly, double negation can be used in JavaScript to convert any value to a Boolean object.

if(!!healthCare.exists) // will be true

if(!!healthCare.isExpensive) // will be false

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Bash Shortcuts

A nice list of bash shortcuts

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Quick command line spell check of a single word or sentence

Here’s a short bash script, using aspell, to spell check a word or several words from the command line. It’s simple but effective. If anyone else knows a cleaner or even simpler way, please let us know.


#!/bin/bash

WORD=$1
TEMPFILE=temp.spell

echo $WORD > $TEMPFILE
aspell -c $TEMPFILE
cat $TEMPFILE
rm -f $TEMPFILE
rm -f $TEMPFILE.bak

I named this script “spell”. Simply use it like so:

$ spell reccomend

Aspell will run and you can make your changes.

reccomend

1) recommends 6) commend
2) recommend 7) recommenced
3) reckoned 8) recommended
4) regiment 9) Redmond
5) recombined 0) rejoined
i) Ignore I) Ignore all
r) Replace R) Replace all
a) Add l) Add Lower
b) Abort x) Exit

In this case, if I type “2″, the correction will be made, aspell will exit and the spelling I chose gets printed out.

Here’s an example of how to use it with a sentence.

$ spell "I go to the storede and buy beakh clotes"

Now aspell will take you through each misspelled word. After you finish the corrections it will exit and print out the corrected sentence or series of words.

I go to the store and buy beach clothes

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]