Search in sources :

Example 11 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project com.revolsys.open by revolsys.

the class DatabaseConfigurer method postProcessBeanFactory.

/**
 * @param beanFactory The bean factory the bean is loaded from.
 */
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) {
    final Map<String, String> properties = new HashMap<>();
    Connection connection = null;
    try {
        connection = JdbcUtils.getConnection(this.dataSource);
        final String sql = "SELECT " + this.keyColumnName + ", " + this.valueColumnName + " FROM " + this.tableName;
        final Statement statement = connection.createStatement();
        final ResultSet results = statement.executeQuery(sql);
        while (results.next()) {
            final String key = results.getString(1);
            final String value = results.getString(2);
            properties.put(key, value);
        }
    } catch (final SQLException e) {
        throw new BeanInitializationException(e.getMessage(), e);
    } finally {
        JdbcUtils.release(connection, this.dataSource);
    }
    processProperties(beanFactory, properties);
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 12 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project com.revolsys.open by revolsys.

the class DatabasePropertyOverrideConfigurer method applyMapPropertyValue.

/**
 * Apply the given property value to the corresponding map property on the
 * bean.
 *
 * @param factory The bean factory.
 * @param beanName The name of the bean.
 * @param propertyName The name of the property.
 * @param mapKey The key in the map to set.
 * @param value The value to set.
 */
protected void applyMapPropertyValue(final ConfigurableListableBeanFactory factory, final String beanName, final String propertyName, final String mapKey, final String value) {
    this.beanNames.add(beanName);
    final BeanDefinition bd = factory.getBeanDefinition(beanName);
    final MutablePropertyValues values = bd.getPropertyValues();
    final PropertyValue propertyValue = values.getPropertyValue(propertyName);
    if (propertyValue != null) {
        final Object objectValue = propertyValue.getValue();
        if (objectValue instanceof Map) {
            final Map map = (Map) objectValue;
            map.put(mapKey, value);
        } else {
            throw new BeanInitializationException("Bean property [" + beanName + "." + propertyName + "] is not a Map");
        }
    } else {
        final Map<String, Object> map = new HashMap<>();
        map.put(mapKey, value);
        values.addPropertyValue(propertyName, map);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) HashMap(java.util.HashMap) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project OpenOLAT by OpenOLAT.

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)

Example 14 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project qpp-conversion-tool by CMSgov.

the class DynamoDbConfig method dynamoDbMapper.

/**
 * Creates a DynamoDB object mapper {@link Bean} that interacts with {@link DynamoDBTable} annotated POJOs.
 * Based on an available {@link AmazonDynamoDB} and {@link AWSKMS} {@link Bean}.
 *
 * Creates a DynamoDB object that depends on two different environment variables. {@code DYNAMO_TABLE_NAME} and
 * {@code KMS_KEY}.
 *
 * If {@code DYNAMO_TABLE_NAME} is specified, the value will be used for all read/write from/to the table of that name
 * regardless of what is specified for {@link DynamoDBTable}.  Else, the table specified for {@link DynamoDBTable} will be
 * used like normal.
 *
 * If {@code KMS_KEY} is specified, items in the tables will be encrypted.  Else, the items will not be encrypted.
 *
 * @param dynamoDbClient The {@link AmazonDynamoDB} {@link Bean}.
 * @return The DynamoDB object mapper
 */
@Bean
public DynamoDBMapper dynamoDbMapper(AmazonDynamoDB dynamoDbClient) {
    DynamoDBMapper dynamoDbMapper;
    final Optional<String> kmsKey = getOptionalProperty(Constants.KMS_KEY_ENV_VARIABLE);
    final Optional<String> tableName = getOptionalProperty(Constants.DYNAMO_TABLE_NAME_ENV_VARIABLE);
    final Optional<String> noAudit = getOptionalProperty(Constants.NO_AUDIT_ENV_VARIABLE);
    if (!noAudit.isPresent()) {
        if (tableName.isPresent() && kmsKey.isPresent()) {
            API_LOG.info("Using DynamoDB table name {} and KMS key {}.", tableName, kmsKey);
            dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, tableNameOverrideConfig(tableName.get()), encryptionTransformer(kmsKey.get()));
        } else if (kmsKey.isPresent()) {
            API_LOG.warn("Using KMS key {}, but no DynamoDB table name specified.", tableName);
            dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, getDynamoDbMapperConfig(), encryptionTransformer(kmsKey.get()));
        } else {
            API_LOG.error(NO_KMS_KEY + " This is a fatal error.");
            throw new BeanInitializationException(NO_KMS_KEY);
        }
    } else {
        API_LOG.info("Will not save any audit information.");
        dynamoDbMapper = null;
    }
    return dynamoDbMapper;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Bean(org.springframework.context.annotation.Bean)

Example 15 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project spring-framework by spring-projects.

the class ViewResolverRegistry method scriptTemplate.

/**
 * Register a script template view resolver with an empty default view name prefix and suffix.
 * <p><strong>Note</strong> that you must also configure script templating by
 * adding a {@link ScriptTemplateConfigurer} bean.
 * @since 5.0.4
 */
public UrlBasedViewResolverRegistration scriptTemplate() {
    if (!checkBeanOfType(ScriptTemplateConfigurer.class)) {
        throw new BeanInitializationException("In addition to a script template view resolver " + "there must also be a single ScriptTemplateConfig bean in this web application context " + "(or its parent): ScriptTemplateConfigurer is the usual implementation. " + "This bean may be given any name.");
    }
    ScriptRegistration registration = new ScriptRegistration();
    UrlBasedViewResolver resolver = registration.getViewResolver();
    if (this.applicationContext != null) {
        resolver.setApplicationContext(this.applicationContext);
    }
    this.viewResolvers.add(resolver);
    return registration;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) UrlBasedViewResolver(org.springframework.web.reactive.result.view.UrlBasedViewResolver) ScriptTemplateConfigurer(org.springframework.web.reactive.result.view.script.ScriptTemplateConfigurer)

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