Package grails.core

Class DefaultGrailsApplication

  • All Implemented Interfaces:
    GrailsApplication, groovy.lang.GroovyObject, java.util.EventListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>, org.springframework.context.event.SmartApplicationListener, org.springframework.core.Ordered

    public class DefaultGrailsApplication
    extends AbstractGrailsApplication
    implements org.springframework.beans.factory.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
    See Also:
    GrailsPluginManager, DefaultGrailsPluginManager, ArtefactHandler, ArtefactInfo
    • Field Detail

      • GETCLASSESPROP_PATTERN

        protected static final java.util.regex.Pattern GETCLASSESPROP_PATTERN
      • GETCLASSESMETH_PATTERN

        protected static final java.util.regex.Pattern GETCLASSESMETH_PATTERN
      • ISCLASS_PATTERN

        protected static final java.util.regex.Pattern ISCLASS_PATTERN
      • GETCLASS_PATTERN

        protected static final java.util.regex.Pattern GETCLASS_PATTERN
      • allClasses

        protected java.lang.Class<?>[] allClasses
      • log

        protected static org.apache.commons.logging.Log log
      • loadedClasses

        protected java.util.Set<java.lang.Class<?>> loadedClasses
      • artefactHandlersByName

        protected java.util.Map<java.lang.String,​ArtefactHandler> artefactHandlersByName
      • allArtefactClasses

        protected java.util.List<java.lang.Class<?>> allArtefactClasses
      • artefactInfo

        protected java.util.Map<java.lang.String,​ArtefactInfo> artefactInfo
      • allArtefactClassesArray

        protected java.lang.Class<?>[] allArtefactClassesArray
      • resources

        protected org.springframework.core.io.Resource[] resources
      • initialised

        protected boolean initialised
      • applicationClass

        protected grails.core.GrailsApplicationClass applicationClass
      • proxyMappingContext

        protected org.grails.datastore.mapping.model.MappingContext proxyMappingContext
      • mappingContext

        protected org.grails.datastore.mapping.model.MappingContext mappingContext
    • Constructor Detail

      • DefaultGrailsApplication

        public DefaultGrailsApplication()
        Creates a new empty Grails application.
      • DefaultGrailsApplication

        public DefaultGrailsApplication​(grails.core.GrailsApplicationClass applicationClass)
        Creates a new empty Grails application.
      • DefaultGrailsApplication

        public DefaultGrailsApplication​(java.lang.ClassLoader classLoader)
      • DefaultGrailsApplication

        public DefaultGrailsApplication​(java.lang.Class<?>... classes)
        Construct an application for the given classes
        Parameters:
        classes - The classes
      • DefaultGrailsApplication

        public DefaultGrailsApplication​(java.lang.Class<?>[] classes,
                                        java.lang.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​(org.springframework.core.io.Resource[] resources)
        Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names
      • DefaultGrailsApplication

        public DefaultGrailsApplication​(org.grails.io.support.Resource[] resources)
        Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names
    • Method Detail

      • getApplicationClass

        public grails.core.GrailsApplicationClass getApplicationClass()
        Returns:
        The application class
      • initArtefactHandlers

        protected void initArtefactHandlers()
        Initialises the default set of ArtefactHandler instances.
        See Also:
        ArtefactHandler
      • getAllArtefacts

        public java.lang.Class<?>[] getAllArtefacts()
        Returns all the classes identified as artefacts by ArtefactHandler instances.
        Specified by:
        getAllArtefacts in interface GrailsApplication
        Returns:
        An array of classes
      • populateAllClasses

        protected java.lang.Class<?>[] populateAllClasses()
      • configureLoadedClasses

        protected void configureLoadedClasses​(java.lang.Class<?>[] classes)
        Configures the loaded classes within the GrailsApplication instance using the registered ArtefactHandler instances.
        Parameters:
        classes - The classes to configure
      • refreshArtefactGrailsClassCaches

        protected void refreshArtefactGrailsClassCaches()
        Tell all our artefact info objects to update their internal state after we've added a bunch of classes.
      • addToLoaded

        protected void addToLoaded​(java.lang.Class<?> clazz)
      • getArtefactCount

        protected int getArtefactCount​(java.lang.String artefactType)
        Retrieves the number of artefacts registered for the given artefactType as defined by the ArtefactHandler.
        Parameters:
        artefactType - The type of the artefact as defined by the ArtefactHandler
        Returns:
        The number of registered artefacts
      • getAllClasses

        public java.lang.Class<?>[] getAllClasses()
        Retrieves all classes loaded by the GrailsApplication.
        Specified by:
        getAllClasses in interface GrailsApplication
        Returns:
        All classes loaded by the GrailsApplication
      • getClassForName

        public java.lang.Class<?> getClassForName​(java.lang.String className)
        Retrieves a class from the GrailsApplication for the given name.
        Specified by:
        getClassForName in interface GrailsApplication
        Overrides:
        getClassForName in class AbstractGrailsApplication
        Parameters:
        className - The class name
        Returns:
        Either the Class instance or null if it doesn't exist
      • 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. 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 org.springframework.core.io.Resource getResourceForClass​(java.lang.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​(java.lang.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​(java.lang.String artefactType,
                                        java.lang.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​(java.lang.String artefactType,
                                        java.lang.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​(java.lang.String artefactType,
                                       java.lang.String name)
        Retrieves an artefact for the given type and name.
        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​(java.lang.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
      • getFirstArtefact

        protected GrailsClass getFirstArtefact​(java.lang.String artefactType)
      • getArtefacts

        public GrailsClass[] getArtefacts​(java.lang.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
      • addArtefact

        public GrailsClass addArtefact​(java.lang.String artefactType,
                                       java.lang.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​(java.lang.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 a 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​(java.lang.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
      • initializeArtefacts

        protected void initializeArtefacts​(java.lang.String artefactType)
        Re-initialize the artefacts of the specified type. This gives handlers a chance to update caches etc.
        Parameters:
        artefactType - The type of artefact to init
      • clear

        public void clear()
        Clears the application returning it to an empty state. Very dangerous method, use with caution.
      • initializeArtefacts

        protected void initializeArtefacts​(ArtefactHandler handler)
        Re-initialize the artefacts of the specified type. This gives handlers a chance to update caches etc.
        Parameters:
        handler - The handler to register
      • getArtefactInfo

        protected DefaultArtefactInfo getArtefactInfo​(java.lang.String artefactType,
                                                      boolean create)
        Get or create the cache of classes for the specified artefact type.
        Parameters:
        artefactType - The name of an artefact type
        create - Set to true if you want non-existent caches to be created
        Returns:
        The cache of classes for the type, or null if no cache exists and create is false
      • getArtefactInfo

        public ArtefactInfo getArtefactInfo​(java.lang.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 java.lang.Object invokeMethod​(java.lang.String methodName,
                                             java.lang.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
        Parameters:
        methodName - The name of the method
        args - The arguments to the method
        Returns:
        The return value of the method TODO Need to add matches for addClass(java.lang.Class) and addClass(GrailsClass)
      • getProperty

        public java.lang.Object getProperty​(java.lang.String propertyName)
        Override property access and hit on xxxxClasses to return class arrays of artefacts.
        Specified by:
        getProperty in interface groovy.lang.GroovyObject
        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
      • initialiseGroovyExtensionModules

        protected static void initialiseGroovyExtensionModules()
      • getArtefactForFeature

        public GrailsClass getArtefactForFeature​(java.lang.String artefactType,
                                                 java.lang.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
      • 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
      • getArtefactByLogicalPropertyName

        public GrailsClass getArtefactByLogicalPropertyName​(java.lang.String type,
                                                            java.lang.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​(java.lang.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
      • setBeanClassLoader

        public void setBeanClassLoader​(java.lang.ClassLoader classLoader)
        Specified by:
        setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware
        Overrides:
        setBeanClassLoader in class AbstractGrailsApplication
      • addOverridableArtefact

        public void addOverridableArtefact​(java.lang.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
      • addOverridableArtefact

        public GrailsClass addOverridableArtefact​(java.lang.String artefactType,
                                                  java.lang.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
      • addArtefact

        protected GrailsClass addArtefact​(java.lang.String artefactType,
                                          java.lang.Class<?> artefactClass,
                                          boolean overrideable)
      • getMappingContext

        public org.grails.datastore.mapping.model.MappingContext getMappingContext()
        Specified by:
        getMappingContext in interface GrailsApplication
        Returns:
        The GORM MappingContext. Returns null if none present
      • setMappingContext

        public void setMappingContext​(org.grails.datastore.mapping.model.MappingContext mappingContext)
        Description copied from interface: GrailsApplication
        Configures the MappingContext for this application
        Specified by:
        setMappingContext in interface GrailsApplication
        Parameters:
        mappingContext - The mapping context