Posts

Showing posts from 2011

Installing and configuring Cband for Apache2 on Ubuntu

Image
If you need to manage bandwidth usage on Apache web server (particularly on a per virtualhost basis) CBand could be that hero you were hoping for. Below are the steps I followed to get up and running with a basic CBand setup.



Installation

Install additional apache libraries. Only one of these two maybe required.
apt-get install apache2-prefork-dev
apt-get install apache2-threaded-dev

Download the module
wget http://dembol.org/downloads/cband/mod-cband-0.9.7.5.tgz
Untar the file
tar -xzf mod-cband-0.9.7.5.tgz
Enter newly created directory
cd mod-cband-0.9.7.5
Add -lm to the APXS OPTS in Makefile.in like so:
APXS_OPTS=-lm -Wc,-Wall -Wc,-DDST_CLASS=@DST_CLASS@
Configure and install
./configure
make
make install

Restart Apache
/etc/apache2/restart


Hopefully you should now have successfully installed Cband on your Ubuntu server.
Now let's configure it.

Configuration

In order to improve performance of Cband, add the following to /etc/apache2/mods-available/cband.conf
CBandScoreFlushPeriod 1
CBand…

Groovy safe navigation gotcha

I just came across this Groovy gotcha today while accessing a list within a chain of attributes. I had a statement like this in a gsp:
${invoice?.invoiceItems[0]?.name}
This is the (erroneous) recommended style when data binding many ended associations in Grails, but it could easily arise in other scenarios. If I try this "safe" statement where invoiceItems is null, I'll see a null pointer exception. In fact I'll even see it when invoice is null. The reason for the exception is a combination of how lists and the safe navigation work in Groovy.

Lists are much more elegant in Groovy than Java, as they share the square brackets that primitive arrays use. Creating a list is as easy as writing:
def x = [1,2,3]
Just like an array you can access the first element of this list like so:
println x[0]
However, unlike a primitive array the [] operator on a list is mapped (by some Groovy magic) behind the scenes to the getAt method of the particular List object. We can expand our exampl…

Mass find and replace on Mac OSX with SED

Lets say you are doing some grand refactoring, and you want to change a class name from Dog to Pooch in multiple files.
There are a couple of ways you can do it with the sed command. One uses the -exec argument and the other uses the xargs program. Though I'm not sure why, I favor the xargs strategy. In Linux to do a mass find and replace for our Dog to Pooch example you could do something like:
find . -name "*.php" -print | xargs sed -i 's/Dog/Pooch/g'

But if you try this same command In Mac OSX you will see an error like this:
extra characters at the end of g command

The reason for this is the command in Mac OSX the command doesn't trust you. I wonder if rm -rf / works in Mac OSX? Well I'm not going to find out ;-) Mac OSX will create a backup file in case you mess up your find and replace, and it requires an extension for this file. With that in mind, the following command will work in Mac OSx:
find . -name "*.php" -print | xargs sed -i.bak …

Debugging dependencies in Grails

Image
In Grails, and indeed large Java projects generally, dependency resolution can become quite a headache. Tools like Maven and Ivy are a huge help of course, but even they can't prevent tedious days trying to fix that conflict where two classes of the same name are loaded by the same class loader. Grails provides a feature that helps navigate through the complex web of depencies that your web application will no doubt accrue.

Issue the command: grails dependency-report
or if your using the maven-grails plugin: mvn grails:exec -Dcommand="dependency-report"

This will generate a number of reports in the /target/dependency-reports folder for you. The html reports can of course be opened in the browser of your choice. The graph reports (in graphml format) however need a special and - to me at least - uncommon tool. The tool that seems most popular is yEd. Fortunately yEd is available for Linux, Mac, and Windows.

After you have download and installed yEd, open one of the generated …

Groovy XML Parser Gotcha - it's not a Map your parsing!

When I first looked at examples of using the Groovy XmlParser it appeared that the parser took some nice raw XML and turned it into a map for easy traversing. I could take the following source:

<person>
<face>
<eyes>
blue
</eyes>
</face>
&lt/person>

And parse it find the eye color like this:

def xml = new XmlParser().parseText(xmlSrc)
assert xml.person.face.eyes.text() == 'blue'


Confident in my understanding, I thought it would be trivial to handle the following XML source I was receiving from a domain registrar:

<?xml version="1.0"?>
<interface-response>
<ErrCount> 1</ErrCount >
<errors>
&ly;Err1> Do main name not available </Err1>
</errors>
</interface-response>

I parsed the source in order to extract the errors from the response as follows:

def xml = new XmlParser().parseText(xm…

Configuring Logging of Bind DNS in Ubuntu

After installing the Bind9 DNS server in Ubuntu, you might find the need to play with the logging settings. Logging is essential when your trying to figure out why the hell DNS setup is not working as expected. Without any explicit logging configuration (from you or your colleagues) Bind will log messages of the category "default" to "/var/log/syslog". In order to customize the configuration to your own needs try the following steps.

Step 1: Create a new file to write your logs to
The location of this file is up to you, but due to AppArmor restrictions in Ubuntu the easiest strategy is to place it in "/var/log/named". If you need to place it somewhere else you might need to alter your AppArmor configuration.
mkdir /var/log/named
touch /var/log/named/debug.log
touch /var/log/named/query.log
Step 2: Define the configuration
You can place your logging configuration in either "/etc/bind/named.conf" or "/etc/bind/named.conf.local". If there is a b…

A Custom Dialog Box in JQuery Tutorial

Image
In the Jquery world, constructing a basic custom dialog box can be quite trivial. Using Jquery UI, creating a dialog box like the above can be done with this little snippet of code:

<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<script>
$( "#dialog" ).dialog();
</script>

Web development is easy right? Well in a perfect world yes, but as any developer will tell you there are two worlds: the ideal world, and the real world. JQuery UI is a fine toolkit when one is looking for a standard off the shelf styled widget, but not when something more custom is desired - even with help from the excellent Themeroller.





The recent Twitter UI redesign does everything it can to ensure users will use their browser's back button less. Many of the actions possible do not change the princi…

Simplifying a javascript jump menu

Image
The jump menu is a reasonably common UI design pattern on the web. It is used by big guns such as Facebook and Twitter, and has been dissected and reproduced many times over.

When I set about implementing my own jump menu I found opening and closing the menu when it is clicked on was rather trivial.

$('.jump-menu').click(function(e){
toggleMenu();
}

The difficulty came in closing the menu when there is a click outside the jump menu. Googling for answers, I found the scenario of closing a menu on clicking outside it to be a well trodden path. Stackoverflow had plenty of examples and possible solutions such as here and here. I even found a plugin created by Ben Alman that extended Jquery to provide a custom clickoutside event. Many of the proposed solutions were fragile, and I was slow to add a new plugin to my web application - as good and all as the implementation appeared. I really wanted to understand the problem before opting to take a proposed solution off the shelf.



The st…

Compiling brunch from source

Background:Brunch is a client side web app framework built using a number of open source tools and libs from the world of javascript and node.js: CoffeeScript, Backbone, Eco templates, and more.

The stepsPull source from Github
git pull https://github.com/brunch/brunch
check if you have all the brunch dependencies:
./bin/brunch --version
Building from source means you don't get the dependency management of npm as easily.
You must have node installed, and the required packages:
Note: at time of writing Stitch was not defined in npm dependencies but required
npm install stitch
Compile brunch with Cake:
cake watch
To create a project:
./bin/brunch new --output $locationOfNewProject

Nerd Tree: A File Explorer with Mac Vim

The one thing missing from the great text editor that is Mac Vim is a project / file explorer side bar. Competing text editors such as Sublime and Text Mate have one, so why not MacVim? It doesn't seem like it would require too much additional work of the developers. Reading through debates the community has had on the issue, the reason appears to be the elitist and idealistic nature of the Mac Vim community. Please don't read that last sentence pejoratively; these guys are purists, and very good at what they do. There were quite a few in the community who believed NERDTree was more than enough for any code jocky's needs, so I decided to checkout what all the fuss was about.

I use pathogen to manage vim plugins, and it is especially useful in conjunction with a public source control system like github. Once you have pathogen installed simply pull the NerdTree Github project to your vim plugins directory.

cd ~/.vim/bundle
git clone git://github.com/scrooloose/nerdtree.git


Next…