Search in sources :

Example 6 with GrailsControllerClass

use of grails.core.GrailsControllerClass in project grails-core by grails.

the class DefaultGrailsApplicationAttributes method getControllerName.

private String getControllerName(ServletRequest request) {
    String controllerName = (String) request.getAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE);
    if (controllerName == null || controllerName.length() == 0) {
        GroovyObject controller = getController(request);
        if (controller != null) {
            controllerName = (String) controller.getProperty("controllerName");
            request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, controllerName);
            if (controller instanceof GrailsControllerClass) {
                String namespace = ((GrailsControllerClass) controller).getNamespace();
                if (namespace != null) {
                    request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAMESPACE_ATTRIBUTE, namespace);
                }
            }
        }
    }
    return controllerName != null ? controllerName : "";
}
Also used : GrailsControllerClass(grails.core.GrailsControllerClass) GroovyObject(groovy.lang.GroovyObject)

Example 7 with GrailsControllerClass

use of grails.core.GrailsControllerClass 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 (int i = 0; i < mappings.length; i++) {
            GrailsClass mapping = mappings[i];
            GrailsUrlMappingsClass mappingClass = (GrailsUrlMappingsClass) mapping;
            List<UrlMapping> grailsClassMappings;
            if (Script.class.isAssignableFrom(mappingClass.getClazz())) {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getClazz());
            } else {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getMappingsClosure());
            }
            if (!StringUtils.isEmpty(mapping.getPluginName())) {
                for (int j = 0; j < grailsClassMappings.size(); j++) {
                    grailsClassMappings.get(j).setPluginIndex(i);
                }
            }
            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 : UrlMapping(grails.web.mapping.UrlMapping) 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)

Aggregations

GrailsControllerClass (grails.core.GrailsControllerClass)7 GroovyPageScriptSource (org.grails.gsp.io.GroovyPageScriptSource)3 Config (grails.config.Config)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsClass (grails.core.GrailsClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)1 UrlConverter (grails.web.UrlConverter)1 UrlMapping (grails.web.mapping.UrlMapping)1 GroovyObject (groovy.lang.GroovyObject)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GrailsControllerUrlMappings (org.grails.web.mapping.mvc.GrailsControllerUrlMappings)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1