⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurator.java

📁 jGossip是一个简单而功能强大的Java论坛软件(消息板)
💻 JAVA
字号:
/*
 * $$Id: Configurator.java,v 1.16 2004/05/30 20:03:57 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is JGossip forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on Oct 29, 2003
 *
  */
package org.jresearch.gossip.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jresearch.gossip.dao.drivers.DbDriver;
import org.jresearch.gossip.exception.ConfiguratorException;

import java.io.IOException;
import java.io.InputStream;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.Properties;

import javax.servlet.ServletContext;

import javax.sql.DataSource;


/**
 * DOCUMENT ME!
 *
 * @author dbelov
 */
public class Configurator {
    private static Configurator ourInstance;
    private static Log log = LogFactory.getLog(Configurator.class);
    private Properties props;
    private DataSource dataSource;
    private DbDriver dbDriver;

    private Configurator() {
        this.props = new Properties();
        this.dbDriver = DbDriver.getInstance();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public synchronized static Configurator getInstance() {
        if (ourInstance == null) {
            ourInstance = new Configurator();
        }

        return ourInstance;
    }

    /**
     * DOCUMENT ME!
     *
     * @param key DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws ConfiguratorException DOCUMENT ME!
     */
    public String get(String key) throws ConfiguratorException {
        if (this.props.getProperty(key) == null) {
            throw new ConfiguratorException("Parameter by key=" + key +
                " not found");
        }

        return this.props.getProperty(key);
    }

    /**
     * DOCUMENT ME!
     *
     * @param servletContext DOCUMENT ME!
     *
     * @throws SQLException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public void load(ServletContext servletContext)
        throws SQLException, IOException {
        if (log.isInfoEnabled()) {
            log.info("try to load app.properties");
        }

        InputStream is = servletContext.getResourceAsStream(
                "/WEB-INF/classes/org/jresearch/gossip/resources/app.properties");

        try {
            this.props.load(is);
            is.close();
        } catch (IOException e1) {
            if (log.isErrorEnabled()) {
                log.error("Loading of jGossip's configuration failed " +
                    e1.getMessage());
            }

            throw e1;
        }

        if (log.isInfoEnabled()) {
            log.info("try to load configuration from Data Base");
        }

        Connection conn = this.dataSource.getConnection();

        PreparedStatement st = conn.prepareStatement(dbDriver.getQueries()
                                                             .getForumQueries()
                                                             .getSql_GET_CONSTANTS());
        Statement st2 = conn.createStatement();
        ResultSet rs = null;

        try {
            rs = (ResultSet) st.executeQuery();

            while (rs.next()) {
                if (log.isInfoEnabled()) {
                    log.info(rs.getString("c_name") + " is loaded");
                }

                this.props.put(rs.getString("c_name"), rs.getString("c_value"));
            }

            // refresh all sessions(drop all entries)
            st2.executeUpdate(dbDriver.getQueries().getForumQueries()
                                      .getSql_DELETE_ALL_ENTRIES());
        } catch (SQLException e) {
            if (log.isErrorEnabled()) {
                log.error("Loading of jGossip's configuration failed " +
                    e.getMessage());
            }

            throw e;
        } finally {
            if (rs != null) {
                rs.close();
            }

            st2.close();
            st.close();
            conn.close();
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param servletContext DOCUMENT ME!
     *
     * @throws SQLException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public void reload(ServletContext servletContext)
        throws SQLException, IOException {
        this.props = new Properties();
        load(servletContext);
    }

    /**
     * DOCUMENT ME!
     *
     * @param source
     */
    public void setDataSource(DataSource source) {
        dataSource = source;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -