org.codehaus.groovy.grails.commons
Class DefaultGrailsApplication

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by org.codehaus.groovy.grails.commons.DefaultGrailsApplication
All Implemented Interfaces:
groovy.lang.GroovyObject, GrailsApplication, BeanClassLoaderAware, ApplicationContextAware

public class DefaultGrailsApplication
extends groovy.lang.GroovyObjectSupport
implements GrailsApplication, BeanClassLoaderAware

Default implementation of the GrailsApplication interface that manages application loading, state, and artefact instances.

Upon loading this GrailsApplication will inspect each class using its registered ArtefactHandler instances. Each ArtefactHandler provides knowledge about the conventions used to establish its artefact type. For example controllers use the ControllerArtefactHandler to establish this knowledge.

New ArtefactHandler instances can be registered with the GrailsApplication thus allowing application extensibility.

Since:
0.1

Created: Jul 2, 2005

Author:
Marc Palmer, Steven Devijver, Graeme Rocher
See Also:
GrailsPluginManager, DefaultGrailsPluginManager, ArtefactHandler, ArtefactInfo

Field Summary
 
Fields inherited from interface org.codehaus.groovy.grails.commons.GrailsApplication
APPLICATION_ID, CONFIG_CLASS, DATA_SOURCE_CLASS, ENV_APPLICATION, ENV_DEVELOPMENT, ENV_PRODUCTION, ENV_TEST, ENVIRONMENT, ENVIRONMENT_DEFAULT, GLOBAL_PLUGINS_DIR, PLUGINS_DIR, PROJECT_CLASSES_DIR, PROJECT_META_FILE, PROJECT_RESOURCES_DIR, PROJECT_TEST_CLASSES_DIR, PROJECT_WORK_DIR, WORK_DIR
 
Constructor Summary
DefaultGrailsApplication()
          Creates a new empty Grails application
DefaultGrailsApplication(Class[] classes, ClassLoader classLoader)
          Creates a new GrailsApplication instance using the given classes and GroovyClassLoader
DefaultGrailsApplication(GrailsResourceLoader resourceLoader)
           
DefaultGrailsApplication(Resource[] resources)
          Constructs a GrailsApplication with the given set of groovy sources specified as Spring Resource instances
 
Method Summary
 void addArtefact(Class artefact)
          Adds the given artefact, attempting to determine type from
 GrailsClass addArtefact(String artefactType, Class artefactClass)
          Adds an artefact of the given type for the given Class.
 GrailsClass addArtefact(String artefactType, GrailsClass artefactGrailsClass)
          Adds an artefact of the given type for the given GrailsClass.
 void addOverridableArtefact(Class artefact)
          Adds an artefact that can be overriden by user defined classes
 GrailsClass addOverridableArtefact(String artefactType, Class artefactClass)
          Adds an artefact of the given type for the given Class.
 void configChanged()
          Fired to inform the application when the Config.groovy file changes
 Class[] getAllArtefacts()
          Returns all the classes identified as artefacts by ArtefactHandler instances
 Class[] getAllClasses()
          Retrieves all classes loaded by the GrailsApplication
 GrailsClass getArtefact(String artefactType, String name)
          Retrieves an artefact for the given type and nam
 GrailsClass getArtefactByLogicalPropertyName(String type, String logicalName)
          Retrieves an artefact by its logical property name.
 GrailsClass getArtefactForFeature(String artefactType, Object featureID)
          Get an artefact GrailsClass by a "feature" which depending on the artefact may be a URI or tag name for example
 ArtefactHandler getArtefactHandler(String type)
          Returns the ArtefactHandler for the given type
 ArtefactHandler[] getArtefactHandlers()
          Obtain a list of all the artefact handlers
 ArtefactInfo getArtefactInfo(String artefactType)
          Get the cache of classes for the specified artefact type
 GrailsClass[] getArtefacts(String artefactType)
          Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler
 ArtefactHandler getArtefactType(Class theClass)
          Returns the ArtefactHandler for the given class or null
 Class getClassForName(String className)
          Retrieves a class from the GrailsApplication for the given name
 ClassLoader getClassLoader()
          Returns the class loader instance for the Grails application
 groovy.util.ConfigObject getConfig()
          Returns the ConfigObject instance
 Map getFlatConfig()
          Returns the flatten ConfigObject for use from Java classes
 ApplicationContext getMainContext()
          Returns the Spring context for this application.
 Map getMetadata()
          Get access to the project's metadata, specified in application.properties This provides access to information like required grails version, application name, version etc but NOT general application settings.
 ApplicationContext getParentContext()
          Retrieves the parent ApplicationContext for this GrailsApplication
 Object getProperty(String propertyName)
          Override property access and hit on xxxxClasses to return class arrays of artefacts
 Resource getResourceForClass(Class theClazz)
          Retrieves the Spring Resource that was used to load the given Class
 GrailsResourceLoader getResourceLoader()
           
 boolean hasArtefactHandler(String type)
          Test whether an artefact handler exists for a given type
 void initialise()
          Initialise this GrailsApplication
 Object invokeMethod(String methodName, Object args)
          Overrides method invocation to return dynamic artefact methods We will support getXXXXClasses() and isXXXXClass(class)
 boolean isArtefact(Class theClazz)
          Returns true if the given class is an artefact identified by one of the registered ArtefactHandler instances.
 boolean isArtefactOfType(String artefactType, Class theClazz)
          Returns true if the specified class is of the given artefact type as defined by the ArtefactHandler
 boolean isArtefactOfType(String artefactType, String className)
          Returns true if the specified class name is of the given artefact type as defined by the ArtefactHandler
 boolean isInitialised()
          Returns whether this GrailsApplication has been initialised or not
 boolean isWarDeployed()
          Returns true if this application has been deployed as a WAR file
 void rebuild()
          Rebuilds this Application throwing away the class loader and re-constructing it from the loaded resources again.
 void refresh()
          Refreshes this GrailsApplication, rebuilding all of the artefact definitions as defined by the registered ArtefactHandler instances
 void refreshConstraints()
          Refreshes constraints defined by the DomainClassArtefactHandler
 void registerArtefactHandler(ArtefactHandler handler)
          Registers a new ArtefactHandler that is responsible for identifying and managing an particular artefact type that is defined by some convention
 void setApplicationContext(ApplicationContext applicationContext)
          Sets the parent ApplicationContext for the GrailsApplication
 void setBeanClassLoader(ClassLoader classLoader)
           
 void setMainContext(ApplicationContext context)
          Sets the main Spring context for this application.
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass, setProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultGrailsApplication

public DefaultGrailsApplication()
Creates a new empty Grails application


DefaultGrailsApplication

public DefaultGrailsApplication(Class[] classes,
                                ClassLoader classLoader)
Creates a new GrailsApplication instance using the given classes and GroovyClassLoader

Parameters:
classes - The classes that make up the GrailsApplication
classLoader - The GroovyClassLoader to use

DefaultGrailsApplication

public DefaultGrailsApplication(Resource[] resources)
                         throws IOException
Constructs a GrailsApplication with the given set of groovy sources specified as Spring Resource instances

Parameters:
resources - An array or Groovy sources provides by Spring Resource instances
Throws:
IOException - Thrown when an error occurs reading a Groovy source

DefaultGrailsApplication

public DefaultGrailsApplication(GrailsResourceLoader resourceLoader)
Method Detail

getAllArtefacts

public Class[] getAllArtefacts()
Returns all the classes identified as artefacts by ArtefactHandler instances

Specified by:
getAllArtefacts in interface GrailsApplication
Returns:
An array of classes

getResourceLoader

public GrailsResourceLoader getResourceLoader()

getClassLoader

public ClassLoader getClassLoader()
Description copied from interface: GrailsApplication

Returns the class loader instance for the Grails application

Specified by:
getClassLoader in interface GrailsApplication
Returns:
The ClassLoader instance

getConfig

public groovy.util.ConfigObject getConfig()
Description copied from interface: GrailsApplication
Returns the ConfigObject instance

Specified by:
getConfig in interface GrailsApplication
Returns:
The ConfigObject instance

getFlatConfig

public Map getFlatConfig()
Description copied from interface: GrailsApplication
Returns the flatten ConfigObject for use from Java classes

Specified by:
getFlatConfig in interface GrailsApplication
Returns:
The flattened config

getAllClasses

public Class[] getAllClasses()
Retrieves all classes loaded by the GrailsApplication

Specified by:
getAllClasses in interface GrailsApplication
Returns:
All classes loaded by the GrailsApplication

getMainContext

public ApplicationContext getMainContext()
Description copied from interface: GrailsApplication
Returns the Spring context for this application. Note that this will return null until the application is fully initialised. This context contains all the application artifacts, plugin beans, the works.

Specified by:
getMainContext in interface GrailsApplication

setMainContext

public void setMainContext(ApplicationContext context)
Description copied from interface: GrailsApplication
Sets the main Spring context for this application.

Specified by:
setMainContext in interface GrailsApplication

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)
                           throws BeansException
Sets the parent ApplicationContext for the GrailsApplication

Specified by:
setApplicationContext in interface ApplicationContextAware
Parameters:
applicationContext - The ApplicationContext
Throws:
BeansException - Thrown when an error occurs setting the ApplicationContext

getParentContext

public ApplicationContext getParentContext()
Retrieves the parent ApplicationContext for this GrailsApplication

Specified by:
getParentContext in interface GrailsApplication
Returns:
The parent ApplicationContext

getClassForName

public Class getClassForName(String className)
Retrieves a class from the GrailsApplication for the given name

Specified by:
getClassForName in interface GrailsApplication
Parameters:
className - The class name
Returns:
Either the java.lang.Class instance or null if it doesn't exist

refreshConstraints

public void refreshConstraints()
Refreshes constraints defined by the DomainClassArtefactHandler

Specified by:
refreshConstraints in interface GrailsApplication
To do:
Move this out of GrailsApplication

refresh

public void refresh()
Refreshes this GrailsApplication, rebuilding all of the artefact definitions as defined by the registered ArtefactHandler instances

Specified by:
refresh in interface GrailsApplication

rebuild

public void rebuild()
Description copied from interface: GrailsApplication
Rebuilds this Application throwing away the class loader and re-constructing it from the loaded resources again. This method can only be called in development mode and an error will be thrown if called in a different enivronment

Specified by:
rebuild in interface GrailsApplication

getResourceForClass

public Resource getResourceForClass(Class theClazz)
Retrieves the Spring Resource that was used to load the given Class

Specified by:
getResourceForClass in interface GrailsApplication
Parameters:
theClazz - The class
Returns:
Either a Spring Resource or null if no Resource was found for the given class

isArtefact

public boolean isArtefact(Class theClazz)
Returns true if the given class is an artefact identified by one of the registered ArtefactHandler instances. Uses class name equality to handle class reloading

Specified by:
isArtefact in interface GrailsApplication
Parameters:
theClazz - The class to check
Returns:
True if it is an artefact

isArtefactOfType

public boolean isArtefactOfType(String artefactType,
                                Class theClazz)
Returns true if the specified class is of the given artefact type as defined by the ArtefactHandler

Specified by:
isArtefactOfType in interface GrailsApplication
Parameters:
artefactType - The type of the artefact
theClazz - The class
Returns:
True if it is of the specified artefactType
See Also:
ArtefactHandler

isArtefactOfType

public boolean isArtefactOfType(String artefactType,
                                String className)
Returns true if the specified class name is of the given artefact type as defined by the ArtefactHandler

Specified by:
isArtefactOfType in interface GrailsApplication
Parameters:
artefactType - The type of the artefact
className - The class name
Returns:
True if it is of the specified artefactType
See Also:
ArtefactHandler

getArtefact

public GrailsClass getArtefact(String artefactType,
                               String name)
Retrieves an artefact for the given type and nam

Specified by:
getArtefact in interface GrailsApplication
Parameters:
artefactType - The artefact type as defined by a registered ArtefactHandler
name - The name of the class
Returns:
A GrailsClass instance or null if none could be found for the given artefactType and name

getArtefactType

public ArtefactHandler getArtefactType(Class theClass)
Description copied from interface: GrailsApplication
Returns the ArtefactHandler for the given class or null

Specified by:
getArtefactType in interface GrailsApplication
Parameters:
theClass - The class
Returns:
The ArtefactHandler

getArtefacts

public GrailsClass[] getArtefacts(String artefactType)
Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler

Specified by:
getArtefacts in interface GrailsApplication
Parameters:
artefactType - The type of the artefact defined by the ArtefactHandler
Returns:
An array of classes for the given artefact

getArtefactForFeature

public GrailsClass getArtefactForFeature(String artefactType,
                                         Object featureID)
Description copied from interface: GrailsApplication

Get an artefact GrailsClass by a "feature" which depending on the artefact may be a URI or tag name for example

Specified by:
getArtefactForFeature in interface GrailsApplication
Parameters:
artefactType - The type ID of the artefact, i.e. "TagLib"
featureID - The "feature" ID, say a URL or tag name
Returns:
The grails class or null if none is found

addArtefact

public GrailsClass addArtefact(String artefactType,
                               Class artefactClass)
Adds an artefact of the given type for the given Class.

Specified by:
addArtefact in interface GrailsApplication
Parameters:
artefactType - The type of the artefact as defined by a ArtefactHandler instance
artefactClass - A Class instance that matches the type defined by the ArtefactHandler
Returns:
The GrailsClass if successful or null if it couldn't be added
Throws:
GrailsConfigurationException - If the specified Class is not the same as the type defined by the ArtefactHandler
See Also:
ArtefactHandler

addArtefact

public GrailsClass addArtefact(String artefactType,
                               GrailsClass artefactGrailsClass)
Adds an artefact of the given type for the given GrailsClass.

Specified by:
addArtefact in interface GrailsApplication
Parameters:
artefactType - The type of the artefact as defined by a ArtefactHandler instance
artefactGrailsClass - A GrailsClass instance that matches the type defined by the ArtefactHandler
Returns:
The GrailsClass if successful or null if it couldn't be added
Throws:
GrailsConfigurationException - If the specified GrailsClass is not the same as the type defined by the ArtefactHandler
See Also:
ArtefactHandler

registerArtefactHandler

public void registerArtefactHandler(ArtefactHandler handler)
Registers a new ArtefactHandler that is responsible for identifying and managing an particular artefact type that is defined by some convention

Specified by:
registerArtefactHandler in interface GrailsApplication
Parameters:
handler - The ArtefactHandler to regster

hasArtefactHandler

public boolean hasArtefactHandler(String type)
Description copied from interface: GrailsApplication

Test whether an artefact handler exists for a given type

Specified by:
hasArtefactHandler in interface GrailsApplication
Parameters:
type - The type of the handler
Returns:
True if it does

getArtefactHandlers

public ArtefactHandler[] getArtefactHandlers()
Description copied from interface: GrailsApplication

Obtain a list of all the artefact handlers

Specified by:
getArtefactHandlers in interface GrailsApplication
Returns:
The list, possible empty but not null, of all currently registered handlers

getArtefactHandler

public ArtefactHandler getArtefactHandler(String type)
Description copied from interface: GrailsApplication
Returns the ArtefactHandler for the given type

Specified by:
getArtefactHandler in interface GrailsApplication
Parameters:
type - The artefact handler type
Returns:
The artefact handler

getArtefactInfo

public ArtefactInfo getArtefactInfo(String artefactType)

Get the cache of classes for the specified artefact type

Specified by:
getArtefactInfo in interface GrailsApplication
Parameters:
artefactType - The name of an artefact type
Returns:
The cache of classes for the type, or null if no cache exists

invokeMethod

public Object invokeMethod(String methodName,
                           Object args)

Overrides method invocation to return dynamic artefact methods

We will support getXXXXClasses() and isXXXXClass(class)

Specified by:
invokeMethod in interface groovy.lang.GroovyObject
Overrides:
invokeMethod in class groovy.lang.GroovyObjectSupport
Parameters:
methodName - The name of the method
args - The arguments to the method
Returns:
The return value of the method
To do:
Need to add matches for addClass(java.lang.Class) and addClass(GrailsClass)

getProperty

public Object getProperty(String propertyName)

Override property access and hit on xxxxClasses to return class arrays of artefacts

Specified by:
getProperty in interface groovy.lang.GroovyObject
Overrides:
getProperty in class groovy.lang.GroovyObjectSupport
Parameters:
propertyName - The name of the property, if it ends in *Classes then match and invoke internal ArtefactHandler
Returns:
All the artifacts or delegate to super.getProperty

initialise

public void initialise()
Description copied from interface: GrailsApplication
Initialise this GrailsApplication

Specified by:
initialise in interface GrailsApplication

isInitialised

public boolean isInitialised()
Description copied from interface: GrailsApplication
Returns whether this GrailsApplication has been initialised or not

Specified by:
isInitialised in interface GrailsApplication
Returns:
True if it has been initialised

getMetadata

public Map getMetadata()
Description copied from interface: GrailsApplication

Get access to the project's metadata, specified in application.properties

This provides access to information like required grails version, application name, version etc but NOT general application settings.

Specified by:
getMetadata in interface GrailsApplication
Returns:
A read-only Map of data about the application, not environment specific

getArtefactByLogicalPropertyName

public GrailsClass getArtefactByLogicalPropertyName(String type,
                                                    String logicalName)
Description copied from interface: GrailsApplication
Retrieves an artefact by its logical property name. For example the logical property name of BookController would be book

Specified by:
getArtefactByLogicalPropertyName in interface GrailsApplication
Parameters:
type - The artefact type
logicalName - The logical name
Returns:
The GrailsClass or null if it doesn't exist

addArtefact

public void addArtefact(Class artefact)
Description copied from interface: GrailsApplication
Adds the given artefact, attempting to determine type from

Specified by:
addArtefact in interface GrailsApplication
Parameters:
artefact - The artefact to add

isWarDeployed

public boolean isWarDeployed()
Description copied from interface: GrailsApplication
Returns true if this application has been deployed as a WAR file

Specified by:
isWarDeployed in interface GrailsApplication
Returns:
True if the application is WAR deployed

setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
Specified by:
setBeanClassLoader in interface BeanClassLoaderAware

addOverridableArtefact

public void addOverridableArtefact(Class artefact)
Description copied from interface: GrailsApplication
Adds an artefact that can be overriden by user defined classes

Specified by:
addOverridableArtefact in interface GrailsApplication
Parameters:
artefact - An overridable artefact

configChanged

public void configChanged()
Description copied from interface: GrailsApplication
Fired to inform the application when the Config.groovy file changes

Specified by:
configChanged in interface GrailsApplication

addOverridableArtefact

public GrailsClass addOverridableArtefact(String artefactType,
                                          Class artefactClass)
Adds an artefact of the given type for the given Class.

Parameters:
artefactType - The type of the artefact as defined by a ArtefactHandler instance
artefactClass - A Class instance that matches the type defined by the ArtefactHandler
Returns:
The GrailsClass if successful or null if it couldn't be added
Throws:
GrailsConfigurationException - If the specified Class is not the same as the type defined by the ArtefactHandler
See Also:
ArtefactHandler


Copyright (c) 2005-2009 The Grails project