1 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 ; 19 import java.util.regex.Pattern ; 20 21 25 29 public class PingConfig 30 { 31 private static final Log logger = LogFactory.getLog(PingConfig.class); 32 33 34 private PingConfig() 36 { 37 } 38 39 static final String 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 private static final String 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 private static final String PINGS_LOG_ONLY_PROP = "pings.logOnly"; 53 private static final boolean PINGS_LOG_ONLY_DEFAULT = false; 54 55 private static final String PINGS_DISALLOW_CUSTOM_TARGETS_PROP = "pings.disallowCustomTargets"; 60 private static final boolean PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT = false; 61 62 private static final String PINGS_DISABLE_PING_USAGE_PROP = "pings.disablePingUsage"; 66 private static final boolean PINGS_DISABLE_PING_USAGE_DEFAULT = false; 67 68 private static final String PINGS_SUSPEND_PING_PROCESSING_PROP = "pings.suspendPingProcessing"; 73 74 private static final String PINGS_INITIAL_COMMON_TARGETS_PROP = "pings.initialCommonTargets"; 81 82 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 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 113 public static boolean getLogPingsOnly() 114 { 115 return getBooleanProperty(PINGS_LOG_ONLY_PROP, PINGS_LOG_ONLY_DEFAULT); 116 } 117 118 124 public static boolean getDisallowCustomTargets() 125 { 126 return getBooleanProperty(PINGS_DISALLOW_CUSTOM_TARGETS_PROP, PINGS_DISALLOW_CUSTOM_TARGETS_DEFAULT); 127 } 128 129 136 public static boolean getDisablePingUsage() 137 { 138 return getBooleanProperty(PINGS_DISABLE_PING_USAGE_PROP, PINGS_DISABLE_PING_USAGE_DEFAULT); 139 } 140 141 148 public static boolean getSuspendPingProcessing() 149 { 150 return RollerRuntimeConfig.getBooleanProperty(PINGS_SUSPEND_PING_PROCESSING_PROP); 151 } 152 153 private static final Pattern PING_TARGET_SPEC = Pattern.compile("\\{\\{(.*?)\\}\\{(.*?)\\}\\}"); 155 156 166 public static void initializeCommonTargets() throws RollerException 167 { 168 String 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 [] configuredTargets = configuredVal.trim().split(","); 182 for (int i = 0; i < configuredTargets.length; i++) 183 { 184 String thisTarget = configuredTargets[i].trim(); 186 if (thisTarget.length() == 0) continue; 188 Matcher m = PING_TARGET_SPEC.matcher(configuredTargets[i].trim()); 190 if (m.matches() && m.groupCount() == 2) 191 { 192 String name = m.group(1); 193 String 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 209 219 private static int getIntegerProperty(String propName, int defaultValue, int min, int max) 220 { 221 String 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 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 255 private static boolean getBooleanProperty(String propName, boolean defaultValue) 256 { 257 String 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 |