Monday, March 30, 2015

Grails dumping SQL statements

Edit the DataSource.groovy


dataSource {
   dbCreate = ...
   url = ...
   ...
   logSql = true
}

And in the Config.groovy:


log4j = {
   ...
   debug 'org.hibernate.SQL'
   trace 'org.hibernate.type'
}

Wednesday, March 25, 2015

Grails JSON encoding applyCodec

I had some data that I wanted to render in the GSP page, however, by default single quotes were being encoded as

"


To prevent grails from encoding your output, wrap it in the undocumented  applyCodec tag as follows:


var selectedItems = <g:applyCodec encodeAs="none">${data as grails.converters.JSON}</g:applyCodec>

Reference:


http://aruizca.com/how-to-render-json-properly-without-escaping-quotes-inside-a-gsp-script-tag/

Monday, March 9, 2015

Starting elasticsearch "Too small initial heap for new size specified"

When starting elasticserach for the first time I received the following error:


Error occurred during initialization of VM
Too small initial heap for new size specified


Added the following to elasticsearch.bat


set JAVA_OPTS=-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m -Xmx1536m -XX:NewSize=1024m -XX:MaxNewSize=1024m -XX:PermSize=768m -XX:MaxPermSize=768m -XX:+DisableExplicitGC

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