use of cn.taketoday.context.ApplicationContextException in project today-infrastructure by TAKETODAY.
the class FreeMarkerView method checkResource.
/**
* Check that the FreeMarker template used for this view exists and is valid.
* <p>Can be overridden to customize the behavior, for example in case of
* multiple templates to be rendered into a single view.
*/
@Override
public boolean checkResource(Locale locale) throws Exception {
String url = getUrl();
Assert.state(url != null, "'url' not set");
try {
// Check that we can get the template, even if we might subsequently get it again.
getTemplate(url, locale);
return true;
} catch (FileNotFoundException ex) {
// Allow for ViewResolver chaining...
return false;
} catch (ParseException ex) {
throw new ApplicationContextException("Failed to parse [" + url + "]", ex);
} catch (IOException ex) {
throw new ApplicationContextException("Failed to load [" + url + "]", ex);
}
}
use of cn.taketoday.context.ApplicationContextException in project today-infrastructure by TAKETODAY.
the class MissingWebServerFactoryBeanFailureAnalyzerTests method missingServletWebServerFactoryBeanFailure.
@Test
void missingServletWebServerFactoryBeanFailure() {
ApplicationContextException failure = createFailure(new ServletWebServerApplicationContext());
assertThat(failure).isNotNull();
FailureAnalysis analysis = new MissingWebServerFactoryBeanFailureAnalyzer().analyze(failure);
assertThat(analysis).isNotNull();
assertThat(analysis.getDescription()).isEqualTo("Web application could not be started as there was no " + ServletWebServerFactory.class.getName() + " bean defined in the context.");
assertThat(analysis.getAction()).isEqualTo("Check your application's dependencies for a supported servlet_web web server.\nCheck the configured web " + "application type.");
}
use of cn.taketoday.context.ApplicationContextException in project today-infrastructure by TAKETODAY.
the class ApplicationContextSupport method setApplicationContext.
@Override
public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
if (context == null && !isContextRequired()) {
// Reset internal context state.
this.applicationContext = null;
this.messageSourceAccessor = null;
} else if (this.applicationContext == null) {
// Initialize with passed-in context.
if (!requiredContextClass().isInstance(context)) {
throw new ApplicationContextException("Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
}
this.applicationContext = context;
this.messageSourceAccessor = new MessageSourceAccessor(context);
initApplicationContext(context);
} else {
// Ignore reinitialization if same context passed in.
if (this.applicationContext != context) {
throw new ApplicationContextException("Cannot reinitialize with different application context: current one is [" + this.applicationContext + "], passed-in one is [" + context + "]");
}
}
}
use of cn.taketoday.context.ApplicationContextException in project today-infrastructure by TAKETODAY.
the class ContextUtils method loadFromMetaInfoClass.
public static Set<String> loadFromMetaInfoClass(final String resource) {
Assert.notNull(resource, "META-INF resource must not be null");
if (resource.startsWith("META-INF")) {
LinkedHashSet<String> ret = new LinkedHashSet<>();
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
if (classLoader == null) {
classLoader = ContextUtils.class.getClassLoader();
}
try {
Enumeration<URL> resources = classLoader.getResources(resource);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
String className;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), Constant.DEFAULT_CHARSET))) {
while ((className = reader.readLine()) != null) {
if (StringUtils.isNotEmpty(className)) {
// @since 3.0 FIX empty lines
ret.add(className);
}
}
}
}
return ret;
} catch (IOException e) {
throw new ApplicationContextException("Exception occurred when load from '" + resource + '\'', e);
}
}
throw new IllegalArgumentException("Resource must start with 'META-INF'");
}
use of cn.taketoday.context.ApplicationContextException in project today-infrastructure by TAKETODAY.
the class ContextUtils method loadFromMetaInfo.
// META-INF
// ----------------------
/**
* Scan classes set from META-INF/xxx
*
* @param resource Resource file start with 'META-INF'
* @return Class set from META-INF/xxx
* @throws ApplicationContextException If any {@link IOException} occurred
*/
public static Set<Class<?>> loadFromMetaInfo(final String resource) {
Assert.notNull(resource, "META-INF resource must not be null");
if (resource.startsWith("META-INF")) {
Set<Class<?>> ret = new HashSet<>();
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
if (classLoader == null) {
classLoader = ContextUtils.class.getClassLoader();
}
Charset charset = Constant.DEFAULT_CHARSET;
try {
Enumeration<URL> resources = classLoader.getResources(resource);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
String className = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), charset))) {
while ((className = reader.readLine()) != null) {
if (StringUtils.isNotEmpty(className)) {
// @since 3.0 FIX empty lines
ret.add(classLoader.loadClass(className));
}
}
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Class file: '" + className + "' not in " + url);
}
}
return ret;
} catch (IOException e) {
throw new ApplicationContextException("Exception occurred when load from '" + resource + '\'', e);
}
}
throw new IllegalArgumentException("Resource must start with 'META-INF'");
}
Aggregations