1 18 19 package org.apache.jmeter.modifiers; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 25 import org.apache.jmeter.engine.event.LoopIterationEvent; 26 import org.apache.jmeter.engine.event.LoopIterationListener; 27 import org.apache.jmeter.processor.PreProcessor; 28 import org.apache.jmeter.testelement.AbstractTestElement; 29 import org.apache.jmeter.testelement.TestElement; 30 import org.apache.jmeter.testelement.property.BooleanProperty; 31 import org.apache.jmeter.testelement.property.CollectionProperty; 32 import org.apache.jmeter.testelement.property.PropertyIterator; 33 import org.apache.jmeter.threads.JMeterVariables; 34 import org.apache.jorphan.logging.LoggingManager; 35 import org.apache.log.Logger; 36 37 40 public class UserParameters 41 extends AbstractTestElement 42 implements Serializable , PreProcessor, LoopIterationListener 43 { 44 private static final Logger log = LoggingManager.getLoggerForClass(); 45 46 public static final String NAMES = "UserParameters.names"; 47 public static final String THREAD_VALUES = "UserParameters.thread_values"; 48 public static final String PER_ITERATION = "UserParameters.per_iteration"; 49 50 58 private Integer lock = new Integer (0); 59 60 public CollectionProperty getNames() 61 { 62 return (CollectionProperty) getProperty(NAMES); 63 } 64 65 public CollectionProperty getThreadLists() 66 { 67 return (CollectionProperty) getProperty(THREAD_VALUES); 68 } 69 70 75 public void setNames(Collection list) 76 { 77 setProperty(new CollectionProperty(NAMES, list)); 78 } 79 80 85 public void setNames(CollectionProperty list) 86 { 87 setProperty(list); 88 } 89 90 96 public void setThreadLists(Collection threadLists) 97 { 98 setProperty(new CollectionProperty(THREAD_VALUES, threadLists)); 99 } 100 101 107 public void setThreadLists(CollectionProperty threadLists) 108 { 109 setProperty(threadLists); 110 } 111 112 private CollectionProperty getValues() 113 { 114 CollectionProperty threadValues = 115 (CollectionProperty) getProperty(THREAD_VALUES); 116 if (threadValues.size() > 0) 117 { 118 return (CollectionProperty) threadValues.get( 119 getThreadContext().getThreadNum() 120 % threadValues.size()); 121 } 122 else 123 { 124 return new CollectionProperty("noname", new LinkedList ()); 125 } 126 } 127 128 public boolean isPerIteration() 129 { 130 return getPropertyAsBoolean(PER_ITERATION); 131 } 132 133 public void setPerIteration(boolean perIter) 134 { 135 setProperty(new BooleanProperty(PER_ITERATION, perIter)); 136 } 137 138 public void process() 139 { 140 if (log.isDebugEnabled()) 141 { 142 log.debug(Thread.currentThread().getName() + " process " + isPerIteration()); } 144 if (!isPerIteration()) 145 { 146 setValues(); 147 } 148 } 149 150 private void setValues() 151 { 152 synchronized (lock) 153 { 154 if (log.isDebugEnabled()) 155 { 156 log.debug(Thread.currentThread().getName() + " Running up named: " + getName()); } 158 PropertyIterator namesIter = getNames().iterator(); 159 PropertyIterator valueIter = getValues().iterator(); 160 JMeterVariables jmvars = getThreadContext().getVariables(); 161 while (namesIter.hasNext() && valueIter.hasNext()) 162 { 163 String name = namesIter.next().getStringValue(); 164 String value = valueIter.next().getStringValue(); 165 if (log.isDebugEnabled()) 166 { 167 log.debug(Thread.currentThread().getName()+" saving variable: "+name+"="+value); } 169 jmvars.put(name, value); 170 } 171 } 172 } 173 174 177 public void iterationStart(LoopIterationEvent event) 178 { 179 if (log.isDebugEnabled()) 180 { 181 log.debug(Thread.currentThread().getName() + " iteration start " + isPerIteration()); } 183 if (isPerIteration()) 184 { 185 setValues(); 186 } 187 } 188 189 199 public Object clone() 200 { 201 UserParameters up = (UserParameters) super.clone(); 202 up.lock = lock; return up; 204 } 205 206 209 protected void mergeIn(TestElement element) 210 { 211 } 213 } 214 | Popular Tags |