About Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.

Grails Goodness: Accessing Resources with Resource and ResourceLocator

Grails uses Spring and we can piggyback on the Spring support for resource loading to find for examples files in the classpath of our application. We can use the Spring org.springframework.core.io.Resource or org.springframework.core.io.ResourceLoader interface to find resources in our application.

And since Grails 2.0 we can also use the org.codehaus.groovy.grails.core.io.ResourceLocator interface. In our code we can use the grailsResourceLocator service which implements the ResourceLocator interface. We must inject the grailsResourceLocator service into our code and we use the method findResourceForURI(String) to find a resource. The advantage of the grailsResourceLocator service is that it knows about a Grails application. For example resources in plugins can also be accessed.

First we look at a sample Grails service with a Resource property with the name template. In our code we get the actual resource using the getURL() method. The value of the Resource property we set in grails-app/conf/Config.groovy. We rely on the automatic conversion of properties of Spring so we can use a value like classpath:filename.txt and it will be converted to a Resource implementation.

package com.mrhaki.templates
 
import groovy.text.SimpleTemplateEngine
import org.springframework.core.io.Resource
 
class MessageService {
 
    Resource template
 
    String followUpMessage(final String user, final String subject) {
        final Map binding = [user: user, subject: subject]
        final SimpleTemplateEngine templateEngine = new SimpleTemplateEngine()
        templateEngine.createTemplate(template.URL).make(binding)
    }
}

In grails-app/conf/Config.groovy we define:

...
beans {
    messageService {
        template = 'classpath:/com/mrhaki/templates/mail.template'
    }
}
...

If we use the grailsResourceLocator we get the following service implementation:

package com.mrhaki.templates
 
import groovy.text.SimpleTemplateEngine
 
class MessageService {
 
    def grailsResourceLocator
 
    String template
 
    String followUpMessage(final String user, final String subject) {
        final Resource template = grailsResourceLocator.findResourceForURI(template)
        final Map binding = [user: user, subject: subject]
        final SimpleTemplateEngine templateEngine = new SimpleTemplateEngine()
        templateEngine.createTemplate(template.URL).make(binding)
    }
}

Code written with Grails 2.2.4

Original article
 

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply


eight + = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books