Archive

Posts Tagged ‘Grails’

Grails 1.2 installed on new computer

November 21st, 2009 No comments

Naturally I run into the same installation issues I ran into before. As noted in the previous post the paths were the issue. Apparently double-quotes aren’t accepted in the path variables anymore, I’m assuming it’s not Windows 7. Rather the latest version of the Groovy installation. Yet, I’m not sure.

Anyway I moved the JDK into a position where the path contains no spaces and made sure that my environment variables no longer had any quotes in them. This is how my development is set up now:

image I also set up my variables accordingly:

image

I’m installing the latest build snapshot so I can test Grails 1.2 on AppEngine. I ran into this issue earlier which apparently is fixed now.

Tags:

AppEngine start up cost

October 16th, 2009 No comments

I’m struggling with an issue where Google AppEngine will take over 30 seconds to do the initial loading of my hosted web-application. Now apparently it does take a while to load applications that rely on allot of different jar files. Which mine naturally does.

There are however some rumors on the AppEngine group that they are looking to improve the loading time of the applications. Here is a link to one of the discussions. Now this  is just one, I’ve seen others where similar statements have been made.

Now I’m looking forward to get the speed up on my application. Especially if I don’t have to do much about it. However there are some tips out there, due to my lack of computer at the moment I haven’t tested the recommendations but in the thread called: startup takes forver “stumpy” has a few recommendations to improve the start time for Google AppEngine for Java.

Fun times with JSON Rendering

September 25th, 2009 No comments

I spent the last couple of hours on my pet project working with a specific issue. After moving it’s hosting to Google AppEngine the classes have meta data stored in them. I’m not allowed to reflect over the meta data (not sure I want to either). So JSON rendering has to be done on something other than the domain class.

This is the same Issue I posted about in a previous post, about rendering lists as JSON.

Now I’m rendering a single instance of a domain class. Now for the fun part. I can’t render JSON with the following statement.

render [
 id: user.id,
 name: user.name,
 email: user.email,
 password: user.password
 ] as JSON

The above statement wont even compile. I also noticed is that the following will produce a rendered output, that cant be recognized at the client side javascript:

render (
 id: user.id,
 name: user.name,
 email: user.email,
 password: user.password
 ) as JSON

I suspect that the above code will be transformed to a parameter listing for the render as JSON code.

After allot of trying I finally figure out that this works:

def userJson = [
 id: user.id,
 name: user.name,
 email: user.email,
 password: user.password]
 render userJson as JSON

Fun!  :) I can render the variable holding the map, but not the map directly.

Tags:

Google AppEngine and Grails 1.2-M2

September 25th, 2009 4 comments

I upgraded this morning, figuring that the changes related to the .gsp views and their pre-compiling would reduce the cold start issues I’m having with Google AppEngine.

However the 1.2-M2 release of Grails, doesn’t fly on Google AppEngine right now. As far as I can see on the mailing list others have had this issue as well.

The problem I’m experiencing is that the LogManager cannot instantiate.

Context initialization failed
org.springframework.beans.factory.access.BootstrapException: Error executing bootstraps; nested exception is org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: java.util.logging.LogManager is a restricted class. Please see the Google App Engine developer's guide for more details.
	at org.codehaus.groovy.grails.web.context.GrailsContextLoader.createWebApplicationContext(GrailsContextLoader.java:74)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
	at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:530)

And

Context initialization failed
org.springframework.beans.factory.access.BootstrapException: Error executing bootstraps; nested exception is org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: Could not initialize class com.google.apphosting.runtime.security.shared.stub.java.util.logging.LogManager

Update: Chad has created a JIRA for this issue located here:http://jira.codehaus.org/browse/GRAILSPLUGINS-1546

Also here is a link to the discussion on the Grails thread:

http://www.nabble.com/java.util.logging.LogManager-is-a-restricted-class.-Please-see-the–Google-App-Engine-developer%27s-guide-for-more-details.-td25453128.html

Finding entities with GORM-JPA

September 24th, 2009 No comments

I’ve been having this issue for a while. Putting it off, but now I had to get my hands dirty. The findBy… methods weren’t returning any objects even though there were data fitting the specified values.

So tonight I finally took the time to take a look at it. My theory was that I could figure out what was wrong by reading the code in JpaPluginSupport.groovy, located in the GORM-JPA plugin.

Now I didn’t find out why its not working. It even looks like its not supported, but I can’t say for sure. What I did however, was find a workaround.

Instead of doing

User.findByEmail(newSaga.email)

I do

User.findWhere(email: newSaga.email)

Using the findWhere method I get results. This is the workaround I’ll go for for now.

Sending mail on Google appengine with Grails

September 16th, 2009 2 comments

Sending emails with Google AppEngine is quite straight forward once you get a hang of things. I ran into some issues, for example I never got the high level api’s to work. The code executed but the emails were never received. Instead I went for Google AppEngines own “low level” api, it worked much better.

What I did is that I set up a really simple MailService, similar to that which is installed with the Mail plug in. However, I do not handle templates instead I just send strings with the mail message.

package mail

import com.google.appengine.api.mail.MailService
import com.google.appengine.api.mail.MailService.Message
import com.google.appengine.api.mail.MailServiceFactory
import javax.mail.MessagingException
import javax.mail.internet.AddressException

class MailService {

 boolean transactional = true

 def sendMail(subject, msgBody, too) {

 try {
 def service = MailServiceFactory.getMailService();

 def msg = new com.google.appengine.api.mail.MailService.Message();
 msg.setSender("xxx");
 msg.setTo(too);
 msg.setSubject(subject);
 msg.setHtmlBody(msgBody);
 service.send(msg);

 } catch (AddressException e) {
 // ...
 } catch (MessagingException e) {
 // ...
 }

 }
}

As you can see it is quite straight forward. I’d like to mention though that in the above code you see a red xxx. This is where you put your sender address. It needs to either be the email to an admin (registered developer) of the app, or it has to be a user logged in using Google’s own login.

With the low-level api it throws an exception like the one below:

Uncaught exception from servlet
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender

Good luck!

Google AppEngine does not like leftshift

August 19th, 2009 No comments

I ran into an issue last night where Google AppEngine wouldn’t persist my entities. If I create an entity using the parameters provided by Grails; then the Save operations would fail with the following error

Uncaught exception from servlet
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.lang.StringBuilder.leftShift() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [(login.email  = ?1 )]
	at com.studentsonly.grails.plugins.uiperformance.CacheFilter.doFilter(CacheFilter.java:71)
	at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
	at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
	at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:237)
	at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
	at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:139)
	at com.google.apphosting.runtime.JavaRuntime.handleRequest(JavaRuntime.java:235)
	at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4823)
	at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4821)
	at com.google.net.rpc.impl.BlockingApplicationHandler.handleRequest(BlockingApplicationHandler.java:24)
	at com.google.net.rpc.impl.RpcUtil.runRpcInApplication(RpcUtil.java:359)
	at com.google.net.rpc.impl.Server$2.run(Server.java:820)
	...

The issue is described here. I tried their solution and it works fine for me.

Grails and Google AppEngine Beginners Guide

August 12th, 2009 21 comments

I recently moved one of my applications to Google AppEngine. What I found missing was an explanatory tutorial to get started. As I mostly work with C#, I kept running into issues simply because I wasn’t too familiar with the technologies used.

Thus I’m writing this easy to follow all-steps-included tutorial, to help people whom are in a similar position to quickly get started developing with Grails for Google AppEngine.

This tutorial will take you through setting up a simple Grails application running on Google AppEngine; using the app-engine and GORM-JPA plugins to Grails. If you run into trouble, I’ve added a trouble shooting section at the end.

First of make sure you have the latest version of the Google AppEngine SDK, and Grails 1.1.1 or better. If you want to deploy your application to Google AppEngine, make sure you have an active account.

I also added a Known Issues section at the end, it’s there to assist you if you run into trouble.

Lets get started.

Create a Grails Application

First create a Grails Application using the create-app command. For this tutorial we’re creating an application called AppEngineDemo; but the name is not important.

C:\Users\Morten\IdeaProjects>grails create-app
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects
Running script c:\dev\grails-1.1.1\scripts\CreateApp_.groovy
Environment set to development
Application name not specified. Please enter:
AppEngineDemo

Install the App-Engine plugin

The app-engine plugin requires a environment variable to be set to the location in which the Google SDK is installed. I recommend you take a minute or two to read through the plugin information page. I’ll add some examples form running the installation on the command line.

Move to the newly created application and install the app-engine plugin to the application.

C:\Users\Morten\IdeaProjects>cd AppEngineDemo

C:\Users\Morten\IdeaProjects\AppEngineDemo>grails install-plugin app-engine
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects\AppEngineDemo
Running script c:\dev\grails-1.1.1\scripts\InstallPlugin.groovy
Environment set to development
Reading remote plugin list ...
Reading remote plugin list ...
Plugin list out-of-date, retrieving..

During the installation the plugin will ask you which persistence provider you want to use. We want to use the GORM-JPA plugin so select JPA for persistence.

Do you want to use JPA or JDO for persistence? (jpa, jdo)
jpa

This will create a file called persistance.xml in your Grails conf directory. In this file we will specify which domain objects we can persist to the Google AppEngine data storage.

Install GORM-JPA plugin

The GORM-JPA plugin will allow us to use the convenient dynamic methods for persistence; for example save() or findby…()

It is a straight forward to install the plugin

C:\Users\Morten\IdeaProjects\AppEngineDemo>grails install-plugin gorm-jpa
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects\AppEngineDemo
Running script c:\dev\grails-1.1.1\scripts\InstallPlugin.groovy
Environment set to development
Reading remote plugin list ...

Create a domain class

We want to create a domain class for the application, in this tutorial we will create a domain class called Note.

C:\Users\Morten\IdeaProjects\AppEngineDemo>grails create-domain-class
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects\AppEngineDemo
Running script c:\dev\grails-1.1.1\scripts\CreateDomainClass.groovy
Environment set to development
Domain class name not specified. Please enter:
Note
Created DomainClass for Note
Created Tests for Note

Next we generate the necessary controllers and views for the Note domain class.

C:\Users\Morten\IdeaProjects\AppEngineDemo>grails generate-all Note
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects\AppEngineDemo
Running script c:\dev\grails-1.1.1\scripts\GenerateAll.groovy
---
Generating views for domain class Note ...
Generating controller for domain class Note ...
Finished generation for domain class Note

Move the domain classes into a package

The storage platform used by Google AppEngine doesn’t allow you to have your persisted domain classes in the default package. Not to worry, this is also good practice. We move our Note domain class into the package called persisted.

package persisted;

import javax.persistence.*;
// import com.google.appengine.api.datastore.Key;

class Note implements Serializable {

 Long id

}

Note the green code line in the above code.

Might as well add some code while were at it. The complete code for the Note domain class should be the following:

package persisted;

import javax.persistence.*;
// import com.google.appengine.api.datastore.Key;

class Note implements Serializable {

 Long id

 String message
}

Update the domain class with annotations

Now we must specify that the Note class can be persisted, and how it will be persisted. This is done using JPA annotations.

You might have noticed the @Entity, @Id,  @GeneratedValue… lines in the above code. These are JPA annotations added by Grails when you created the domain class. Now we must specify one for our added Message property.

We just want to say that it should be persisted along with the class. For this we add the @Basic annotation above the line declaring the message property. This will tell the persistence layer that this property should be saved.

package persisted;

import javax.persistence.*;
// import com.google.appengine.api.datastore.Key;

@Entity
class Note implements Serializable {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 Long id

@Basic
 String message

}

A list of the JPA annotations and their usages can be found here.

Generate the views and controllers

Now it’s time to generate view and controllers for the Note class. This is done as normal, specifying which class to generate for.

C:\Users\Morten\IdeaProjects\AppEngineDemo>grails generate-all persisted.Note
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: c:\dev\grails-1.1.1

Base Directory: C:\Users\Morten\IdeaProjects\AppEngineDemo
Running script c:\dev\grails-1.1.1\scripts\GenerateAll.groovy
Environment set to development
 [groovyc] Compiling 1 source file to C:\Users\Morten\.grails\1.1.1\projects\AppEngineDemo\classes
 [copy] Copying 1 file to C:\Users\Morten\.grails\1.1.1\projects\AppEngineDemo
 [groovyc] Compiling 1 source file to C:\Users\Morten\.grails\1.1.1\projects\AppEngineDemo\classes
 [copy] Copying 1 file to C:\Users\Morten\.grails\1.1.1\projects\AppEngineDemo
Generating views for domain class persisted.Note ...
Generating controller for domain class persisted.Note ...
Finished generation for domain class persisted.Note
C:\Users\Morten\IdeaProjects\AppEngineDemo>

Notice that I specified the package name for the class.

First time deployment to Google AppEngine

Create an application on Google AppEngine using Google’s AppEngine website. When you create your application you specify an name for it. You need to include this name in the grails-app/conf/config.groovy file in your application. Add the following line to the file:

google.appengine.application = "<your application name>"

To host your application on Google AppEngine you need to set the version to a number between 1-100. It has to be an integer, floats are not accepted.

then do the following:

grails set-version 1
grails app-engine package
$APPENGINE_HOME/bin/appcfg.sh update ./target/war

and now to deploy

grails app-engine deploy

Congratulations, your first, basic Google AppEngine application is now running.

Known Issues

Since Grails on Google AppEngine is pretty new, not to mention Java on Google AppEngine. There are still some issues to work through.

Staging dir not cleared

Before we start I just want to mention some known Issues. There is an issue where the staging dir is not removed, you have to delete this manually. You can find it under:

 <user directory>\.grails\1.1.1\projects\<projectname>\stage

the 1.1.1 is the version Grails your using. This causes issues if you install the ui-performance plugin.

Rendering JSON

There is also an issue with rendering JSON. Rendering JSON can cause access exceptions when rendering entities fetched using the GORM. Please see this blog post for details.

Update: I recentlly wrote this post on Fun times with JSON rendering. Containing more details about JSON Rendering.

Generating controllers

You can run into issues generating controllers and views for your domain classes. The current workaround as far as I know is to install the hibernate plugin, generate the views or controllers, then uninstall the hibernate plugin. You can see the details here.

Command Line too long on Windows machines

If your on a Windows machine you might run into a problem where you cannot enhance your classes (preparing them for storage on the GoogleAppEngine data store). The problem is that the command line when enhancing them becomes too long. You can find more details in my previous blog posts on the subject.

Saving Entities with constraints

If you add constraints to your classes you can run into issues where Google AppEngine will try to save the entity using the wrong class. More details in this blog post.

Recommended reading

Here is link to the open JIRA issues for the AppEngine plugin.

If you find any errors in this tutorial or have ideas to improve it, please let me know.

Hope it helps!

Rendering lists from Google AppEngine as JSON

August 12th, 2009 No comments

I ran into another problem with Google AppEngine. I finally got it to save my entities, only to find it crashing as I wanted to send them back again rendered as JSON.

I had a piece of code looking like this:

def services = {
 params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
 render Offering.list(params) as JSON;
 }

The code is simple enough, it loads all the Service offerings using GORM and then renders them as JSON using the built in converter. This resulted in the following error:

Uncaught exception from servlet
org.codehaus.groovy.runtime.InvokerInvocationException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts
...
Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts
	at grails.converters.JSON.value(JSON.java:199)
	at grails.converters.JSON.convertAnother(JSON.java:156)
...
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_$3.run(Method_.java:149)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.privilegedInvoke(Method_.java:147)
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.invoke(Method_.java:120)
	... 26 more
Caused by: java.lang.reflect.InvocationTargetException
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_$3.run(Method_.java:149)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.privilegedInvoke(Method_.java:147)
	at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.invoke(Method_.java:120)
...
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission getClassLoader)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.ClassLoader.getParent(Unknown Source)
	... 59 more

The problem is that the GORM loads the class with some meta data attached to it. I’m not allowed to reflect over this meta data, so the normal converter fails with the above error. I took the privilege to shorten the stack trace somewhat.

The work around is simple and elegant. I use the groovy collection method called select. With this I can construct a list with maps with the fields I want in my JSON and render them using the normal converter.

def services = {
 params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
 def list =  Offering.list(params);
 render list.collect { offering->[id:offering.id, name:offering.name]} as JSON
 }

The above code works fine on Google AppEngine.

I hope this helps someone else.

Moving from Mor.ph to Google AppEngine

August 10th, 2009 No comments

Moving from Mor.ph AppSpace to Google AppEngine isn’t as easy as I hoped.

Installing AppEngine plug in

Initially moving the Application by installing the app-engine plug in is straight forward. Except that the command-line written in the plug in page for executing the initial setup is wrong. Ive corrected it now.

Windows users need to run the update command with the following command-line:

%APPENGINE_HOME%/bin/appcfg.cmd update ./target/war

Also unless the app-engine code executes correctly it will not clean up the staging directory. This causes problems if your using plug ins such as the ui-performance plug in. That is written to execute on a “clean” staging directory. If you it’s not cleaned up the plug in will try to compress the gziped version the files as well.

GORM

The problem however is that the GORM functions are not available, since the AppEngine is using a different data storage not supported by Hibernate.

There is a solution to this, install the GORM-JPA plug in. This gives you most GORM functions, but using Google AppEngines data storage.

Issues

From here I’ve run into several issues along the way. The framework for deploying to Google AppEngine crashes when running on Windows machines; if you have allot of libraries and classes in your project. Google AppEngine fails to save my entities, JSON conversion is acting up as it tries to serialize class meta data for some reason, which is were I’m currently stuck.

Light at the end of the Tunnel

Now I’d also like to mention that these issues are being ironed out. There are people looking at most of them, it’s just that Grails on Google AppEngine is relatively young so to speak. Even Java on Google AppEngine is quite new, if not Google AppEngine in it self.

Also I’d also like to mention the dashboard on Google AppEngine. It’s great!

The Dashboard on Google AppEngine gives the overview logs I got from Mor.ph, but they are presented better. They also give me some summaries on where most errors occur (which URLs crash) and which URLs are consuming the most CPU. This helps me to optimize specific URLs.

Since my application uses a REST service consumed through AJAX I can optimize parts of the application on the back end, and monitor the usage of parts of the web service.