Posts

Showing posts from 2010

Accessing appName property from controller in Grails

It is trivial to access the appName property from within the config.groovy file, and it is a common practice to do so. Here is an example of specifying an external config file based on the name of the grails application:

grails.config.locations = ["file:$userHome/.osaas/$appName.groovy"


I could find no where obvious that described how to access this appName property from a constructor. You would expect the following to work but it does not:

grailsApplication.config.appName


The following does work however

grailsApplication.metadata['app.name']

Tracing DNS resolution

When you need to trace how dns is resolving your server here is a way to do it.
First run this nslookup query:

nslookup -q=ns example.com a.root-servers.net
Authoritative answers can be found from:
com nameserver = h.gtld-servers.net.
com nameserver = d.gtld-servers.net.
com nameserver = k.gtld-servers.net.
com nameserver = g.gtld-servers.net.
com nameserver = c.gtld-servers.net.
com nameserver = f.gtld-servers.net.
com nameserver = j.gtld-servers.net.


Now choose one of the results and run the query again like this:

nslookup -q=ns example.com a.gtld-servers.net
Authoritative answers can be found from:
example.com nameserver = ns1.example.com.
example.com nameserver = ns2.example.com.
example.com nameserver = ns3.example.com.
ns1.example.com internet address = 10.12.12.12


You can repeat the above if you still haven't reached your destination

A Javascript best practice: one special semicolon

It is becoming common practice to concatenate javascript files. Such concatenation means that only one HTTP request is required to download the necessary javascript rather than several. This reduction in HTTP overhead reduces the time taken to download the particular web application. This concatenation can introduce an unforeseen problem: if, in one file, some code is not properly finished (i.e. with a semicolon), and the proceeding file begins with a closure
being opened, the interpreter will break.

Jquery plugins often begin with closures and so are susceptible to this problem. Beginning the file with a semicolon will prevent this kind of problem. Where a file begins with a closure, best practice should dictate that it begins with a semicolon. An example of this practice in action is source code of the jquery form plugin.



References:
http://groups.google.com/group/jquery-dev/browse_thread/thread/2ce10bc1bcf6a8d1?pli=1

Overcomming the virtual hosts/HTTPs conflict on Amazon AWS

Previously I've described the conflict between virtual hosts and HTTPs. For those that use Amazon's EC2 cloud, the below describes an elegant remedy for that conflict .

Assumed Basic Setup
For the purposes of this example, I'll assume you have an EC2 instance of Ubuntu Lucid with an apache server serving two websites: example.com and eazybusiness.com. Both have their own virtual host definitions, each containing it's corresponding site's specific configuration. If you need more information on how to configure your virtual hosts I direct you to apache's comprehensive documentation on the subject.

The virtual host definitions would look something like:

<VirtualHost *:80>
ServerName www.eazybusiness.com

ServerAdmin admin@eazybusiness.com
DocumentRoot /var/sites/eazybusiness.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com

ServerAdmin admin@example.com
DocumentRoot /var/sites/example.com/
</V…

Multiple HTTPs websites on one webserver

A long time ago, servers were very slow and a web server could only handle one web site. In time, increased computing power and virtual hosts technology allowed one webserver to handle multiple web sites. At the same time as websites started to share the one webserver, HTTPS was coming to the fore as the defato means of securing web communication from fraudsters, snoopers, and other nefarious trades. Unfortunately, due to the nature of the technologies HTTPS and virtual hosts were not fully compatible.

Virtual hosts work based on host/server names. Examples of hostnames are example.com, eazybusiness.com, and blogger.com. The hostname provided by the client browser determined what one of its web sites the webserver servered to that client.

In order for a website to be able to offer the security of HTTPS, it must provide its own unique SSL certificate. This SSL cert is proof of the site's authenticity, and forms part of the protocol used to secure the communication between a user'…

Error sending Mail: 501 Syntax: HELO hostname

The exact error I found was:

javax.mail.MessagingException: 501 Syntax: HELO hostname


To fix it I had to set the hostname of the server


hostname $hostname.tld

Grails/Groovy deleting an element in a collection while iterating over it

I ran into a recent problem whereby I wanted to delete a sub-set of the elements in a list after processing all elements in the list. The general gist of the logic was:


apples.each { apple ->
if(apple.red) apple.delete()
}


which resulted in a java.util.ConcurrentModificationException exception.

The solution is to remove the element from the interator before deleting it.


Iterator itr = apples.iterator()
while(itr.hasNext()){
def apple = itr.next()
itr.remove()
apple.delete()
}


For more a little more detail see this thread

Dynamic methods with javascript

I recently had an implementation requirement to be able to call a method given a string with the name of the method.

I came across two strategies for achieving this:

1. Eval


function a(){
alert('a called');
}
eval('a');


2. Map


function a(){
alert('a called');
}
window['a']();

Daylight Saving Time for Ubunu Linux

If you find the time of your Ubuntu machine out by an hour due to DST, then the following steps might sort it out for you:


locate the name of the timezone that is suitable for you

ls /usr/share/zoneinfo


Use zic to set the zone correctly for your localtime

zic -l $zoneName


Copying EC2 Instances Between Regions

On Amazon AWS EC2, there are different regions. Presently there are three regions. EU-West, US-West, and US-East. To move/copy instances between regions is not trivial, and is not possible using the GUI (either elasticfox or amazon console).

The following is what has worked for me to copy from EU-WEST to US-WEST:

Determine the name of the bucket your current instance is stored at
The bucket name is usually part of the manifest url and is something of the form:
$bucket_name/$manifest_name.xml

Determine suitable kernel and ramdisk ids in the target region
Each AMI image requires references to suitable kernels and ramdisks. This references differ from region to region, so suitable ids need to be researched for your target region. For best results, select akis and aris that are as close as possible to those in the original region

ec2-describe-images -o amazon --region $target_region | grep $kernel_description



Using the API tools provided by Amazon, run the following Command

./ec2-migrate-image -o …

Jar Scanning

Sometimes it can be helpful to track down where exactly a class is within a library of jars.

One means of doing it is to run something like the following from the command line:

find . -name \*jar -print -exec jar -tvf {} \; | nawk '/YOURSEARCHSTRING/ || /jar/ {print} '

Another means is to use a java based jarscan tool.
The tool comes with a .bat file, which is not exactly ideal for linux development.
I created an equivalent .sh file with the following contents:

java -jar $JARSCAN_HOME/jarscan.jar $1 $2

and added an alias to my ~/.bashrc file of the form:

alias jarscan='~/dev/apps/jarscan/jarscan.sh'

Now you have a command line jarscanner. Navigate to any directory, and to run a recursive jar search enter a short command like the following:

jarscan MBeanServer

Maven Tricks

see all the libs that end up on the classpath of your maven project:

mvn dependency:build-classpath | sed 's/:/\n/g' > cp.txt

Jar search:
find . -name \*jar -print -exec jar -tvf {} \; | nawk '/YOURSEARCHSTRING/ || /jar/ {print} '

or use jarscan tool

bash script:
java -jar $JARSCAN_HOME/jarscan.jar $1 $2

alias added to .bashrc:
alias jarscan='~/dev/apps/jarscan/jarscan.sh'

and your good to go. Now you can run a recursive search of jars in any directory with a command like:
jarscan MBean

The Wonder of GIT

Setup your identity:

git config --global user.name "FirstName LastName"
git config --global user.email "user@example.com"

Make git a little prettier:

git config --global color.ui "auto"

The basic format of the push operation is:

git push $repository-location $source-branch:$dest-branch

Publish a local branch, remotely:

git push origin $local-branch:$remote-branch

Delete a remote branch:

git push origin :$remote-ref

Once a remote branch is deleted, other team members must delete references to the deleted branches by running:

git remote prune origin

Create a local branch to track a remote branch:

git branch --track local-branch-2 origin/remote-branch-2

Work on a remote branch other than master

git fetch // to ensure git local knows about remote branch
git checkout --track -b $local-branch origin/$remote-branch

To find some code you previously had in a file

// Find the relevant commit
git log /path/to/file
// Now view the changes in that file since that particular commit
git diff…