Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.AbstractXmlConfig

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationKey;
import org.apache.commons.configuration.XMLConfiguration;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;

import java.io.*;
import java.util.List;

/**
* @author Kurt Sung
*/
public class AbstractXmlConfig {
    private XMLConfiguration config;
    protected String name;
    public static final String ROOT_ELEMENT = "ChaiDB";
    protected static final char PROPERTY_DELIMITER = ConfigurationKey.PROPERTY_DELIMITER;
    protected static final String ESCAPED_DELIMITER = ConfigurationKey.ESCAPED_DELIMITER;

    public AbstractXmlConfig(String filename) throws ChaiDBException {
        this.name = filename;
        if (!(new File(filename).exists())) {
            createNewFile(filename, ROOT_ELEMENT);
        }
        try {
            config = new XMLConfiguration(filename);
        } catch (ConfigurationException e) {
            createNewFile(filename, ROOT_ELEMENT);
            try {
                config = new XMLConfiguration(filename);
            } catch (ConfigurationException e1) {
                throw new ChaiDBException(ErrorCode.CONFIG_FILE_READ_ERROR, e);
            }
        }
        config.setEncoding("UTF-8");
    }

    protected void createNewFile(String filename, String rootElement) throws ChaiDBException {
        try {
            DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
            os.writeBytes(AbstractControlFile.XML_HEADER + "\n");
            os.writeBytes("<" + rootElement + "/>");
            os.close();
        } catch (IOException e) {
            throw new ChaiDBException(ErrorCode.CONFIG_FILE_CREATE_ERROR, e);

        }
    }

    public synchronized void setProperty(String name, String value) throws ChaiDBException {
        config.reload();
        config.setProperty(name, value);
        save();
    }

    protected synchronized void save() throws ChaiDBException {
        try {
            config.save();
        } catch (ConfigurationException e) {
            throw new ChaiDBException(ErrorCode.CONFIG_FILE_WRITE_ERROR, e);
        }
    }

    protected synchronized void resetProperty(String name, String value) throws ChaiDBException {
        config.reload();
        config.clearProperty(name);
        config.addProperty(name, value);
        save();
    }

    protected String getProperty(String name) {
        return (String) config.getProperty(name);
    }

    protected void addProperty(String name, Object value) {
        config.addProperty(name, value);
    }

    protected List getList(String path) {
        return config.getList(path);
    }

    protected int getMaxIndex(String path) {
        return config.getMaxIndex(path);
    }

    protected void clearTree(String path) {
        config.clearTree(path);
    }

    protected synchronized void reload() {
        config.reload();
    }
}
TOP

Related Classes of org.chaidb.db.helper.AbstractXmlConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.