Grails 1.0.3 Released (Graeme Rocher)

, , June 6th, 2008

We’ve (G2One Inc and the Grails development team) just released Grails 1.0.3, which includes 230 issues resolutions and improvements since the last release. The release notes go through the full details, including outlining some of the new features like enum support and interactive mode.

Grails has come a long way, since the release of 1.0 Grails has been downloaded over 186000 times averaging out to around 50000 times per month. That puts it on par or not far behind some of the biggest open source projects like Spring, Hibernate and Struts in terms of downloads.

The most exciting thing for me though is the plug-in community with over 70 plug-ins in the repository some of the new ones include Axis 2 support, Java2D with GraphicsBuilder and profiling (contributed by one of the biggest Grails users LinkedIn) and debugging plug-ins. Awesome stuff.

Now I’m shifting my focus to the second edition of the book, and feature development for Grails 1.1.

- Graeme Rocher

Tags: , ,

Plug-in: Migrating from Struts to Grails (Graeme Rocher)

, March 4th, 2008

Grails is a full stack framework that provides everything from the build system to the persistence layer. Struts 1 is a first generation Java web framework that is still very popular, but it only solves the controller/view layer.

This plug-in allows you to use Struts 1 as a the controller/view layer for Grails and also provides a migration path to Grails from Struts 1.

Getting Started

Once you have created a Grails application, you need to install the plug-in. The plug-in is hosted in the Grails central repository and can be installed with:

grails install-plugin struts1

Alternatively you can download the plugin as a zip file from here: http://svn.codehaus.org/grails-plugins/grails-struts1/tags/LATEST_RELEASE/

And then run

grails install-plugin /path/to/file/grails-struts1-1.3.8.zip

Either way it will set up struts with the most common servlet mapping of *.do. The next thing you need to do is change the way Grails deals with file extensions otherwise Grails will swallow the *.do mapping. To do this set the following property in grails-app/conf/Config.groovy:

grails.mime.file.extensions = false

Now you have setup Struts 1 inside Grails and there is a struts-config.xml and validation.xml within /web-app/WEB-INF. Now when you run

grails run-app

Struts will load inside Grails

Example Application

You can download an example of the Struts mailreader application runing inside Grails.

Migrating an Existing Struts application

To migrate an existing Struts 1 application you can simply:

  1. Copy your struts-config.xml and validation.xml into web-app/WEB-INF overriting the existing ones if necessary
  2. Copy Java sources into src/java
  3. Copy all dependant JAR files into the lib directory

Forwarding to a GSP view from Struts

You’ll notice that by default the struts-config.xml setup by Grails has the following mapping:

<action
            path=“/Welcome”
            forward=“/index.gsp”/>

Notice how we’re able to map the URI /Welcome.do to a GSP page.

Mapping a Struts action to a Grails controller

If you prefer to write your controller logic in Grails controller you can do so and use the ControllerActionProxy class to map to it. For example given the following Grails controller:

class TestController {
	def foo = {
		println “EXECUTING FOO”
	}
}

To map to this controller with ControllerActionProxy simply add this to struts-config.xml:

<action
            path=“/test/foo”
            type=“org.codehaus.grails.struts.action.ControllerActionProxy”
            />

Now when you go to /test/foo.do in the browser the ControllerActionProxy will automatically map onto the Grails controller.

Even better Grails’ view mapping conventions will be honoured and in this case the view grails-app/views/test/foo.gsp will be rendered.

The Struts 1 plug-in also adds two new implicit properties to the Grails controllers:

  • actionForm - An instance of the Struts ActionForm class if one is configured for the Action

Graeme Rocher (Original)

Tags: ,

Grails vs Rails Myth #1: Grails has a fraction of what Rails has to offer

, June 21st, 2007

n this post the author proclaims boldly that Grails has a “fraction of RoRs functionality”. So in the spirit of Relevance’s myth series (I’m not sure I’ll do more than one of these, we’ll see) let me sum up my feelings on this.

I would like to have jumped right onto the Rails bandwagon if it hadn’t been for the fact that ActiveRecord offers only a fraction of what Hibernate does.

  • Where is the proper transaction & conversation support?
  • Why is it that it hits the db orders of magnitude harder that Hibernate does and is infinitely slower?
  • Where is the criteria support? What about distributed caching?

I would be Ruby maniac right now if Ruby didn’t offer only a fraction of what is available in Java. From the reams of web frameworks, to the dozens of persistence engines, to distributed caches, enterprise integration tools and testing frameworks. Java has it all. There is literally a library for everything.

And I would probably have said a long goodbye to Java, if it wasn’t for the fantastic innovation that is happening in projects like Spring and Hibernate and the libraries that integrate with them (Quartz, Sitemesh, Compass, Acegi, Webflow et al) which Grails is built on. Want an RMI, burlap, http, soap or DWR service? Just expose one. Need advanced declarative security at the web and business layer level? Plug it right in. Job scheduling? Job done. Search? Sure no problem.

Spring and all the projects that integrate with it, make the Java ecosystem a very happy place indeed. So no, Grails might not have RJS (yet), migrations (yet) or xyz feature from Rails, but it has plenty, thanks to the Java eco-system, to make up for it and then some.

Graeme Rocher

Tags: ,

Groovy Adds Support for Generics

, June 9th, 2007

From the Groovy Mailing List:

Hi all,

the compiler is now able to support most of the generics declaration
features. It is now possible to use generics in methods, fields and
class definitions using wildcards, lower and upper bounds. The
implementation might have several bugs, I tried to find them, but of
course I can’t foresee all cases.

Things that are currently missing:
* covariant return types (might come soon)
* compile time checks for correct usage of the number of parameters
(might not yet be implemented as it requires java5 features)

have fun

bye blackdrag


Jochen “blackdrag” Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

Generics support in will be useful in Grails, according to Graeme Rocher:

>
> will Grails now make use of it? List<Books> looks useful to Grails.

Yeh definitely for those who want to use EJB3 style mappings instead
of GORM style mappings

Cheers
Graeme

Tags: ,

Writing Groovy DSLs (Guillaume Laforge & Graeme Rocher comment)

, June 9th, 2007

Warner Onstine described his recipe for writing Domain Specific Languages (DSLs) in Groovy.

The original post is here.

This post highlights the comments on that post:

Comments

 

  1. Graeme Rocher about 18 hours later:

    nteresting, though I find that in the beginning it seems like a good idea to extend BuilderSupport, but later you realise it is too limiting and overriding invokeMethod is far more flexible as far as the syntax options you have available to you

    It of course depends on the DSL you are writing

    Cheers Graeme

  2. Warner Onstine 1 day later:

    This comment came from Guillaume Laforge (Groovy lead) on the Groovy users list:

    This is a nice wrap-up of the steps to follow. The thing I feel is missing a little is the “invention” of the DSL in itself with the end-user. I think we have to stress that part more. You have to know what’s possible to do with Groovy in terms of DSL, you have to speak at length with your users, and incrementally design on paper (or on screen) what the DSL will look like. I think this step should be emphasized a bit more in the approach.

  3. Warner Onstine 1 day later:

    Guillaume, I definitely agree. I wanted to come up with some basics, but the user-level involvement definitely has to be there. It’s hidden right now in the Prototype phase I talk about, but should be more fully discussed in a future post. I have several posts in my head right now so I’ll jot this down as one to write up ;-)

Leave a response on the original post

Tags: ,

Meet The People Behind Groovy & Grails

, , , , June 9th, 2007


Click To Play

Tags: , , , ,

Dynamic Groovy: Groovy’s Equivalent to Ruby Open Classes

June 1st, 2007

A lot of hype has been made of Ruby’s open classes, and for good reason as they’re pretty darn cool. Since Groovy classes compile down to byte code (ie a Groovy class is a Java class) it has always been a little more problematic to add dynamic features to classes you don’t have control over.

If you’re the class implementor for example you could override invokeMethod and getProperty, but if you’re not your only option was Groovy categories, which aren’t nearly as elegant, or implementing your own custom MetaClass which exposed you to Groovy internals and wasn’t very fun.

However, this is all changing because I’ve just committed major improvements and written the documentation for Groovy’s dynamic meta class mechanism, the ExpandoMetaClass. This is the system I discussed with Neal Ford at JavaOne who felt, prior to our discussion, that Ruby had the upper hand because of open classes. After our discussion and a short demo of what ExpandoMetaClass is capable of, Neal changed his mind and upgraded Groovy’s rating on his language scale.

So in the next beta release, you’ll see a new improved version of ExpandoMetaClass with features such as:

Here is a little example:


String.metaClass.swapCase = {->
   def sb = new StringBuffer()
   delegate.each {
        sb << (Character.isUpperCase(it as char) ?
               Character.toLowerCase(it as char) :
               Character.toUpperCase(it as char))
   }
   sb.toString()
}
assert "UpAndDown" == "uPaNDdOWN".swapCase() 

ExpandoMetaClass has been at the heart of Grails for a while, so is completely production ready. Now its time to bring it to the masses with the next beta release of Groovy targeted for the end of June.

Graeme Rocher

Tags:

Groovy and Grails: Three Worries (Graeme Rocher responds)

May 17th, 2007

I had the pleasure of meeting and having dinner (at a wondefully steriotypical American diner) with Stuart Halloway along with other NFJS guys like Ted Neward and Brian Goetz at JavaOne. Since then Stuart took the time to write a nice entry with a few pleasantries that he discovered about Groovy & Grails (Thanks Stuart!).

In a follow-up post entitled Groovy & Grails: Three Worries he raises a few concerns which I commented on, but would like to take the time to address here too.

1. Lack of Open Classes in Groovy

His first concern centers around the fact that Ruby has an open class system whilst Groovy does not. The reasons for this are simply Groovy compiles into byte code directly and there is a one-to-one mapping between a Groovy class and a class file. This is unlike JRuby where you need to to use proxying trickys in order to do things like call Ruby from Java.

So essentially Groovy can’t modify classes directly because as far as the JVM is concerned once a class is loaded it cannot be changed. Of course this is completely sensible as the Java platform has the concept of threading. Changing class definitions at runtime in a multi-threaded environment would be rather problematic from the JVMs perspective. Since Ruby has no concept of threading at this time of writing the language and runtime has no such concerns.

So given these constraints in Groovy we have a different way of doing things. Stuart presented the following example:


class Person {
  def firstName;
}
p = new Person();
class Person {
  def lastName;
}
p.lastName = 'Halloway'

In Groovy (with the upcoming 1.1 release) this would be implemented as:


class Person {
  def firstName;
}
p = new Person();
p.metaClass.lastName = 'Halloway'

p.lastName = "Rocher"

This uses Groovy’s ExpandoMetaClass mechanism. Since in Groovy we can’t change classes, we instead have a MetaClass for each java.lang.Class. The MetaClass defines the behaviour of the actual class. You change the MetaClass you change the class. So Groovy might not have “open” classes, but it has an equivalent mechanism for achieving the same thing.

2. OMG! Grails is not written in 100% Groovy!

Yes that is right Grails is around 80% Java and 20% Groovy. The reasons for this are several. Firstly, Java still makes a great language for writing low-level plumbing code. For dealing with all the complexities that the users of the higher level interface (Grails users) don’t have to deal with. Secondly, since Grails is written in Java we get the performance benefits of this. Grails is between 50 and 300% faster than Rails depending on the test. Rails might be implemented in 100% Ruby, but it has very well documented performance troubles because of this.

Do we eat our own dog food? Hell yeah, we have a plug-in system where plugins are all written in Groovy. All of our custom tag libraries are written in Groovy. Groovy Server Pages (GSP), our view technologies, is all Groovy. So we eat plenty of it trust me. Finally, the Groovy community has never been the one to cry out and tell everyone to ditch Java. No, our philosophy is to choose the right tool for the job, and Java is still a great tool in my toolbox just as Groovy is another. The fact that they work so seamlessly together is what makes the pairing so special.

3. The old static typing debate

So is static typing useful anyway? Well yes, it has been useful for a number of things for us such as enabling us to implement more intelligent scaffolding based on the types (java.util.Date, java.lang.String) etc. Also we could implement things like command objects which using static type information.

In addition at the moment the tool support just is not there yet to use either Ruby or Groovy for large complicated applications. Static typing analysis and the features that it brings to modern IDEs like refactoring and code navigation are just in a different league in Java. I hope this will change in time and since Groovy supports both static and dynamic typing I believe the tools vendors will have an easier time creating quality tools for it.

This is also where Grails fits quite nicely into the picture as it allows you to mix Groovy for the simple to medium complex stuff and Java to do the heavy lifting. IMO Groovy gets the balance here just right.

Graeme Rocher

Tags:

Grails + Wicket: The Wonders of the Grails Plug-in system (Graeme Rocher)

May 17th, 2007

Update: A shorter Wicket plug-in installation guide has been added here

A few people have been asking me recently to integrate the Wicket project as a view technology for Grails. What is Wicket? It is a component oriented framework kind of like JSF, but unlike JSF it uses convention-over-configuration and doesn’t require you to edit reams of XML. So in this sense its aims are more inline with Grails’ aims.

So the question was how do we integrate these two frameworks? Well I thought it couldn’t be that difficult and as it turns out, it isn’t. So what I did was create a plug-in with “grails create-plugin wicket”. The next thing I needed were some jars, so I got Wicket 1.2.6, Wicket Extensions 1.2.6 jar and the Wicket Spring integration jars for 1.2.6 and put them in the plug-ins lib directory.

Job done, now we need to set-up a convention that makes sense for both Grails and Wicket:

1) Since controllers are essentially roughly analogous to the Wicket Application object I decided that the convention would be to have a WebApplication.groovy file in the grails-app/controllers directory.
2) Wickets HTML components are like the views, so they can live in the grails-app/views directory

So to follow the HelloWorld example on the Wicket page we end up with something like this being the structure:

So just for clarify the WebApplication.groovy file looks like this:


import wicket.Page;
import wicket.spring.*;

public class WebApplication extends SpringWebApplication
{
public Class getHomePage()
{
return HelloWorld.class;
}
}

Whilst HelloWorld.groovy looks like this:


import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;

public class HelloWorld extends WebPage
{
public HelloWorld()
{
   add(new Label("message", "Hello World!"))
}
}

Finally, the HelloWorld.html file looks like this:


<html>
<body>
<span wicket:id="message">Message goes here</span>
</body>
</html>

Ok so with that done, let’s take advantage of the conventions we have in place here. When I created the plug-in with “grails create-plugin” it also created me a WicketGrailsPlugin.groovy file in the root of the plugin project. Since the normal controller mechanism no longer makes sense we modify the plug-in to “evict” the controllers plug-in when installed:


class WicketGrailsPlugin {
def version = 0.1
def dependsOn = [:]
def evicts = ['controllers'] // evict the controllers plugin
 ...
}

With that done we now need to configure the Wicket “applicationBean” in Spring. To do this we’re going to use the GrailsApplication object and find a class that is an instance of a Wicket Application. This class just happens to be the one we defined earlier in grails-app/controllers:


import wicket.*
class WicketGrailsPlugin {
...
def doWithSpring = {
def applicationClass = application
                                   .allClasses
                                   .find { Application.class.isAssignableFrom(it) }

if(applicationClass) {
applicationBean(applicationClass) // defines the spring bean
}
}
}

We’re of course using the Spring BeanBuilder here to define the bean definition if the Spring context. Ok job done.

Now we need to modify web.xml.. but wait. In Grails even web.xml is created on the fly so other plugins can participate in its generation. So we can do this in the plug-in file again:


import wicket.*
class WicketGrailsPlugin {
...
def doWithWebDescriptor = { xml ->
   def servlets = xml.servlet[0]

   servlets + {
       servlet {
           ‘servlet-name’('wicket’)
           ’servlet-class’('wicket.protocol.http.WicketServlet’)
           ‘init-param‘ {
               ‘param-name’(’applicationFactoryClassName‘)
               ‘param-value’('wicket.spring.SpringWebApplicationFactory’)
           }
           ‘load-on-startup‘(1)
       }
   }

   def mappings = xml.’servlet-mapping’[0]
   mappings + {
     ‘servlet-mapping’ {
           ‘servlet-name’('wicket’)
           ‘url-pattern’('/app/*’)
      }
   }
}
}

Here we use Groovy’s XmlSlurper DSL to modify and the XML simply by using the + operator and some Groovy mark-up. We again use the Wicket Spring integration support to get it all working, so when Wicket loads it will actually look for the bean created by the Grails plug-in. And that’s it, to test the plug-in we can type “grails run-app” and navigate to http://localhost:8080/grails-wicket/app and we see:

Not hugely impressive I know, but what it does show is Wicket running as the view tech in Grails which means that Wicket components and applications can use GORM to simplify the ORM layer of Wicket applications and other features of Grails like Services and Jobs. This took me all of 20 minutes to write, in fact I’ve spent longer writing this article than the plug-in. Not that it is complete of course, things left to do are to add reloading support to the Groovy files that Wicket users. Advice from the Wicket community on how to do that would be appreciated. And of course I haven’t tested it against any more complex examples yet.

Now of course it wouldn’t be a plug-in if it couldn’t be installed into other apps. To do this we package it up with “grails package-plugin” which will create a zip. I’ve placed in plug-in at http://dist.codehaus.org/grails-plugins/ so in any Grails application you can now do:

grails install-plugin Wicket 0.1

or directly from the URL:

grails install-plugin http://dist.codehaus.org/grails-plugins/grails-Wicket-0.1.zip

And it will install this version of the plug-in.

Note: The plug-in doesn’t work with the Grails URL mapping mechanism in 0.5 so you need to delete any grails-app/conf/*UrlMappings.groovy files otherwise you’ll get an exception.

Plug-in sources can be found here: http://svn.grails-plugins.codehaus.org/browse/grails-plugins/grails-wicket/trunk

Graeme Rocher

Tags: