1 18 19 package org.apache.jmeter.testelement; 20 21 import java.io.Serializable ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 import org.apache.jmeter.control.NextIsNullException; 30 import org.apache.jmeter.samplers.Sampler; 31 import org.apache.jmeter.testelement.property.CollectionProperty; 32 import org.apache.jmeter.testelement.property.JMeterProperty; 33 import org.apache.jmeter.testelement.property.MapProperty; 34 import org.apache.jmeter.testelement.property.MultiProperty; 35 import org.apache.jmeter.testelement.property.NullProperty; 36 import org.apache.jmeter.testelement.property.PropertyIterator; 37 import org.apache.jmeter.testelement.property.PropertyIteratorImpl; 38 import org.apache.jmeter.testelement.property.StringProperty; 39 import org.apache.jmeter.testelement.property.TestElementProperty; 40 import org.apache.jmeter.threads.JMeterContext; 41 import org.apache.jmeter.threads.JMeterContextService; 42 import org.apache.jorphan.logging.LoggingManager; 43 import org.apache.log.Logger; 44 45 48 public abstract class AbstractTestElement implements TestElement, Serializable 49 { 50 private static final Logger log = LoggingManager.getLoggerForClass(); 51 52 private Map propMap = Collections.synchronizedMap(new HashMap ()); 53 private Set temporaryProperties; 54 55 private boolean runningVersion = false; 56 57 private transient JMeterContext threadContext = null; 59 private transient String threadName = null; 60 61 public Object clone() 62 { 63 TestElement clonedElement = null; 64 try 65 { 66 clonedElement = (TestElement) this.getClass().newInstance(); 67 68 PropertyIterator iter = propertyIterator(); 69 while (iter.hasNext()) 70 { 71 clonedElement.setProperty((JMeterProperty) iter.next().clone()); 72 } 73 clonedElement.setRunningVersion(runningVersion); 74 } 75 catch (Exception e) 76 {} 77 return clonedElement; 78 } 79 80 public void clear() 81 { 82 propMap.clear(); 83 } 84 85 public void removeProperty(String key) 86 { 87 propMap.remove(key); 88 } 89 90 public boolean equals(Object o) 91 { 92 if (o instanceof AbstractTestElement) 93 { 94 return ((AbstractTestElement) o).propMap.equals(propMap); 95 } 96 else 97 { 98 return false; 99 } 100 } 101 102 112 118 public void addTestElement(TestElement el) 119 { 120 mergeIn(el); 121 } 122 123 public void setName(String name) 124 { 125 setProperty(new StringProperty(TestElement.NAME, name)); 126 } 127 128 public String getName() 129 { 130 return getProperty(TestElement.NAME).getStringValue(); 131 } 132 133 138 public JMeterProperty getProperty(String key) 139 { 140 JMeterProperty prop = (JMeterProperty) propMap.get(key); 141 if (prop == null) 142 { 143 prop = new NullProperty(key); 146 } 147 return prop; 148 } 149 150 public void traverse(TestElementTraverser traverser) 151 { 152 PropertyIterator iter = propertyIterator(); 153 traverser.startTestElement(this); 154 while (iter.hasNext()) 155 { 156 traverseProperty(traverser, iter.next()); 157 } 158 traverser.endTestElement(this); 159 } 160 161 protected void traverseProperty( 162 TestElementTraverser traverser, 163 JMeterProperty value) 164 { 165 traverser.startProperty(value); 166 if (value instanceof TestElementProperty) 167 { 168 ((TestElement) value.getObjectValue()).traverse(traverser); 169 } 170 else if (value instanceof CollectionProperty) 171 { 172 traverseCollection((CollectionProperty) value, traverser); 173 } 174 else if (value instanceof MapProperty) 175 { 176 traverseMap((MapProperty) value, traverser); 177 } 178 traverser.endProperty(value); 179 } 180 181 protected void traverseMap(MapProperty map, TestElementTraverser traverser) 182 { 183 PropertyIterator iter = map.valueIterator(); 184 while (iter.hasNext()) 185 { 186 traverseProperty(traverser, iter.next()); 187 } 188 } 189 190 protected void traverseCollection( 191 CollectionProperty col, 192 TestElementTraverser traverser) 193 { 194 PropertyIterator iter = col.iterator(); 195 while (iter.hasNext()) 196 { 197 traverseProperty(traverser, iter.next()); 198 } 199 } 200 201 public int getPropertyAsInt(String key) 202 { 203 return getProperty(key).getIntValue(); 204 } 205 206 public boolean getPropertyAsBoolean(String key) 207 { 208 return getProperty(key).getBooleanValue(); 209 } 210 211 public boolean getPropertyAsBoolean(String key,boolean defaultVal) 212 { 213 JMeterProperty jmp = getProperty(key); 214 return jmp instanceof NullProperty ? defaultVal: jmp.getBooleanValue(); 215 } 216 217 public float getPropertyAsFloat(String key) 218 { 219 return getProperty(key).getFloatValue(); 220 } 221 222 public long getPropertyAsLong(String key) 223 { 224 return getProperty(key).getLongValue(); 225 } 226 227 public double getPropertyAsDouble(String key) 228 { 229 return getProperty(key).getDoubleValue(); 230 } 231 232 public String getPropertyAsString(String key) 233 { 234 return getProperty(key).getStringValue(); 235 } 236 237 protected void addProperty(JMeterProperty property) 238 { 239 if (isRunningVersion()) 240 { 241 setTemporary(property); 242 } 243 else 244 { 245 clearTemporary(property); 246 } 247 JMeterProperty prop = getProperty(property.getName()); 248 249 if (prop instanceof NullProperty 250 || (prop instanceof StringProperty 251 && prop.getStringValue().equals(""))) 252 { 253 propMap.put(property.getName(), property); 254 } 255 else 256 { 257 prop.mergeIn(property); 258 } 259 } 260 261 protected void clearTemporary(JMeterProperty property) 262 { 263 if(temporaryProperties != null) 264 { 265 temporaryProperties.remove(property); 266 } 267 } 268 269 273 protected void logProperties() 274 { 275 if (log.isDebugEnabled()) 276 { 277 PropertyIterator iter = propertyIterator(); 278 while (iter.hasNext()) 279 { 280 JMeterProperty prop = iter.next(); 281 log.debug( 282 "Property " 283 + prop.getName() 284 + " is temp? " 285 + isTemporary(prop) 286 + " and is a " 287 + prop.getObjectValue()); 288 } 289 } 290 } 291 292 public void setProperty(JMeterProperty property) 293 { 294 if (isRunningVersion()) 295 { 296 if (getProperty(property.getName()) instanceof NullProperty) 297 { 298 addProperty(property); 299 } 300 else 301 { 302 getProperty(property.getName()).setObjectValue( 303 property.getObjectValue()); 304 } 305 } 306 else 307 { 308 propMap.put(property.getName(), property); 309 } 310 } 311 312 public void setProperty(String name, String value) 313 { 314 setProperty(new StringProperty(name, value)); 315 } 316 317 public PropertyIterator propertyIterator() 318 { 319 return new PropertyIteratorImpl(propMap.values()); 320 } 321 322 protected void mergeIn(TestElement element) 323 { 324 PropertyIterator iter = element.propertyIterator(); 325 while (iter.hasNext()) 326 { 327 JMeterProperty prop = iter.next(); 328 addProperty(prop); 329 } 330 } 331 332 335 public boolean isRunningVersion() 336 { 337 return runningVersion; 338 } 339 340 344 public void setRunningVersion(boolean runningVersion) 345 { 346 this.runningVersion = runningVersion; 347 PropertyIterator iter = propertyIterator(); 348 while (iter.hasNext()) 349 { 350 iter.next().setRunningVersion(runningVersion); 351 } 352 } 353 354 public void recoverRunningVersion() 355 { 356 Iterator iter = propMap.entrySet().iterator(); 357 while (iter.hasNext()) 358 { 359 Map.Entry entry = (Map.Entry )iter.next(); 360 JMeterProperty prop = (JMeterProperty)entry.getValue(); 361 if (isTemporary(prop)) 362 { 363 iter.remove(); 364 clearTemporary(prop); 365 } 366 else 367 { 368 prop.recoverRunningVersion(this); 369 } 370 } 371 emptyTemporary(); 372 } 373 374 protected void emptyTemporary() 375 { 376 if(temporaryProperties != null) 377 { 378 temporaryProperties.clear(); 379 } 380 } 381 382 protected Sampler nextIsNull() throws NextIsNullException 383 { 384 return null; 385 } 386 389 public boolean isTemporary(JMeterProperty property) 390 { 391 if(temporaryProperties == null) 392 { 393 return false; 394 } 395 else 396 { 397 return temporaryProperties.contains(property); 398 } 399 } 400 401 404 public void setTemporary(JMeterProperty property) 405 { 406 if(temporaryProperties == null) 407 { 408 temporaryProperties = new HashSet (); 409 } 410 temporaryProperties.add(property); 411 if(property instanceof MultiProperty) 412 { 413 PropertyIterator iter = ((MultiProperty)property).iterator(); 414 while(iter.hasNext()) 415 { 416 setTemporary(iter.next()); 417 } 418 } 419 } 420 421 424 public JMeterContext getThreadContext() { 425 if (threadContext == null) 426 { 427 431 threadContext = JMeterContextService.getContext(); 434 } 435 return threadContext; 436 } 437 440 public void setThreadContext(JMeterContext inthreadContext) { 441 if (threadContext != null) 442 { 443 if (inthreadContext != threadContext) 444 throw new RuntimeException ("Attempting to reset the thread context"); 445 } 446 this.threadContext = inthreadContext; 447 } 448 451 public String getThreadName() { 452 return threadName; 453 } 454 457 public void setThreadName(String inthreadName) { 458 if (threadName != null) 459 { 460 if (!threadName.equals(inthreadName)) 461 throw new RuntimeException ("Attempting to reset the thread name"); 462 } 463 this.threadName = inthreadName; 464 } 465 466 469 public void threadFinished() { 470 } 471 472 475 public void threadStarted() { 476 } 477 478 public boolean canRemove(){ 480 return true; 481 } 482 } 483 | Popular Tags |