February 28, 2013 0 minutes and 37 seconds read

Grails migrations and 'Waiting for changelog lock'

Sometimes it may happen that the automatic migrations in a Grails project may come to a hold due to the fact that Liquibase keeps waiting for a changelog lock. At the end this will result in a application that is not going to be deployed.

...
Waiting for changelog lock....
Waiting for changelog lock....
Waiting for changelog lock....
...

To solve this take the following steps:

  • Stop the application container (example: Tomcat)
  • In the database look for a table called DATABASECHANGELOGLOCK
  • In the table there is a record with id=1, change the following values:
locked -> 0
lockgranted -> null
lockedby -> null
  • After updating this record start the application container

Notes:
To see who has locked the database (normally the local machine):

select * from DATABASECHANGELOGLOCK;
To update the record
update DATABASECHANGELOGLOCK
set locked=0, lockgranted=null, lockedby=null
where id=1

January 13, 2013 0 minutes and 38 seconds read

Testing constraints with Build Test Data

The Spock & Build Test Data plugins both are wonderful additions to your toolkit if you are creating tests without getting into the hassle of constantly building up your object graph. You can focus on what you want to test!

When testing constraints on an object it can be used as follows:

class Foo {
    String name   // name of foo
    Integer age   // age of foo

    static constraints = {
        name nullable: false, blank: false // name may never be nullable or blank
        age nullable: true
    }
}

And the Spock test

@Build(Foo)
@TestFor(Foo)
class FooSpec extends Specification {

    def "Name of Foo must exist"() {
        given: "setting up the constraints"
        mockForConstraintsTests(Foo)

        when: "creating a Foo"
        Foo foo = Foo.buildWithoutSave()

        then: "validation should trigger"
        assertFalse foo.validate()

        and: "validation error should be on the value field"
        assert foo.errors.allErrors.first().field == "name"
    }

}

September 18, 2012 0 minutes and 39 seconds read

Troubles getting vsftpd user logged in?

Recently i had the pleasure of installing and using vsftp on a Ubuntu Linux 12.04 system. I closely followed the instructions but keeped getting my head banging against a wall with an error message: 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

After some googling around i found the following fix.. chmod the folder that the ftp user comes in to as he first login (root folder) by using in terminal:

sudo chmod a-w /home/user

Create a subfolder within the folder, either by the use of GUI or if you only have terminal it’s:

sudo mkdir /home/user/newfolder

Now you should be able to log in and read write within the “newfolder”.

You will NOT be able to write in the root folder itself from the ftp with the chmod a-w, so that is the reason for the subfolder, there you can.

http://askubuntu.com/questions/128180/vsftpd-stopped-working-after-update


September 17, 2012 2 minutes and 4 seconds read

Loading Grails configuration files update!

We recently changed the way how we load configuration files in a Grails project. Normally we to use the .properties file format, but this has some serious disadvantages.

  • You cannot deal with all Grails Mail settings in the configuration file
  • You cannot use the log4j DSL to extract the logging configuration outside your application
  • etc..

In our hunt for a good way to load configuration files we asked question on the mailinglist and also found this blogpost which was the start for our implementation of loading the external configuration files.

We modified some small things and added a way of loading a configuration file that is resident in the root of a Grails project. So when developing with IntelliJ for example the config file is at your fingertips in the root of the application project structure. We must also note that we are very happy with the fact that the Grails community was more then helpfull in helping us out here!

// -------------------------------------------------------------------------------- //
// - START: CONFIGURATION FILE LOADING -------------------------------------------- //
// -------------------------------------------------------------------------------- //
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
def ENV_NAME = "${appName}.config.location"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}
println "--------------------------------------------------------------------------------"
println "- Loading configuration file                                                   -"
println "--------------------------------------------------------------------------------"
// 1: check for environment variable that has been set! This variable must point to the
// configuration file that must be used. Can be a .groovy or .properties file!
if(System.getenv(ENV_NAME) && new File(System.getenv(ENV_NAME)).exists()) {
    println("Including System Environment configuration file: " + System.getenv(ENV_NAME))
    grails.config.locations << "file:" + System.getenv(ENV_NAME)

// 2: check for commandline properties!
// Use it like (examples):
//      grails -D[name of app].config.location=/tmp/[name of config file].groovy run-app
// or
//      grails -D[name of app].config.location=/tmp/[name of config file].properties run-app
//
} else if(System.getProperty(ENV_NAME) && new File(System.getProperty(ENV_NAME)).exists()) {
    println "Including configuration file specified on command line: " + System.getProperty(ENV_NAME)
    grails.config.locations << "file:" + System.getProperty(ENV_NAME)

// 3: check on local project config file in the project root directory
} else if (new File("./${appName}-config.groovy").exists()) {
    println "*** User defined config: file:./${appName}-config.groovy ***"
    grails.config.locations = ["file:./${appName}-config.groovy"]
} else if (new File("./${appName}-config.properties").exists()) {
    println "*** User defined config: file:./${appName}-config.properties ***"
    grails.config.locations = ["file:./${appName}-config.groovy"]

// 4: check on local project config file in ${userHome}/.grails/...
} else if (new File("${userHome}/.grails/${appName}-config.groovy").exists()) {
    println "*** User defined config: file:${userHome}/.grails/${appName}-config.groovy ***"
    grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
} else if (new File("${userHome}/.grails/${appName}-config.properties").exists()) {
    println "*** User defined config: file:${userHome}/.grails/${appName}-config.properties ***"
    grails.config.locations = ["file:${userHome}/.grails/${appName}-config.properties"]

// 5: we have problem!!
} else {
    println "********************************************************************************"
    println "* No external configuration file defined                                       *"
    println "********************************************************************************"
}
println "(*) grails.config.locations = ${grails.config.locations}"
println "--------------------------------------------------------------------------------"
// -------------------------------------------------------------------------------- //
// - END: CONFIGURATION FILE LOADING ---------------------------------------------- //
// -------------------------------------------------------------------------------- //

September 16, 2012 0 minutes and 51 seconds read

Loading external Configuration files in a Grails application

The use of ‘Config.groovy’ as a placeholder for configuration settings is nice, but not always sufficient. The ‘Config.groovy’ file will get compiled and packaged inside the WAR file you are creating. If you want to externalize the configuration and have a need to configure settings outside the deployed (WAR file) application you can use property files (.properties) to achieve that.

A simple mechanism to load these property files is to place a short snippet of code in the ‘Config.groovy’ that will load a specific configuration file from the filesystem, depending on the availability.

grails.config.locations = ["classpath:application-config.properties", "file:./application-config.properties"]

This snippet will first try to load the property file from the classpath and if that fails you have a backup on the filesystem. This opens opportunities to load a different property file during development! When you deploy the application you can place the ‘application-config.properties’ file inside a folder which is available in the classpath. For Apache Tomcat this would be the ’lib’ folder!

This gives the opportunity to configure the application outside the ‘Config.groovy’ file so any changes made the the property file will be reflected in your environment.


September 15, 2012 1 minutes and 1 second read

Combining ImageMagick and Grails

When there is a need to work with images (thumbnailing, watermark, resize etc.) there is always ImageMagick that comes to the rescue. Combining this image utility powerhouse with the Grails framework is a task which can be easily accomplished.

Steps:

  • Install ImageMagick according to the installation instructions.
  • It contains a utility called convert which we will need later on! This utility takes care of the conversion of images to thumbnails, watermarks etc. So remember where this utility is installed on your system!
  • Make sure that ImageMagick is installed correctly be converting an image to a thumbnail by using the following command in a terminal.
/opt/local/bin/convert <filename> -thumbnail 70x70 <thumbnail-filename>

example:

/opt/local/bin/convert /tmp/image-001.jpg -thumbnail 70x70 /tmp/thumbnail-image-001.jpg

Create some code that calls the ImageMagick convert utility with the correct parameters to enable you to achieve what you want. Something like below:

def createThumbnail(File file) {
   def command = "/opt/local/bin/convert ${file.canonicalPath} " +
                 "-thumbnail 70x70 " +
                 "/images/thumbs" + File.separator + "${file.name}"
   def proc = Runtime.getRuntime().exec(command)
   int exitStatus;
   while (true) {
       try {
           exitStatus = proc.waitFor();
           break;
       } catch (java.lang.InterruptedException e) {
           log.debug("Creating thumbnail - Interrupted: Ignoring and waiting")
       }
   }
    if (exitStatus != 0) {
        log.error("Error executing command: exitStatus=[${exitStatus}]")
    }
    log.debug("Succesfully created thumbnail")
    return (exitStatus == 0);
}

The above should give you some idea on how you could integrate Grails and ImageMagick into your own application.


September 14, 2012 1 minutes and 7 seconds read

Installing Vimball plugins when using Pathogen

No need to discuss that Vim is truly a great text editor. Wealth of features, great speed and extensive support for plugins. The installation of plugins is very easy. If you want to learn how to install plugins, make sure to check out the wiki.

Pathogen

When you instal a plugin one may copy the files to the plugin directory. In a later stage you also want to delete a plugin and then the hunt for files starts. You need to track down which files belong to the specific plugin you want to delete. Pathogen to the rescue :)

Pathogen enables you to create sub-folders inside a bundle-folder which will acts a place holder for all your plugins nicely separated in a ‘folder per plugin’ structure. So if you need to delete a plugin then you just delete the correct plugin-folder and everything is gone.

Normal installation of a Vim script is standard, you create a sub-folder below the bundle-folder, copy the Vim script and all is ok. BUT when you want to use a vimball then you need to do some additional steps.

  • Create a folder in which you want to later on extract the vimball. Preferably below the ‘bundle’ folder.
mkdir ~/.vim/bundle/align
```console

* Open the vimball with command ‘:e 'location of your vimball’/‘name of your vimball'
```console
:e ~/Downloads/Align.vba
  • Tell Vim to use the vimball by issuing command ‘:UseVimball ’location to extract’'
:UseVimball ~/.vim/bundle/align
  • Restart your Vim and your plugin should be available.

September 13, 2012 0 minutes and 44 seconds read

Installing Markdown on OSX and use it inside VIM

Back again to one of my favorites which is called Markdown. Once every now and then i forget how easy it is. Normally i use Textmate to do all my writing, but recently i have picked up VIM to do some editing etc. Why i did chose VIM? I will not trouble you with that decision :)

Using Textmate everything is easy, but when you want to use Markdown inside VIM it is somewhat different. But anything is different when using VIM :)

Steps

  • download Markdown from - The home of Markdown, It’s usual place as this is a Perl script you need to put it somewhere so OSX is able to execute it.
  • start your terminal and create a directory inside usr/local/bin
  • extract the downloaded file and put the Markdown.pl file * inside the user/local/bin directory
  • inside the terminal chmod the Markdown.pl to 777
  • using the Installing Markdown as OSX Service creates a service to use Markdown
  • You are done… :)