pingconfig.java

来自「这个weblogging 设计得比较精巧」· Java 代码 · 共 268 行

JAVA
268
字号
/* * Copyright (c) 2005 * Anil R. Gangolli. All rights reserved. * * Distributed with the Roller Weblogger Project under the terms of the Roller Software * License */package org.roller.config;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.roller.RollerException;import org.roller.model.PingTargetManager;import org.roller.model.RollerFactory;import org.roller.pojos.PingTargetData;import java.util.regex.Matcher;import java.util.regex.Pattern;// This may need to move to a different package, but it seems appropriate here in the current structure.// Previous placement in the presentation.pings package introduced the undesirable dependency of the// business package on the presentation package./** * Thin wrapper around RollerConfig and RollerRuntimeConfig for centralizing access to the many configurable * settings for pings. */public class PingConfig{    private static final Log logger = LogFactory.getLog(PingConfig.class);    // Inhibit construction    private PingConfig()    {    }    // Config property for maximim ping attempts.    static final String MAX_PING_ATTEMPTS_PROP = "pings.maxPingAttempts";    private static final int MAX_PING_ATTEMPTS_DEFAULT = 3;    private static final int MAX_PING_ATTEMPTS_MIN = 1;    private static final int MAX_PING_ATTEMPTS_MAX = 10;    // Config property for queue processing interval    private static final String QUEUE_PROCESSING_INTERVAL_PROP = "pings.queueProcessingIntervalMins";    private static final int QUEUE_PROCESSING_INTERVAL_DEFAULT = 5;    private static final int QUEUE_PROCESSING_INTERVAL_MIN = 0;    private static final int QUEUE_PROCESSING_INTERVAL_MAX = 120;    // PingConfig property for logging pings (not actually performing them).  Used for debugging.    private static final String PINGS_LOG_ONLY_PROP = "pings.logOnly";    private static final boolean PINGS_LOG_ONLY_DEFAULT = false;    // PingConfig property for controlling whether or not to allow custom ping targets    // ("Weblog:Custom Ping Targets" page and actions).  If absent, this defaults to false.    // with the enabledProperty behavior in editor-menu.xml.    // NOTE: If this property name is changed, editor-menu.xml must also be adjusted.    private static final String PINGS_DISALLOW_CUSTOM_TARGETS_PROP = "pings.disallowCustomTargets";    private static final boolean PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT = false;    // PingConfig property for controlling whether or not to allow usage of pings    // ("Weblog:Pings" page and actions).  If absent, this defaults to false    // NOTE: If this property name is changed, editor-menu.xml must also be adjusted.    private static final String PINGS_DISABLE_PING_USAGE_PROP = "pings.disablePingUsage";    private static final boolean PINGS_DISABLE_PING_USAGE_DEFAULT = false;    // PingConfig property for controlling suspending the processing of pings.  If true,    // new auto ping requests are not queued, any existing queued requests are not processed,    // and sending a manual ping results in a  message saying pings have been disabled.    // NOTE: This is a "runtime" property settable on the Admin:PingConfig page, default is false.    private static final String PINGS_SUSPEND_PING_PROCESSING_PROP = "pings.suspendPingProcessing";    // PingConfig property determining the initial common ping targets.  If the list of common    // ping targets is empty on startup, the value of this property is used to populate initial values.    // The value takes the form of comma-separated ping targets where each ping target is specified in    // the form {{name}{pingurl}}.  If an administrator wants to disable this initialization, in order to    // maintain an empty list of common targets, the administrator can disable the initialization by    // commenting out this property in the config file.    private static final String PINGS_INITIAL_COMMON_TARGETS_PROP = "pings.initialCommonTargets";    /**     * Get the maximum number of ping attempts that should be made for each ping queue entry before we give up. If we     * get apparently transient failures while trying to perform the ping, the entry is requeued for processing on later     * passes through the queue until this number of attempts has been reached.     *     * @return the configured (or default) maximum number of ping attempts     */    public static int getMaxPingAttempts()    {        return getIntegerProperty(MAX_PING_ATTEMPTS_PROP, MAX_PING_ATTEMPTS_DEFAULT,            MAX_PING_ATTEMPTS_MIN, MAX_PING_ATTEMPTS_MAX);    }    /**     * Get the ping queue processing interval in minutes.     *     * @return the configured (or default) queue processing interval in minutes.     */    public static int getQueueProcessingIntervalMins()    {        return getIntegerProperty(QUEUE_PROCESSING_INTERVAL_PROP, QUEUE_PROCESSING_INTERVAL_DEFAULT,            QUEUE_PROCESSING_INTERVAL_MIN, QUEUE_PROCESSING_INTERVAL_MAX);    }    /**     * Get the logs only setting.  Get configuration value determining whether pings are to be logged only (not sent).     * This configuration setting is used for development and debugging.     *     * @return the configured (or default) value of the logs only setting.     */    public static boolean getLogPingsOnly()    {        return getBooleanProperty(PINGS_LOG_ONLY_PROP, PINGS_LOG_ONLY_DEFAULT);    }    /**     * Determine whether the configuration disallows custom ping targets.  If this is true, users are not allowed to     * create or edit custom ping targets, and any auto ping configs that use them are ignored.     *     * @return the configured (or default) value of the "disallow custom targets" setting.     */    public static boolean getDisallowCustomTargets()    {        return getBooleanProperty(PINGS_DISALLOW_CUSTOM_TARGETS_PROP, PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT);    }    /**     * Determine whether the configuration disables ping usage (configuration of auto pings and sending of manual     * pings).  If this is true, all auto ping configus are removed at startup, the Weblog:Pings UI and the associated     * actions are disabled.     *     * @return the configured (or default) value of the enable ping usage setting.     */    public static boolean getDisablePingUsage()    {        return getBooleanProperty(PINGS_DISABLE_PING_USAGE_PROP, PINGS_DISABLE_PING_USAGE_DEFAULT);    }    /**     * Determine whether ping processing is suspended.  If this is true, new auto ping requests are not     * queued, any existing queued requests are not processed, and sending a manual ping results in a message saying     * pings have been disabled.     *     * @return the configured (or default) value of the suspend ping processing setting.     */    public static boolean getSuspendPingProcessing()    {        return RollerRuntimeConfig.getBooleanProperty(PINGS_SUSPEND_PING_PROCESSING_PROP);    }    // Each initial commmon ping target is specified in the format {{name}{url}}    private static final Pattern PING_TARGET_SPEC = Pattern.compile("\\{\\{(.*?)\\}\\{(.*?)\\}\\}");    /**     * Initialize the common ping targets from the configuration properties. If the current list of common ping targets     * is empty, and the <code>PINGS_INITIAL_COMMON_TARGETS_PROP</code> property is present in the configuration then,     * this method will use that value to initialize the common targets.  This is called on each server startup.     * <p/>     * Note: this is expected to be called during initialization  with transaction demarcation being handled by the     * caller.     *     * @see org.roller.presentation.RollerContext#contextInitialized(javax.servlet.ServletContextEvent)     */    public static void initializeCommonTargets() throws RollerException    {        String configuredVal = RollerConfig.getProperty(PINGS_INITIAL_COMMON_TARGETS_PROP);        if (configuredVal == null || configuredVal.trim().length() == 0)        {            if (logger.isDebugEnabled()) logger.debug("No (or empty) value of " + PINGS_INITIAL_COMMON_TARGETS_PROP + " present in the configuration.  Skipping initialization of commmon targets.");            return;        }        PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager();        if (!pingTargetMgr.getCommonPingTargets().isEmpty())        {            if (logger.isDebugEnabled()) logger.debug("Some common ping targets are present in the database already.  Skipping initialization.");            return;        }        String[] configuredTargets = configuredVal.trim().split(",");        for (int i = 0; i < configuredTargets.length; i++)        {            // Trim space around the target spec            String thisTarget = configuredTargets[i].trim();            // skip empty ones            if (thisTarget.length() == 0) continue;            // parse the ith target and store it            Matcher m = PING_TARGET_SPEC.matcher(configuredTargets[i].trim());            if (m.matches() && m.groupCount() == 2)            {                String name = m.group(1);                String url = m.group(2);                logger.info("Creating common ping target '" + name + "' from configuration properties.");                PingTargetData pingTarget = pingTargetMgr.createCommonPingTarget(name, url);                pingTargetMgr.storePingTarget(pingTarget);            }            else            {                logger.error("Unable to parse configured initial ping target '" + configuredTargets[i] +                    "'. Skipping this target. Check your setting of the property " + PINGS_INITIAL_COMMON_TARGETS_PROP);            }        }    }    // TODO: Refactor functionality below to RollerConfig?    /**     * Get the value of an integer configuration property.     *     * @param propName     the property name     * @param defaultValue the default value if the property is not present     * @param min          the minimum allowed value     * @param max          the maximum allowed value     * @return the value as an integer; the default value if no configured value is present or if the configured value     *         is out of the specified range.     */    private static int getIntegerProperty(String propName, int defaultValue, int min, int max)    {        String configuredVal = RollerConfig.getProperty(propName);        if (configuredVal == null)        {            if (logger.isDebugEnabled()) logger.debug("PingConfig property '" + propName + "' is not present in the configuration.  Using default value: " + defaultValue);            return defaultValue;        }        int val;        try        {            val = Integer.parseInt(configuredVal);        }        catch (NumberFormatException ex)        {            logger.error("ERROR: PingConfig property '" + propName + "' is not an integer value.  Using default value: " + defaultValue);            return defaultValue;        }        if (val < min || val > max)        {            logger.error("ERROR: PingConfig property '" + propName + "' is outside the required range (" + min + ", " + max + ").  Using default value: " + defaultValue);            return defaultValue;        }        return val;    }    /**     * Get the value of a boolean property with specified default.     *     * @param propName     the property name     * @param defaultValue the default value if the property is not present     * @return the configured value or the default if it the configured value is not present.     */    private static boolean getBooleanProperty(String propName, boolean defaultValue)    {        String configuredVal = RollerConfig.getProperty(propName);        if (configuredVal == null)        {            if (logger.isDebugEnabled()) logger.debug("PingConfig property '" + propName + "' is not present in the configuration.  Using default value: " + defaultValue);            return defaultValue;        }        return Boolean.valueOf(configuredVal).booleanValue();    }}

⌨️ 快捷键说明

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