Home » Grails » Grails Bootstrap – Under the hood

Grails Bootstrap – Under the hood

In this post I shall give an overview of sequence of events that happens during bootstrapping of Grails application

Bootstrap can be divided into following 4  steps from conceptual point of view

  • Loading Plugins and init grailsApplication(Implementation of GrailsApplication Interface  )
  • Create Parent Application Context
  • Create Grails Root Application Context
  • Create WebAppliationContext for GrailsDispatcherServlet

Below is diagrammatic representation of same

Grails-BootStrap

Grails-BootStrap

Lets look at each of above steps in details

Loading Plugins and GrailsApplication

Before talking about workflow of loading plugins,  I want briefly discuss Gant Scripts

 Gant Scripts

  • These are essentially Groovy wrapper around ant scripts.They support concept of targets and dependencies
  • Grails searches for these scripts in below directories
    • USER_HOME/.grails/scripts
    • PROJECT_HOME/scripts
    • PROJECT_HOME/plugins/*/scripts
    • GRAILS_HOME/scripts
  • when you run any grails command example.-  grails run-app, it will search for RunApp.groovy in above directories and execute default target in that script
  • Scripts which do not have ‘_’ ( underscore) in their name,  have a default target and can be run using grails command
    • examples – RunApp, CreateScript,CreateService etc
  • Any script whose  name begins with ‘_’ (underscore) are internal scripts, i.e they  are used by other scripts, and do not have default target
    • example  -  _PluginDependencies.groovy, _PackagePlugins.groovy etc
  • include keyword specifies other scripts to be included in particular script
  • depends keyword executes target from other script.

More about Gant scripts can be found here

Now lets look at the workflow. It can be represented by below diagram

Grails-LoadPlugins

Grails-LoadPlugins

Workflow Key Points

  • There are 4 main Gant scripts shown in workflow – RunApp.groovy, _GrailsPackage.groovy, _PluginDependencies.groovy, _GrailsRun.groovy
  • Each box in diagram represents target which is being executed
  • Sequence of action is represented by arrows . Dashed line indicates return to caller script
  • RunApp.groovy is called when you run command , grails run-app ($GRAILS_HOME/scripts/RunApp.groovy)
  • RunApp has dependency named packageApp( from _GrailsPackage.groovy) . Two important targets called by packageApp are
    • loadPlugins
      • it initializes Java Objects DefaultGrailsApplication, DefaultGrailsPluginManager (if you want profile time taken to load individual plugins, you could set enableProfile to true and it will load ProfilingGrailsPluginManager)
      • loads individual plugins  and  stores  them as list in DefaultPluginManager
      • perform artefactConfigurations for  plugins . (Artefacts are resources that fulfil some  convention eg Controller, Services.)
      • initializes  grailsApplication
        • populatingAllClasses – essentially store all the classes which are loaded in a variable
        • ConfigureLoadedClasses
          • initialize Artifact Handlers  – eg. ControllerArtefactHandler, ServiceArtefactHandler etc…
          • inspect loaded classes and determine which ones are artefacts
    • generateWebXML
      • calls doWebDescriptor for each plugin
      • Generates web.xml  and stores it @  /USER_HOME/.grails/2.1.1/projects/PROJECT_NAME/resources
  • Finally runApp is called which starts Embeded TomcatServer

Root Application Context

When  GrailsContextLoader loads and below are 3 steps performed by it

  • Create  Parent Application Context
  • Create  Root Application Context
  • Execute Grails Bootstrap classes

Lets look at second step above in more details .

Grails-RootAppContext

Grails-RootAppContext

Below are key Points

  • create WebRunTimeSpringConfig
    • Generation of Spring Config at runtime
  • registerProvidedArtifacts – register Precompiled Artifacts
  • pluginManager.doRunTimeSpringConfig
    • Hook into Grails runtime configuration, configure beans.
    • run closure doWithSpring associated with each plugin
  • InitApplicationContext
    • Create GrailsApplicationContext
  • RegisterBeans
    • Register BeanConfigs,BeanNames, Aliases with BeanRegistry.
  • Refresh the context created
  • pluginManager.doDynamicMethods
    • register dynamic methods with any classes based on some criteria (inspect properties, add new methods for benchmarking etc)
  • pluginManager.doPostProcessing
    • execute doWithApplicationContext associated with each plugin

WebApplicationContext and GrailsDispatcherServlet

This is the last step before application launches. Key points regards this

  • an instance of GrailsDispatcherServlet is created
  • Initialize and configure WebApplicationContext . Below are strategies initialized  for default settings
    • MultiPartResolver – ContentLengthAwareCommonsMultipartResolver
    • LocalResolver – SessionLocaleResolver
    • ThemeResolver – FixedThemeResolver
    • HandlerMapping
      • GrailsControllerHandlerMapping
      • RequestMappingHandlerMapping
    • HandlerAdapters
      • RequestMappingHandlerAdapter
      • SimpleControllerHandlerAdapter
    • HandlerExceptionResolver – GrailsExceptionResolver
    • ViewNameTranslator – DefaultRequestToViewNameTranslator
    • ViewResolvers – ScaffoldingViewResolver
    • FlashMapManager – SessionFlashMapManager.

You can read about DispatcherServlet in one of my other article. Once above step is complete , application is ready to run.

A good presentation about architecture of Grails can be found here . Presentation is old, but core-concepts are explained very elegantly.

About these ads

4 Comments

  1. vamsi v says:

    Very useful, I am impressed by your in depth coverage. I want to develop some short tutorials in line with Rails Casts. Would you like to help/ partner with me. thanks. (vamsi v)

  2. vamsi v says:

    Sure,
    Thanks for responding, my email is viz.ply@gmail.com, I live in Dallas, TX.
    Please send in the email with your contact info and I shall put together some details.

  3. Akshay says:

    Good description and explanation
    but i am lacking inline comparison of Grails with Spring MVC
    i believe these two are conceptually same for some initial only
    and your posted URL – http://www.slideshare.net/hominhchuc/graemerocher-grailsplug-inarchitecture-presentation
    is not working; kindly rectify and/or correct t this

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: