com.athena.chameleon.common.utils.PropertyUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.chameleon.common.utils.PropertyUtil.java

Source

/*
 * Copyright 2012 the original author or authors.
 *
 * 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.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2012. 8. 20.      First Draft.
 */
package com.athena.chameleon.common.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * filtering.properties ? config ? ? ??   key?  value ?     ?.
 * <b>Spring util:property  ?  ? </b>
 * </pre>
 * 
 * @author Sang-cheon Park
 * @version 1.0
 */
public class PropertyUtil {

    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);

    private static Properties[] properties = new Properties[0];
    private static Exception exception;

    static {
        try {
            Properties prop = new Properties();
            prop.load(PropertyUtil.class.getResourceAsStream("/filtering.properties"));
            properties = (Properties[]) ArrayUtils.add(properties, prop);

            File file = new File(PropertyUtil.class.getResource("/config/").getFile());
            File[] files = null;

            if (file != null && file.isDirectory()) {
                files = file.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".properties");
                    }
                });

                for (File f : files) {
                    prop = new Properties();
                    prop.load(new BufferedInputStream(new FileInputStream(f)));

                    properties = (Properties[]) ArrayUtils.add(properties, prop);
                }
            }
        } catch (FileNotFoundException e) {
            logger.error("Property file(s) are not exist.", e);
            exception = e;
        } catch (IOException e) {
            logger.error("Property file(s) or directory cannot read.", e);
            exception = e;
        } catch (Exception e) {
            logger.error("Unhandled exception has occurred.", e);
            exception = e;
        }
    }

    /**
     * <pre>
     *  key?  property ? .
     * ? key     ? .
     * </pre>
     *
     * @param key
     * @return value
     * @throws Exception ? ?? ?? load   ?
     */
    public static String getProperty(String key) throws Exception {
        if (exception != null) {
            throw exception;
        }

        String value = null;
        for (Properties p : properties) {
            if ((value = p.getProperty(key)) != null) {
                break;
            }
        }

        return value;
    }//end of getProperty()

}//end of PropertyUtil.java