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.testelement.property;
020:
021: import java.util.Map;
022:
023: import org.apache.jmeter.testelement.TestElement;
024:
025: public class MapProperty extends MultiProperty {
026:
027: private static final long serialVersionUID = 221L; // Remember to change this when the class changes ...
028:
029: private Map value;
030:
031: private transient Map savedValue = null;
032:
033: public MapProperty(String name, Map value) {
034: super (name);
035: log.info("map = " + value);
036: this .value = normalizeMap(value);
037: log.info("normalized map = " + this .value);
038: }
039:
040: public MapProperty() {
041: super ();
042: }
043:
044: public boolean equals(Object o) {
045: if (o instanceof MapProperty) {
046: if (value != null) {
047: return value.equals(((JMeterProperty) o)
048: .getObjectValue());
049: }
050: }
051: return false;
052: }
053:
054: public void setObjectValue(Object v) {
055: if (v instanceof Map) {
056: setMap((Map) v);
057: }
058: }
059:
060: public void addProperty(JMeterProperty prop) {
061: addProperty(prop.getName(), prop);
062: }
063:
064: public JMeterProperty get(String key) {
065: return (JMeterProperty) value.get(key);
066: }
067:
068: /**
069: * Figures out what kind of properties this collection is holding and
070: * returns the class type.
071: *
072: * @see AbstractProperty#getPropertyType()
073: */
074: protected Class getPropertyType() {
075: if (value.size() > 0) {
076: return valueIterator().next().getClass();
077: }
078: return NullProperty.class;
079: }
080:
081: /**
082: * @see JMeterProperty#getStringValue()
083: */
084: public String getStringValue() {
085: return value.toString();
086: }
087:
088: /**
089: * @see JMeterProperty#getObjectValue()
090: */
091: public Object getObjectValue() {
092: return value;
093: }
094:
095: public Object clone() {
096: MapProperty prop = (MapProperty) super .clone();
097: prop.value = cloneMap();
098: return prop;
099: }
100:
101: private Map cloneMap() {
102: try {
103: Map newCol = (Map) value.getClass().newInstance();
104: PropertyIterator iter = valueIterator();
105: while (iter.hasNext()) {
106: JMeterProperty item = iter.next();
107: newCol.put(item.getName(), item.clone());
108: }
109: return newCol;
110: } catch (Exception e) {
111: log.error("Couldn't clone map", e);
112: return value;
113: }
114: }
115:
116: public PropertyIterator valueIterator() {
117: return getIterator(value.values());
118: }
119:
120: public void addProperty(String name, JMeterProperty prop) {
121: if (!value.containsKey(name)) {
122: value.put(name, prop);
123: }
124: }
125:
126: public void setMap(Map newMap) {
127: value = normalizeMap(newMap);
128: }
129:
130: /**
131: * @see JMeterProperty#recoverRunningVersion(TestElement)
132: */
133: public void recoverRunningVersion(TestElement owner) {
134: if (savedValue != null) {
135: value = savedValue;
136: }
137: recoverRunningVersionOfSubElements(owner);
138: }
139:
140: public void clear() {
141: value.clear();
142: }
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see MultiProperty#iterator()
148: */
149: public PropertyIterator iterator() {
150: return valueIterator();
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see JMeterProperty#setRunningVersion(boolean)
157: */
158: public void setRunningVersion(boolean running) {
159: super.setRunningVersion(running);
160: if (running) {
161: savedValue = value;
162: } else {
163: savedValue = null;
164: }
165: }
166: }
|