Search in sources :

Example 1 with ConfigSlurper

use of groovy.util.ConfigSlurper in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 2 with ConfigSlurper

use of groovy.util.ConfigSlurper in project grails-core by grails.

the class ParseTests method testParseWithUTF8.

public void testParseWithUTF8() throws Exception {
    // This is some unicode Chinese (who knows what it says!)
    String src = "Chinese text: 㐡㐷㐿㑃㐐㐅㢳㢚㥞㥇㫛㭚㭧";
    // Sanity check the string loaded OK as unicode - it won't look right if you output it, default stdout is not UTF-8
    // on many OSes
    assertEquals(src.indexOf('?'), -1);
    ConfigObject config = new ConfigSlurper().parse("grails.views.gsp.encoding = \"UTF-8\"");
    buildMockRequest(config);
    ParsedResult output = null;
    try {
        output = parseCode("myTest4", src);
    } finally {
        RequestContextHolder.resetRequestAttributes();
    }
    String expected = makeImports() + "\n" + "class myTest4 extends org.grails.gsp.GroovyPage {\n" + "public String getGroovyPageFileName() { \"myTest4\" }\n" + "public Object run() {\n" + "Writer out = getOut()\n" + "Writer expressionOut = getExpressionOut()\n" + "registerSitemeshPreprocessMode()\n" + "printHtmlPart(0)\n" + "}\n" + GSP_FOOTER;
    assertEquals(trimAndRemoveCR(expected), trimAndRemoveCR(output.generatedGsp));
    assertEquals(src, output.htmlParts[0]);
}
Also used : ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 3 with ConfigSlurper

use of groovy.util.ConfigSlurper in project cas by apereo.

the class GroovyConfigurationPropertiesLoader method load.

@Override
public PropertySource load() {
    val properties = new LinkedHashMap<>();
    val slurper = new ConfigSlurper();
    applicationProfiles.forEach(Unchecked.consumer(profile -> {
        slurper.setEnvironment(profile);
        slurper.registerConditionalBlock("profiles", profile);
        val bindings = CollectionUtils.wrap("profile", profile, "logger", LOGGER);
        slurper.setBinding(bindings);
        val groovyConfig = slurper.parse(getResource().getURL());
        val pp = groovyConfig.toProperties();
        LOGGER.debug("Found settings [{}] in Groovy file [{}]", pp.keySet(), getResource());
        properties.putAll(pp);
    }));
    return finalizeProperties(decryptProperties(properties));
}
Also used : lombok.val(lombok.val) ConfigSlurper(groovy.util.ConfigSlurper) LinkedHashMap(java.util.LinkedHashMap) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Unchecked(org.jooq.lambda.Unchecked) PropertySource(org.springframework.core.env.PropertySource) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) CollectionUtils(org.apereo.cas.util.CollectionUtils) lombok.val(lombok.val) Resource(org.springframework.core.io.Resource) LinkedHashMap(java.util.LinkedHashMap) ConfigSlurper(groovy.util.ConfigSlurper)

Example 4 with ConfigSlurper

use of groovy.util.ConfigSlurper in project grails-core by grails.

the class AbstractGrailsMockTests method setUp.

@Override
protected final void setUp() throws Exception {
    ExpandoMetaClass.enableGlobally();
    super.setUp();
    System.out.println("Setting up test");
    ctx = new MockApplicationContext();
    ctx.registerMockBean(GrailsApplication.CLASS_LOADER_BEAN, gcl);
    onSetUp();
    ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    if (ClassUtils.isPresent("Config", gcl)) {
        ConfigObject config = new ConfigSlurper().parse(gcl.loadClass("Config"));
        ga.setConfig(new PropertySourcesConfig(config));
    }
    ga.setApplicationContext(ctx);
    ga.initialise();
    ctx.registerMockBean(GrailsApplication.APPLICATION_ID, ga);
    postSetUp();
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) ConfigObject(groovy.util.ConfigObject) MockApplicationContext(org.grails.support.MockApplicationContext) ConfigSlurper(groovy.util.ConfigSlurper)

Example 5 with ConfigSlurper

use of groovy.util.ConfigSlurper in project grails-core by grails.

the class AbstractGrailsPluginManager method informOfClassChange.

public void informOfClassChange(File file, @SuppressWarnings("rawtypes") Class cls) {
    if (file.getName().equals(CONFIG_FILE)) {
        ConfigSlurper configSlurper = getConfigSlurper(application);
        ConfigObject c;
        try {
            c = configSlurper.parse(file.toURI().toURL());
            application.getConfig().merge(c);
            final Map flat = c.flatten();
            application.getConfig().merge(flat);
            application.configChanged();
            informPluginsOfConfigChange();
        } catch (Exception e) {
            // ignore
            LOG.debug("Error in changing Config", e);
        }
    } else {
        if (cls != null) {
            MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
            registry.removeMetaClass(cls);
            ExpandoMetaClass newMc = new ExpandoMetaClass(cls, true, true);
            newMc.initialize();
            registry.setMetaClass(cls, newMc);
            Enhanced en = AnnotationUtils.findAnnotation(cls, Enhanced.class);
            if (en != null) {
                Class<?>[] mixinClasses = en.mixins();
                if (mixinClasses != null) {
                    DefaultGroovyMethods.mixin(newMc, mixinClasses);
                }
            }
        }
        for (GrailsPlugin grailsPlugin : pluginList) {
            if (grailsPlugin.hasInterestInChange(file.getAbsolutePath())) {
                try {
                    if (cls == null) {
                        grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, new FileSystemResource(file));
                    } else {
                        grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, cls);
                    }
                    Environment.setCurrentReloadError(null);
                } catch (Exception e) {
                    LOG.error("Plugin " + grailsPlugin + " could not reload changes to file [" + file + "]: " + e.getMessage(), e);
                    Environment.setCurrentReloadError(e);
                }
            }
        }
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) Enhanced(grails.artefact.Enhanced) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) FileSystemResource(org.springframework.core.io.FileSystemResource) ConfigObject(groovy.util.ConfigObject) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(org.grails.config.NavigableMap) BeansException(org.springframework.beans.BeansException) PluginException(grails.plugins.exceptions.PluginException) ConfigSlurper(groovy.util.ConfigSlurper)

Aggregations

ConfigSlurper (groovy.util.ConfigSlurper)6 ConfigObject (groovy.util.ConfigObject)5 HashMap (java.util.HashMap)2 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)2 MockApplicationContext (org.grails.support.MockApplicationContext)2 Decorator (com.opensymphony.module.sitemesh.Decorator)1 Page (com.opensymphony.module.sitemesh.Page)1 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)1 Enhanced (grails.artefact.Enhanced)1 Config (grails.config.Config)1 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)1 GrailsPlugin (grails.plugins.GrailsPlugin)1 PluginException (grails.plugins.exceptions.PluginException)1 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyObject (groovy.lang.GroovyObject)1 MetaClassRegistry (groovy.lang.MetaClassRegistry)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1