org.openflamingo.engine.configuration.ConfigurationManager.java Source code

Java tutorial

Introduction

Here is the source code for org.openflamingo.engine.configuration.ConfigurationManager.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openflamingo.engine.configuration;

import org.apache.commons.lang.StringUtils;
import org.openflamingo.el.ELUtils;
import org.openflamingo.model.site.Configuration;
import org.openflamingo.model.site.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Flamingo Site XML? Property Key Value ?  Configuration Manager.
 *
 * @author Byoung Gon, Kim
 * @since 0.1
 */
@Component
public class ConfigurationManager implements InitializingBean {

    /**
     * SLF4J Logging
     */
    private Logger logger = LoggerFactory.getLogger(ConfigurationManager.class);

    /**
     * Flamingo Site XML? JAXB Configuration Object
     */
    @Autowired
    private org.openflamingo.model.site.Configuration configuration;

    /**
     * Flamingo Site XML? Property Key Value   map
     */
    private Map<String, String> map;

    /**
     * Flamingo Site XML? Property Key Value   properties.
     */
    private Properties props;

    /**
     * Flamingo Site XML? Property Key  Property   Map
     */
    private Map<String, Property> propertyMap = new TreeMap<String, Property>();

    /**
     * System Properties ? 
     */
    private @Value("#{config['system.properties.apply']}") boolean applySystemProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.map = getConfiguratioMap(configuration);
        this.props = ConfigurationUtils.mapToProperties(map);
    }

    /**
     * Flamingo Site XML ??   Key Value Map .
     *
     * @param configuration {@link org.openflamingo.model.site.Configuration}
     * @return Key Value Map
     */
    public Map getConfiguratioMap(org.openflamingo.model.site.Configuration configuration) {
        List<Property> properties = configuration.getProperty();
        Map<String, String> map = new TreeMap<String, String>();
        for (Property property : properties) {
            if (!StringUtils.isEmpty(property.getName())) {
                String name = property.getName();
                String value = property.getValue();
                propertyMap.put(name, property);

                if (StringUtils.isEmpty(value)) {
                    String defautlVaule = property.getDefautlVaule();
                    if (!StringUtils.isEmpty(defautlVaule)) {
                        map.put(name, defautlVaule);
                    }
                } else {
                    map.put(name, value);
                }
            }
        }

        // if applySystemProperties = true, then inject System Properties to Map
        if (applySystemProperties) {
            Properties props = System.getProperties();
            Enumeration<?> names = props.propertyNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                String value = System.getProperty(name);
                map.put(name, value);
            }
        }
        return map;
    }

    /**
     * Flamingo Site XML? Property Key Value   properties? .
     *
     * @return Flamingo Site XML? Property Key Value   properties
     */
    public Properties getProperties() {
        return this.props;
    }

    /**
     *  ? ? .
     *
     * @return ?
     */
    public int size() {
        return map.size();
    }

    /**
     *  ?? EL? ?.
     *
     * @param value  ?
     * @return EL? ? ?? ? ?
     */
    public String evaluate(String value) {
        return ELUtils.resolve(props, value);
    }

    /**
     *  Key? Value ?.
     *
     * @param key Key
     * @return Key? Value?  ? ?
     */
    public String get(String key) {
        String value = map.get(key);
        if (StringUtils.isEmpty(value)) {
            return value;
        }
        return ELUtils.resolve(props, value);
    }

    /**
     *  Key? Value ?.
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key? Value?  ? ?
     */
    public String get(String key, String defaultValue) {
        String value = map.get(key);
        if (!StringUtils.isEmpty(value)) {
            return ELUtils.resolve(props, value);
        }
        return defaultValue;
    }

    /**
     *  Key  ?.
     *
     * @param key   ? Key
     * @return   <tt>false</tt>
     */
    public boolean containsKey(String key) {
        return map.containsKey(key);
    }

    /**
     *  Key?  Long Value .
     *
     * @param key Key
     * @return Key?  Long Value
     */
    public long getLong(String key) {
        return Long.parseLong(get(key));
    }

    /**
     *  Key?  Boolean Value .
     *
     * @param key Key
     * @return Key?  Boolean Value
     */
    public boolean getBoolean(String key) {
        try {
            return Boolean.parseBoolean(get(key));
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     *  Key?  Boolean Value .
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key?  Boolean Value
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        if (containsKey(key)) {
            return Boolean.parseBoolean(get(key));
        } else {
            return defaultValue;
        }
    }

    /**
     *  Key?  Long Value . ?  ? .
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key?  Long Value
     */
    public long getLong(String key, long defaultValue) {
        if (containsKey(key)) {
            return Long.parseLong(get(key));
        } else {
            return defaultValue;
        }
    }

    /**
     * EL? Resolved? Key ? Property List .
     *
     * @return Resolved Key? Property List
     */
    public List<Property> resolvedProperties() {
        List<Property> properties = new ArrayList<Property>();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            String value = map.get(key);
            logger.debug("Key '{}' :: Value '{}' ==> Resovled Value '{}'", new String[] { key, value, get(key) });
            if (propertyMap.get(key).isExpose()) {
                properties.add(new Property(key, get(key), propertyMap.get(key).isExpose()));
            }
        }
        return properties;
    }

    ////////////////////////////////////////////////////
    // Spring Framework Setter Injection
    ////////////////////////////////////////////////////

    /**
     * {@link org.openflamingo.model.site.Configuration}? .
     *
     * @param configuration {@link org.openflamingo.model.site.Configuration}
     */
    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }
}