Example usage for org.springframework.util CollectionUtils mergePropertiesIntoMap

List of usage examples for org.springframework.util CollectionUtils mergePropertiesIntoMap

Introduction

In this page you can find the example usage for org.springframework.util CollectionUtils mergePropertiesIntoMap.

Prototype

@SuppressWarnings("unchecked")
public static <K, V> void mergePropertiesIntoMap(@Nullable Properties props, Map<K, V> map) 

Source Link

Document

Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.

Usage

From source file:net.alpha.velocity.spring.support.VelocityEngineFactory.java

/**
 * Prepare the VelocityEngine instance and return it.
 *
 * @return the VelocityEngine instance//  w w w  .j av a2  s .  c o m
 * @throws IOException       if the config file wasn't found
 * @throws VelocityException on Velocity initialization failure
 */
public VelocityEngine createVelocityEngine() throws IOException, VelocityException {
    VelocityEngine velocityEngine = newVelocityEngine();
    Map<String, Object> props = new HashMap<String, Object>();

    // Load config file if set.
    if (this.configLocation != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading Velocity config from [" + this.configLocation + "]");
        }
        CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation),
                props);
    }

    // Merge local properties if set.
    if (!this.velocityProperties.isEmpty()) {
        props.putAll(this.velocityProperties);
    }

    // Set a resource loader path, if required.
    if (this.resourceLoaderPath != null) {
        initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath);
    }

    // Log via Commons Logging?
    if (this.overrideLogging) {
        velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
    }

    // Apply properties to VelocityEngine.
    for (Map.Entry<String, Object> entry : props.entrySet()) {
        velocityEngine.setProperty(entry.getKey(), entry.getValue());
    }

    postProcessVelocityEngine(velocityEngine);

    // Perform actual initialization.
    velocityEngine.init();

    return velocityEngine;
}

From source file:com.saysth.commons.quartz.SchedulerFactoryBean.java

/**
 * Load and/or apply Quartz properties to the given SchedulerFactory.
 * // www  . j  a va2  s  . c o  m
 * @param schedulerFactory
 *            the SchedulerFactory to initialize
 */
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {

    if (!(schedulerFactory instanceof StdSchedulerFactory)) {
        if (this.configLocation != null || this.quartzProperties != null || this.taskExecutor != null
                || this.dataSource != null) {
            throw new IllegalArgumentException(
                    "StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);
        }
        // Otherwise assume that no initialization is necessary...
        return;
    }

    Properties mergedProps = new Properties();

    if (this.resourceLoader != null) {
        mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
                ResourceLoaderClassLoadHelper.class.getName());
    }

    if (this.taskExecutor != null) {
        mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
                LocalTaskExecutorThreadPool.class.getName());
    } else {
        // Set necessary default properties here, as Quartz will not apply
        // its default configuration when explicitly given properties.
        mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
        mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
    }

    if (this.configLocation != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading Quartz config from [" + this.configLocation + "]");
        }
        PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
    }

    CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);

    if (this.dataSource != null) {
        mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
    }

    // Make sure to set the scheduler name as configured in the Spring
    // configuration.
    if (this.schedulerName != null) {
        mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
    }

    ((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
}

From source file:org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.java

/**
 * Load the specified NamespaceHandler mappings lazily.
 *//* w w w  .j a v  a2 s  . c o  m*/
private Map<String, Object> getHandlerMappings() {
    Map<String, Object> handlerMappings = this.handlerMappings;
    if (handlerMappings == null) {
        synchronized (this) {
            handlerMappings = this.handlerMappings;
            if (handlerMappings == null) {
                try {
                    Properties mappings = PropertiesLoaderUtils.loadAllProperties(this.handlerMappingsLocation,
                            this.classLoader);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Loaded NamespaceHandler mappings: " + mappings);
                    }
                    Map<String, Object> mappingsToUse = new ConcurrentHashMap<>(mappings.size());
                    CollectionUtils.mergePropertiesIntoMap(mappings, mappingsToUse);
                    handlerMappings = mappingsToUse;
                    this.handlerMappings = handlerMappings;
                } catch (IOException ex) {
                    throw new IllegalStateException("Unable to load NamespaceHandler mappings from location ["
                            + this.handlerMappingsLocation + "]", ex);
                }
            }
        }
    }
    return handlerMappings;
}

From source file:org.springframework.beans.factory.xml.PluggableSchemaResolver.java

/**
 * Load the specified schema mappings lazily.
 *//*  w  ww .  j  a  v  a2 s.  co  m*/
private Map<String, String> getSchemaMappings() {
    Map<String, String> schemaMappings = this.schemaMappings;
    if (schemaMappings == null) {
        synchronized (this) {
            schemaMappings = this.schemaMappings;
            if (schemaMappings == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading schema mappings from [" + this.schemaMappingsLocation + "]");
                }
                try {
                    Properties mappings = PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation,
                            this.classLoader);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Loaded schema mappings: " + mappings);
                    }
                    Map<String, String> mappingsToUse = new ConcurrentHashMap<>(mappings.size());
                    CollectionUtils.mergePropertiesIntoMap(mappings, mappingsToUse);
                    schemaMappings = mappingsToUse;
                    this.schemaMappings = schemaMappings;
                } catch (IOException ex) {
                    throw new IllegalStateException("Unable to load schema mappings from location ["
                            + this.schemaMappingsLocation + "]", ex);
                }
            }
        }
    }
    return schemaMappings;
}

From source file:org.springframework.jmx.export.naming.KeyNamingStrategy.java

/**
 * Merges the {@code Properties} configured in the {@code mappings} and
 * {@code mappingLocations} into the final {@code Properties} instance
 * used for {@code ObjectName} resolution.
 *//*  w w  w .j  a v a2  s  .c o m*/
@Override
public void afterPropertiesSet() throws IOException {
    this.mergedMappings = new Properties();
    CollectionUtils.mergePropertiesIntoMap(this.mappings, this.mergedMappings);

    if (this.mappingLocations != null) {
        for (Resource location : this.mappingLocations) {
            if (logger.isInfoEnabled()) {
                logger.info("Loading JMX object name mappings file from " + location);
            }
            PropertiesLoaderUtils.fillProperties(this.mergedMappings, location);
        }
    }
}

From source file:org.springframework.jndi.JndiTemplate.java

/**
 * Create a new JNDI initial context. Invoked by {@link #getContext}.
 * <p>The default implementation use this template's environment settings.
 * Can be subclassed for custom contexts, e.g. for testing.
 * @return the initial Context instance//from   w  ww .  j  a v  a2  s.c om
 * @throws NamingException in case of initialization errors
 */
protected Context createInitialContext() throws NamingException {
    Hashtable<?, ?> icEnv = null;
    Properties env = getEnvironment();
    if (env != null) {
        icEnv = new Hashtable<>(env.size());
        CollectionUtils.mergePropertiesIntoMap(env, icEnv);
    }
    return new InitialContext(icEnv);
}

From source file:org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean.java

/**
 * Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".
 * <p>Can be used to override values in a JDO properties config file,
 * or to specify all necessary properties locally.
 * <p>Can be populated with a String "value" (parsed via PropertiesEditor)
 * or a "props" element in XML bean definitions.
 */// www . j  ava  2 s. c o m
public void setJdoProperties(Properties jdoProperties) {
    CollectionUtils.mergePropertiesIntoMap(jdoProperties, this.jdoPropertyMap);
}

From source file:org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean.java

/**
 * Initialize the PersistenceManagerFactory for the given location.
 * @throws IllegalArgumentException in case of illegal property values
 * @throws IOException if the properties could not be loaded from the given location
 * @throws JDOException in case of JDO initialization errors
 *//*  www.  j  av a 2  s . c o  m*/
@Override
public void afterPropertiesSet() throws IllegalArgumentException, IOException, JDOException {
    if (this.persistenceManagerFactoryName != null) {
        if (this.configLocation != null || !this.jdoPropertyMap.isEmpty()) {
            throw new IllegalStateException("'configLocation'/'jdoProperties' not supported in "
                    + "combination with 'persistenceManagerFactoryName' - specify one or the other, not both");
        }
        if (logger.isInfoEnabled()) {
            logger.info("Building new JDO PersistenceManagerFactory for name '"
                    + this.persistenceManagerFactoryName + "'");
        }
        this.persistenceManagerFactory = newPersistenceManagerFactory(this.persistenceManagerFactoryName);
    }

    else {
        Map<String, Object> mergedProps = new HashMap<String, Object>();
        if (this.configLocation != null) {
            if (logger.isInfoEnabled()) {
                logger.info("Loading JDO config from [" + this.configLocation + "]");
            }
            CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation),
                    mergedProps);
        }
        mergedProps.putAll(this.jdoPropertyMap);
        logger.info("Building new JDO PersistenceManagerFactory");
        this.persistenceManagerFactory = newPersistenceManagerFactory(mergedProps);
    }

    // Build default JdoDialect if none explicitly specified.
    if (this.jdoDialect == null) {
        this.jdoDialect = new DefaultJdoDialect(this.persistenceManagerFactory.getConnectionFactory());
    }
}

From source file:org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.java

/**
 * Specify JPA properties, to be passed into
 * {@code Persistence.createEntityManagerFactory} (if any).
 * <p>Can be populated with a String "value" (parsed via PropertiesEditor) or a
 * "props" element in XML bean definitions.
 * @see javax.persistence.Persistence#createEntityManagerFactory(String, java.util.Map)
 * @see javax.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)
 */// w w  w .ja  v  a2  s  . com
public void setJpaProperties(Properties jpaProperties) {
    CollectionUtils.mergePropertiesIntoMap(jpaProperties, this.jpaPropertyMap);
}

From source file:org.springframework.yarn.configuration.EnvironmentFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {
    environment = createEnvironment();/* ww w .j  a  v  a  2  s .c  om*/

    if (properties != null) {
        // if we have properties, merge those into environment
        CollectionUtils.mergePropertiesIntoMap(properties, environment);
    }

    boolean addDelimiter = false;

    // set CLASSPATH variable if there's something to set
    StringBuilder classPathEnv = new StringBuilder();
    if (StringUtils.hasText(classpath)) {
        classPathEnv.append(classpath);
        addDelimiter = true;
    }

    ArrayList<String> paths = new ArrayList<String>();

    if (includeBaseDirectory) {
        paths.add("./*");
    }

    if (useDefaultYarnClasspath) {
        if (log.isDebugEnabled()) {
            log.debug("Trying to use a default yarn classpath");
        }

        String defaultYarnClasspathString = "";
        if (StringUtils.hasText(defaultYarnAppClasspath)) {
            defaultYarnClasspathString = defaultYarnAppClasspath;
        } else if (configuration != null) {
            defaultYarnClasspathString = configuration.get(YarnConfiguration.YARN_APPLICATION_CLASSPATH);
            if (!StringUtils.hasText(defaultYarnClasspathString)) {
                // 2.3.x changed yarn.application.classpath to be empty in yarn-default.xml, so
                // if we got empty, fall back to DEFAULT_YARN_APPLICATION_CLASSPATH
                defaultYarnClasspathString = StringUtils
                        .arrayToCommaDelimitedString(YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH);
                log.info("Yarn classpath from configuration empty, fall back to " + defaultYarnClasspathString);
            } else {
                log.info("Yarn classpath from configuration " + defaultYarnClasspathString);
            }
        }
        paths.addAll(StringUtils.commaDelimitedListToSet(defaultYarnClasspathString));
    }

    if (useDefaultMapreduceClasspath) {
        if (log.isDebugEnabled()) {
            log.debug("Trying to use a mr yarn classpath");
        }
        String defaultMapreduceClasspathString = "";
        if (StringUtils.hasText(defaultMapreduceAppClasspath)) {
            defaultMapreduceClasspathString = defaultMapreduceAppClasspath;
        } else if (configuration != null) {
            defaultMapreduceClasspathString = configuration.get("mapreduce.application.classpath");
            if (!StringUtils.hasText(defaultMapreduceClasspathString)) {
                // using reflection with this because we don't have these
                // classes in a project classpath and this is just
                // a fall back for default value
                defaultMapreduceClasspathString = readStaticField("org.apache.hadoop.mapreduce.MRJobConfig",
                        "DEFAULT_MAPREDUCE_APPLICATION_CLASSPATH", getClass().getClassLoader());
                log.info("Mapreduce classpath from configuration empty, fall back to "
                        + defaultMapreduceClasspathString);
            } else {
                log.info("Mapreduce classpath from configuration " + defaultMapreduceClasspathString);
            }
        }
        paths.addAll(StringUtils.commaDelimitedListToSet(defaultMapreduceClasspathString));
    }

    Iterator<String> iterator = paths.iterator();
    // add delimiter if we're about to add something
    if (iterator.hasNext()) {
        classPathEnv.append(addDelimiter ? delimiter : "");
    }
    while (iterator.hasNext()) {
        classPathEnv.append(iterator.next());
        if (iterator.hasNext()) {
            classPathEnv.append(delimiter);
        }
    }

    String classpathString = classPathEnv.toString();
    if (StringUtils.hasText(classpathString)) {
        environment.put("CLASSPATH", classpathString);
        log.info("Adding CLASSPATH=" + classpathString);
    }
}