KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > config > PingConfig


1 /*
2  * Copyright (c) 2005
3  * Anil R. Gangolli. All rights reserved.
4  *
5  * Distributed with the Roller Weblogger Project under the terms of the Roller Software
6  * License
7  */

8
9 package org.roller.config;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.roller.RollerException;
14 import org.roller.model.PingTargetManager;
15 import org.roller.model.RollerFactory;
16 import org.roller.pojos.PingTargetData;
17
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 // This may need to move to a different package, but it seems appropriate here in the current structure.
22
// Previous placement in the presentation.pings package introduced the undesirable dependency of the
23
// business package on the presentation package.
24

25 /**
26  * Thin wrapper around RollerConfig and RollerRuntimeConfig for centralizing access to the many configurable
27  * settings for pings.
28  */

29 public class PingConfig
30 {
31     private static final Log logger = LogFactory.getLog(PingConfig.class);
32
33
34     // Inhibit construction
35
private PingConfig()
36     {
37     }
38
39     // Config property for maximim ping attempts.
40
static final String JavaDoc MAX_PING_ATTEMPTS_PROP = "pings.maxPingAttempts";
41     private static final int MAX_PING_ATTEMPTS_DEFAULT = 3;
42     private static final int MAX_PING_ATTEMPTS_MIN = 1;
43     private static final int MAX_PING_ATTEMPTS_MAX = 10;
44
45     // Config property for queue processing interval
46
private static final String JavaDoc QUEUE_PROCESSING_INTERVAL_PROP = "pings.queueProcessingIntervalMins";
47     private static final int QUEUE_PROCESSING_INTERVAL_DEFAULT = 5;
48     private static final int QUEUE_PROCESSING_INTERVAL_MIN = 0;
49     private static final int QUEUE_PROCESSING_INTERVAL_MAX = 120;
50
51     // PingConfig property for logging pings (not actually performing them). Used for debugging.
52
private static final String JavaDoc PINGS_LOG_ONLY_PROP = "pings.logOnly";
53     private static final boolean PINGS_LOG_ONLY_DEFAULT = false;
54
55     // PingConfig property for controlling whether or not to allow custom ping targets
56
// ("Weblog:Custom Ping Targets" page and actions). If absent, this defaults to false.
57
// with the enabledProperty behavior in editor-menu.xml.
58
// NOTE: If this property name is changed, editor-menu.xml must also be adjusted.
59
private static final String JavaDoc PINGS_DISALLOW_CUSTOM_TARGETS_PROP = "pings.disallowCustomTargets";
60     private static final boolean PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT = false;
61
62     // PingConfig property for controlling whether or not to allow usage of pings
63
// ("Weblog:Pings" page and actions). If absent, this defaults to false
64
// NOTE: If this property name is changed, editor-menu.xml must also be adjusted.
65
private static final String JavaDoc PINGS_DISABLE_PING_USAGE_PROP = "pings.disablePingUsage";
66     private static final boolean PINGS_DISABLE_PING_USAGE_DEFAULT = false;
67
68     // PingConfig property for controlling suspending the processing of pings. If true,
69
// new auto ping requests are not queued, any existing queued requests are not processed,
70
// and sending a manual ping results in a message saying pings have been disabled.
71
// NOTE: This is a "runtime" property settable on the Admin:PingConfig page, default is false.
72
private static final String JavaDoc PINGS_SUSPEND_PING_PROCESSING_PROP = "pings.suspendPingProcessing";
73
74     // PingConfig property determining the initial common ping targets. If the list of common
75
// ping targets is empty on startup, the value of this property is used to populate initial values.
76
// The value takes the form of comma-separated ping targets where each ping target is specified in
77
// the form {{name}{pingurl}}. If an administrator wants to disable this initialization, in order to
78
// maintain an empty list of common targets, the administrator can disable the initialization by
79
// commenting out this property in the config file.
80
private static final String JavaDoc PINGS_INITIAL_COMMON_TARGETS_PROP = "pings.initialCommonTargets";
81
82     /**
83      * Get the maximum number of ping attempts that should be made for each ping queue entry before we give up. If we
84      * get apparently transient failures while trying to perform the ping, the entry is requeued for processing on later
85      * passes through the queue until this number of attempts has been reached.
86      *
87      * @return the configured (or default) maximum number of ping attempts
88      */

89     public static int getMaxPingAttempts()
90     {
91         return getIntegerProperty(MAX_PING_ATTEMPTS_PROP, MAX_PING_ATTEMPTS_DEFAULT,
92             MAX_PING_ATTEMPTS_MIN, MAX_PING_ATTEMPTS_MAX);
93     }
94
95     /**
96      * Get the ping queue processing interval in minutes.
97      *
98      * @return the configured (or default) queue processing interval in minutes.
99      */

100     public static int getQueueProcessingIntervalMins()
101     {
102         return getIntegerProperty(QUEUE_PROCESSING_INTERVAL_PROP, QUEUE_PROCESSING_INTERVAL_DEFAULT,
103             QUEUE_PROCESSING_INTERVAL_MIN, QUEUE_PROCESSING_INTERVAL_MAX);
104     }
105
106
107     /**
108      * Get the logs only setting. Get configuration value determining whether pings are to be logged only (not sent).
109      * This configuration setting is used for development and debugging.
110      *
111      * @return the configured (or default) value of the logs only setting.
112      */

113     public static boolean getLogPingsOnly()
114     {
115         return getBooleanProperty(PINGS_LOG_ONLY_PROP, PINGS_LOG_ONLY_DEFAULT);
116     }
117
118     /**
119      * Determine whether the configuration disallows custom ping targets. If this is true, users are not allowed to
120      * create or edit custom ping targets, and any auto ping configs that use them are ignored.
121      *
122      * @return the configured (or default) value of the "disallow custom targets" setting.
123      */

124     public static boolean getDisallowCustomTargets()
125     {
126         return getBooleanProperty(PINGS_DISALLOW_CUSTOM_TARGETS_PROP, PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT);
127     }
128
129     /**
130      * Determine whether the configuration disables ping usage (configuration of auto pings and sending of manual
131      * pings). If this is true, all auto ping configus are removed at startup, the Weblog:Pings UI and the associated
132      * actions are disabled.
133      *
134      * @return the configured (or default) value of the enable ping usage setting.
135      */

136     public static boolean getDisablePingUsage()
137     {
138         return getBooleanProperty(PINGS_DISABLE_PING_USAGE_PROP, PINGS_DISABLE_PING_USAGE_DEFAULT);
139     }
140
141     /**
142      * Determine whether ping processing is suspended. If this is true, new auto ping requests are not
143      * queued, any existing queued requests are not processed, and sending a manual ping results in a message saying
144      * pings have been disabled.
145      *
146      * @return the configured (or default) value of the suspend ping processing setting.
147      */

148     public static boolean getSuspendPingProcessing()
149     {
150         return RollerRuntimeConfig.getBooleanProperty(PINGS_SUSPEND_PING_PROCESSING_PROP);
151     }
152
153     // Each initial commmon ping target is specified in the format {{name}{url}}
154
private static final Pattern JavaDoc PING_TARGET_SPEC = Pattern.compile("\\{\\{(.*?)\\}\\{(.*?)\\}\\}");
155
156     /**
157      * Initialize the common ping targets from the configuration properties. If the current list of common ping targets
158      * is empty, and the <code>PINGS_INITIAL_COMMON_TARGETS_PROP</code> property is present in the configuration then,
159      * this method will use that value to initialize the common targets. This is called on each server startup.
160      * <p/>
161      * Note: this is expected to be called during initialization with transaction demarcation being handled by the
162      * caller.
163      *
164      * @see org.roller.presentation.RollerContext#contextInitialized(javax.servlet.ServletContextEvent)
165      */

166     public static void initializeCommonTargets() throws RollerException
167     {
168         String JavaDoc configuredVal = RollerConfig.getProperty(PINGS_INITIAL_COMMON_TARGETS_PROP);
169         if (configuredVal == null || configuredVal.trim().length() == 0)
170         {
171             if (logger.isDebugEnabled()) logger.debug("No (or empty) value of " + PINGS_INITIAL_COMMON_TARGETS_PROP + " present in the configuration. Skipping initialization of commmon targets.");
172             return;
173         }
174         PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager();
175         if (!pingTargetMgr.getCommonPingTargets().isEmpty())
176         {
177             if (logger.isDebugEnabled()) logger.debug("Some common ping targets are present in the database already. Skipping initialization.");
178             return;
179         }
180
181         String JavaDoc[] configuredTargets = configuredVal.trim().split(",");
182         for (int i = 0; i < configuredTargets.length; i++)
183         {
184             // Trim space around the target spec
185
String JavaDoc thisTarget = configuredTargets[i].trim();
186             // skip empty ones
187
if (thisTarget.length() == 0) continue;
188             // parse the ith target and store it
189
Matcher JavaDoc m = PING_TARGET_SPEC.matcher(configuredTargets[i].trim());
190             if (m.matches() && m.groupCount() == 2)
191             {
192                 String JavaDoc name = m.group(1);
193                 String JavaDoc url = m.group(2);
194                 logger.info("Creating common ping target '" + name + "' from configuration properties.");
195                 PingTargetData pingTarget = pingTargetMgr.createCommonPingTarget(name, url);
196                 pingTargetMgr.storePingTarget(pingTarget);
197             }
198             else
199             {
200                 logger.error("Unable to parse configured initial ping target '" + configuredTargets[i] +
201                     "'. Skipping this target. Check your setting of the property " + PINGS_INITIAL_COMMON_TARGETS_PROP);
202             }
203         }
204     }
205
206
207     // TODO: Refactor functionality below to RollerConfig?
208

209     /**
210      * Get the value of an integer configuration property.
211      *
212      * @param propName the property name
213      * @param defaultValue the default value if the property is not present
214      * @param min the minimum allowed value
215      * @param max the maximum allowed value
216      * @return the value as an integer; the default value if no configured value is present or if the configured value
217      * is out of the specified range.
218      */

219     private static int getIntegerProperty(String JavaDoc propName, int defaultValue, int min, int max)
220     {
221         String JavaDoc configuredVal = RollerConfig.getProperty(propName);
222         if (configuredVal == null)
223         {
224             if (logger.isDebugEnabled()) logger.debug("PingConfig property '" + propName + "' is not present in the configuration. Using default value: " + defaultValue);
225             return defaultValue;
226         }
227
228         int val;
229         try
230         {
231             val = Integer.parseInt(configuredVal);
232         }
233         catch (NumberFormatException JavaDoc ex)
234         {
235             logger.error("ERROR: PingConfig property '" + propName + "' is not an integer value. Using default value: " + defaultValue);
236             return defaultValue;
237         }
238
239         if (val < min || val > max)
240         {
241             logger.error("ERROR: PingConfig property '" + propName + "' is outside the required range (" + min + ", " + max + "). Using default value: " + defaultValue);
242             return defaultValue;
243         }
244
245         return val;
246     }
247
248     /**
249      * Get the value of a boolean property with specified default.
250      *
251      * @param propName the property name
252      * @param defaultValue the default value if the property is not present
253      * @return the configured value or the default if it the configured value is not present.
254      */

255     private static boolean getBooleanProperty(String JavaDoc propName, boolean defaultValue)
256     {
257         String JavaDoc configuredVal = RollerConfig.getProperty(propName);
258         if (configuredVal == null)
259         {
260             if (logger.isDebugEnabled()) logger.debug("PingConfig property '" + propName + "' is not present in the configuration. Using default value: " + defaultValue);
261             return defaultValue;
262         }
263         return Boolean.valueOf(configuredVal).booleanValue();
264     }
265
266
267 }
268
Popular Tags