Search in sources :

Example 11 with GrailsApplication

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

the class GrailsWebUtil method lookupApplication.

/**
     * Looks up a GrailsApplication instance from the ServletContext.
     *
     * @param servletContext The ServletContext
     * @return A GrailsApplication or null if there isn't one
     */
public static GrailsApplication lookupApplication(ServletContext servletContext) {
    if (servletContext == null) {
        return null;
    }
    GrailsApplication grailsApplication = (GrailsApplication) servletContext.getAttribute(ApplicationAttributes.APPLICATION);
    if (grailsApplication != null) {
        return grailsApplication;
    }
    final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (context == null || !context.containsBean(GrailsApplication.APPLICATION_ID)) {
        return null;
    }
    return context.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
}
Also used : GrailsApplication(grails.core.GrailsApplication) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 12 with GrailsApplication

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

the class GrailsApplicationAttributesTests method getAttributesForClasses.

private GrailsApplicationAttributes getAttributesForClasses(Class<?>[] classes, GroovyClassLoader gcl) {
    MockApplicationContext context = new MockApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, context);
    GrailsApplication app = new DefaultGrailsApplication(classes, gcl);
    app.initialise();
    context.registerMockBean(GrailsApplication.APPLICATION_ID, app);
    GrailsClass[] controllers = app.getArtefacts(ControllerArtefactHandler.TYPE);
    for (int i = 0; i < controllers.length; i++) {
        context.registerMockBean(controllers[i].getFullName(), controllers[i].newInstance());
    }
    GrailsClass[] taglibs = app.getArtefacts(TagLibArtefactHandler.TYPE);
    for (int i = 0; i < taglibs.length; i++) {
        context.registerMockBean(taglibs[i].getFullName(), taglibs[i].newInstance());
    }
    return new DefaultGrailsApplicationAttributes(servletContext);
}
Also used : GrailsClass(grails.core.GrailsClass) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MockServletContext(org.springframework.mock.web.MockServletContext) MockApplicationContext(org.grails.support.MockApplicationContext)

Example 13 with GrailsApplication

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

the class GrailsLayoutDecoratorMapper method init.

@Override
public void init(Config c, Properties properties, DecoratorMapper parentMapper) throws InstantiationException {
    super.init(c, properties, parentMapper);
    ServletContext servletContext = c.getServletContext();
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (applicationContext != null) {
        GrailsApplication grailsApplication = applicationContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
        groovyPageLayoutFinder = grailsApplication.getMainContext().getBean("groovyPageLayoutFinder", GroovyPageLayoutFinder.class);
    }
}
Also used : GrailsApplication(grails.core.GrailsApplication) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 14 with GrailsApplication

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

the class GroovyPage method initRun.

public void initRun(Writer target, OutputContext outputContext, GroovyPageMetaInfo metaInfo) {
    OutputEncodingStackAttributes.Builder attributesBuilder = new OutputEncodingStackAttributes.Builder();
    if (metaInfo != null) {
        setJspTags(metaInfo.getJspTags());
        setJspTagLibraryResolver(metaInfo.getJspTagLibraryResolver());
        setGspTagLibraryLookup(metaInfo.getTagLibraryLookup());
        setHtmlParts(metaInfo.getHtmlParts());
        setPluginContextPath(metaInfo.getPluginPath());
        attributesBuilder.outEncoder(metaInfo.getOutEncoder());
        attributesBuilder.staticEncoder(metaInfo.getStaticEncoder());
        attributesBuilder.expressionEncoder(metaInfo.getExpressionEncoder());
        attributesBuilder.defaultTaglibEncoder(metaInfo.getTaglibEncoder());
        applyModelFieldsFromBinding(metaInfo.getModelFields());
    }
    attributesBuilder.allowCreate(true).topWriter(target).autoSync(false).pushTop(true);
    attributesBuilder.outputContext(outputContext);
    attributesBuilder.inheritPreviousEncoders(false);
    outputStack = OutputEncodingStack.currentStack(attributesBuilder.build());
    out = outputStack.getOutWriter();
    staticOut = outputStack.getStaticWriter();
    expressionOut = outputStack.getExpressionWriter();
    this.outputContext = outputContext;
    if (outputContext != null) {
        outputContext.setCurrentWriter(out);
        GrailsApplication grailsApplication = outputContext.getGrailsApplication();
        if (grailsApplication != null) {
            rawEncoder = WithCodecHelper.lookupEncoder(grailsApplication, "Raw");
        }
    }
    setVariableDirectly(OUT, out);
    setVariableDirectly(EXPRESSION_OUT, expressionOut);
}
Also used : GrailsApplication(grails.core.GrailsApplication) OutputEncodingStackAttributes(org.grails.taglib.encoder.OutputEncodingStackAttributes)

Example 15 with GrailsApplication

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

the class ConstraintsEvaluatingPropertyTests method ensureConstraintsPresent.

@SuppressWarnings("rawtypes")
private void ensureConstraintsPresent(String[] classSource, int classIndexToTest, int constraintCount) throws Exception {
    // We need to do a real test here to make sure
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class[] classes = new Class[classSource.length];
    for (int i = 0; i < classSource.length; i++) {
        classes[i] = gcl.parseClass(classSource[i]);
    }
    GrailsApplication ga = new DefaultGrailsApplication(classes[classIndexToTest]);
    ga.initialise();
    new MappingContextBuilder(ga).build(classes[classIndexToTest]);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, classes[classIndexToTest].getName());
    Map constraints = domainClass.getConstrainedProperties();
    grails.gorm.validation.ConstrainedProperty p = (grails.gorm.validation.ConstrainedProperty) constraints.get("name");
    Collection cons = p.getAppliedConstraints();
    assertEquals("Incorrect number of constraints extracted: " + constraints, constraintCount, cons.size());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map) grails.gorm.validation(grails.gorm.validation)

Aggregations

GrailsApplication (grails.core.GrailsApplication)18 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)9 GrailsDomainClass (grails.core.GrailsDomainClass)8 GroovyClassLoader (groovy.lang.GroovyClassLoader)8 Map (java.util.Map)4 CollectionDataBindingSource (grails.databinding.CollectionDataBindingSource)3 IncludingPluginFilter (org.grails.plugins.IncludingPluginFilter)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 DataBindingSource (grails.databinding.DataBindingSource)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 GrailsClass (grails.core.GrailsClass)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 DataBinder (grails.databinding.DataBinder)1 grails.gorm.validation (grails.gorm.validation)1 Constrained (grails.gorm.validation.Constrained)1 ValidationErrors (grails.validation.ValidationErrors)1 GrailsWebDataBinder (grails.web.databinding.GrailsWebDataBinder)1