Search in sources :

Example 11 with ApplicationContextException

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'");
}
Also used : InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ApplicationContextException(cn.taketoday.context.ApplicationContextException) URL(java.net.URL) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with ApplicationContextException

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);
    }
}
Also used : ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 13 with ApplicationContextException

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);
}
Also used : MissingWebServerFactoryBeanException(cn.taketoday.framework.web.context.MissingWebServerFactoryBeanException) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 14 with ApplicationContextException

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();
}
Also used : ServletException(jakarta.servlet.ServletException) ServletWebServerFactory(cn.taketoday.framework.web.servlet.server.ServletWebServerFactory) WebServer(cn.taketoday.framework.web.server.WebServer) WebServerGracefulShutdownLifecycle(cn.taketoday.framework.web.context.WebServerGracefulShutdownLifecycle) ServletContext(jakarta.servlet.ServletContext) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 15 with ApplicationContextException

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);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ParseException(freemarker.core.ParseException) IOException(java.io.IOException) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Aggregations

ApplicationContextException (cn.taketoday.context.ApplicationContextException)25 IOException (java.io.IOException)8 Test (org.junit.jupiter.api.Test)7 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)6 ApplicationContextInitializer (cn.taketoday.context.ApplicationContextInitializer)4 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)4 ServletWebServerFactory (cn.taketoday.framework.web.servlet.server.ServletWebServerFactory)4 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 URL (java.net.URL)4 LinkedHashSet (java.util.LinkedHashSet)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)2 MessageSourceAccessor (cn.taketoday.context.support.MessageSourceAccessor)2 MissingWebServerFactoryBeanException (cn.taketoday.framework.web.context.MissingWebServerFactoryBeanException)2 WebServerGracefulShutdownLifecycle (cn.taketoday.framework.web.context.WebServerGracefulShutdownLifecycle)2 ReactiveWebServerApplicationContext (cn.taketoday.framework.web.reactive.context.ReactiveWebServerApplicationContext)2 ReactiveWebServerFactory (cn.taketoday.framework.web.reactive.server.ReactiveWebServerFactory)2 WebServer (cn.taketoday.framework.web.server.WebServer)2 ServletWebServerApplicationContext (cn.taketoday.framework.web.servlet.context.ServletWebServerApplicationContext)2