All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.itool.utils.PropertiesManager Maven / Gradle / Ivy

/*
 * Copyright 2018 jw362j.
 *
 * 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 com.mycomm.itool.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 *
 * @author jw362j
 */
public class PropertiesManager {

    private static final Map vars = new HashMap();

    public static void LoadAndRefreshAllProperties(String propertiesDir) {
        if (propertiesDir == null || propertiesDir.length() == 0) {
            throw new IllegalArgumentException("propertiesDir is null ");
        }
        File confFolder = new File(propertiesDir);
        if (!confFolder.exists()) {
            throw new IllegalArgumentException("propertiesDir does not exists,give up! ");
        }
        if (!confFolder.isDirectory()) {
            throw new IllegalArgumentException("propertiesDir is not a directory,give up! ");
        }

        File[] fs = confFolder.listFiles();
        if (fs == null || fs.length == 0) {
            throw new IllegalArgumentException("there are no propertie files in:" + propertiesDir);
        }
        InputStream fis = null;
        for (File f : fs) {
            if (!f.getAbsolutePath().endsWith("properties")) {
                continue;
            }

            try {
                Properties properties = new Properties();
                fis = new FileInputStream(f);
                properties.load(fis);
                mergeVars(properties);
            } catch (FileNotFoundException e) {
                throw new IllegalArgumentException("FileNotFoundException:" + e.getMessage());
            } catch (IOException e) {
                throw new IllegalArgumentException("IOException:" + e.getMessage());
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException ex) {
                    throw new IllegalArgumentException("IOException:" + ex.getMessage());
                }
            }
        }
    }
    
     public static Map getProperty() {
         return vars;
     }

    private static void mergeVars(Properties ps) {
        Set keys = ps.stringPropertyNames();
        for (String mykey : keys) {
            if (mykey != null && (!"".equals(mykey)) && (ps.get(mykey) != null)) {
                vars.put(mykey, ps.get(mykey).toString());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy