yooli88

MVNForumConfig

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/MVNForumConfig.java,v 1.75 2005/01/29 19:29:26 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.75 $
 * $Date: 2005/01/29 19:29:26 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * 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 any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 * @author: Igor Manic   imanic@users.sourceforge.net
 */
package com.mvnforum;

import java.io.*;
import java.util.Locale;

import com.mvnforum.db.DAOFactory;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import net.myvietnam.mvncore.configuration.DOM4JConfiguration;
import net.myvietnam.mvncore.db.DBUtils;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.info.DatabaseInfo;
import net.myvietnam.mvncore.security.FloodControl;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MVNForumConfig {

    private static Log log = LogFactory.getLog(MVNForumConfig.class);

    private MVNForumConfig() {
    }

    private static final String OPTION_FILE_NAME = "mvnforum.xml";

    private static boolean m_shouldRun = true;
    public static boolean isShouldRun() {
        return m_shouldRun;
    }

    private static String  m_reason = "Normal System";
    public static String getReason() {
        return m_reason;
    }

    private static boolean m_showUserArea = true;
    public static boolean getShouldShowUserArea() {
        return m_showUserArea;
    }
    public static void setShouldShowUserArea(boolean showUserArea) {
        m_showUserArea = showUserArea;
    }

    /**
     * This method could be use to stop run the forum in some condition.
     * Some use could be a page that immediately stop the forum for security.
     * Other usage is to check to run on some environment such as Servlet 2.3 or later
     *
     * @param shouldRun boolean the new shouldRun
     * @param reason String the reason of the action, this reason will
     *               be shown in the error page
     */
    public static void setShouldRun(boolean shouldRun, String reason) {
        m_shouldRun = shouldRun;
        m_reason = reason;
    }

    private static boolean m_guestUserInDatabase = false;
    public static boolean isGuestUserInDatabase() {
        return m_guestUserInDatabase;
    }

    private static String tempDir = "";
    private static String searchPostIndexDir = "";
    private static String searchCompanyIndexDir = "";
    private static String searchMemberIndexDir = "";
    private static String attachmentDir = "";
    private static String pmAttachmentDir = "";
    private static String backupDir = "";
    private static String templateDir = "";
    private static String logDir = "";
    private static String avatarDir = "";

    private static String LOG_FILE= "";

    private static void setMVNForumHome(String home) {
        // now check the read/write permission by writing a temp file
        try {
            // always create a dir, if the dir already exitsted, nothing happen
            FileUtil.createDirs(home, true);

            String tempFilename = home + File.separatorChar + "mvnforum_tempfile.tmp";
            File tempFile = new File(tempFilename);
            if (log.isDebugEnabled()) {
                log.debug("Temp file = " + tempFilename);
                log.debug("Absolute filename of temp file = " + tempFile.getAbsolutePath());
            }

            FileOutputStream fos = new FileOutputStream(tempFilename);
            fos.write(tempFilename.getBytes());
            fos.close();

            tempFile.delete();

            // now create the dirs if not exitst
            tempDir = MVNFORUM_HOME + File.separatorChar + "temp";
            FileUtil.createDirs(tempDir, true);

            searchPostIndexDir = MVNFORUM_HOME + File.separatorChar + "search" + File.separatorChar + "post";
            FileUtil.createDirs(searchPostIndexDir, true);

            searchCompanyIndexDir = MVNFORUM_HOME + File.separatorChar + "search" + File.separatorChar + "company";
            FileUtil.createDirs(searchCompanyIndexDir, true);

            searchMemberIndexDir = MVNFORUM_HOME + File.separatorChar + "search" + File.separatorChar + "member";
            FileUtil.createDirs(searchMemberIndexDir, true);

            attachmentDir = MVNFORUM_HOME + File.separatorChar + "attachment";
            FileUtil.createDirs(attachmentDir, true);

            pmAttachmentDir = MVNFORUM_HOME + File.separatorChar + "pm_attachment";
            FileUtil.createDirs(pmAttachmentDir, true);

            backupDir = MVNFORUM_HOME + File.separatorChar + "backup";
            FileUtil.createDirs(backupDir, true);

            // this dir is created as a recommended folder to store the log files
            logDir = MVNFORUM_HOME + File.separatorChar + "log";
            FileUtil.createDirs(logDir, true);

            avatarDir = MVNFORUM_HOME + File.separatorChar + "memberavatars";
            FileUtil.createDirs(avatarDir, true);

            // this dir is created as a recommended folder to store the template files
            templateDir = MVNFORUM_HOME + File.separatorChar + "template";
            FileUtil.createDirs(templateDir, true);

            // now check the database
            DatabaseInfo databaseInfo = new DatabaseInfo();
            if (databaseInfo.getErrorMessage() != null) {
                log.fatal("Cannot get database connection. Please correct it first.");
                m_shouldRun = false;
                m_reason = "Check your database configuration. Detail : " + databaseInfo.getErrorMessage();
            } else if (DAOFactory.getMemberDAO().getNumberOfMembers() == 0) { // check if no member
                log.fatal("There are no members in database. Please correct it first.");
                m_shouldRun = false;
                m_reason = "There are no members in database.";
            }
            // call this method will print the database type to logger with level INFO
            DBUtils.getDatabaseType();
        } catch (IOException ex) {
            log.fatal("Cannot setup the mvnForumHome folder. Please correct it first.", ex);
            m_shouldRun = false;
            m_reason = "Check your mvnForumHome. Detail : " + ex.getMessage();
        } catch (DatabaseException dbe) {
            log.fatal("Error while access database. Please correct it first.", dbe);
            m_shouldRun = false;
            m_reason = "Error while access database. Detail : " + dbe.getMessage();
        } catch (AssertionException ae) {
            // should never happen, maybe in the future we should remove the Assertion in getNumberOfBeans
            log.fatal("Assertion error. Please correct it first.", ae);
            m_shouldRun = false;
            m_reason = "Assertion error. Detail : " + ae.getMessage();
        }
    }

    private static Configuration freeMarkerConfiguration;
    public static Configuration getFreeMarkerConfiguration() {
        return freeMarkerConfiguration;
    }

    private static String MVNFORUM_HOME     = "mvnForumHome";
    public static String getMVNForumHome() {
        return MVNFORUM_HOME;
    }
    public static String getTempDir() {
        return tempDir;
    }

    public static String getSearchPostIndexDir() {
        return searchPostIndexDir;
    }
    public static String getSearchCompanyIndexDir() {
        return searchCompanyIndexDir;
    }

    public static String getSearchMemberIndexDir() {
        return searchMemberIndexDir;
    }

    public static String getAttachmentDir() {
        return attachmentDir;
    }
    public static String getPmAttachmentDir() {
        return pmAttachmentDir;
    }

    public static String getTemplateDir() {
        return templateDir;
    }

    public static String getBackupDir() {
        return backupDir;
    }

    public static String getLogDir() {
        return logDir;
    }

    public static String getLogFile() {
        return LOG_FILE;
    }

    public static String getAvatarDir() {
        return avatarDir;
    }

    private static String WEBMASTER_EMAIL  = "youremail@yourdomain.com";
    public static String getWebMasterEmail() {
        return WEBMASTER_EMAIL;
    }

    private static String LOGO_URL         = "http://www.mvnForum.com";
    public static String getLogoUrl() {
        return LOGO_URL;
    }

    private static String[] SUPPORTED_LOCALE_NAMES = new String[0];
    private static Locale[] SUPPORTED_LOCALES = new Locale[0];
    public static String[] getSupportedLocaleNames() {
        return SUPPORTED_LOCALE_NAMES;
    }
    public static Locale[] getSupportedLocales() {
        return SUPPORTED_LOCALES;
    }

    private static String DEFAULT_LOCALE_NAME = "en";
    private static Locale DEFAULT_LOCALE      = null;
    public static String getDefaultLocaleName() {
        return DEFAULT_LOCALE_NAME;
    }
    public static Locale getDefaultLocale() {
        return DEFAULT_LOCALE;
    }

    /**
     * Default username of a virtual Guest user. Will be overriden with the data
     * from the database, if it exists (for the Guest user).
     * Admin can give him a name he wants, like "Guest", "Anonymous", "Surfer",
     * or even use a language different than English.
     */
    private static String DEFAULT_GUEST_NAME   = "Guest";
    public static String getDefaultGuestName() {
        return DEFAULT_GUEST_NAME;
    }

    private static int DEFAULT_GUEST_TIMEZONE = 0;
    public static int getDefaultGuestTimeZone() {
        return DEFAULT_GUEST_TIMEZONE;
    }

    //public final static boolean PRINT_STACK_TRACE = false;

    /**
     * By default, mvnForum disable passwordless authentication
     * If you want to authenticate user from realm or customized methods,
     * then set the variable to true (AT YOUR OWN RISK, although I have not
     * found any security issues until now)
     */
    private static boolean ENABLE_PASSWORDLESS_AUTH = false;
    public static boolean getEnablePasswordlessAuth() {
        return ENABLE_PASSWORDLESS_AUTH;
    }

    private static boolean REQUIRE_ACTIVATION = false;
    public static boolean getRequireActivation() {
        return REQUIRE_ACTIVATION;
    }

    private static boolean ENABLE_LOGIN_INFO_IN_COOKIE = true;
    public static boolean getEnableLoginInfoInCookie() {
        return ENABLE_LOGIN_INFO_IN_COOKIE;
    }

    private static boolean ENABLE_LOGIN_INFO_IN_SESSION = true;
    public static boolean getEnableLoginInfoInSession() {
        return ENABLE_LOGIN_INFO_IN_SESSION;
    }

    private static boolean ENABLE_LOGIN_INFO_IN_REALM = false;
    public static boolean getEnableLoginInfoInRealm() {
        return ENABLE_LOGIN_INFO_IN_REALM;
    }

    private static boolean ENABLE_LOGIN_INFO_IN_CUSTOMIZATION = false;
    public static boolean getEnableLoginInfoInCustomization() {
        return ENABLE_LOGIN_INFO_IN_CUSTOMIZATION;
    }

    private static boolean ENABLE_LOGIN = true;
    public static boolean getEnableLogin() {
        return ENABLE_LOGIN;
    }

    private static boolean ENABLE_GUEST_VIEW_IMAGE_ATTACHMENT = false;
    public static boolean getEnableGuestViewImageAttachment() {
        return ENABLE_GUEST_VIEW_IMAGE_ATTACHMENT;
    }

    private static boolean ENABLE_NEW_MEMBER = true;
    public static boolean getEnableNewMember() {
        return ENABLE_NEW_MEMBER;
    }

    private static boolean ENABLE_NEW_POST = true;
    public static boolean getEnableNewPost() {
        return ENABLE_NEW_POST;
    }

    private static boolean ENABLE_AVATAR = true;
    public static boolean getEnableAvatar() {
        return ENABLE_AVATAR;
    }

    private static boolean ENABLE_EMOTICON = true;
    public static boolean getEnableEmoticon() {
        return ENABLE_EMOTICON;
    }

    private static boolean ENABLE_RSS = true;
    public static boolean getEnableRSS() {
        return ENABLE_RSS;
    }

    private static boolean ENABLE_SEARCH = true;
    public static boolean getEnableSearch() {
        return ENABLE_SEARCH;
    }
    private static boolean ENABLE_WATCH = true;
    public static boolean getEnableWatch() {
        return ENABLE_WATCH;
    }

    private static boolean ENABLE_ATTACHMENT = true;
    public static boolean getEnableAttachment() {
        return ENABLE_ATTACHMENT;
    }

    private static boolean ENABLE_MESSAGE_ATTACHMENT = true;
    public static boolean getEnableMessageAttachment() {
        return ENABLE_MESSAGE_ATTACHMENT;
    }

    private static boolean ENABLE_MOST_ACTIVE_THREADS = true;
    public static boolean getEnableMostActiveThreads() {
        return ENABLE_MOST_ACTIVE_THREADS;
    }

    private static boolean ENABLE_MOST_ACTIVE_MEMBERS = true;
    public static boolean getEnableMostActiveMembers() {
        return ENABLE_MOST_ACTIVE_MEMBERS;
    }

    private static boolean ENABLE_SITE_STATISTICS_OVERVIEW = true;
    public static boolean getEnableSiteStatisticsOverview() {
        return ENABLE_SITE_STATISTICS_OVERVIEW;
    }

    private static int MAX_ATTACHMENT_SIZE = 1024;
    public static int getMaxAttachmentSize() {
        return MAX_ATTACHMENT_SIZE;
    }

    private static int MAX_MESSAGE_ATTACHMENT_SIZE = 1024;
    public static int getMaxMessageAttachmentSize() {
        return MAX_MESSAGE_ATTACHMENT_SIZE;
    }

    // Default is false, but in KG it should be true
    private static boolean ENABLE_AUTO_FORUM_OWNER = false;
    public static boolean getEnableAutoForumOwner() {
        return ENABLE_AUTO_FORUM_OWNER;
    }

    private static boolean ENABLE_CAPTCHA = false;
    public static boolean getEnableCaptcha() {
        return ENABLE_CAPTCHA;
    }

    private static boolean ENABLE_PORTAL_LIKE_INDEX_PAGE = true;
    public static boolean getEnablePortalLikeIndexPage() {
        return ENABLE_PORTAL_LIKE_INDEX_PAGE;
    }

    private static boolean ENABLE_ADMIN_CAN_CHANGE_PASSWORD = true;
    public static boolean getEnableAdminCanChangePassword() {
        return ENABLE_ADMIN_CAN_CHANGE_PASSWORD;
    }

    private static boolean ENABLE_SHOW_LAST_LOGIN = true;
    public static boolean getEnableShowLastLogin() {
        return ENABLE_SHOW_LAST_LOGIN;
    }

    private static boolean ENABLE_ONLINE_USERS = true;
    public static boolean getEnableOnlineUsers() {
        return ENABLE_ONLINE_USERS;
    }

    private static boolean ENABLE_DUPLICATE_ONLINE_USERS = true;
    public static boolean getEnableDuplicateOnlineUsers() {
        return ENABLE_DUPLICATE_ONLINE_USERS;
    }

    private static boolean ENABLE_INVISIBLE_USERS = true;
    public static boolean getEnableInvisibleUsers() {
        return ENABLE_INVISIBLE_USERS;
    }

    private static boolean ENABLE_LISTMEMBERS = true;
    public static boolean getEnableListMembers() {
        return ENABLE_LISTMEMBERS;
    }

    private static boolean ENABLE_PRIVATE_MESSAGE = true;
    public static boolean getEnablePrivateMessage() {
        return ENABLE_PRIVATE_MESSAGE;
    }

    private static boolean ENABLE_PUBLIC_MESSAGE = true;
    public static boolean getEnablePublicMessage() {
        return ENABLE_PUBLIC_MESSAGE;
    }

    // Default is false, but in Marco it should be true
    private static boolean ENABLE_COMPANY = false;
    public static boolean getEnableCompany() {
        return ENABLE_COMPANY;
    }

    // Default is false, but in Marco it should be true or false
    private static boolean ENABLE_LISTCOMPANIES = false;
    public static boolean getEnableListCompanies() {
        return ENABLE_LISTCOMPANIES;
    }

    private static int EXPIRE_DATE_TUTOR = 90;
    public static int getExpireDateTutor() {
        return EXPIRE_DATE_TUTOR;
    }

    private static int EXPIRE_SOON_DATE = 7;
    public static int getUserTutorExpireSoonDate() {
        return EXPIRE_SOON_DATE;
    }

    /**
     * This is the maximum number of favorite threads that a user can add
     */
    private static int MAX_FAVORITE_THREAD = 128;
    public static int getMaxFavoriteThread() {
        return MAX_FAVORITE_THREAD;
    }

    private static int MAX_PRIVATE_MESSAGE = 128;
    public static int getMaxPrivateMessage() {
        return MAX_PRIVATE_MESSAGE;
    }

    private static int HOT_TOPIC_THRESHOLD = 10;
    public static int getHotTopicThreshold() {
        return HOT_TOPIC_THRESHOLD;
    }

    private static int MAX_POSTS_PER_HOUR = 20;
    public static int getMaxPostsPerHour() {
        return MAX_POSTS_PER_HOUR;
    }

    private static int MAX_MEMBERS_PER_HOUR = 2;
    public static int getMaxMembersPerHour() {
        return MAX_MEMBERS_PER_HOUR;
    }

    private static int MAX_LOGINS_PER_HOUR = 5;
    public static int getMaxLoginsPerHour() {
        return MAX_LOGINS_PER_HOUR;
    }

    private static int MAX_MESSAGES_PER_HOUR = 5;
    public static int getMaxMessagesPerHour() {
        return MAX_MESSAGES_PER_HOUR;
    }

    private static int MAX_ACTIVE_THREADS = 5;
    public static int getMaxActiveThreads() {
        return MAX_ACTIVE_THREADS;
    }

    private static int MAX_ACTIVE_MEMBERS = 5;
    public static int getMaxActiveMembers() {
        return MAX_ACTIVE_MEMBERS;
    }

    /** Do we allow storing backup files on the server? Currently not used. */
    static boolean ENABLE_BACKUP_ON_SERVER = true;
    public static final String BACKUP_FILE_PREFIX = "mvnForum-";
    public static final String BACKUP_FILE_MainXmlFileNameInZip = "IMPORT.xml";
    public static final String BACKUP_FILE_AvatarsDirNameInZip = "AVATARS/"; //must end with '/'
    public static final String BACKUP_FILE_AttachsDirNameInZip = "ATTACHMENTS/"; //must end with '/'

    /**
     * Maximum size of the import file (in bytes) we will allow to be uploaded
     * to server before processing.
     */
    private static int MAX_IMPORT_SIZE = 4096000;
    /**
     * Maximum size of the import file (in bytes) we will allow to be uploaded
     * to server before processing.
     */
    public static int getMaxImportSize() {
        return MAX_IMPORT_SIZE;
    }

    /**
     * Type of import/export file: mvnForum XML.
     * Import only database info, no attachments, message folders, avatars, ...
     */
    public static final int IMPORTEXPORT_TYPE_MVN_XML  = 0;

    /**
     * Type of import file: mvnForum ZIP.
     * Also import attachments, avatars, message folders
     */
    public static final int IMPORTEXPORT_TYPE_MVN_ZIP  = 1;

    /**
     * Type of import file: Jive XML.
     * Using <code>http://www.jivesoftware.com/jive.dtd</code>, xmlversion="1.0".
     */
    public static final int IMPORTEXPORT_TYPE_JIVE_XML = 2;

    /**
     * Output all messages, including errors, important messages and
     * informational/status messages. This constant is used for specifing
     * the level of output in various processess throughout the application.
     */
    public static final int MESSAGE_LEVEL_ALL_MESSAGES       = 0;

    /**
     * Output only error messages and important messages (no
     * informational/status messages). This constant is used for specifing
     * the level of output in various processess throughout the application.
     */
    public static final int MESSAGE_LEVEL_IMPORTANT_MESSAGES = 1;

    /**
     * Output only error messages (no important messages, no
     * informational/status messages). This constant is used for specifing
     * the level of output in various processess throughout the application.
     */
    public static final int MESSAGE_LEVEL_ONLY_ERRORS        = 2;

    public final static long SESSION_DURATION    = 30 * DateUtil.MINUTE;// SHOULD NOT less than 15 minutes

    //public static final boolean DEFAULT_MESSAGE_ENABLE  = true;
    //public static final boolean DEFAULT_MEMBER_ENABLE   = true;

    /**
     * Configurable number of days that a user can edit a post
     */
    private static int MAX_EDIT_DAYS   = 7;
    public static int getMaxEditDays() {
        return MAX_EDIT_DAYS;
    }

    /**
     * Configurable number of days that a user can attach file to a post
     */
    private static int MAX_ATTACH_DAYS = 1;
    public static int getMaxAttachDays() {
        return MAX_ATTACH_DAYS;
    }

    /**
     * Configurable number of days that a user can delete a post
     */
    private static int MAX_DELETE_DAYS = 1;
    public static int getMaxDeleteDays() {
        return MAX_DELETE_DAYS;
    }

    /**
     * Default number of rows (of Guest user) shown per page
     */
    private static int ROWS_PER_PAGE = 10;
    public static int getRowsPerPage() {
        return ROWS_PER_PAGE;
    }

    /**
     * This is the number of rows returned when list threads for RSS
     */
    private static int ROWS_PER_RSS = 15;//RSS 0.91
    public static int getRowsPerRSS() {
        return ROWS_PER_RSS;
    }

    /**
     * This is the default value of watch option
     * @see com.mvnforum.db.WatchBean for the constant values
     */
    private static int DEFAULT_WATCH_OPTION = 0;
    public static int getDefaultWatchOption() {
        return DEFAULT_WATCH_OPTION;
    }

    private static int DEFAULT_MODERATION_OPTION = 0;
    public static int getDefaultModerationOption() {
        return DEFAULT_MODERATION_OPTION;
    }

    private static int MAX_CHARS_IN_SHORT_SUMMARY = 100;
    public static int getMaxCharsInShortSummary() {
        //getMaxCharsInPostInIndex
        return MAX_CHARS_IN_SHORT_SUMMARY;
    }

    private static int MAX_CHARS_IN_LONG_SUMMARY = 1000;
    public static int getMaxCharsInLongSummary() {
        //getMaxCharsInPostInListthreads()
        return MAX_CHARS_IN_LONG_SUMMARY;
    }

    private static int MAX_CHARS_IN_RSS = 500;
    public static int getMaxCharsInRss() {
        return MAX_CHARS_IN_RSS;
    }

    /**
     * This is the number of reply rows returned when addpost (reply to a topic)
     * /forum/addpost
     */
    public static final int ROWS_IN_LAST_REPLIES = 5;

    /*
    private static boolean parseBooleanValue(String propertyValue, boolean defaultValue) {
        String result = "true";
        try {
            result = propertyValue.trim();
            if ((result.equalsIgnoreCase("false")) || (result.equalsIgnoreCase("no"))) {
                return false;
            } else if ((result.equalsIgnoreCase("true")) || (result.equalsIgnoreCase("yes"))) {
                return true;
            } else {
                log.warn("Invalid boolean value in properties file. Should be \"true\", \"false\", \"yes\" or \"no\".");
                return defaultValue;
            }
        } catch (Exception e) {
            log.warn(e.getMessage());
            return defaultValue;
        }
    }

    private static int parseIntValue(String propertyValue, int defaultValue) {
        try {
            return Integer.parseInt(propertyValue.trim());
        } catch (Exception e) {
            log.warn(e.getMessage());
            return defaultValue;
        }
    }
    */
    private static int parseIntSizeValue(String propertyValue, int defaultValue) {
        try {
            String temp = propertyValue.trim();
            if (temp.endsWith("B") || temp.endsWith("b")) {
                temp = temp.substring(0, temp.length() - 1);
            }
            switch (temp.charAt(temp.length()-1)) {
                case 'K': case 'k':
                    //examples (ending 'B' was cut before): "1K", "1KB", "1k", "1kB", "1 K", "1 KB", "1 k", "1 kB"
                    return 1024 * Integer.parseInt(temp.substring(0, temp.length() - 1).trim());
                case 'M': case 'm':
                    //examples (ending 'B' was cut before): "1M", "1MB", "1m", "1mB", "1 M", "1 MB", "1 m", "1 mB"
                    return 1024 * 1024 * Integer.parseInt(temp.substring(0, temp.length() - 1).trim());
                default:
                    //examples (ending 'B' was cut before): "1", "1B", "1 B"
                    return Integer.parseInt(temp.trim());
            }
        } catch (Exception e) {
            log.warn(e.getMessage());
            return defaultValue;
        }
    }

    static {
        try {
            load();

            // now check if Guest user is in database or not
            try {
                DAOFactory.getMemberDAO().getMember_forPublic(MVNForumConstant.MEMBER_ID_OF_GUEST);
                m_guestUserInDatabase = true;
            } catch (ObjectNotFoundException ex) {
                // dont have Guest user in database, just ignore
            } catch (Exception ex) {
                log.info("Error occured when get Guest user.", ex);
            }

            // Load FreeMarker configuration
            freeMarkerConfiguration = Configuration.getDefaultConfiguration();
            FileTemplateLoader templateLoader = new FileTemplateLoader(new File(MVNForumConfig.getTemplateDir()));
            log.debug("Template directory = " + MVNForumConfig.getTemplateDir());
            freeMarkerConfiguration.setTemplateLoader(templateLoader);
        } catch (Exception e) {
            String message = "com.mvnforum.MVNForumConfig: Can't read the configuration file: '" + OPTION_FILE_NAME + "'. Make sure the file is in your CLASSPATH";
            log.error(message, e);
            m_shouldRun = false;
            m_reason = message;
        }
    }

    public static void load() {
        reload();
    }

    public static void reload() {

        String strPathName = FileUtil.getServletClassesPath();
        String configFilename = strPathName + OPTION_FILE_NAME;
        try {
            DOM4JConfiguration conf = new DOM4JConfiguration(new File(configFilename));

            MVNFORUM_HOME       = conf.getString("mvnforumconfig.mvnforum_home", "");
            setMVNForumHome(MVNFORUM_HOME);

            WEBMASTER_EMAIL     = conf.getString("mvnforumconfig.webmaster_email", "");

            LOGO_URL            = conf.getString("mvnforumconfig.logo_url", LOGO_URL);

            LOG_FILE = conf.getString("mvnforumconfig.mvnforum_log", "");

            String supportedLocales = conf.getString("mvnforumconfig.supported_locales", "");
            SUPPORTED_LOCALE_NAMES = StringUtil.getStringArray(supportedLocales, ";");

            SUPPORTED_LOCALES = new Locale[SUPPORTED_LOCALE_NAMES.length];
            for (int i = 0; i < SUPPORTED_LOCALE_NAMES.length; i++) {
                String localeName = SUPPORTED_LOCALE_NAMES[i];
                SUPPORTED_LOCALES[i] = MyUtil.getLocale(localeName);
            }

            try {
                DEFAULT_LOCALE_NAME = conf.getString("mvnforumconfig.default_locale_name", "");
                if (DEFAULT_LOCALE_NAME.length() == 0) {
                    DEFAULT_LOCALE_NAME = "en";
                }
            } catch (Exception ex) {
                log.warn(ex.getMessage());
            }
            DEFAULT_LOCALE = MyUtil.getLocale(DEFAULT_LOCALE_NAME);

            try {
                DEFAULT_GUEST_NAME = conf.getString("mvnforumconfig.default_guest_name", DEFAULT_GUEST_NAME);
            } catch (Exception ex) {
                log.warn(ex.getMessage());
            }

            DEFAULT_GUEST_TIMEZONE = conf.getInt("mvnforumconfig.default_guest_timezone", DEFAULT_GUEST_TIMEZONE);
            if ( Math.abs(DEFAULT_GUEST_TIMEZONE) > 12 )
                DEFAULT_GUEST_TIMEZONE = 0;

            ENABLE_PASSWORDLESS_AUTH = conf.getBoolean("mvnforumconfig.enable_passwordless_auth", false);
            REQUIRE_ACTIVATION = conf.getBoolean("mvnforumconfig.require_activation", false);
            ENABLE_LOGIN_INFO_IN_COOKIE = conf.getBoolean("mvnforumconfig.enable_login_info_in_cookie", true);
            ENABLE_LOGIN_INFO_IN_SESSION = conf.getBoolean("mvnforumconfig.enable_login_info_in_session", true);
            ENABLE_LOGIN_INFO_IN_REALM = conf.getBoolean("mvnforumconfig.enable_login_info_in_realm", false);
            ENABLE_LOGIN_INFO_IN_CUSTOMIZATION = conf.getBoolean("mvnforumconfig.enable_login_info_in_customization", false);

            ENABLE_LOGIN = conf.getBoolean("mvnforumconfig.enable_login", true);
            ENABLE_NEW_MEMBER = conf.getBoolean("mvnforumconfig.enable_new_member", true);
            ENABLE_NEW_POST = conf.getBoolean("mvnforumconfig.enable_new_post", true);
            ENABLE_AVATAR = conf.getBoolean("mvnforumconfig.enable_avatar", true);
            ENABLE_EMOTICON = conf.getBoolean("mvnforumconfig.enable_emoticon", true);
            ENABLE_RSS = conf.getBoolean("mvnforumconfig.enable_rss", true);
            ENABLE_SEARCH = conf.getBoolean("mvnforumconfig.enable_search", true);
            ENABLE_WATCH = conf.getBoolean("mvnforumconfig.enable_watch", true);
            ENABLE_ATTACHMENT = conf.getBoolean("mvnforumconfig.enable_attachment", true);
            ENABLE_MESSAGE_ATTACHMENT = conf.getBoolean("mvnforumconfig.enable_message_attachment", true);
            ENABLE_CAPTCHA = conf.getBoolean("mvnforumconfig.enable_captcha", false);
            ENABLE_PORTAL_LIKE_INDEX_PAGE = conf.getBoolean("mvnforumconfig.enable_portal_like_index_page", true);
            ENABLE_ADMIN_CAN_CHANGE_PASSWORD = conf.getBoolean("mvnforumconfig.enable_admin_can_change_password", true);
            ENABLE_SHOW_LAST_LOGIN = conf.getBoolean("mvnforumconfig.enable_show_last_login", true);
            ENABLE_ONLINE_USERS = conf.getBoolean("mvnforumconfig.enable_online_users", true);
            ENABLE_LISTMEMBERS = conf.getBoolean("mvnforumconfig.enable_listmembers", true);
            ENABLE_DUPLICATE_ONLINE_USERS = conf.getBoolean("mvnforumconfig.enable_duplicate_onlineusers", true);
            ENABLE_INVISIBLE_USERS = conf.getBoolean("mvnforumconfig.enable_invisible_users", true);
            ENABLE_PRIVATE_MESSAGE = conf.getBoolean("mvnforumconfig.enable_private_message", true);
            ENABLE_PUBLIC_MESSAGE = conf.getBoolean("mvnforumconfig.enable_public_message", false);
            ENABLE_GUEST_VIEW_IMAGE_ATTACHMENT = conf.getBoolean("mvnforumconfig.enable_guest_view_image_attachment", false);
            ENABLE_MOST_ACTIVE_THREADS = conf.getBoolean("mvnforumconfig.enable_most_active_threads", true);
            ENABLE_MOST_ACTIVE_MEMBERS = conf.getBoolean("mvnforumconfig.enable_most_active_members", true);
            ENABLE_SITE_STATISTICS_OVERVIEW = conf.getBoolean("mvnforumconfig.enable_site_statistics_overview", false);

            /*
            // Netmama
            ENABLE_COMPANY = conf.getBoolean("mvnforumconfig.enable_company", false);
            ENABLE_LISTCOMPANIES = conf.getBoolean("mvnforumconfig.enable_listcompanies", false);
            EXPIRE_DATE_TUTOR = conf.getInt("mvnforumconfig.expire_date_tutor", 90);
            EXPIRE_SOON_DATE = conf.getInt("mvnforumconfig.expire_soon_date", 7);
            */
            MAX_ATTACHMENT_SIZE = parseIntSizeValue(conf.getString("mvnforumconfig.max_attachment_size"), 1024);
            if (MAX_ATTACHMENT_SIZE < -1) MAX_ATTACHMENT_SIZE = 0;// -1 is a valid value in common-upload, mean no maximum file size
            MAX_MESSAGE_ATTACHMENT_SIZE = parseIntSizeValue(conf.getString("mvnforumconfig.max_message_attachment_size"), 1024);
            if (MAX_MESSAGE_ATTACHMENT_SIZE < -1) MAX_MESSAGE_ATTACHMENT_SIZE = 0;// -1 is a valid value in common-upload, mean no maximum file size

            MAX_FAVORITE_THREAD = conf.getInt("mvnforumconfig.max_favorite_thread", 128);
            if (MAX_FAVORITE_THREAD < 0) MAX_FAVORITE_THREAD = 0;

            MAX_PRIVATE_MESSAGE = conf.getInt("mvnforumconfig.max_private_message", 128);
            if (MAX_PRIVATE_MESSAGE < 0) MAX_PRIVATE_MESSAGE = 0;

            MAX_EDIT_DAYS = conf.getInt("mvnforumconfig.max_edit_days", 7);
            if (MAX_EDIT_DAYS < 0) MAX_EDIT_DAYS = 0;

            MAX_ATTACH_DAYS = conf.getInt("mvnforumconfig.max_attach_days", 1);
            if (MAX_ATTACH_DAYS < 0) MAX_ATTACH_DAYS = 0;

            MAX_DELETE_DAYS = conf.getInt("mvnforumconfig.max_delete_days", 1);
            if (MAX_DELETE_DAYS < 0) MAX_DELETE_DAYS = 0;

            ROWS_PER_PAGE = conf.getInt("mvnforumconfig.rows_per_page", 10);
            if (ROWS_PER_PAGE < 5) ROWS_PER_PAGE = 5;

            ROWS_PER_RSS = conf.getInt("mvnforumconfig.rows_per_rss", 15);
            if (ROWS_PER_RSS < 5) ROWS_PER_RSS = 5;

            HOT_TOPIC_THRESHOLD = conf.getInt("mvnforumconfig.hot_topic_threshold", 10);
            if (HOT_TOPIC_THRESHOLD < 5) HOT_TOPIC_THRESHOLD = 5;

            MAX_POSTS_PER_HOUR = conf.getInt("mvnforumconfig.max_posts_per_hour", MAX_POSTS_PER_HOUR);
            if (MAX_POSTS_PER_HOUR < 0) MAX_POSTS_PER_HOUR = 0;

            MAX_MEMBERS_PER_HOUR = conf.getInt("mvnforumconfig.max_members_per_hour", MAX_MEMBERS_PER_HOUR);
            if (MAX_MEMBERS_PER_HOUR < 0) MAX_MEMBERS_PER_HOUR = 0;

            MAX_LOGINS_PER_HOUR = conf.getInt("mvnforumconfig.max_logins_per_hour", MAX_LOGINS_PER_HOUR);
            if (MAX_LOGINS_PER_HOUR < 0) MAX_LOGINS_PER_HOUR = 0;

            MAX_MESSAGES_PER_HOUR = conf.getInt("mvnforumconfig.max_messages_per_hour", MAX_MESSAGES_PER_HOUR);
            if (MAX_MESSAGES_PER_HOUR < 0) MAX_MESSAGES_PER_HOUR = 0;

            MAX_CHARS_IN_SHORT_SUMMARY = conf.getInt("mvnforumconfig.max_chars_in_short_summary", MAX_CHARS_IN_SHORT_SUMMARY);
            if (MAX_CHARS_IN_SHORT_SUMMARY <= 0) MAX_CHARS_IN_SHORT_SUMMARY = Integer.MAX_VALUE;

            MAX_CHARS_IN_LONG_SUMMARY = conf.getInt("mvnforumconfig.max_chars_in_long_summary", MAX_CHARS_IN_LONG_SUMMARY);
            if (MAX_CHARS_IN_LONG_SUMMARY <= 0) MAX_CHARS_IN_LONG_SUMMARY = Integer.MAX_VALUE;

            MAX_CHARS_IN_RSS = conf.getInt("mvnforumconfig.max_chars_in_rss", MAX_CHARS_IN_RSS);
            if (MAX_CHARS_IN_RSS <= 0) MAX_CHARS_IN_RSS = Integer.MAX_VALUE;

            ENABLE_BACKUP_ON_SERVER = conf.getBoolean("mvnforumconfig.enable_backup_on_server", true);
            MAX_IMPORT_SIZE = parseIntSizeValue(conf.getString("mvnforumconfig.max_import_size", "4096000"), 4096000 );

            DEFAULT_WATCH_OPTION = conf.getInt("mvnforumconfig.default_watch_option", 0);
            if (DEFAULT_WATCH_OPTION < 0 || DEFAULT_WATCH_OPTION > 4) DEFAULT_WATCH_OPTION = 0;

            DEFAULT_MODERATION_OPTION = conf.getInt("mvnforumconfig.default_moderation_option", 0);
            if (DEFAULT_MODERATION_OPTION < 0 || DEFAULT_MODERATION_OPTION > 4) DEFAULT_MODERATION_OPTION = 0;

            FloodControl.setOption(MVNForumGlobal.FLOOD_ID_NEW_MEMBER, MAX_MEMBERS_PER_HOUR);
            FloodControl.setOption(MVNForumGlobal.FLOOD_ID_NEW_POST, MAX_POSTS_PER_HOUR);
            FloodControl.setOption(MVNForumGlobal.FLOOD_ID_LOGIN, MAX_LOGINS_PER_HOUR);
            FloodControl.setOption(MVNForumGlobal.FLOOD_ID_NEW_MESSAGE, MAX_MESSAGES_PER_HOUR);
        } catch (Exception e) {
            // Please note that for security reason, the full path file name is logged
            // to the log file only. And the reason that show on the web should only
            // show the filename only
            String message = "com.mvnforum.MVNForumConfig: Can't read the configuration file: '" + configFilename + "'. Make sure the file is in your CLASSPATH";
            log.error(message, e);
            m_shouldRun = false;
            m_reason = "com.mvnforum.MVNForumConfig: Can't read the configuration file: '" + OPTION_FILE_NAME + "'. Make sure the file is in your CLASSPATH";
        }
    }
}

posted on 2007-09-11 11:31 迷茫在java的世界里 阅读(512) 评论(0)  编辑  收藏 所属分类: mvn学习笔记


只有注册用户登录后才能发表评论。


网站导航: