1 18 19 package org.apache.roller.config; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.roller.RollerException; 24 import org.apache.roller.model.PingTargetManager; 25 import org.apache.roller.model.RollerFactory; 26 import org.apache.roller.pojos.PingTargetData; 27 28 import java.util.*; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 32 36 42 public class PingConfig { 43 private static final Log logger = LogFactory.getLog(PingConfig.class); 44 45 46 static final String MAX_PING_ATTEMPTS_PROP = "pings.maxPingAttempts"; 48 private static final int MAX_PING_ATTEMPTS_DEFAULT = 3; 49 private static final int MAX_PING_ATTEMPTS_MIN = 1; 50 private static final int MAX_PING_ATTEMPTS_MAX = 10; 51 52 private static final String QUEUE_PROCESSING_INTERVAL_PROP = "pings.queueProcessingIntervalMins"; 54 private static final int QUEUE_PROCESSING_INTERVAL_DEFAULT = 5; 55 private static final int QUEUE_PROCESSING_INTERVAL_MIN = 0; 56 private static final int QUEUE_PROCESSING_INTERVAL_MAX = 120; 57 58 private static final String PINGS_LOG_ONLY_PROP = "pings.logOnly"; 60 private static final boolean PINGS_LOG_ONLY_DEFAULT = false; 61 62 private static final String PINGS_DISALLOW_CUSTOM_TARGETS_PROP = "pings.disallowCustomTargets"; 67 private static final boolean PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT = false; 68 69 private static final String PINGS_DISABLE_PING_USAGE_PROP = "pings.disablePingUsage"; 73 private static final boolean PINGS_DISABLE_PING_USAGE_DEFAULT = false; 74 75 private static final String PINGS_SUSPEND_PING_PROCESSING_PROP = "pings.suspendPingProcessing"; 80 81 private static final String PINGS_INITIAL_COMMON_TARGETS_PROP = "pings.initialCommonTargets"; 88 89 90 private static final String PINGS_VARIANT_OPTIONS_PROP = "pings.variantOptions"; 95 private static final Map configuredVariants = new HashMap(); 101 102 103 private PingConfig() { 105 } 106 107 114 public static int getMaxPingAttempts() { 115 return getIntegerProperty(MAX_PING_ATTEMPTS_PROP, MAX_PING_ATTEMPTS_DEFAULT, MAX_PING_ATTEMPTS_MIN, MAX_PING_ATTEMPTS_MAX); 116 } 117 118 123 public static int getQueueProcessingIntervalMins() { 124 return getIntegerProperty(QUEUE_PROCESSING_INTERVAL_PROP, QUEUE_PROCESSING_INTERVAL_DEFAULT, QUEUE_PROCESSING_INTERVAL_MIN, QUEUE_PROCESSING_INTERVAL_MAX); 125 } 126 127 128 134 public static boolean getLogPingsOnly() { 135 return getBooleanProperty(PINGS_LOG_ONLY_PROP, PINGS_LOG_ONLY_DEFAULT); 136 } 137 138 144 public static boolean getDisallowCustomTargets() { 145 return getBooleanProperty(PINGS_DISALLOW_CUSTOM_TARGETS_PROP, PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT); 146 } 147 148 155 public static boolean getDisablePingUsage() { 156 return getBooleanProperty(PINGS_DISABLE_PING_USAGE_PROP, PINGS_DISABLE_PING_USAGE_DEFAULT); 157 } 158 159 166 public static boolean getSuspendPingProcessing() { 167 return RollerRuntimeConfig.getBooleanProperty(PINGS_SUSPEND_PING_PROCESSING_PROP); 168 } 169 170 private static final Pattern NESTED_BRACE_PAIR = Pattern.compile("\\{\\{(.*?)\\}\\{(.*?)\\}\\}"); 174 175 185 public static void initializeCommonTargets() throws RollerException { 186 String configuredVal = RollerConfig.getProperty(PINGS_INITIAL_COMMON_TARGETS_PROP); 187 if (configuredVal == null || configuredVal.trim().length() == 0) { 188 if (logger.isDebugEnabled()) { 189 logger.debug("No (or empty) value of " + PINGS_INITIAL_COMMON_TARGETS_PROP + " present in the configuration. Skipping initialization of commmon targets."); 190 } 191 return; 192 } 193 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 194 if (!pingTargetMgr.getCommonPingTargets().isEmpty()) { 195 if (logger.isDebugEnabled()) { 196 logger.debug("Some common ping targets are present in the database already. Skipping initialization."); 197 } 198 return; 199 } 200 201 String [] configuredTargets = configuredVal.trim().split(","); 202 for (int i = 0; i < configuredTargets.length; i++) { 203 String thisTarget = configuredTargets[i].trim(); 205 if (thisTarget.length() == 0) continue; 207 Matcher m = NESTED_BRACE_PAIR.matcher(thisTarget); 209 if (m.matches() && m.groupCount() == 2) { 210 String name = m.group(1).trim(); 211 String url = m.group(2).trim(); 212 logger.info("Creating common ping target '" + name + "' from configuration properties."); 213 PingTargetData pingTarget = new PingTargetData(null, name, url, null, false); 214 pingTargetMgr.savePingTarget(pingTarget); 215 } else { 216 logger.error("Unable to parse configured initial ping target '" + thisTarget + "'. Skipping this target. Check your setting of the property " + PINGS_INITIAL_COMMON_TARGETS_PROP); 217 } 218 } 219 } 220 221 224 public static void initializePingVariants() { 225 String configuredVal = RollerConfig.getProperty(PINGS_VARIANT_OPTIONS_PROP); 226 if (configuredVal == null || configuredVal.trim().length() == 0) { 227 if (logger.isDebugEnabled()) { 228 logger.debug("No (or empty) value of " + PINGS_VARIANT_OPTIONS_PROP + " present in the configuration. Skipping initialization of ping variants."); 229 } 230 return; 231 } 232 String [] variants = configuredVal.trim().split(","); 233 for (int i = 0; i < variants.length; i++) { 234 String thisVariant = variants[i].trim(); 235 if (thisVariant.length() == 0) continue; 236 Matcher m = NESTED_BRACE_PAIR.matcher(thisVariant); 237 if (m.matches() && m.groupCount() == 2) { 238 String url = m.group(1).trim(); 239 String optionsList = m.group(2).trim(); 240 Set variantOptions = new HashSet(); 241 String [] options = optionsList.split(","); 242 for (int j = 0; j < options.length; j++) { 243 String option = options[j].trim().toLowerCase(); 244 if (option.length() > 0) { 245 variantOptions.add(option); 246 } 247 } 248 if (!variantOptions.isEmpty()) { 249 configuredVariants.put(url, variantOptions); 250 } else { 251 logger.warn("Ping variant entry for url '" + url + "' has an empty variant options list. Ignored."); 252 } 253 } else { 254 logger.error("Unable to parse configured ping variant '" + thisVariant + "'. Skipping this variant. Check your setting of the property " + PINGS_VARIANT_OPTIONS_PROP); 255 } 256 } 257 } 258 259 266 public static Set getVariantOptions(String pingTargetUrl) { 267 Set variantOptions = (Set) configuredVariants.get(pingTargetUrl); 268 if (variantOptions == null) { 269 variantOptions = Collections.EMPTY_SET; 270 } 271 return variantOptions; 272 } 273 274 275 277 287 private static int getIntegerProperty(String propName, int defaultValue, int min, int max) { 288 String configuredVal = RollerConfig.getProperty(propName); 289 if (configuredVal == null) { 290 if (logger.isDebugEnabled()) { 291 logger.debug("PingConfig property '" + propName + "' is not present in the configuration. Using default value: " + defaultValue); 292 } 293 return defaultValue; 294 } 295 296 int val; 297 try { 298 val = Integer.parseInt(configuredVal); 299 } catch (NumberFormatException ex) { 300 logger.error("ERROR: PingConfig property '" + propName + "' is not an integer value. Using default value: " + defaultValue); 301 return defaultValue; 302 } 303 304 if (val < min || val > max) { 305 logger.error("ERROR: PingConfig property '" + propName + "' is outside the required range (" + min + ", " + max + "). Using default value: " + defaultValue); 306 return defaultValue; 307 } 308 309 return val; 310 } 311 312 319 private static boolean getBooleanProperty(String propName, boolean defaultValue) { 320 String configuredVal = RollerConfig.getProperty(propName); 321 if (configuredVal == null) { 322 if (logger.isDebugEnabled()) { 323 logger.debug("PingConfig property '" + propName + "' is not present in the configuration. Using default value: " + defaultValue); 324 } 325 return defaultValue; 326 } 327 return Boolean.valueOf(configuredVal).booleanValue(); 328 } 329 330 331 } 332 | Popular Tags |