001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.threads;
020:
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.jmeter.util.JMeterUtils;
027:
028: /**
029: * @version $Revision: 493779 $
030: */
031: public class JMeterVariables {
032: private Map variables = new HashMap();
033:
034: private int iteration = 0;
035:
036: // Property names to preload into JMeter variables:
037: private static final String[] PRE_LOAD = { "START.MS", "START.YMD",
038: "START.HMS" };
039:
040: public JMeterVariables() {
041: preloadVariables();
042: }
043:
044: private void preloadVariables() {
045: for (int i = 0; i < PRE_LOAD.length; i++) {
046: String property = PRE_LOAD[i];
047: String value = JMeterUtils.getProperty(property);
048: if (value != null) {
049: variables.put(property, value);
050: }
051: }
052: }
053:
054: public String getThreadName() {
055: return Thread.currentThread().getName();
056: }
057:
058: public int getIteration() {
059: return iteration;
060: }
061:
062: public void incIteration() {
063: iteration++;
064: }
065:
066: // Does not appear to be used
067: public void initialize() {
068: variables.clear();
069: preloadVariables();
070: }
071:
072: public Object remove(String key) {
073: return variables.remove(key);
074: }
075:
076: public void put(String key, String value) {
077: variables.put(key, value);
078: }
079:
080: public void putObject(String key, Object value) {
081: variables.put(key, value);
082: }
083:
084: public void putAll(Map vars) {
085: variables.putAll(vars);
086: }
087:
088: public void putAll(JMeterVariables vars) {
089: putAll(vars.variables);
090: }
091:
092: /**
093: * Returns null values if variable doesn't exist. Users of this must check
094: * for null.
095: */
096: public String get(String key) {
097: return (String) variables.get(key);
098: }
099:
100: public Object getObject(String key) {
101: return variables.get(key);
102: }
103:
104: public Iterator getIterator() {
105: return Collections.unmodifiableMap(variables).entrySet()
106: .iterator();
107: }
108: }
|