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.protocol.ldap.config.gui;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.jmeter.testelement.property.CollectionProperty;
028: import org.apache.jmeter.testelement.property.PropertyIterator;
029: import org.apache.jmeter.testelement.property.TestElementProperty;
030: import org.apache.jmeter.config.ConfigTestElement;
031:
032: // Mark Walsh, 2002-08-03 add method:
033: // addArgument(String name, Object value, Object metadata)
034: // Modify methods:
035: // toString(), addEmptyArgument(), addArgument(String name, Object value)
036:
037: /**
038: * A set of LDAPArgument objects. author Dolf Smits(Dolf.Smits@Siemens.com)
039: * created Aug 09 2003 11:00 AM company Siemens Netherlands N.V..
040: *
041: * Based on the work of:
042: *
043: * author Michael Stover author Mark Walsh
044: */
045:
046: public class LDAPArguments extends ConfigTestElement implements
047: Serializable {
048: /** The name of the property used to store the arguments. */
049: public static final String ARGUMENTS = "Arguments.arguments"; //$NON-NLS$
050:
051: /**
052: * Create a new Arguments object with no arguments.
053: */
054: public LDAPArguments() {
055: setProperty(new CollectionProperty(ARGUMENTS, new ArrayList()));
056: }
057:
058: /**
059: * Get the arguments.
060: *
061: * @return the arguments
062: */
063: public CollectionProperty getArguments() {
064: return (CollectionProperty) getProperty(ARGUMENTS);
065: }
066:
067: /**
068: * Clear the arguments.
069: */
070: public void clear() {
071: super .clear();
072: setProperty(new CollectionProperty(ARGUMENTS, new ArrayList()));
073: }
074:
075: /**
076: * Set the list of arguments. Any existing arguments will be lost.
077: *
078: * @param arguments
079: * the new arguments
080: */
081: public void setArguments(List arguments) {
082: setProperty(new CollectionProperty(ARGUMENTS, arguments));
083: }
084:
085: /**
086: * Get the arguments as a Map. Each argument name is used as the key, and
087: * its value as the value.
088: *
089: * @return a new Map with String keys and values containing the arguments
090: */
091: public Map getArgumentsAsMap() {
092: PropertyIterator iter = getArguments().iterator();
093: Map argMap = new HashMap();
094: while (iter.hasNext()) {
095: LDAPArgument arg = (LDAPArgument) iter.next()
096: .getObjectValue();
097: argMap.put(arg.getName(), arg.getValue());
098: }
099: return argMap;
100: }
101:
102: /**
103: * Add a new argument with the given name and value.
104: *
105: * @param name
106: * the name of the argument
107: * @param value
108: * the value of the argument
109: */
110: public void addArgument(String name, String value, String opcode) {
111: addArgument(new LDAPArgument(name, value, opcode, null));
112: }
113:
114: /**
115: * Add a new argument.
116: *
117: * @param arg
118: * the new argument
119: */
120: public void addArgument(LDAPArgument arg) {
121: TestElementProperty newArg = new TestElementProperty(arg
122: .getName(), arg);
123: if (isRunningVersion()) {
124: this .setTemporary(newArg);
125: }
126: getArguments().addItem(newArg);
127: }
128:
129: /**
130: * Add a new argument with the given name, value, and metadata.
131: *
132: * @param name
133: * the name of the argument
134: * @param value
135: * the value of the argument
136: * @param metadata
137: * the metadata for the argument
138: */
139: public void addArgument(String name, String value, String opcode,
140: String metadata) {
141: addArgument(new LDAPArgument(name, value, opcode, metadata));
142: }
143:
144: /**
145: * Get a PropertyIterator of the arguments.
146: *
147: * @return an iteration of the arguments
148: */
149: public PropertyIterator iterator() {
150: return getArguments().iterator();
151: }
152:
153: /**
154: * Create a string representation of the arguments.
155: *
156: * @return the string representation of the arguments
157: */
158: public String toString() {
159: StringBuffer str = new StringBuffer();
160: PropertyIterator iter = getArguments().iterator();
161: while (iter.hasNext()) {
162: LDAPArgument arg = (LDAPArgument) iter.next()
163: .getObjectValue();
164: final String metaData = arg.getMetaData();
165: str.append(arg.getName());
166: if (metaData == null) {
167: str.append("="); //$NON-NLS$
168: } else {
169: str.append(metaData);
170: }
171: str.append(arg.getValue());
172: if (iter.hasNext()) {
173: str.append("&"); //$NON-NLS$
174: }
175: }
176: return str.toString();
177: }
178:
179: /**
180: * Remove the specified argument from the list.
181: *
182: * @param row
183: * the index of the argument to remove
184: */
185: public void removeArgument(int row) {
186: if (row < getArguments().size()) {
187: getArguments().remove(row);
188: }
189: }
190:
191: /**
192: * Remove the specified argument from the list.
193: *
194: * @param arg
195: * the argument to remove
196: */
197: public void removeArgument(LDAPArgument arg) {
198: PropertyIterator iter = getArguments().iterator();
199: while (iter.hasNext()) {
200: LDAPArgument item = (LDAPArgument) iter.next()
201: .getObjectValue();
202: if (arg.equals(item)) {
203: iter.remove();
204: }
205: }
206: }
207:
208: /**
209: * Remove the argument with the specified name.
210: *
211: * @param argName
212: * the name of the argument to remove
213: */
214: public void removeArgument(String argName) {
215: PropertyIterator iter = getArguments().iterator();
216: while (iter.hasNext()) {
217: LDAPArgument arg = (LDAPArgument) iter.next()
218: .getObjectValue();
219: if (arg.getName().equals(argName)) {
220: iter.remove();
221: }
222: }
223: }
224:
225: /**
226: * Remove all arguments from the list.
227: */
228: public void removeAllArguments() {
229: getArguments().clear();
230: }
231:
232: /**
233: * Add a new empty argument to the list. The new argument will have the
234: * empty string as its name and value, and null metadata.
235: */
236: public void addEmptyArgument() {
237: addArgument(new LDAPArgument("", "", "", null));
238: }
239:
240: /**
241: * Get the number of arguments in the list.
242: *
243: * @return the number of arguments
244: */
245: public int getArgumentCount() {
246: return getArguments().size();
247: }
248:
249: /**
250: * Get a single argument.
251: *
252: * @param row
253: * the index of the argument to return.
254: * @return the argument at the specified index, or null if no argument
255: * exists at that index.
256: */
257: public LDAPArgument getArgument(int row) {
258: LDAPArgument argument = null;
259:
260: if (row < getArguments().size()) {
261: argument = (LDAPArgument) getArguments().get(row)
262: .getObjectValue();
263: }
264:
265: return argument;
266: }
267: }
|