Search in sources :

Example 41 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project openolat by klemens.

the class WebappHelper method testUtf8FileSystem.

/**
 * Test if filesystem is capable to store UTF-8 characters
 * Try to read/write a file with UTF-8 chars in the filename in a temporary directory.
 */
private void testUtf8FileSystem() {
    File tmpDir = new File(new File(WebappHelper.getUserDataRoot()), "tmp");
    if (!tmpDir.exists())
        tmpDir.mkdir();
    File writeFile = new File(tmpDir, "UTF-8 test läsÖiç-首页|新");
    if (writeFile.exists()) {
        // remove exising files first
        writeFile.delete();
    }
    try {
        writeFile.createNewFile();
    } catch (IOException e) {
        log.warn("No UTF-8 capable filesystem found! Error while writing testfile to filesystem", e);
    }
    // try to lookup file: get files from filesystem and search for file we created above
    File[] tmpFiles = tmpDir.listFiles();
    boolean foundUtf8File = false;
    if (tmpFiles != null) {
        for (int i = 0; i < tmpFiles.length; i++) {
            File tmpFile = tmpFiles[i];
            if (tmpFile.getName().equals("UTF-8 test läsÖiç-首页|新")) {
                foundUtf8File = true;
                break;
            }
        }
    }
    if (foundUtf8File) {
        // test ok
        log.info("UTF-8 capable filesystem detected");
    } else {
        // test failed
        log.warn("No UTF-8 capable filesystem found! Could not read / write UTF-8 characters from / to filesystem! " + "You probably misconfigured your system, try setting your LANG variable to a correct value.");
        log.warn("Your current file encoding configuration: java.nio.charset.Charset.defaultCharset().name()::" + java.nio.charset.Charset.defaultCharset().name() + " (the one used) and your system property file.encoding::" + System.getProperty("file.encoding") + " (the one configured)");
    }
    // try to delete file anyway
    writeFile.delete();
    if (!foundUtf8File && WebappHelper.enforceUtf8Filesystem) {
        throw new BeanInitializationException("System startup aborted to to file system missconfiguration. See previous warnings in logfile and fix your " + "Java environment. This check can be disabled by setting enforce.utf8.filesystem=false, but be aware that the " + "decision to use a certain encoding on the filesystem is a one-time decision. You can not cange to UTF-8 later!");
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) IOException(java.io.IOException) File(java.io.File)

Aggregations

BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)41 IOException (java.io.IOException)9 BeansException (org.springframework.beans.BeansException)7 HashMap (java.util.HashMap)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 Field (java.lang.reflect.Field)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2