use of org.craftercms.engine.util.spring.context.RestrictedApplicationContext in project engine by craftercms.
the class SiteContextFactory method getApplicationContext.
protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext, URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths, ResourceLoader resourceLoader) {
String siteName = siteContext.getSiteName();
logger.info("--------------------------------------------------");
logger.info("<Loading application context for site: " + siteName + ">");
logger.info("--------------------------------------------------");
try {
Resource appContextResource = null;
for (int i = applicationContextPaths.length - 1; i >= 0; i--) {
Resource resource = resourceLoader.getResource(applicationContextPaths[i]);
if (resource.exists()) {
appContextResource = resource;
break;
}
}
if (appContextResource != null) {
GenericApplicationContext appContext;
if (disableVariableRestrictions) {
appContext = new GenericApplicationContext(globalApplicationContext);
} else {
appContext = new RestrictedApplicationContext(globalApplicationContext, defaultPublicBeans);
}
appContext.setClassLoader(classLoader);
if (!enableExpressions) {
appContext.addBeanFactoryPostProcessor(factory -> factory.setBeanExpressionResolver(null));
}
if (config != null) {
MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
propertySources.addFirst(new ApacheCommonsConfiguration2PropertySource(CONFIG_BEAN_NAME, config));
appContext.getBeanFactory().registerSingleton(CONFIG_BEAN_NAME, config);
}
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
reader.loadBeanDefinitions(appContextResource);
appContext.refresh();
return appContext;
} else {
return null;
}
} catch (Exception e) {
throw new SiteContextCreationException("Unable to load application context for site '" + siteName + "'", e);
} finally {
logger.info("--------------------------------------------------");
logger.info("</Loading application context for site: " + siteName + ">");
logger.info("--------------------------------------------------");
}
}
Aggregations