Search in sources :

Example 6 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testExceptionHandling.

@Test
public void testExceptionHandling() {
    ExceptionThrower bean = new ExceptionThrower();
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();
    try {
        proxy.doTest();
    } catch (Exception ex) {
        assertTrue("Invalid exception class", ex instanceof ApplicationContextException);
    }
    assertTrue("Catch was not invoked", proxy.isCatchInvoked());
    assertTrue("Finally was not invoked", proxy.isFinallyInvoked());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ApplicationContextException(org.springframework.context.ApplicationContextException) ApplicationContextException(org.springframework.context.ApplicationContextException) Test(org.junit.Test)

Example 7 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project musiccabinet by hakko.

the class DirectoryBrowserService method getContent.

private DirectoryContent getContent(Directory dir) {
    Set<File> foundFiles = new HashSet<>();
    Set<String> foundSubDirs = new HashSet<>();
    DirectoryContent content = new DirectoryContent(dir.getPath(), foundSubDirs, foundFiles);
    Path path = Paths.get(dir.getPath());
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path file : stream) {
            BasicFileAttributeView view = getFileAttributeView(file, BasicFileAttributeView.class);
            BasicFileAttributes attr = view.readAttributes();
            if (attr.isDirectory()) {
                foundSubDirs.add(file.toAbsolutePath().toString());
            } else if (attr.isRegularFile()) {
                foundFiles.add(new File(file, attr));
            }
        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new ApplicationContextException("Couldn't read " + dir.getPath(), e);
    }
    return content;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) ApplicationContextException(org.springframework.context.ApplicationContextException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(com.github.hakko.musiccabinet.domain.model.library.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 8 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project uPortal by Jasig.

the class VersionVerifier method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    for (final Map.Entry<String, Version> productVersionEntry : this.requiredProductVersions.entrySet()) {
        final String product = productVersionEntry.getKey();
        final Version dbVersion = this.versionDao.getVersion(product);
        if (dbVersion == null) {
            throw new ApplicationContextException("No Version exists for " + product + " in the database. Please check the upgrade instructions for this release.");
        }
        final Version codeVersion = productVersionEntry.getValue();
        final Field mostSpecificMatchingField = VersionUtils.getMostSpecificMatchingField(dbVersion, codeVersion);
        switch(mostSpecificMatchingField) {
            //Versions completely match
            case LOCAL:
                {
                    logger.info("Software and Database versions are both {} for {}", dbVersion, product);
                    continue;
                }
            //Versions match except for local part
            case PATCH:
            //Versions match except for patch.local part
            case MINOR:
                {
                    //If db is before code and auto-update is enabled run hibernate-update
                    final Field upgradeField = mostSpecificMatchingField.getLessImportant();
                    if (dbVersion.isBefore(codeVersion) && this.updatePolicy != null && (upgradeField.equals(this.updatePolicy) || upgradeField.isLessImportantThan(this.updatePolicy))) {
                        logger.info("Automatically updating database from {} to {} for {}", dbVersion, codeVersion, product);
                        this.portalShellBuildHelper.hibernateUpdate("automated-hibernate-update", product, true, null);
                        continue;
                    } else if (codeVersion.isBefore(dbVersion)) {
                        //It is ok to run older code on a newer DB within the local/patch range
                        continue;
                    }
                }
            //Versions match except for minor.patch.local part
            case MAJOR:
            //Versions do not match at all
            default:
                {
                    if (dbVersion.isBefore(codeVersion)) {
                        throw new ApplicationContextException("Database Version for " + product + " is " + dbVersion + " but the code version is " + codeVersion + ". Please check the upgrade instructions for this release");
                    } else {
                        throw new ApplicationContextException("Database Version for " + product + " is " + dbVersion + " but the code version is " + codeVersion + ". It is not possible to run ");
                    }
                }
        }
    }
}
Also used : Field(org.apereo.portal.version.om.Version.Field) Version(org.apereo.portal.version.om.Version) ApplicationContextException(org.springframework.context.ApplicationContextException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 9 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithInvalidContext.

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown ApplicationContextException");
    } catch (ApplicationContextException ex) {
        // expected
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 10 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class FrameworkServlet method createWebApplicationContext.

/**
	 * Instantiate the WebApplicationContext for this servlet, either a default
	 * {@link org.springframework.web.context.support.XmlWebApplicationContext}
	 * or a {@link #setContextClass custom context class}, if set.
	 * <p>This implementation expects custom contexts to implement the
	 * {@link org.springframework.web.context.ConfigurableWebApplicationContext}
	 * interface. Can be overridden in subclasses.
	 * <p>Do not forget to register this servlet instance as application listener on the
	 * created context (for triggering its {@link #onRefresh callback}, and to call
	 * {@link org.springframework.context.ConfigurableApplicationContext#refresh()}
	 * before returning the context instance.
	 * @param parent the parent ApplicationContext to use, or {@code null} if none
	 * @return the WebApplicationContext for this servlet
	 * @see org.springframework.web.context.support.XmlWebApplicationContext
	 */
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
    Class<?> contextClass = getContextClass();
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + contextClass.getName() + "'" + ", using parent context [" + parent + "]");
    }
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Fatal initialization error in servlet with name '" + getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext");
    }
    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
    wac.setEnvironment(getEnvironment());
    wac.setParent(parent);
    wac.setConfigLocation(getContextConfigLocation());
    configureAndRefreshWebApplicationContext(wac);
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) ApplicationContextException(org.springframework.context.ApplicationContextException)

Aggregations

ApplicationContextException (org.springframework.context.ApplicationContextException)17 Test (org.junit.Test)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 IOException (java.io.IOException)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 ServletContextEvent (javax.servlet.ServletContextEvent)2 DirectoryContent (com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent)1 File (com.github.hakko.musiccabinet.domain.model.library.File)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DirectoryIteratorException (java.nio.file.DirectoryIteratorException)1 Path (java.nio.file.Path)1 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletException (javax.servlet.ServletException)1