1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.protocol.ldap.config.gui; 20 21 import java.io.Serializable; 22 import java.util.ArrayList; 23 import java.util.HashMap; 24 import java.util.List; 25 import java.util.Map; 26 27 import org.apache.jmeter.testelement.property.CollectionProperty; 28 import org.apache.jmeter.testelement.property.PropertyIterator; 29 import org.apache.jmeter.testelement.property.TestElementProperty; 30 import org.apache.jmeter.config.ConfigTestElement; 31 32 /** 33 * A set of LDAPArgument objects. author Dolf Smits(Dolf.Smits@Siemens.com) 34 * created Aug 09 2003 11:00 AM company Siemens Netherlands N.V.. 35 * 36 * Based on the work of: 37 * 38 * author Michael Stover author Mark Walsh 39 */ 40 41 public class LDAPArguments extends ConfigTestElement implements Serializable { 42 /** The name of the property used to store the arguments. */ 43 public static final String ARGUMENTS = "Arguments.arguments"; //$NON-NLS$ 44 45 /** 46 * Create a new Arguments object with no arguments. 47 */ 48 public LDAPArguments() { 49 setProperty(new CollectionProperty(ARGUMENTS, new ArrayList())); 50 } 51 52 /** 53 * Get the arguments. 54 * 55 * @return the arguments 56 */ 57 public CollectionProperty getArguments() { 58 return (CollectionProperty) getProperty(ARGUMENTS); 59 } 60 61 /** 62 * Clear the arguments. 63 */ 64 public void clear() { 65 super.clear(); 66 setProperty(new CollectionProperty(ARGUMENTS, new ArrayList())); 67 } 68 69 /** 70 * Set the list of arguments. Any existing arguments will be lost. 71 * 72 * @param arguments 73 * the new arguments 74 */ 75 public void setArguments(List arguments) { 76 setProperty(new CollectionProperty(ARGUMENTS, arguments)); 77 } 78 79 /** 80 * Get the arguments as a Map. Each argument name is used as the key, and 81 * its value as the value. 82 * 83 * @return a new Map with String keys and values containing the arguments 84 */ 85 public Map getArgumentsAsMap() { 86 PropertyIterator iter = getArguments().iterator(); 87 Map argMap = new HashMap(); 88 while (iter.hasNext()) { 89 LDAPArgument arg = (LDAPArgument) iter.next().getObjectValue(); 90 argMap.put(arg.getName(), arg.getValue()); 91 } 92 return argMap; 93 } 94 95 /** 96 * Add a new argument with the given name and value. 97 * 98 * @param name 99 * the name of the argument 100 * @param value 101 * the value of the argument 102 */ 103 public void addArgument(String name, String value, String opcode) { 104 addArgument(new LDAPArgument(name, value, opcode, null)); 105 } 106 107 /** 108 * Add a new argument. 109 * 110 * @param arg 111 * the new argument 112 */ 113 public void addArgument(LDAPArgument arg) { 114 TestElementProperty newArg = new TestElementProperty(arg.getName(), arg); 115 if (isRunningVersion()) { 116 this.setTemporary(newArg); 117 } 118 getArguments().addItem(newArg); 119 } 120 121 /** 122 * Add a new argument with the given name, value, and metadata. 123 * 124 * @param name 125 * the name of the argument 126 * @param value 127 * the value of the argument 128 * @param metadata 129 * the metadata for the argument 130 */ 131 public void addArgument(String name, String value, String opcode, String metadata) { 132 addArgument(new LDAPArgument(name, value, opcode, metadata)); 133 } 134 135 /** 136 * Get a PropertyIterator of the arguments. 137 * 138 * @return an iteration of the arguments 139 */ 140 public PropertyIterator iterator() { 141 return getArguments().iterator(); 142 } 143 144 /** 145 * Create a string representation of the arguments. 146 * 147 * @return the string representation of the arguments 148 */ 149 public String toString() { 150 StringBuffer str = new StringBuffer(); 151 PropertyIterator iter = getArguments().iterator(); 152 while (iter.hasNext()) { 153 LDAPArgument arg = (LDAPArgument) iter.next().getObjectValue(); 154 final String metaData = arg.getMetaData(); 155 str.append(arg.getName()); 156 if (metaData == null) { 157 str.append("="); //$NON-NLS$ 158 } else { 159 str.append(metaData); 160 } 161 str.append(arg.getValue()); 162 if (iter.hasNext()) { 163 str.append("&"); //$NON-NLS$ 164 } 165 } 166 return str.toString(); 167 } 168 169 /** 170 * Remove the specified argument from the list. 171 * 172 * @param row 173 * the index of the argument to remove 174 */ 175 public void removeArgument(int row) { 176 if (row < getArguments().size()) { 177 getArguments().remove(row); 178 } 179 } 180 181 /** 182 * Remove the specified argument from the list. 183 * 184 * @param arg 185 * the argument to remove 186 */ 187 public void removeArgument(LDAPArgument arg) { 188 PropertyIterator iter = getArguments().iterator(); 189 while (iter.hasNext()) { 190 LDAPArgument item = (LDAPArgument) iter.next().getObjectValue(); 191 if (arg.equals(item)) { 192 iter.remove(); 193 } 194 } 195 } 196 197 /** 198 * Remove the argument with the specified name. 199 * 200 * @param argName 201 * the name of the argument to remove 202 */ 203 public void removeArgument(String argName) { 204 PropertyIterator iter = getArguments().iterator(); 205 while (iter.hasNext()) { 206 LDAPArgument arg = (LDAPArgument) iter.next().getObjectValue(); 207 if (arg.getName().equals(argName)) { 208 iter.remove(); 209 } 210 } 211 } 212 213 /** 214 * Remove all arguments from the list. 215 */ 216 public void removeAllArguments() { 217 getArguments().clear(); 218 } 219 220 /** 221 * Add a new empty argument to the list. The new argument will have the 222 * empty string as its name and value, and null metadata. 223 */ 224 public void addEmptyArgument() { 225 addArgument(new LDAPArgument("", "", "", null)); 226 } 227 228 /** 229 * Get the number of arguments in the list. 230 * 231 * @return the number of arguments 232 */ 233 public int getArgumentCount() { 234 return getArguments().size(); 235 } 236 237 /** 238 * Get a single argument. 239 * 240 * @param row 241 * the index of the argument to return. 242 * @return the argument at the specified index, or null if no argument 243 * exists at that index. 244 */ 245 public LDAPArgument getArgument(int row) { 246 LDAPArgument argument = null; 247 248 if (row < getArguments().size()) { 249 argument = (LDAPArgument) getArguments().get(row).getObjectValue(); 250 } 251 252 return argument; 253 } 254 }