use of cn.taketoday.context.ApplicationContextException in project today-framework 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'");
}
use of cn.taketoday.context.ApplicationContextException in project today-framework by TAKETODAY.
the class DelegatingApplicationContextInitializer method getInitializerClass.
private Class<ApplicationContextInitializer> getInitializerClass(String className) throws LinkageError {
try {
Class<ApplicationContextInitializer> initializerClass = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
Assert.isAssignable(ApplicationContextInitializer.class, initializerClass);
return initializerClass;
} catch (ClassNotFoundException ex) {
throw new ApplicationContextException("Failed to load context initializer class [" + className + "]", ex);
}
}
use of cn.taketoday.context.ApplicationContextException in project today-framework by TAKETODAY.
the class ServletWebServerApplicationContext method getWebServerFactory.
/**
* Returns the {@link ServletWebServerFactory} that should be used to create the
* embedded {@link WebServer}. By default this method searches for a suitable bean in
* the context itself.
*
* @return a {@link ServletWebServerFactory} (never {@code null})
*/
protected ServletWebServerFactory getWebServerFactory() {
// Use bean names so that we don't consider the hierarchy
StandardBeanFactory beanFactory = getBeanFactory();
Set<String> beanNames = beanFactory.getBeanNamesForType(ServletWebServerFactory.class);
if (beanNames.size() == 0) {
throw new MissingWebServerFactoryBeanException(getClass(), ServletWebServerFactory.class, ApplicationType.SERVLET_WEB);
}
if (beanNames.size() > 1) {
throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to multiple " + "ServletWebServerFactory beans : " + StringUtils.collectionToCommaDelimitedString(beanNames));
}
return beanFactory.getBean(CollectionUtils.firstElement(beanNames), ServletWebServerFactory.class);
}
use of cn.taketoday.context.ApplicationContextException in project today-framework by TAKETODAY.
the class ServletWebServerApplicationContext method createWebServer.
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
webServer = factory.getWebServer(getSelfInitializer());
StandardBeanFactory beanFactory = getBeanFactory();
beanFactory.registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this, webServer));
beanFactory.registerSingleton("webServerGracefulShutdown", new WebServerGracefulShutdownLifecycle(webServer));
this.webServer = webServer;
} else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
} catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
use of cn.taketoday.context.ApplicationContextException in project today-framework 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);
}
}
Aggregations