Search in sources :

Example 1 with ArtefactAdditionEvent

use of grails.core.events.ArtefactAdditionEvent in project grails-core by grails.

the class UrlMappingsHolderFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    Assert.state(applicationContext != null, "Property [applicationContext] must be set!");
    Assert.state(grailsApplication != null, "Property [grailsApplication] must be set!");
    List urlMappings = new ArrayList();
    List excludePatterns = new ArrayList();
    GrailsClass[] mappings = grailsApplication.getArtefacts(UrlMappingsArtefactHandler.TYPE);
    final DefaultUrlMappingEvaluator mappingEvaluator = new DefaultUrlMappingEvaluator(applicationContext);
    mappingEvaluator.setPluginManager(pluginManager);
    if (mappings.length == 0) {
        urlMappings.addAll(mappingEvaluator.evaluateMappings(DefaultUrlMappings.getMappings()));
    } else {
        for (GrailsClass mapping : mappings) {
            GrailsUrlMappingsClass mappingClass = (GrailsUrlMappingsClass) mapping;
            List grailsClassMappings;
            if (Script.class.isAssignableFrom(mappingClass.getClazz())) {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getClazz());
            } else {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getMappingsClosure());
            }
            urlMappings.addAll(grailsClassMappings);
            if (mappingClass.getExcludePatterns() != null) {
                excludePatterns.addAll(mappingClass.getExcludePatterns());
            }
        }
    }
    DefaultUrlMappingsHolder defaultUrlMappingsHolder = new DefaultUrlMappingsHolder(urlMappings, excludePatterns, true);
    Config config = grailsApplication.getConfig();
    Integer cacheSize = config.getProperty(URL_MAPPING_CACHE_MAX_SIZE, Integer.class, null);
    if (cacheSize != null) {
        defaultUrlMappingsHolder.setMaxWeightedCacheCapacity(cacheSize);
    }
    Integer urlCreatorCacheSize = config.getProperty(URL_CREATOR_CACHE_MAX_SIZE, Integer.class, null);
    if (urlCreatorCacheSize != null) {
        defaultUrlMappingsHolder.setUrlCreatorMaxWeightedCacheCapacity(urlCreatorCacheSize);
    }
    // call initialize() after settings are in place
    defaultUrlMappingsHolder.initialize();
    UrlConverter urlConverter = applicationContext.containsBean(UrlConverter.BEAN_NAME) ? applicationContext.getBean(UrlConverter.BEAN_NAME, UrlConverter.class) : null;
    final GrailsControllerUrlMappings grailsControllerUrlMappings = new GrailsControllerUrlMappings(grailsApplication, defaultUrlMappingsHolder, urlConverter);
    ((ConfigurableApplicationContext) applicationContext).addApplicationListener(new ApplicationListener<ArtefactAdditionEvent>() {

        @Override
        public void onApplicationEvent(ArtefactAdditionEvent event) {
            GrailsClass artefact = event.getArtefact();
            if (artefact instanceof GrailsControllerClass) {
                grailsControllerUrlMappings.registerController((GrailsControllerClass) artefact);
            }
        }
    });
    urlMappingsHolder = grailsControllerUrlMappings;
}
Also used : GrailsControllerUrlMappings(org.grails.web.mapping.mvc.GrailsControllerUrlMappings) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GrailsControllerClass(grails.core.GrailsControllerClass) Config(grails.config.Config) ArrayList(java.util.ArrayList) GrailsUrlMappingsClass(grails.core.GrailsUrlMappingsClass) ArtefactAdditionEvent(grails.core.events.ArtefactAdditionEvent) GrailsClass(grails.core.GrailsClass) UrlConverter(grails.web.UrlConverter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ArtefactAdditionEvent

use of grails.core.events.ArtefactAdditionEvent in project grails-core by grails.

the class DefaultGrailsApplication method addArtefact.

protected GrailsClass addArtefact(String artefactType, Class<?> artefactClass, boolean overrideable) {
    ArtefactHandler handler = artefactHandlersByName.get(artefactType);
    if (handler != null && handler.isArtefact(artefactClass)) {
        GrailsClass artefactGrailsClass;
        if (handler instanceof DomainClassArtefactHandler) {
            artefactGrailsClass = ((DomainClassArtefactHandler) handler).newArtefactClass(artefactClass, proxyMappingContext);
        } else {
            artefactGrailsClass = handler.newArtefactClass(artefactClass);
        }
        artefactGrailsClass.setGrailsApplication(this);
        // Store the GrailsClass in cache
        DefaultArtefactInfo info = getArtefactInfo(artefactType, true);
        if (overrideable) {
            info.addOverridableGrailsClass(artefactGrailsClass);
        } else {
            info.addGrailsClass(artefactGrailsClass);
        }
        info.updateComplete();
        addToLoaded(artefactClass);
        if (isInitialised()) {
            initializeArtefacts(artefactType);
            ApplicationContext context = getMainContext();
            if (context instanceof ConfigurableApplicationContext && contextInitialized && ((ConfigurableApplicationContext) context).isActive()) {
                context.publishEvent(new ArtefactAdditionEvent(artefactGrailsClass));
            }
        }
        return artefactGrailsClass;
    }
    throw new GrailsConfigurationException("Cannot add " + artefactType + " class [" + artefactClass + "]. It is not a " + artefactType + "!");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GrailsConfigurationException(org.grails.core.exceptions.GrailsConfigurationException) DomainClassArtefactHandler(org.grails.core.artefact.DomainClassArtefactHandler) DomainClassArtefactHandler(org.grails.core.artefact.DomainClassArtefactHandler) ArtefactAdditionEvent(grails.core.events.ArtefactAdditionEvent)

Aggregations

ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 Config (grails.config.Config)1 GrailsClass (grails.core.GrailsClass)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 UrlConverter (grails.web.UrlConverter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DomainClassArtefactHandler (org.grails.core.artefact.DomainClassArtefactHandler)1 GrailsConfigurationException (org.grails.core.exceptions.GrailsConfigurationException)1 GrailsControllerUrlMappings (org.grails.web.mapping.mvc.GrailsControllerUrlMappings)1 ApplicationContext (org.springframework.context.ApplicationContext)1