Sunday, March 1, 2015

Upgrading from Grails 2.3.x to Grails 2.4.x

Upgrading from Grails 2.3.x to Grails 2.4.x

Previous upgrades in Grails were simple where a single 'upgrade' command  made it extremely easy.
However, it's not quite as easy for upgrades to Grails 2.4.4.
There's a reference guide on how to do the upgrade which does require a bit of tinkering found here:

http://grails.github.io/grails-doc/2.4.x/guide/upgradingFrom23.html

If you rely on several plugins there's a good chance you'll need to update nearly all of your plugins to the recent version. To find the latest plugin versions do a search here:

http://grails.org/plugins


Spring security

Furthermore, if you rely on the spring-security plugin, you may need to upgrade all the package names as they have changed:

From:
org.codehaus.groovy.grails.plugins.springsecurity

To:
grails.plugin.springsecurity


Also if you use the @Secured annotation update the import to be:
grails.plugin.springsecurity.annotation.Secured

To find other package name changes, double check the source code here:
https://github.com/grails-plugins/grails-spring-security-core/tree/master/src/main/groovy/grails/plugin/springsecurity

Furthermore, the recent version of the plugin spring-security-core:2.0-RC4 by default relies on pessimistic locking (http://grails-plugins.github.io/grails-spring-security-core/guide/requestMappings.html). Since my app is mostly public, I've added 2 configuration parameters to the Config.groovy file


// allow public access:  http://grails-plugins.github.io/grails-spring-security-core/guide/requestMappings.html
grails.plugin.springsecurity.rejectIfNoRule = false
grails.plugin.springsecurity.fii.rejectPublicInvocations = false

I had forgotten to update the spring-security configuration parameters as the names have changed which resulted in the following error:


Message: Dynamic method get<Artefact>Class(artefactName) requires a single String parameter
    Line | Method
->>   57 | loadUserByUsername    in grails.plugin.springsecurity.userdetails.GormUserDetailsService
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     49 | getGrantedAuthorities in grails.plugin.springsecurity.ldap.userdetails.DatabaseOnlyLdapAuthoritiesPopulator
|     76 | attemptAuthentication in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
|     49 | doFilter              in     ''
|     82 | doFilter . . . . . .  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker             in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run                   in java.lang.Thread

For example, the parameter for grails.plugins.springsecurity.providerNames has changed to grails.plugin.springsecurity.providerNames. Notice that 'plugins' has changed to 'plugin'

 

LDAP spring security

The names of the configuration parameters for the latest LDAP 2.0-RC2 have changed. At the time of writing, there was also no updated documentation (since it was a release candidate). However, if you need to see what configuration parameters look like, have a look at the source code:

https://github.com/grails-plugins/grails-spring-security-ldap/blob/master/grails-app/conf/DefaultLdapSecurityConfig.groovy

Notice that 'plugins' has changed to 'plugin' 

Remove grailsResourceLoader from applicationContext.xml

Update the web-app/WEB-INF/applicationContext.xml file by removing any references to grailsResourceLoader to be as follows:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="grailsApplication" class="org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean">
        <description>Grails application factory bean</description>
        <property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
    </bean>

    <bean id="pluginManager" class="org.codehaus.groovy.grails.plugins.GrailsPluginManagerFactoryBean">
        <description>A bean that manages Grails plugins</description>
        <property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
        <property name="application" ref="grailsApplication" />
    </bean>

    <bean id="grailsConfigurator" class="org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator">
        <constructor-arg>
            <ref bean="grailsApplication" />
        </constructor-arg>
        <property name="pluginManager" ref="pluginManager" />
    </bean>    

    <bean id="characterEncodingFilter" class="org.springframework.web.filter.CharacterEncodingFilter">
        <property name="encoding">
            <value>utf-8</value>
        </property>
    </bean>
</beans>

 

No suitable classloader found for 'Grab'

Rather than trying to load the dependencies at runtime, try adding the dependencies to grails-app/conf/BuildConfig.groovy

 

Database drivers

 Even though I had the mySQL driver installed in the lib folder, there was an error when trying to run the app:
ClassNotFoundException: com.mysql.jdbc.Driver

To get it working, I added the library to the BuildConfig.groovy file as follows:
runtime 'mysql:mysql-connector-java:5.1.24'

 

Sitemesh errors:

When starting up the application, I got a sitemesh error as follows:

2015-03-02 09:43:47,277 [localhost-startStop-1] ERROR [localhost].[/mutations]  - Exception starting filter sitemesh
Message: org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter
    Line | Method
->>   59 | findClass in org.grails.plugins.tomcat.ParentDelegatingClassLoader
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    423 | loadClass in java.lang.ClassLoader
|    356 | loadClass in     ''
|    334 | innerRun  in java.util.concurrent.FutureTask$Sync
|    166 | run . . . in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread
Error |
 It appears that sitemesh has been removed and any references to it should be removed as well.
Under the folder /src/templates/war/web.xml, edit the file to remove the following XML snippets:


    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>

    </filter-mapping>

Broken legacy GSP views

In the BuildConfig.groovy file, check to make sure that the asset-pipline is not included

java.lang.ClassNotFoundException: org.springframework.mock.web.MockHttpServletRequest

It seems building a production WAR file relies on a missing MockHttpServletRequest class. The bug has been raised in this link:  https://jira.grails.org/browse/GRAILS-11497

The workaround is to include the spring-test JAR file in your dependencies as follows:



runtime 'org.springframework:spring-test:3.2.13.RELEASE'

 

Conflicting module versions

When trying to run the application you may get the following error:

Caused by: groovy.lang.GroovyRuntimeException: Conflicting module versions

To fix this issue do the following:
  1. open eclipse go to "Run as" then "Run Configurations"
  2. choose "Environment" tab , then choose "replace native environment with specified environment "

 

 References:

http://grails.1312388.n4.nabble.com/upgrade-from-2-3-to-2-4-quot-Context-initialization-failed-quot-td4657057.html

http://stackoverflow.com/questions/24437231/upgrading-a-grails-app-from-1-3-7-to-2-4-1

http://stackoverflow.com/questions/25427852/using-groovy-library-in-grails-and-getting-no-suitable-classloader-found-for-gr

https://groups.google.com/forum/#!topic/grails-dev-discuss/KltT793KC8o 

http://grails-plugins.github.io/grails-spring-security-core/guide/requestMappings.html 

https://github.com/robfletcher/grails-gson/issues/38

http://stackoverflow.com/questions/15079286/grails-groovy-ggts-conflicting-module-versions-on-run-app

Wednesday, October 15, 2014

Linux server thrashing using vmstat

You can use the vmstat command to detect thrashing of memory and hard disk as follows:

vmstat 2
where 2 represents the number of seconds to repeat.

You may see output similar to this

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free          buff      cache      si   so    bi    bo   in   cs us sy id wa st
 1  0   1600 2244636 193816 1154164    0    0     1     9    0    0  1  0 98  0  0
 1  0   1600 2244496 193816 1154184    0    0     0    46 1768 1432 26  1 73  0  0
 1  0   1600 2244496 193816 1154184    0    0     0     0 1756 1383 25  1 74  0  0
 1  0   1600 2244496 193816 1154184    0    0     0     0 1732 1318 26  0 74  0  0



The parameters of interest are si and so (swap in, swap out).
In this case a value of 0, means there is no thrashing

References:

http://www.linuxjournal.com/article/8178

Monday, May 19, 2014

Automated database backups

Automated Database Backups

Postgres

Assuming we are using Linux, create a file called .pgpass in the user’s home directory with the following details:
hostname:port:database:username:password

Then from the command-line run the following command:
pg_dump –U user dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Mysql

Assuming we are using Linux, create a file called ~/.my.cnf in the user’s home directory with the following details:
[mysqldump]
user=mysqluser
password=secret

Then from the command-line
mysqldump –u user dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Crontab

If the command is successful, then create and add this command to a script, for example runBackup.sh
Then schedule the job to run by editing your crontab:
crontab -e
For example, to run once a week on Saturday at 2AM:
0 2 * * 6 ~/runBackup.sh

 

References




Wednesday, May 7, 2014

Automated tomcat start up on server reboot



Automated start up on server reboot


Create a new tomcat user using the following command:
adduser tomcat

Create the following file /etc/init.d/tomcat:
#!/bin/bash
#
# tomcat       
#
# chkconfig:
# description: Start up the Tomcat servlet engine.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
CATALINA_HOME="/usr/local/apache-tomcat-7.0.52"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/startup.sh
        fi
        ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
        fi
        ;;
 *)
        echo $"Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit $RETVAL

Edit the file and change CATALINE_HOME to the location of where tomcat is found.

Grant execution write of the new file using the following command:
chmod 755 /etc/init.d/tomcat

Reassign the owner of the tomcat folder and all subfolders to the tomcat user:
chown tomcat:tomcat –R /usr/local/apache-tomcat-7.0.52


To start up on server reboot:
ln –s ../init.d/tomcat S71tomcat