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);
}
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);
}
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);
}
}
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);
}
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());
}
Aggregations