1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.BufferedReader ; 40 import java.io.File ; 41 import java.io.FileNotFoundException ; 42 import java.io.FileReader ; 43 import java.io.IOException ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 48 import net.sourceforge.cruisecontrol.util.OSEnvironment; 49 50 import org.apache.log4j.Logger; 51 import org.jdom.Attribute; 52 import org.jdom.Element; 53 import org.jdom.output.XMLOutputter; 54 55 59 public class ProjectXMLHelper implements ProjectHelper { 60 private static final Logger LOG = Logger.getLogger(ProjectXMLHelper.class); 62 63 private Map projectProperties; 64 private PluginRegistry projectPlugins; 65 66 public ProjectXMLHelper() { 67 this.projectProperties = new HashMap (); 68 this.projectPlugins = PluginRegistry.createRegistry(PluginRegistry.loadDefaultPluginRegistry()); 69 } 70 71 public ProjectXMLHelper(Map projectProperties, PluginRegistry projectPlugins) { 72 this.projectProperties = projectProperties; 73 this.projectPlugins = projectPlugins; 74 } 75 76 79 public Object configurePlugin(Element pluginElement, boolean skipChildElements) 80 throws CruiseControlException { 81 String name = pluginElement.getName(); 82 PluginXMLHelper pluginHelper = new PluginXMLHelper(this); 83 String pluginName = pluginElement.getName(); 84 85 LOG.debug("configuring plugin " + pluginElement.getName() + " skip " + skipChildElements); 86 87 if (projectPlugins.isPluginRegistered(pluginName)) { 88 Object pluginInstance = getConfiguredPlugin(pluginHelper, pluginElement.getName()); 89 if (pluginInstance != null) { return pluginHelper.configure(pluginElement, pluginInstance, skipChildElements); 91 } 92 return pluginHelper.configure(pluginElement, projectPlugins.getPluginClass(pluginName), skipChildElements); 93 } else { 94 throw new CruiseControlException("Unknown plugin for: <" + name + ">"); 95 } 96 } 97 98 99 108 Object getConfiguredPlugin(PluginXMLHelper pluginHelper, String pluginName) throws CruiseControlException { 109 final Class pluginClass = projectPlugins.getPluginClass(pluginName); 110 if (pluginClass == null) { 111 return null; 112 } 113 Object configuredPlugin = null; 114 Element pluginElement = projectPlugins.getPluginConfig(pluginName); 115 if (pluginElement != null) { 116 parsePropertiesInElement(pluginElement); 121 configuredPlugin = pluginHelper.configure(pluginElement, pluginClass, false); 122 } 123 return configuredPlugin; 124 } 125 126 134 private void parsePropertiesInElement(Element element) throws CruiseControlException { 135 parsePropertiesInElement(element, this.projectProperties, CruiseControlConfig.FAIL_UPON_MISSING_PROPERTY); 136 } 137 138 139 140 146 public static void registerProperty(Map props, Element propertyElement, 147 boolean failIfMissing) throws CruiseControlException { 148 String fileName = parsePropertiesInString(props, propertyElement.getAttributeValue("file"), 150 failIfMissing); 151 String environment = parsePropertiesInString(props, 152 propertyElement.getAttributeValue("environment"), 153 failIfMissing); 154 String propName = parsePropertiesInString(props, propertyElement.getAttributeValue("name"), 155 failIfMissing); 156 String propValue = propertyElement.getAttributeValue("value"); 157 String toUpperValue = parsePropertiesInString(props, 158 propertyElement.getAttributeValue("toupper"), 159 failIfMissing); 160 boolean toupper = "true".equalsIgnoreCase(toUpperValue); 161 162 if (fileName != null && fileName.trim().length() > 0) { 165 File file = new File (fileName); 166 try { 168 BufferedReader reader = new BufferedReader (new FileReader (file)); 169 String line; 173 while ((line = reader.readLine()) != null) { 174 line = line.trim(); 175 if (line.length() == 0 || line.charAt(0) == '#') { 176 continue; 177 } 178 int index = line.indexOf('='); 179 if (index < 0) { 180 continue; 181 } 182 String parsedName 183 = parsePropertiesInString(props, line.substring(0, index).trim(), failIfMissing); 184 String parsedValue 185 = parsePropertiesInString(props, line.substring(index + 1).trim(), failIfMissing); 186 setProperty(props, parsedName, parsedValue); 187 } 188 reader.close(); 189 } catch (FileNotFoundException e) { 190 throw new CruiseControlException( 191 "Could not load properties from file \"" + fileName 192 + "\". The file does not exist", e); 193 } catch (IOException e) { 194 throw new CruiseControlException( 195 "Could not load properties from file \"" + fileName 196 + "\".", e); 197 } 198 } else if (environment != null) { 199 Iterator variables = new OSEnvironment().getEnvironment().iterator(); 201 while (variables.hasNext()) { 202 String line = (String ) variables.next(); 203 int index = line.indexOf('='); 204 if (index < 0) { 205 continue; 206 } 207 StringBuffer name = new StringBuffer (environment); 209 name.append("."); 210 if (toupper) { 211 name.append(line.substring(0, index).toUpperCase()); 212 } else { 213 name.append(line.substring(0, index)); 214 } 215 String parsedValue = parsePropertiesInString(props, line.substring(index + 1), failIfMissing); 216 setProperty(props, name.toString(), parsedValue); 217 } 218 } else { 219 if (propName == null) { 221 throw new CruiseControlException("Bad property definition - " 222 + new XMLOutputter().outputString(propertyElement)); 223 } 224 if (propValue == null) { 225 throw new CruiseControlException( 226 "No value provided for property \"" + propName + "\" - " 227 + new XMLOutputter().outputString(propertyElement)); 228 } 229 String parsedValue = parsePropertiesInString(props, propValue, failIfMissing); 230 setProperty(props, propName, parsedValue); 231 } 232 } 233 234 private static void setProperty(Map props, String name, String parsedValue) { 236 LOG.debug("Setting property \"" + name + "\" to \"" + parsedValue + "\"."); 237 props.put(name, parsedValue); 238 } 239 240 241 251 static String parsePropertiesInString(Map props, String string, 252 boolean failIfMissing) throws CruiseControlException { 253 if (string != null) { 254 int startIndex = string.indexOf("${"); 255 if (startIndex != -1) { 256 int openedBrackets = 1; 257 int lastStartIndex = startIndex + 2; 258 int endIndex; 259 do { 260 endIndex = string.indexOf("}", lastStartIndex); 261 int otherStartIndex = string.indexOf("${", lastStartIndex); 262 if (otherStartIndex != -1 && otherStartIndex < endIndex) { 263 openedBrackets++; 264 lastStartIndex = otherStartIndex + 2; 265 } else { 266 openedBrackets--; 267 if (openedBrackets == 0) { 268 break; 269 } 270 lastStartIndex = endIndex + 1; 271 } 272 } while (true); 273 if (endIndex < startIndex + 2) { 274 throw new CruiseControlException("Unclosed brackets in " + string); 275 } 276 String property = string.substring(startIndex + 2, endIndex); 277 String propertyName = parsePropertiesInString(props, property, failIfMissing); 279 String value = "".equals(propertyName) ? "" : (String ) props.get(propertyName); 280 if (value == null) { 281 if (failIfMissing) { 282 throw new CruiseControlException("Property \"" + propertyName 283 + "\" is not defined. Please check the order in which you have used your properties."); 284 } else { 285 value = "${" + propertyName + "}"; 287 } 288 } 289 LOG.debug("Replacing the string \"" + propertyName + "\" with \"" + value + "\"."); 290 string = string.substring(0, startIndex) + value 291 + parsePropertiesInString(props, string.substring(endIndex + 1), failIfMissing); 292 } 293 } 294 return string; 295 296 } 297 298 public static void parsePropertiesInElement(Element element, 300 Map props, 301 boolean failIfMissing) 302 throws CruiseControlException { 303 304 for (Iterator children = element.getChildren().iterator(); children.hasNext(); ) { 306 parsePropertiesInElement((Element) children.next(), props, failIfMissing); 307 } 308 309 for (Iterator attributes = element.getAttributes().iterator(); attributes.hasNext(); ) { 311 Attribute attribute = (Attribute) attributes.next(); 312 attribute.setValue(parsePropertiesInString(props, attribute.getValue(), failIfMissing)); 313 } 314 315 String text = element.getTextTrim(); 317 if (text.length() > 0) { 318 element.setText(parsePropertiesInString(props, text, failIfMissing)); 319 } 320 } 321 } 322 | Popular Tags |