Blog Migrated to JBake

17 říjen 2014

Thanks to great Cédric Champeau’s blog post how to setup JBake on GitHub Pages it was quite easy to migrate my previous Jekyll blog to JBake.

Many thanks for such a great guide.

As the guide is bit outdated, here’s few more steps you need to do:

  1. You can use Groovy enVironmental Manager to install JBake

  2. You have to use Gradle 1.10 to make the Gradle script working (maybe few more versions are compatibile as well)

  3. You have to use stable version of the jbake plugin (me.champeau.gradle:jbake-gradle-plugin:0.1 is working well for me)

  4. You have to create gh-pages branch first (orphan branch would do the best job)

  5. You may want to use Groovy templates instead of FreeMarker ones (run jbake -i groovy instead of jbake -i)

  6. You may want to add dependency publish.dependsOn(jbake) for the publish task so the jbake will be executed before the publishing when needed

Gaelyk 2.0 Released

17 květen 2013

Gaelyk 2.0 is finally out. Some of the new features were already listed on this site.

The most important features are:

Gaelyk 2.0 is already available on Maven Central or you can download it from Gaelyk Download Page.

What's new in Gaelyk 2.0 Session at Gr8Conf

If you are attending Gr8Conf EU next week, I'll be glad meeting you at my session What is new in Gaelyk 2.0 where all the new features will be shown.

Gaelyk Spock and core plugins

Gaelyk related projects were relased as well:

Thank all the great people helping with this release!

Everyday Gaelyk: Common Query DSL pitfalls

03 duben 2013

Gaelyk Query DSL makes quering
Google App Engine Datastore a lot simpler but sometimes
it isn't working as expected.

Variable naming conflict

Some of problems origins from using the variable or property names which are already taken.
Names of the properties in where clause are converted to String automatically. What property we will be querying?

Variable name conflict

def count = 123
// a lot of code here so you've already forgotten you have such a variable

def maxCount = 100

datastore.execute {
    from Item
    where count <= maxCount
}

This query will be translated as 123 <= 100 which probably isn't what you wanted.

Binding variable name conflict

Previous example was quite obvious but what if we have following query:

Binding variable name conflict

datastore.execute {
    from Item
    where users > 10
}

Instead of getting result of items having more than ten users we get empty result set. It's
because users is Gaelyk's shortcut to UserService
so the where clause is translated to something like UserService@xyz123 > 10 which obviously returns no results.

If you run into name conflict just use good old String as property name in the where clause such as where 'users' > 10.

Entity pitfalls

@Entity annotation adds sevral useful methods to the POGO class such as findAll which resemble their
Grails counterparts. But don't get confused. The syntax of
using such methods differs slightly. find, findAll or count method are just shortcuts to Query DSL!

Using findAll method

@Entity class Item {
    int count
}

Item.findAll { count == 10 }

The query listed above will return all the items since the condition is ignored
because the where keyword is missing.

Using findAll method with where

@Entity class Item {
    int count
}

Item.findAll { where count == 10 }

We have added the where keyword to the findAll method but now we'll always get empty result because all field of @Entity class
are unindexed by default. You need to mark the field @Indexed to use it in queries.

Using findAll method with where on indexed field

@Entity class Item {
    @Indexed int count
}

Item.findAll { where count == 10 }

Summary

Using Query DSL you should basically always take care about two important things:

  1. Property names are always converted to String, this is may make mess if the variable with the same name is already defined (even in Gaelyk shortcut bindings). Use String constants instead of property names in case of any possible name conflict.
  2. All properties you're querying must be indexed. When using @Entity annotations all the properties are unindexed by default.

Everyday Gaelyk: Handle long running datastore queries gracefully

21 březen 2013

Google App Engine is full of hidden traps. One of them is the need to restart long running queries when
the query expired causing following exception to be thrown:

The requested query has expired. Please restart it with the last cursor to read more results

In Gaelyk 2.0
you can now tell your query
to restart automatically if this happen. Restarting happen under the hood so you can use for loops flawlessly.

To enable automatic restarting of queries add restart automatically statement to your query dsl.
Currenty, you can only use this option on iterate method, because it doesn't make much sense with execute
method.

Simple restarting query

def comments = datastore.iterate {
    from Comment
    limit 50000
    restart automatically
}

for(comment in comments){
    // would throw exception randomly for long running queries
    process comment
}

You can use restart automatically with select all and select keys query types. It also works with
coerced queries using as Type keyword e.g. form 'Comment' as Comment.

Everyday Gaelyk: Save your query dsl for later use

21 březen 2013

In Gaelyk you can already save query created by query dsl using datastore.query{...} and than excute it using datastore prepare function but this method only support a subset of dsl statements - namely
select, from, where, and and sort. Corection using as or setting limit or offset is simply ignored.
In Gaelyk 2.0 you can use datastore.build{...} method
to create instance of QueryBuilder.

Warning! Thanks to Groovy 2.0 extension modules you can use helper method on any DatastoreService instance even outside groovlets or templates but you still have to assing the DatastoreService in datastore variable and call the methods on that variable to make query dsl transformation working.

This is current limitation and should change in time of final Gaelyk 2.0 release.

Let's show the difference between datastore.query and datastore.build methods:

Query method example

Query query = datastore.query {
    from 'Comment' as Comment
    where author == 10
    sort by crate desc
    limit 10        
}

PreparedQuery pq = datastore.prepare query

// The limit is gone! We need to set it again
FetchOptions.Builder options = withLimit(10)

// set the offset if there is page param specified
if(params.page){
    options = options.offset((params.page as int) * 10)
}

def comments = pq.asList(options.build())

// and we also get only entities not comments
for(Entity e in entity){
    // which needs to be coerced manually
    Comment comment = e as Comment
    process comment
}

When using datastore.query method a portion of query parameters where erased, expecially loosing coercion
is very confusing. On the other hand if you use datastore.build instead than all the information are kept.

Build method example

QueryBuiler dsl = datastore.build {
    from 'Comment' as Comment
    where author == 10
    sort by crate desc
    limit 10
}

// set the offset if there is page param specified
if(params.page){
    dsl.offset((params.page as int) * 10)
}

def comments = dsl.execute()

// no need to coerce manually
for(Comment in comments){
    process comment
}

Everyday Gaelyk: Find blob file by name

19 březen 2013

Google App Engine has sort of support for saving files to the blobstore. Gaelyk adds some nice sugar to the files service. You can easily create file in blobstore using following shortcut:

Creating file in the blobstore

def file = files.createNewBlobFile("text/plain", "hello.txt")

file.withWriter { writer ->
    writer << "some content"
}

It is very nice that you have to set the content type and the name of the file but neither the content type nor the file
name is used anywhere. If you serve the file from the blobstore
the content type doesn't seem to be set to the response and of course you can't find file by its name easily. But it doesn't mean there
is no way how to do it.

Google offers class BlobInfoFactory
to query the information about stored blobs but you would have to walk throught all the blobs in your application to find the proper blob.
Luckily, blob information is just plain entity and
BlobInfoFactory
exposes fields BlobInfoFactory.KIND, BlobInfoFactory.CREATION and BlobInfoFactory.FILENAME which can help you find the desired file.

String name = "hello.txt"
def files = datastore.execute {
    from BlobInfoFactory.KIND
    where BlobInfoFactory.FILENAME == name
    sort desc by BlobInfoFactory.CREATION
}
if(files){
    new BlobKey(files[0].key.name).serve(response)
} else {
    response.status = 404
    out << "No such file $name"
}

Since the file name isn't guaranteed to be unique, you can get more than one result. Sorting the blob information by its creation time
help you to get the latest file as the first element of returned list. This might be acctually handy if you want to implement very simple and
inefficient version system. You can create new blob key from the name of the entity returned from the query and using this key you can serve
the file to the client.

Everyday Gaelyk: Solving java.lang.NoClassDefFoundError: com/google/appengine/api/search/AddResponse

19 březen 2013

Today, Google App Engine Team said farewell to deprecated classes in version 1.7.6 causing sevral sites crashed. Even
the application was deployed with older SDK the AddResponse
class just disappeared. Yes, we all were warned in that release notes but who cares about the warnings. I wish they have mentioned
when the new version will be released too so we'll be extra careful today.

For Gaelyk users there are two choices to fix your application:

Both solutions was discussed in this thread.
If you use GaelykCategory class directly in your code, 1.3 snapshot is better option for you because the class is no longer present in the
code base of 2.0 snapshots. The category class was broken into serval separate extension modules.

If you're using plain Java in your Google App Engine application, just switch to latest 1.7.6 SDK and compliation errors will
guide you.

Everyday Gaelyk: Simplify flow in groovlets by handling their return values

17 březen 2013

Gaelyk plugin system
allows you to define after hook which is called when groovlet
execution is finished. Since Gaelyk 2.0 the
result returned from the groovlet is bound to the after closure so you can e.g. turn that result into the JSON response.

Let say you have a groovlet serving some JSON content by id and you want to simplify it's flow.

JSON groovlet, standard version

 response.contentType = 'application/json'

 if(users.isUserLoggedIn){
     response.status = 401
     json(error: 'You must be logged in') 
 } else {
     // uses @Entity annotation
     Item item = Item.get(params.id as long)
     if(!item){
         response.status = 404
         json(error: 'Item not found')
     } else {
         json(id: item.id, title: item.title)
     }
 }

In fact, this example is pretty trivial but still it would be handy if we can cut off some unnecessary branching:

JSON groovlet, version with plugin

 // see following
 request.renderAsJson = true

 if(users.isUserLoggedIn){
     return [status: 401, error: 'You must be logged in'] 
 }

 // uses @Entity annotation
 Item item = Item.get(params.id as long)
 if(!item){
     return [status: 404, error: 'Item not found']
 }

 [id: item.id, title: item.title]

This can be actually quite simply accomplished by creating plugin which will render groovlet result as JSON.
You can follow the guide on Gaelyk website.
Basically, you need a script containing the after handler:

JSON renderer plugin

after {
    // render as json only when this attribute is set to groovy truth
    if(request.renderAsJson){
        // sets the json
    response.contentType = 'application/json'
        // result is stored in 'result' closure variable
    if(result instanceof Map && result?.status){
            // set the status from the status map value
        response.status = result.remove('status') as int
    }
        // render the result
    JsonBuilder json = new JsonBuilder()
    json result
    json.writeTo(response.writer)
    }
}

Save the script as WEB-INF/plugins/jsonRenderer.groovy and create another file WEB-INF/plugins.groovy with single line install jsonRenderer
and you can try it yourself in your Galeyk 2.0 application.

If you got another idea how to use result of groovlet exection in your application, please, leave a message under the post.

Everyday Gaelyk: Handling input parameters gracefully

16 březen 2013

The params object added to groovlets and templates has one
very enpleasant feature that it either contains values of type String or String[] depending on how many parameters were submitted. In
Gaelyk 2.0 is now handling parameters much easier.

Prior Gaelyk 2.0 following unpleasant situation may occur:

Problems with params prior Gaelyk 2.0

// query = ?tag=one&tag=two
params.tag == ['one','two'] as String[]
// this is ok
params.tag.collect { it } == ['one', 'two']

// query = ?tag=one
params.tag == 'one'
// oops, String.each iterates over characters
params.tag.collect { it } == ['o', 'n', 'e']

// query = ?limit=100
params.limit == '100'
params.limit as int == 100

// query = ?limit=100&limit=200
params.limit == ['100', '200'] as String[]
try {
    params.limit as int
    assert false
} catch(ClassCastException e) {
    // cannot cast String[] to int
    assert true
}

These casts are now safe in Gaelyk 2.0.

Safe parameters handling in Gaelyk 2.0

// query = ?tag=one&tag=two
params.tag == ['one','two'] as String[]
// this is ok
params.tag.collect { it } == ['one', 'two']

// query = ?tag=one
params.tag == 'one'
// oops, String.each iterates over characters
params.tag.collect { it } == ['o', 'n', 'e']
// use as String[] to ensure having String array
(params.tag as String[]).collect { it } == ['one']

// query = ?limit=100
params.limit == '100'
params.limit as int == 100

// query = ?limit=100&limit=200
params.limit == ['100', '200'] as String[]
// first value is taken on cast to single value (not array or collection)
params.limit as int == 100

Simply said use as String[] on values from params whenever you expect multiple values and as <type supported by String.asType() or String> if you expect single value.

Everyday Gaelyk: Living on the edge with Gaelyk snapshots

16 březen 2013

I've setup Gaelyk Continuous Integration Server on CloudBees few months ago so each time
the change is pushed to Gaelyk repository and the tests are passing then the new JARs are pushed to the snapshot repository currently hosted at
SonaType OSS. If you like living on the edge and using the latest features you can
use these in your application.

Updating Gradle build is pretty easy. Only thing you need to do is to declare
SonaType snapshot repository in repositories configuration closure

Repositories configuration

repositories {
    ...
    mavenRepo url: 'https://oss.sonatype.org/content/repositories/snapshots/'
}

and change the version of your Gaelyk dependency to the snapshot version, 2.0-SNAPSHOT at the time of writing.

Dependencies configuration

dependencies {
    ...
    compile 'org.gaelyk:gaelyk:2.0-SNAPSHOT'
}

EDIT
By default Gradle caches the snapshot versions.
To disable caching for all snapshot versions add following to your build.gradle file:

Disable snapshot versions caching

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

If you haven't adopted Gradle for Gaelyk development, you can download latest snapshots from
Sonatype OSS Snapshot Repository. Be sure you are using
the same Groovy
and GAE SDK version as listed in common build configuration.

Here you go, you're now using the latest version of Gaelyk available.

Everyday Gaelyk: More readable routes with optional path parameters

16 březen 2013

At the time of writing, the original Gaelyk documentation
still contains the verbose way how to define following routes with optional path variables.
Gaelyk 2.0 helps you
to get rid of lot of boilerplate routes definition.

Current approach to define optional path variables

get "/article/@year/@month/@day/@title", 
    forward: "/article.groovy?year=@year&month=@month&day=@day&title=@title"
get "/article/@year/@month/@day",        
    forward: "/article.groovy?year=@year&month=@month&day=@day"
get "/article/@year/@month",             
    forward: "/article.groovy?year=@year&month=@month"
get "/article/@year",                    
    forward: "/article.groovy?year=@year"
get "/article",                          
    forward: "/article.groovy"

Defining Optional Path Variables in Gaelyk 2.0

As soon as Gaelyk 2.0 is released
you can save a lot of typing, because adding a question mark ? at the end of path variable definition such as @title
has the same effect multiple declaring the multiple routes as described above.

New way to define optional path variables

get "/article/@year?/@month?/@day?/@title?", forward: "/article.groovy"

If the path variable is present it is appended as path_variable_name=@path_variable_name to destination specified in forward or redirect
definition e.g. /article/2013 is forwarded to /article.groovy?year=2013.

Handling trailing slashes

If the route defined ends with slash, all the routes matched must
ends with slash and vice versa, e.g. /article/2013 will match the route above but /article/2013/ won't because the route does not end
with slash.

Mixing optional and required path variables

Of course there is no need to have all the parameters optional. For example, we can make year required:

Mixed optional and required path variables

get "/article/@year/@month?/@day?/@title?", forward: "/article.groovy"

Making Path Variables Sticky

Sometimes you want to have some path variable recognized even if the previous are mising. For example, you want to be able to specify paging using @page
path variable, which itself is optional as well the other path variables. You can do this by assigning the path variable unique prefix like page-
as in following examples. Take a note you can have multiple path variables with prefixes as long as the prefixes are unique. Also routes are
handled as special type of regular expressions so you can use some features like [io]n to specify that the prefix might be either in or on.

Sticky path variable

get "/article/@year?/@month?/@day?/@title?/page-@page?/[io]n-@tag", forward: "/article.groovy"

This route will match following situations and many others:

Sticky path variable usage

/article/in-groovy               => /article.groovy?tag=groovy
/article/page-1                  => /article.groovy?page=1
/article/2013/page-2/on-groovy   => /article.groovy?year=2013&page=2&tag=groovy
/article/2013/03/16              => /article.groovy?year=2013&month=03&day=16

Everyday Gaelyk: Adding methods to groovlets and templates

15 březen 2013

Since Gaelyk 2.0 is build on the top of Groovy 2.1 branch,
you can use extension modules to add useful methods to particular classes.
As all groovlets and templates are basically scripts,
all of them extend groovy.lang.Script class.
Every method added to groovy.lang.Script will be available in your groovlets and templates as well as other scripts and can be easily used
nearly as easy as Grails tags.

Simple Method

If you create simple extension class like following

Extension class

package cz.orany.vladimir.gaelyk

class GroovyTemplatesExtensions {

    static String helloWorld(Script self) { "Hello World" }
}

and register it in org.codehaus.groovy.runtime.ExtensionModule file in the META-INF/services directory

Extension module descriptor

moduleName=gaelyk-scripts-module
moduleVersion=1.0
extensionClasses= cz.orany.vladimir.gaelyk.GroovyTemplatesExtension
staticExtensionClasses=

you will be able to call the method from your template, e.g. test.gtpl

Example template

<html>
   <head><title>Test hello</title></head>
   <body>${helloWorld()}</body>
</html>

Advanced tag-like method

Of course you can create more sofisticated methods which be close to Grails tags.

Secured 'tag'

<% secured { %>
     <p>This is only visible when the user is signed in</p>
<% } %>

The usage is really simple, the implementation isn't difficult either. You basically write same code as you would write in the template:

Method implementation

static void secured(Script self, Closure body) {
    self.with {
        if(users.userLoggedIn){
            body()
        }
    }
}

The core trick is using the context of current script by calling self.with method. In that case you get simply access to all the bindings available
in the script itself.


Older posts are available in the archive.