Package: grails.core

## [Java] Class DefaultGrailsApplication

- grails.core.DefaultGrailsApplication

- All Implemented Interfaces and Traits:
    org.springframework.beans.factory.BeanClassLoaderAware

      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.
  Authors:
    Marc Palmer
    Steven Devijver
    Graeme Rocher
  See Also:
    GrailsPluginManager
    DefaultGrailsPluginManager
    ArtefactHandler
    ArtefactInfo
  Since:
    0.1

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `protected static java.util.regex.Pattern` | `GETCLASSESMETH_PATTERN` |
    `protected static java.util.regex.Pattern` | `GETCLASSESPROP_PATTERN` |
    `protected static java.util.regex.Pattern` | `GETCLASS_PATTERN` |
    `protected static java.util.regex.Pattern` | `ISCLASS_PATTERN` |
    `protected java.util.List<java.lang.Class<?>>` | `allArtefactClasses` |
    `protected java.lang.Class<?>[]` | `allArtefactClassesArray` |
    `protected java.lang.Class<?>[]` | `allClasses` |
    `protected GrailsApplicationClass` | `applicationClass` |
    `protected ArtefactHandler[]` | `artefactHandlers` |
    `protected java.util.Map<java.lang.String, ArtefactHandler>` | `artefactHandlersByName` |
    `protected java.util.Map<java.lang.String, ArtefactInfo>` | `artefactInfo` |
    `private static boolean` | `extensionMethodsInitialized` |
    `protected boolean` | `initialised` |
    `protected java.util.Set<java.lang.Class<?>>` | `loadedClasses` |
    `protected static org.apache.commons.logging.Log` | `log` |
    `protected org.grails.datastore.mapping.model.MappingContext` | `mappingContext` |
    `protected org.grails.datastore.mapping.model.MappingContext` | `proxyMappingContext` |
    `protected org.springframework.core.io.Resource[]` | `resources` |

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class AbstractGrailsApplication` | `applicationMeta, classLoader, config, contextInitialized, parentContext`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `DefaultGrailsApplication ()` Creates a new empty Grails application.
    `DefaultGrailsApplication (GrailsApplicationClass applicationClass)` Creates a new empty Grails application.
    `DefaultGrailsApplication (java.lang.ClassLoader classLoader)`
    `DefaultGrailsApplication (java.lang.Class<?> classes)` Construct an application for the given classes
    `DefaultGrailsApplication (java.lang.Class<?>[] classes, java.lang.ClassLoader classLoader)` Creates a new GrailsApplication instance using the given classes and GroovyClassLoader.
    `DefaultGrailsApplication (org.springframework.core.io.Resource[] resources)` Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names
    `DefaultGrailsApplication (Resource[] resources)` Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public GrailsClass` | `addArtefact(java.lang.String artefactType, java.lang.Class artefactClass)` Adds an artefact of the given type for the given Class.
    `` | `public GrailsClass` | `addArtefact(java.lang.String artefactType, GrailsClass artefactGrailsClass)` Adds an artefact of the given type for the given GrailsClass.
    `` | `public void` | `addArtefact(java.lang.Class artefact)`
    `` | `protected GrailsClass` | `addArtefact(java.lang.String artefactType, java.lang.Class<?> artefactClass, boolean overrideable)`
    `` | `public void` | `addOverridableArtefact(java.lang.Class artefact)`
    `` | `public GrailsClass` | `addOverridableArtefact(java.lang.String artefactType, java.lang.Class artefactClass)` Adds an artefact of the given type for the given Class.
    `` | `protected void` | `addToLoaded(java.lang.Class<?> clazz)`
    `` | `private static org.grails.datastore.mapping.model.MappingContext` | `buildMappingContextProxy(DefaultGrailsApplication application)`
    `` | `public void` | `clear()` Clears the application returning it to an empty state.
    `` | `protected void` | `configureLoadedClasses(java.lang.Class<?>[] classes)` Configures the loaded classes within the GrailsApplication instance using the registered ArtefactHandler instances.
    `` | `public java.lang.Class<?>[]` | `getAllArtefacts()` Returns all the classes identified as artefacts by ArtefactHandler instances.
    `` | `public java.lang.Class<?>[]` | `getAllClasses()` Retrieves all classes loaded by the GrailsApplication.
    `` | `public GrailsApplicationClass` | `getApplicationClass()` Returns: The application class
    `` | `public GrailsClass` | `getArtefact(java.lang.String artefactType, java.lang.String name)` Retrieves an artefact for the given type and name.
    `` | `public GrailsClass` | `getArtefactByLogicalPropertyName(java.lang.String type, java.lang.String logicalName)`
    `` | `protected int` | `getArtefactCount(java.lang.String artefactType)` Retrieves the number of artefacts registered for the given artefactType as defined by the ArtefactHandler.
    `` | `public GrailsClass` | `getArtefactForFeature(java.lang.String artefactType, java.lang.Object featureID)`
    `` | `public ArtefactHandler` | `getArtefactHandler(java.lang.String type)`
    `` | `public ArtefactHandler[]` | `getArtefactHandlers()`
    `` | `protected DefaultArtefactInfo` | `getArtefactInfo(java.lang.String artefactType, boolean create)` Get or create the cache of classes for the specified artefact type.
    `` | `public ArtefactInfo` | `getArtefactInfo(java.lang.String artefactType)` Get the cache of classes for the specified artefact type.
    `` | `public ArtefactHandler` | `getArtefactType(java.lang.Class theClass)`
    `` | `public GrailsClass[]` | `getArtefacts(java.lang.String artefactType)` Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler
    `` | `public java.lang.Class<?>` | `getClassForName(java.lang.String className)` Retrieves a class from the GrailsApplication for the given name.
    `` | `public Config` | `getConfig()`
    `` | `protected GrailsClass` | `getFirstArtefact(java.lang.String artefactType)`
    `` | `public org.grails.datastore.mapping.model.MappingContext` | `getMappingContext()`
    `` | `public java.lang.Object` | `getProperty(java.lang.String propertyName)` Override property access and hit on xxxxClasses to return class arrays of artefacts.
    `` | `public org.springframework.core.io.Resource` | `getResourceForClass(java.lang.Class theClazz)` Retrieves the Spring Resource that was used to load the given Class.
    `` | `public boolean` | `hasArtefactHandler(java.lang.String type)`
    `` | `protected void` | `initArtefactHandlers()` Initialises the default set of ArtefactHandler instances.
    `` | `public void` | `initialise()`
    `` | `protected static void` | `initialiseGroovyExtensionModules()`
    `` | `protected void` | `initializeArtefacts(java.lang.String artefactType)` Re-initialize the artefacts of the specified type.
    `` | `protected void` | `initializeArtefacts(ArtefactHandler handler)` Re-initialize the artefacts of the specified type.
    `` | `public java.lang.Object` | `invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)`
    `` | `public java.lang.Object` | `invokeMethod(java.lang.String methodName, java.lang.Object args)`
    `` | `public boolean` | `isArtefact(java.lang.Class theClazz)` Returns true if the given class is an artefact identified by one of the registered ArtefactHandler instances.
    `` | `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.
    `` | `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.
    `` | `public boolean` | `isInitialised()`
    `` | `protected java.lang.Class<?>[]` | `populateAllClasses()`
    `` | `public void` | `rebuild()`
    `` | `public void` | `refresh()` Refreshes this GrailsApplication, rebuilding all of the artefact definitions as defined by the registered ArtefactHandler instances.
    `` | `protected void` | `refreshArtefactGrailsClassCaches()` Tell all our artefact info objects to update their internal state after we've added a bunch of classes.
    `` | `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.
    `` | `public void` | `setBeanClassLoader(java.lang.ClassLoader classLoader)`
    `` | `public void` | `setMappingContext(org.grails.datastore.mapping.model.MappingContext mappingContext)`
    `` | `private void` | `updateArtefactHandlers()`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class AbstractGrailsApplication` | `configChanged, getClassForName, getClassLoader, getConfig, getMainContext, getMetadata, getOrder, getParentContext, isWarDeployed, onApplicationEvent, setApplicationContext, setBeanClassLoader, setConfig, setConfig, setMainContext, supportsEventType, supportsSourceType`

- - ### Field Detail

    - #### protected static final java.util.regex.Pattern GETCLASSESMETH_PATTERN

    - #### protected static final java.util.regex.Pattern GETCLASSESPROP_PATTERN

    - #### protected static final java.util.regex.Pattern GETCLASS_PATTERN

    - #### protected static final java.util.regex.Pattern ISCLASS_PATTERN

    - #### protected java.util.List<java.lang.Class<?>> allArtefactClasses

    - #### protected java.lang.Class<?>[] allArtefactClassesArray

    - #### protected java.lang.Class<?>[] allClasses

    - #### protected GrailsApplicationClass applicationClass

    - #### protected ArtefactHandler[] artefactHandlers

    - #### protected java.util.Map<java.lang.String, ArtefactHandler> artefactHandlersByName

    - #### protected java.util.Map<java.lang.String, ArtefactInfo> artefactInfo

    - #### private static boolean extensionMethodsInitialized

    - #### protected boolean initialised

    - #### protected java.util.Set<java.lang.Class<?>> loadedClasses

    - #### protected static org.apache.commons.logging.Log log

    - #### protected org.grails.datastore.mapping.model.MappingContext mappingContext

    - #### protected org.grails.datastore.mapping.model.MappingContext proxyMappingContext

    - #### protected org.springframework.core.io.Resource[] resources

  - ### Constructor Detail

    - #### public DefaultGrailsApplication()

      Creates a new empty Grails application.

    - #### public DefaultGrailsApplication(GrailsApplicationClass applicationClass)

      Creates a new empty Grails application.

    - #### public DefaultGrailsApplication(java.lang.ClassLoader classLoader)

    - #### public DefaultGrailsApplication(java.lang.Class<?> classes)

      Construct an application for the given classes

      Parameters:
        `classes` - The classes

    - #### 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

    - #### public DefaultGrailsApplication(org.springframework.core.io.Resource[] resources)

      Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names

    - #### public DefaultGrailsApplication(Resource[] resources)

      Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names

  - ### Method Detail

    - #### public GrailsClass addArtefact(java.lang.String artefactType, @java.lang.SuppressWarnings("rawtypes") java.lang.Class artefactClass)

      Adds an artefact of the given type for the given Class.

      throws:
        GrailsConfigurationException If the specified Class is not the same as the type defined by the ArtefactHandler
      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
      See Also:
        ArtefactHandler

    - #### public GrailsClass addArtefact(java.lang.String artefactType, GrailsClass artefactGrailsClass)

      Adds an artefact of the given type for the given GrailsClass.

      throws:
        GrailsConfigurationException If the specified GrailsClass is not the same as the type defined by the ArtefactHandler
      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
      See Also:
        ArtefactHandler

    - #### public void addArtefact(@java.lang.SuppressWarnings("rawtypes") java.lang.Class artefact)

    - #### protected GrailsClass addArtefact(java.lang.String artefactType, java.lang.Class<?> artefactClass, boolean overrideable)

    - #### public void addOverridableArtefact(@java.lang.SuppressWarnings("rawtypes") java.lang.Class artefact)

    - #### public GrailsClass addOverridableArtefact(java.lang.String artefactType, @java.lang.SuppressWarnings("rawtypes") java.lang.Class artefactClass)

      Adds an artefact of the given type for the given Class.

      throws:
        GrailsConfigurationException If the specified Class is not the same as the type defined by the ArtefactHandler
      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
      See Also:
        ArtefactHandler

    - #### protected void addToLoaded(java.lang.Class<?> clazz)

    - #### private static org.grails.datastore.mapping.model.MappingContext buildMappingContextProxy(DefaultGrailsApplication application)

    - #### public void clear()

      Clears the application returning it to an empty state. Very dangerous method, use with caution.

    - #### 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

    - #### public java.lang.Class<?>[] getAllArtefacts()

      Returns all the classes identified as artefacts by ArtefactHandler instances.

      Returns:
        An array of classes

    - #### public java.lang.Class<?>[] getAllClasses()

      Retrieves all classes loaded by the GrailsApplication.

      Returns:
        All classes loaded by the GrailsApplication

    - #### public GrailsApplicationClass getApplicationClass()

      Returns:
        The application class

    - #### public GrailsClass getArtefact(java.lang.String artefactType, java.lang.String name)

      Retrieves an artefact for the given type and name.

      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

    - #### public GrailsClass getArtefactByLogicalPropertyName(java.lang.String type, java.lang.String logicalName)

    - #### 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

    - #### public GrailsClass getArtefactForFeature(java.lang.String artefactType, java.lang.Object featureID)

    - #### public ArtefactHandler getArtefactHandler(java.lang.String type)

    - #### public ArtefactHandler[] getArtefactHandlers()

    - #### 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

    - #### public ArtefactInfo getArtefactInfo(java.lang.String artefactType)

      Get the cache of classes for the specified artefact type.

      Parameters:
        `artefactType` - The name of an artefact type
      Returns:
        The cache of classes for the type, or null if no cache exists

    - #### public ArtefactHandler getArtefactType(@java.lang.SuppressWarnings("rawtypes") java.lang.Class theClass)

    - #### public GrailsClass[] getArtefacts(java.lang.String artefactType)

      Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler

      Parameters:
        `artefactType` - The type of the artefact defined by the ArtefactHandler
      Returns:
        An array of classes for the given artefact

    - #### public java.lang.Class<?> getClassForName(java.lang.String className)

      Retrieves a class from the GrailsApplication for the given name.

      Parameters:
        `className` - The class name
      Returns:
        Either the Class instance or null if it doesn't exist

    - #### public Config getConfig()

    - #### protected GrailsClass getFirstArtefact(java.lang.String artefactType)

    - #### @java.lang.Override public org.grails.datastore.mapping.model.MappingContext getMappingContext()

    - #### @java.lang.Override public java.lang.Object getProperty(java.lang.String propertyName)

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

      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

    - #### public org.springframework.core.io.Resource getResourceForClass(@java.lang.SuppressWarnings("rawtypes") java.lang.Class theClazz)

      Retrieves the Spring Resource that was used to load the given Class.

      Parameters:
        `theClazz` - The class
      Returns:
        Either a Spring Resource or null if no Resource was found for the given class

    - #### public boolean hasArtefactHandler(java.lang.String type)

    - #### @java.lang.SuppressWarnings( "deprecation" ) protected void initArtefactHandlers()

      Initialises the default set of ArtefactHandler instances.

      See Also:
        ArtefactHandler

    - #### public void initialise()

    - #### protected static void initialiseGroovyExtensionModules()

    - #### 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

    - #### 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

    - #### @java.lang.Override public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)

    - #### @java.lang.Override 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)

      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)

    - #### public boolean isArtefact(@java.lang.SuppressWarnings("rawtypes") 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

      Parameters:
        `theClazz` - The class to check
      Returns:
        true if it is an artefact

    - #### public boolean isArtefactOfType(java.lang.String artefactType, @java.lang.SuppressWarnings("rawtypes") java.lang.Class theClazz)

      Returns true if the specified class is of the given artefact type as defined by the ArtefactHandler.

      Parameters:
        `artefactType` - The type of the artefact
        `theClazz` - The class
      Returns:
        true if it is of the specified artefactType
      See Also:
        ArtefactHandler

    - #### 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.

      Parameters:
        `artefactType` - The type of the artefact
        `className` - The class name
      Returns:
        true if it is of the specified artefactType
      See Also:
        ArtefactHandler

    - #### public boolean isInitialised()

    - #### protected java.lang.Class<?>[] populateAllClasses()

    - #### public void rebuild()

    - #### public void refresh()

      Refreshes this GrailsApplication, rebuilding all of the artefact definitions as defined by the registered ArtefactHandler instances.

    - #### protected void refreshArtefactGrailsClassCaches()

      Tell all our artefact info objects to update their internal state after we've added a bunch of classes.

    - #### 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.

      Parameters:
        `handler` - The ArtefactHandler to regster

    - #### public void setBeanClassLoader(java.lang.ClassLoader classLoader)

    - #### @java.lang.Override public void setMappingContext(org.grails.datastore.mapping.model.MappingContext mappingContext)

    - #### private void updateArtefactHandlers()