Search in sources :

Example 1 with ApplicationContextException

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

Example 2 with ApplicationContextException

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.");
}
Also used : ServletWebServerFactory(cn.taketoday.framework.web.servlet.server.ServletWebServerFactory) ServletWebServerApplicationContext(cn.taketoday.framework.web.servlet.context.ServletWebServerApplicationContext) FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis) ApplicationContextException(cn.taketoday.context.ApplicationContextException) Test(org.junit.jupiter.api.Test)

Example 3 with ApplicationContextException

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 + "]");
        }
    }
}
Also used : MessageSourceAccessor(cn.taketoday.context.support.MessageSourceAccessor) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 4 with ApplicationContextException

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

Example 5 with ApplicationContextException

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'");
}
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)

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