egovframework.rte.fdl.property.impl.EgovPropertyServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.property.impl.EgovPropertyServiceImpl.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed 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 egovframework.rte.fdl.property.impl;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.commons.collections.ExtendedProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert;

import egovframework.rte.fdl.cmmn.exception.FdlException;
import egovframework.rte.fdl.property.EgovPropertyService;

/**
 * Property ?  ?
 * <p>
 * <b>NOTE</b>: ?   ?? ?  /?? 
 * ??  .
 * @author   
 * @since 2009.02.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.02.01             ?
 * 
 * </pre>
 */
public class EgovPropertyServiceImpl implements EgovPropertyService, ApplicationContextAware, InitializingBean,
        DisposableBean, ResourceLoaderAware {

    private ExtendedProperties egovProperties = null;
    private ResourceLoader resourceLoader = null;

    private MessageSource messageSource;
    private Set extFileName;
    private Map properties;

    /**
     * boolean ? ?  
     * @param name
     *        ?
     * @return boolean ? 
     */
    public boolean getBoolean(String name) {
        return getConfiguration().getBoolean(name);
    }

    /**
     * boolean ? ?  (?? ?)
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return boolean ? 
     */
    public boolean getBoolean(String name, boolean def) {
        return getConfiguration().getBoolean(name, def);
    }

    /**
     * double ? ?  
     * @param name
     *        ?
     * @return double ? 
     */
    public double getDouble(String name) {
        return getConfiguration().getDouble(name);
    }

    /**
     * double ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return double ? 
     */
    public double getDouble(String name, double def) {
        return getConfiguration().getDouble(name, def);
    }

    /**
     * float ? ?  
     * @param name
     *        ?
     * @return Float ? 
     */
    public float getFloat(String name) {
        return getConfiguration().getFloat(name);
    }

    /**
     * float ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return float ? 
     */
    public float getFloat(String name, float def) {
        return getConfiguration().getFloat(name, def);
    }

    /**
     * int ? ?  
     * @param name
     *        ?
     * @return int ? 
     */
    public int getInt(String name) {
        return getConfiguration().getInt(name);
    }

    /**
     * int ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return int ? 
     */
    public int getInt(String name, int def) {
        return getConfiguration().getInt(name, def);
    }

    /**
     * ?  ? ?
     * @return Key  Iterator
     */
    public Iterator getKeys() {
        return getConfiguration().getKeys();
    }

    /**
     * prefix ?  ? ?
     * @param prefix
     *        prefix
     * @return prefix? ? ?
     */
    public Iterator getKeys(String prefix) {
        // getConfiguration().values();
        return getConfiguration().getKeys(prefix);
    }

    /**
     * long ? ?  
     * @param name
     *        ?
     * @return long ? 
     */
    public long getLong(String name) {
        return getConfiguration().getLong(name);
    }

    /**
     * long ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return long ? 
     */
    public long getLong(String name, long def) {
        return getConfiguration().getLong(name, def);
    }

    /**
     * String ? ?  
     * @param name
     *        ?
     * @return String ? 
     */
    public String getString(String name) {
        return getConfiguration().getString(name);
    }

    /**
     * String ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return String ? 
     */
    public String getString(String name, String def) {
        return getConfiguration().getString(name, def);
    }

    /**
     * String[] ? ?  
     * @param name
     *        ?
     * @return String[] ? 
     */
    public String[] getStringArray(String name) {
        return getConfiguration().getStringArray(name);
    }

    /**
     * Vector ? ?  
     * @param name
     *        ?
     * @return Vector ? 
     */
    public Vector getVector(String name) {
        return getConfiguration().getVector(name);
    }

    /**
     * Vector ? ?  
     * @param name
     *        ?
     * @param def
     *        ? 
     * @return Vector ? 
     */
    public Vector getVector(String name, Vector def) {
        return getConfiguration().getVector(name, def);
    }

    /**
     *  / ? 
     * @return Vector ? 
     */
    public Collection getAllKeyValue() {
        return getConfiguration().values();
    }

    /**
     * egovProperties 
     * @return Properties of requested Service.
     */
    private ExtendedProperties getConfiguration() {
        return egovProperties;
    }

    /**
     * resource  refresh
     */
    public void refreshPropertyFiles() throws FdlException {

        String fileName = null;

        try {

            Iterator it = extFileName.iterator();

            while (it != null && it.hasNext()) {
                // Get element
                Object element = it.next();
                String enc = null;

                if (element instanceof Map) {
                    Map ele = (Map) element;
                    enc = (String) ele.get("encoding");
                    fileName = (String) ele.get("filename");
                } else {
                    fileName = (String) element;
                }
                loadPropertyResources(fileName, enc);
            }

        } catch (Exception e) {
            if (EgovPropertyService.LOGGER.isErrorEnabled()) {
                EgovPropertyService.LOGGER.error(messageSource.getMessage("error.properties.refresh.files",
                        new String[] { fileName }, Locale.getDefault()));
                EgovPropertyService.LOGGER.error(messageSource.getMessage("error.properties.refresh.files.reason",
                        new String[] {}, Locale.getDefault()));
            }
            throw new FdlException("error.properties.refresh.files", new String[] { fileName }, e);

        }
    }

    /**
     * Bean    ?  Property 
     * @throws Exception
     *         fail to initialize
     */
    public void afterPropertiesSet() throws FdlException {
        try {

            egovProperties = new ExtendedProperties();

            // ?? ???
            if (extFileName != null) {
                refreshPropertyFiles();
            }

            Iterator it = properties.entrySet().iterator();
            while (it.hasNext()) {

                Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();

                if (EgovPropertyService.LOGGER.isDebugEnabled()) {
                    EgovPropertyService.LOGGER.debug(messageSource.getMessage("debug.properties.property",
                            new String[] { key, value }, Locale.getDefault()));
                }

                if (key == null || key.equals("")) {
                    if (EgovPropertyService.LOGGER.isErrorEnabled())
                        EgovPropertyService.LOGGER.error(messageSource.getMessage(
                                "error.properties.check.essential", new String[] {}, Locale.getDefault()));
                    throw new FdlException(messageSource, "error.properties.check.essential", null);
                }

                egovProperties.put(key, value);
            }

        } catch (Exception e) {
            if (e instanceof FdlException)
                throw (FdlException) e;
            else {
                if (EgovPropertyService.LOGGER.isErrorEnabled())
                    EgovPropertyService.LOGGER.error(messageSource.getMessage("error.properties.initialize.reason",
                            new String[] {}, Locale.getDefault()));
                throw new FdlException(messageSource, "error.properties.initialize", e);
            }
        }
    }

    /**
     * extFileName?   Attribute ?
     * @param extFileName
     */
    public void setExtFileName(Set extFileName) {
        this.extFileName = extFileName;
    }

    /**
     * properties   Attribute ?
     * @param properties
     */
    public void setProperties(Map properties) {
        this.properties = properties;
    }

    /**
     *  
     */
    public void destroy() {
        egovProperties = null;
    }

    /**
     *  ? 
     */
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * ApplicationContext 
     * @param applicationContext
     *        to be set by container
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.messageSource = (MessageSource) applicationContext.getBean("messageSource");
    }

    /**
     * ?  resources  
     * @param location
     *        ?
     * @param encoding
     *        Encoding 
     * @throws Exception
     */
    private void loadPropertyResources(String location, String encoding) throws Exception {

        if (resourceLoader instanceof ResourcePatternResolver) {
            try {
                Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);

                loadPropertyLoop(resources, encoding);
            } catch (IOException ex) {
                throw new BeanDefinitionStoreException(
                        "Could not resolve Properties resource pattern [" + location + "]", ex);
            }
        } else {

            Resource resource = resourceLoader.getResource(location);
            loadPropertyRes(resource, encoding);
        }

    }

    /**
     *  ?    LOOP 
     * @param resources
     *        
     * @param encoding
     *        ?
     * @throws Exception
     */
    private void loadPropertyLoop(Resource[] resources, String encoding) throws Exception {
        Assert.notNull(resources, "Resource array must not be null");
        for (int i = 0; i < resources.length; i++) {
            loadPropertyRes(resources[i], encoding);
        }
    }

    /**
     * ?  ? egovProperties? 
     * @param resources
     *        
     * @param encoding
     *        ?
     * @throws Exception
     */
    private void loadPropertyRes(Resource resource, String encoding) throws Exception {
        if (EgovPropertyService.LOGGER.isDebugEnabled()) {
            EgovPropertyService.LOGGER.debug(messageSource.getMessage("debug.properties.filename",
                    new String[] { resource.getFilename(), encoding }, Locale.getDefault()));
        }
        ExtendedProperties egovProperty = new ExtendedProperties();
        egovProperty.load(resource.getInputStream(), encoding);
        egovProperties.combine(egovProperty);
    }
}