1 17 18 package org.sape.carbon.core.config.type; 19 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.StringTokenizer ; 23 24 import org.sape.carbon.core.bootstrap.BootStrapper; 25 import org.sape.carbon.core.config.Config; 26 import org.sape.carbon.core.config.Configuration; 27 import org.sape.carbon.core.config.PropertyConfiguration; 28 import org.sape.carbon.core.util.reflection.BeanUtil; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 58 class TokenReplacer { 59 60 63 private Log log = LogFactory.getLog(this.getClass()); 64 65 68 private static final String BEGIN_DELIMETER = "{"; 69 70 74 private static final String END_DELIMETER = "}"; 75 76 80 private static final String ATTRIBUTE_DELIMETER = "$"; 81 82 86 private static final String ESCAPE_DELIMETER = "\\"; 87 88 89 private static final String ALL_DELIMETERS = 90 BEGIN_DELIMETER 91 + END_DELIMETER 92 + ATTRIBUTE_DELIMETER 93 + ESCAPE_DELIMETER; 94 95 98 private String data; 99 100 101 private String replacedValue = null; 102 103 109 public TokenReplacer(String data) { 110 this.data = data; 111 } 112 113 118 public boolean containsTokens() { 119 if (data == null) { 120 return false; 121 } else { 122 return data.indexOf(TokenReplacer.BEGIN_DELIMETER) != -1; 123 } 124 } 125 126 137 public String replaceTokens() throws ConfigurationTypeException { 138 139 if (this.data == null) { 140 return null; 141 } 142 143 if (this.replacedValue == null) { 144 StringBuffer replacedValueBuffer = new StringBuffer (); 145 StringBuffer configurationNameBuffer = new StringBuffer (); 146 StringBuffer configurationAttributeNameBuffer = new StringBuffer (); 147 148 StringBuffer workingBuffer = replacedValueBuffer; 149 150 StringTokenizer tokenizer = new StringTokenizer ( 153 this.data, TokenReplacer.ALL_DELIMETERS, true); 154 155 while (tokenizer.hasMoreTokens()) { 156 String nextToken = tokenizer.nextToken(); 157 158 if (nextToken.equals(TokenReplacer.ESCAPE_DELIMETER)) { 159 if (tokenizer.hasMoreTokens()) { 161 nextToken = tokenizer.nextToken(); 163 if (nextToken.length() == 1 164 && ALL_DELIMETERS.indexOf(nextToken) != -1) { 165 workingBuffer.append(nextToken); 168 } else { 169 workingBuffer.append( 172 TokenReplacer.ESCAPE_DELIMETER); 173 workingBuffer.append(nextToken); 174 } 175 } else { 176 workingBuffer.append(TokenReplacer.ESCAPE_DELIMETER); 179 } 180 181 } else if (nextToken.equals(TokenReplacer.BEGIN_DELIMETER)) { 182 183 if (workingBuffer == configurationNameBuffer) { 184 throw new TokenReplacementException( 188 this.getClass(), 189 "Found 2 " 190 + TokenReplacer.BEGIN_DELIMETER 191 + "'s without an intervening " 192 + TokenReplacer.END_DELIMETER 193 + " in [" 194 + this.data 195 + "]"); 196 } 197 workingBuffer = configurationNameBuffer; 202 203 } else if (nextToken.equals(TokenReplacer.END_DELIMETER)) { 204 workingBuffer = replacedValueBuffer; 209 210 if (configurationNameBuffer.length() == 0) { 214 throw new TokenReplacementException( 215 this.getClass(), 216 "Configuration name not found in " 217 + "token within configuration value [" 218 + this.data 219 + "]"); 220 } 221 222 String replacementValue = 223 lookupConfigurationValue( 224 configurationNameBuffer.toString(), 225 configurationAttributeNameBuffer.toString()); 226 227 workingBuffer.append(replacementValue); 228 configurationNameBuffer = new StringBuffer (); 231 configurationAttributeNameBuffer = new StringBuffer (); 232 233 } else if ( 234 nextToken.equals(TokenReplacer.ATTRIBUTE_DELIMETER)) { 235 236 if (workingBuffer != configurationNameBuffer) { 237 throw new TokenReplacementException( 240 this.getClass(), 241 "Found a " 242 + TokenReplacer.ATTRIBUTE_DELIMETER 243 + " in a bad context within [" 244 + this.data 245 + "]"); 246 } 247 workingBuffer = configurationAttributeNameBuffer; 252 253 } else { 254 workingBuffer.append(nextToken); 257 } 258 } 259 260 this.replacedValue = replacedValueBuffer.toString(); 261 } 262 263 if (log.isDebugEnabled()) { 264 log.debug("Replacing configuration token [" 265 + this.data 266 + "] with [" 267 + this.replacedValue 268 + "]"); 269 } 270 271 return this.replacedValue; 272 } 273 274 283 private String lookupConfigurationValue( 284 String configName, 285 String attributeName) 286 throws TokenReplacementException { 287 288 String attributeValue; 289 290 if ((attributeName == null) || (attributeName.equals(""))) { 291 attributeValue = 293 BootStrapper.getInstance().getDeploymentProperty(configName); 294 } else { 295 Configuration config = 296 Config.getInstance().fetchConfiguration(configName); 297 298 if (config instanceof PropertyConfiguration) { 299 300 attributeValue = 302 ((PropertyConfiguration) config).getProperty(attributeName); 303 304 } else { 305 try { 307 String beanPropertyName = 310 attributeName.substring(0, 1).toLowerCase() 311 + attributeName.substring(1); 312 313 Object attributeObject = 314 BeanUtil.getObjectAttribute(config, beanPropertyName); 315 316 if (attributeObject == null) { 317 attributeValue = null; 318 } else { 319 attributeValue = 321 ConfigurationTypeServiceFactory.getInstance(). 322 toString(attributeObject.getClass(), 323 attributeObject); 324 } 325 326 327 } catch (IllegalArgumentException iae) { 328 throw new TokenReplacementException( 329 this.getClass(), 330 "Exception replacing tokens in configuration: " 331 + "Single attribute [" 332 + attributeName 333 + "] not found " 334 + "in configuration [" 335 + configName 336 + "]", 337 iae); 338 339 } catch (NoSuchMethodException nsme) { 340 throw new TokenReplacementException( 341 this.getClass(), 342 "Exception replacing tokens in configuration: " 343 + "Attribute [" 344 + attributeName 345 + "] not found " 346 + "in configuration [" 347 + configName 348 + "]", 349 nsme); 350 351 } catch (InvocationTargetException ite) { 352 throw new TokenReplacementException( 353 this.getClass(), 354 "Exception replacing tokens in configuration: " 355 + "Attribute [" 356 + attributeName 357 + "] not found " 358 + "in configuration [" 359 + configName 360 + "]", 361 ite.getTargetException()); 362 363 } catch (IllegalAccessException iae) { 364 throw new TokenReplacementException( 365 this.getClass(), 366 "Exception replacing tokens in configuration: " 367 + "Attribute [" 368 + attributeName 369 + "] not found " 370 + "in configuration [" 371 + configName 372 + "]", 373 iae); 374 375 } 376 } 377 } 378 379 if (attributeValue == null) { 380 String message; 381 if ((attributeName == null) || (attributeName.equals(""))) { 382 message = 383 "Deployment Property not found: [" 384 + configName 385 + "]"; 386 387 } else { 388 message = 389 "Configuration attribute not found: " 390 + "ConfigurationDocument name [" 391 + configName 392 + "] attribute name [" 393 + attributeName 394 + "]"; 395 } 396 397 throw new TokenReplacementException(this.getClass(), 398 "Error replacing tokens in configuration: " + message); 399 } 400 401 return attributeValue; 402 } 403 } | Popular Tags |