com.jk.framework.application.config.CommonsConfigManager.java Source code

Java tutorial

Introduction

Here is the source code for com.jk.framework.application.config.CommonsConfigManager.java

Source

/*
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * 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.jk.framework.application.config;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import com.jk.framework.util.CollectionUtil;
import com.jk.framework.util.GeneralUtility;
import com.jk.logging.JKLogger;
import com.jk.logging.JKLoggerFactory;
import com.lowagie.text.pdf.codec.Base64;

/**
 * The Class CommonsConfigManager.
 *
 * @author Jalal Kiswani
 */
public class CommonsConfigManager {

    /**
     * Decode.
     *
     * @param string
     *            the string
     * @return the string
     */
    public static String decode(final String string) {
        return new String(Base64.decode(string));
    }

    /**
     * Encode.
     *
     * @param string
     *            the string
     * @return the string
     */
    public static String encode(final String string) {
        return Base64.encodeBytes(string.getBytes());
    }

    /**
     * The main method.
     *
     * @param args
     *            the arguments
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void main(final String[] args) throws IOException {
        final CommonsConfigManager config = new CommonsConfigManager();
        config.setProperty("Host", "192.168.1.1");
        config.setProperty("Port", "3306");
        config.setProperty("User", "root", true);
        config.setProperty("Password", "Irbid123", true);
        config.storeToXML("d:/config.xml");
        System.out.println("Done");
    }

    private Properties prop = new Properties();

    // static Base64 encDec = Base64. new Base64();
    private String fileName;

    private JKLogger logger = JKLoggerFactory.getLogger(getClass());

    /**
     * Instantiates a new commons config manager.
     */
    public CommonsConfigManager() {
    }

    /**
     * Instantiates a new commons config manager.
     *
     * @param inputStream
     *            the input stream
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public CommonsConfigManager(final InputStream inputStream) throws IOException {
        load(inputStream);
    }

    /**
     * Instantiates a new commons config manager.
     *
     * @param fileName
     *            the file name
     * @throws FileNotFoundException
     *             the file not found exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public CommonsConfigManager(final String fileName) throws FileNotFoundException, IOException {
        this(new FileInputStream(fileName));
        this.fileName = fileName;
    }

    /**
     *
     * @param name
     * @return
     */
    private String fixKey(final String name) {
        return CollectionUtil.fixPropertyKey(name);
    }

    /**
     * Gets the file name.
     *
     * @return the file name
     */
    public String getFileName() {
        return this.fileName;
    }

    /**
     * Gets the int property.
     *
     * @param string
     *            the string
     * @param def
     *            the def
     * @return the int property
     */
    public int getIntProperty(final String string, final int def) {
        final String s = getProperty(string, def + "");
        return Integer.parseInt(s);
    }

    /**
     * Gets the properties.
     *
     * @return the properties
     */
    public Properties getProperties() {
        return this.prop;
    }

    /**
     * Gets the property.
     *
     * @param prop
     *            the prop
     * @return the property
     */
    public String getProperty(final String prop) {
        return getProperty(prop, null);
    }

    /**
     * Gets the property.
     *
     * @param name
     *            the name
     * @param defaultValue
     *            the default value
     * @return the property
     */
    public String getProperty(final String name, final String defaultValue) {
        return getProperty(CollectionUtil.fixPropertyKey(name), defaultValue, false);
    }

    /**
     * Gets the property.
     *
     * @param name
     *            the name
     * @param defaultValue
     *            the default value
     * @param encoded
     *            the encoded
     * @return the property
     */
    public String getProperty(final String name, final String defaultValue, final boolean encoded) {
        String value = this.prop.getProperty(fixKey(name));
        if (value != null) {
            if (encoded && isReadEncoded()) {
                final byte[] decode = Base64.decode(value);
                if (decode != null) {
                    value = new String(decode);
                } else {
                    // the encoded failed , return the value it self without
                    // encoded
                    // value=null;
                }
            }
            return value;
        }
        return defaultValue;
    }

    /**
     * Gets the property as boolean.
     *
     * @param property
     *            the property
     * @param defaultValue
     *            the default value
     * @return the property as boolean
     */
    public boolean getPropertyAsBoolean(final String property, final boolean defaultValue) {
        String prop = getProperty(property);
        if (prop == null) {
            return defaultValue;
        }
        prop = prop.trim().toLowerCase();
        return prop.equals("true") || prop.equals("1");
    }

    /**
     * Gets the property as integer.
     *
     * @param name
     *            the name
     * @param defaultValue
     *            the default value
     * @return the property as integer
     */
    public int getPropertyAsInteger(final String name, final int defaultValue) {
        final String property = getProperty(name, defaultValue + "");
        if (property == null) {
            return 0;
        }
        return Integer.parseInt(property);
    }

    /**
     * Gets the string.
     *
     * @param name
     *            the name
     * @return the string
     */
    public String getString(final String name) {
        return getProperty(name, null);
    }

    /**
     * Gets the string.
     *
     * @param name
     *            the name
     * @param defaultValue
     *            the default value
     * @return the string
     */
    public String getString(final String name, final String defaultValue) {
        return getProperty(name, defaultValue);
    }

    /**
     *
     * @return
     */
    protected boolean isReadEncoded() {
        return Boolean.parseBoolean(this.prop.getProperty("encoded", "true"));
    }

    /**
     * Load.
     *
     * @param inStream
     *            the in stream
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public void load(final InputStream inStream) throws IOException {
        final BufferedInputStream in = new BufferedInputStream(inStream);
        try {
            in.mark(0);
            this.prop.loadFromXML(in);
            // Note : the keys is not fixed if in xml format

        } catch (InvalidPropertiesFormatException e) {
            // not xml , try to load normal properties file
            in.reset();
            this.prop = GeneralUtility.readPropertyStream(in);
        }
        CollectionUtil.fixPropertiesKeys(this.prop);
        System.getProperties().putAll(this.prop);
        logger.debug(System.getProperties().toString().replaceAll(",", "\n"));
    }

    /**
     * Load.
     *
     * @param fileName
     *            the file name
     * @throws FileNotFoundException
     *             the file not found exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public void load(final String fileName) throws FileNotFoundException, IOException {
        load(GeneralUtility.getFileInputStream(fileName));
    }

    /**
     * Sets the property.
     *
     * @param name
     *            the name
     * @param value
     *            the value
     */
    public void setProperty(final String name, final String value) {
        setProperty(name, value, false);
    }

    /**
     * Sets the property.
     *
     * @param name
     *            the name
     * @param value
     *            the value
     * @param encoded
     *            the encoded
     */
    public void setProperty(final String name, String value, final boolean encoded) {
        if (encoded) {
            value = Base64.encodeBytes(value.getBytes());
        }
        this.prop.setProperty(fixKey(name), value);
    }

    /**
     * Store to XML.
     *
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public void storeToXML() throws IOException {
        if (this.fileName != null) {
            storeToXML(this.fileName);
        } else {
            throw new RuntimeException("File name has not been set");
        }
    }

    /**
     * Store to XML.
     *
     * @param os
     *            the os
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public void storeToXML(final OutputStream os) throws IOException {
        this.prop.storeToXML(os, "JK Configuration File");
    }

    /**
     * Store to XML.
     *
     * @param fileName
     *            the file name
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public void storeToXML(final String fileName) throws IOException {
        storeToXML(new FileOutputStream(fileName));
    }

    // //////////////////////////////////////////////////////////////////////////////////
    // check whether the keys available configuration is available
    /**
     * Validate properties.
     *
     * @param keys
     *            the keys
     * @return the string
     */
    // This will be used for mandatory fields
    public String validateProperties(final String... keys) {
        final StringBuffer buf = new StringBuffer();
        for (final String key : keys) {
            final String value = getString(key);
            if (value == null || value.trim().equals("")) {
                buf.append(key + "=\n");
            }
        }
        return buf.length() == 0 ? null : buf.toString();
    }

}