1 18 19 package org.apache.jmeter.gui.action; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.lang.reflect.Modifier ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import javax.swing.SwingUtilities ; 32 33 import org.apache.jmeter.exceptions.IllegalUserActionException; 34 import org.apache.jmeter.gui.GuiPackage; 35 import org.apache.jmeter.util.JMeterUtils; 36 import org.apache.jorphan.logging.LoggingManager; 37 import org.apache.jorphan.reflect.ClassFinder; 38 import org.apache.log.Logger; 39 40 44 public final class ActionRouter implements ActionListener 45 { 46 private Map commands = new HashMap (); 47 private static ActionRouter router; 48 transient private static Logger log = LoggingManager.getLoggerForClass(); 49 private Map preActionListeners = new HashMap (); 50 private Map postActionListeners = new HashMap (); 51 52 private ActionRouter() 53 { 54 } 55 56 public void actionPerformed(final ActionEvent e) 57 { 58 SwingUtilities.invokeLater(new Runnable () 59 { 60 public void run() 61 { 62 performAction(e); 63 } 64 65 }); 66 } 67 68 private void performAction(final ActionEvent e) 69 { 70 try 71 { 72 GuiPackage.getInstance().updateCurrentNode(); 73 Set commandObjects = (Set ) commands.get(e.getActionCommand()); 74 Iterator iter = commandObjects.iterator(); 75 while (iter.hasNext()) 76 { 77 try 78 { 79 Command c = (Command) iter.next(); 80 preActionPerformed(c.getClass(), e); 81 c.doAction(e); 82 postActionPerformed(c.getClass(), e); 83 } 84 catch(IllegalUserActionException err) 85 { 86 JMeterUtils.reportErrorToUser(err.toString()); 87 } 88 catch (Exception err) 89 { 90 log.error("", err); 91 } 92 } 93 } 94 catch (NullPointerException er) 95 { 96 log.error("performAction("+e.getActionCommand()+") " + e.toString() + " caused", er); 97 JMeterUtils.reportErrorToUser( 98 "Sorry, this feature (" 99 + e.getActionCommand() 100 + ") not yet implemented"); 101 } 102 } 103 104 108 public void doActionNow(ActionEvent e) 109 { 110 performAction(e); 111 } 112 113 public Set getAction(String actionName) 114 { 115 Set set = new HashSet (); 116 Set commandObjects = (Set ) commands.get(actionName); 117 Iterator iter = commandObjects.iterator(); 118 while (iter.hasNext()) 119 { 120 try 121 { 122 set.add(iter.next()); 123 } 124 catch (Exception err) 125 { 126 log.error("", err); 127 } 128 } 129 return set; 130 } 131 132 public Command getAction(String actionName, Class actionClass) 133 { 134 Set commandObjects = (Set ) commands.get(actionName); 135 Iterator iter = commandObjects.iterator(); 136 while (iter.hasNext()) 137 { 138 try 139 { 140 Command com = (Command) iter.next(); 141 if (com.getClass().equals(actionClass)) 142 { 143 return com; 144 } 145 } 146 catch (Exception err) 147 { 148 log.error("", err); 149 } 150 } 151 return null; 152 } 153 154 public Command getAction(String actionName, String className) 155 { 156 Set commandObjects = (Set ) commands.get(actionName); 157 Iterator iter = commandObjects.iterator(); 158 while (iter.hasNext()) 159 { 160 try 161 { 162 Command com = (Command) iter.next(); 163 if (com.getClass().getName().equals(className)) 164 { 165 return com; 166 } 167 } 168 catch (Exception err) 169 { 170 log.error("", err); 171 } 172 } 173 return null; 174 } 175 176 185 public void addPreActionListener(Class action, ActionListener listener) 186 { 187 if (action != null) 188 { 189 HashSet set = (HashSet ) preActionListeners.get(action.getName()); 190 if (set == null) 191 { 192 set = new HashSet (); 193 } 194 set.add(listener); 195 preActionListeners.put(action.getName(), set); 196 } 197 } 198 199 209 public void removePreActionListener(Class action, ActionListener listener) 210 { 211 if (action != null) 212 { 213 HashSet set = (HashSet ) preActionListeners.get(action.getName()); 214 if (set != null) 215 { 216 set.remove(listener); 217 preActionListeners.put(action.getName(), set); 218 } 219 } 220 } 221 222 231 public void addPostActionListener(Class action, ActionListener listener) 232 { 233 if (action != null) 234 { 235 HashSet set = (HashSet ) postActionListeners.get(action.getName()); 236 if (set == null) 237 { 238 set = new HashSet (); 239 } 240 set.add(listener); 241 postActionListeners.put(action.getName(), set); 242 } 243 } 244 245 254 public void removePostActionListener(Class action, ActionListener listener) 255 { 256 if (action != null) 257 { 258 HashSet set = (HashSet ) postActionListeners.get(action.getName()); 259 if (set != null) 260 { 261 set.remove(listener); 262 postActionListeners.put(action.getName(), set); 263 } 264 } 265 } 266 267 protected void preActionPerformed(Class action, ActionEvent e) 268 { 269 if (action != null) 270 { 271 HashSet listenerSet = 272 (HashSet ) preActionListeners.get(action.getName()); 273 if (listenerSet != null && listenerSet.size() > 0) 274 { 275 Object [] listeners = listenerSet.toArray(); 276 for (int i = 0; i < listeners.length; i++) 277 { 278 ((ActionListener ) listeners[i]).actionPerformed(e); 279 } 280 } 281 } 282 } 283 284 protected void postActionPerformed(Class action, ActionEvent e) 285 { 286 if (action != null) 287 { 288 HashSet listenerSet = 289 (HashSet ) postActionListeners.get(action.getName()); 290 if (listenerSet != null && listenerSet.size() > 0) 291 { 292 Object [] listeners = listenerSet.toArray(); 293 for (int i = 0; i < listeners.length; i++) 294 { 295 ((ActionListener ) listeners[i]).actionPerformed(e); 296 } 297 } 298 } 299 } 300 301 private void populateCommandMap() 302 { 303 List listClasses; 304 Command command; 305 Iterator iterClasses; 306 Class commandClass; 307 try 308 { 309 listClasses = 310 ClassFinder.findClassesThatExtend( 311 JMeterUtils.getSearchPaths(), 312 new Class [] { 313 Class.forName( 314 "org.apache.jmeter.gui.action.Command")}); 315 commands = new HashMap (listClasses.size()); 316 if (listClasses.size() == 0) 317 { 318 log.warn("!!!!!Uh-oh, didn't find any action handlers!!!!!"); 319 } 320 iterClasses = listClasses.iterator(); 321 while (iterClasses.hasNext()) 322 { 323 String strClassName = (String ) iterClasses.next(); 324 commandClass = Class.forName(strClassName); 325 if (!Modifier.isAbstract(commandClass.getModifiers())) 326 { 327 command = (Command) commandClass.newInstance(); 328 Iterator iter = command.getActionNames().iterator(); 329 while (iter.hasNext()) 330 { 331 String commandName = (String ) iter.next(); 332 Set commandObjects = (Set ) commands.get(commandName); 333 if (commandObjects == null) 334 { 335 commandObjects = new HashSet (); 336 commands.put(commandName, commandObjects); 337 } 338 commandObjects.add(command); 339 } 340 } 341 } 342 } 343 catch (Exception e) 344 { 345 if ("java.awt.HeadlessException".equals(e.getClass().getName())) { 347 log.warn(e.toString()); 348 } 349 else 350 { 351 log.error("exception finding action handlers", e); 352 } 353 } 354 } 355 356 361 public static ActionRouter getInstance() 362 { 363 if (router == null) 364 { 365 router = new ActionRouter(); 366 router.populateCommandMap(); 367 } 368 return router; 369 } 370 } 371 | Popular Tags |