KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > action > ActionRouter


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/action/ActionRouter.java,v 1.22 2004/02/22 19:37:06 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.gui.action;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.swing.SwingUtilities JavaDoc;
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 /**
41  * @author Michael Stover
42  * @version $Revision: 1.22 $
43  */

44 public final class ActionRouter implements ActionListener JavaDoc
45 {
46     private Map JavaDoc commands = new HashMap JavaDoc();
47     private static ActionRouter router;
48     transient private static Logger log = LoggingManager.getLoggerForClass();
49     private Map JavaDoc preActionListeners = new HashMap JavaDoc();
50     private Map JavaDoc postActionListeners = new HashMap JavaDoc();
51
52     private ActionRouter()
53     {
54     }
55
56     public void actionPerformed(final ActionEvent JavaDoc e)
57     {
58         SwingUtilities.invokeLater(new Runnable JavaDoc()
59         {
60             public void run()
61             {
62                 performAction(e);
63             }
64
65         });
66     }
67
68     private void performAction(final ActionEvent JavaDoc e)
69     {
70         try
71         {
72             GuiPackage.getInstance().updateCurrentNode();
73             Set JavaDoc commandObjects = (Set JavaDoc) commands.get(e.getActionCommand());
74             Iterator JavaDoc 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 JavaDoc err)
89                 {
90                     log.error("", err);
91                 }
92             }
93         }
94         catch (NullPointerException JavaDoc 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     /**
105      * To execute an action immediately in the current thread.
106      * @param e the action to execute
107      */

108     public void doActionNow(ActionEvent JavaDoc e)
109     {
110         performAction(e);
111     }
112
113     public Set JavaDoc getAction(String JavaDoc actionName)
114     {
115         Set JavaDoc set = new HashSet JavaDoc();
116         Set JavaDoc commandObjects = (Set JavaDoc) commands.get(actionName);
117         Iterator JavaDoc iter = commandObjects.iterator();
118         while (iter.hasNext())
119         {
120             try
121             {
122                 set.add(iter.next());
123             }
124             catch (Exception JavaDoc err)
125             {
126                 log.error("", err);
127             }
128         }
129         return set;
130     }
131
132     public Command getAction(String JavaDoc actionName, Class JavaDoc actionClass)
133     {
134         Set JavaDoc commandObjects = (Set JavaDoc) commands.get(actionName);
135         Iterator JavaDoc 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 JavaDoc err)
147             {
148                 log.error("", err);
149             }
150         }
151         return null;
152     }
153
154     public Command getAction(String JavaDoc actionName, String JavaDoc className)
155     {
156         Set JavaDoc commandObjects = (Set JavaDoc) commands.get(actionName);
157         Iterator JavaDoc 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 JavaDoc err)
169             {
170                 log.error("", err);
171             }
172         }
173         return null;
174     }
175
176     /**
177      * Allows an ActionListener to receive notification of a command
178      * being executed prior to the actual execution of the command.
179      *
180      * @param action the Class of the command for which the listener will
181      * notifications for. Class must extend
182      * org.apache.jmeter.gui.action.Command.
183      * @param listener the ActionListener to receive the notifications
184      */

185     public void addPreActionListener(Class JavaDoc action, ActionListener JavaDoc listener)
186     {
187         if (action != null)
188         {
189             HashSet JavaDoc set = (HashSet JavaDoc) preActionListeners.get(action.getName());
190             if (set == null)
191             {
192                 set = new HashSet JavaDoc();
193             }
194             set.add(listener);
195             preActionListeners.put(action.getName(), set);
196         }
197     }
198
199     /**
200      * Allows an ActionListener to be removed from receiving
201      * notifications of a command being executed prior to the actual
202      * execution of the command.
203      *
204      * @param action the Class of the command for which the listener will
205      * notifications for. Class must extend
206      * org.apache.jmeter.gui.action.Command.
207      * @param listener the ActionListener to receive the notifications
208      */

209     public void removePreActionListener(Class JavaDoc action, ActionListener JavaDoc listener)
210     {
211         if (action != null)
212         {
213             HashSet JavaDoc set = (HashSet JavaDoc) preActionListeners.get(action.getName());
214             if (set != null)
215             {
216                 set.remove(listener);
217                 preActionListeners.put(action.getName(), set);
218             }
219         }
220     }
221
222     /**
223      * Allows an ActionListener to receive notification of a command
224      * being executed after the command has executed.
225      *
226      * @param action the Class of the command for which the listener will
227      * notifications for. Class must extend
228      * org.apache.jmeter.gui.action.Command.
229      * @param listener
230      */

231     public void addPostActionListener(Class JavaDoc action, ActionListener JavaDoc listener)
232     {
233         if (action != null)
234         {
235             HashSet JavaDoc set = (HashSet JavaDoc) postActionListeners.get(action.getName());
236             if (set == null)
237             {
238                 set = new HashSet JavaDoc();
239             }
240             set.add(listener);
241             postActionListeners.put(action.getName(), set);
242         }
243     }
244
245     /**
246      * Allows an ActionListener to be removed from receiving
247      * notifications of a command being executed after the command has executed.
248      *
249      * @param action the Class of the command for which the listener will
250      * notifications for. Class must extend
251      * org.apache.jmeter.gui.action.Command.
252      * @param listener
253      */

254     public void removePostActionListener(Class JavaDoc action, ActionListener JavaDoc listener)
255     {
256         if (action != null)
257         {
258             HashSet JavaDoc set = (HashSet JavaDoc) 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 JavaDoc action, ActionEvent JavaDoc e)
268     {
269         if (action != null)
270         {
271             HashSet JavaDoc listenerSet =
272                 (HashSet JavaDoc) preActionListeners.get(action.getName());
273             if (listenerSet != null && listenerSet.size() > 0)
274             {
275                 Object JavaDoc[] listeners = listenerSet.toArray();
276                 for (int i = 0; i < listeners.length; i++)
277                 {
278                     ((ActionListener JavaDoc) listeners[i]).actionPerformed(e);
279                 }
280             }
281         }
282     }
283
284     protected void postActionPerformed(Class JavaDoc action, ActionEvent JavaDoc e)
285     {
286         if (action != null)
287         {
288             HashSet JavaDoc listenerSet =
289                 (HashSet JavaDoc) postActionListeners.get(action.getName());
290             if (listenerSet != null && listenerSet.size() > 0)
291             {
292                 Object JavaDoc[] listeners = listenerSet.toArray();
293                 for (int i = 0; i < listeners.length; i++)
294                 {
295                     ((ActionListener JavaDoc) listeners[i]).actionPerformed(e);
296                 }
297             }
298         }
299     }
300
301     private void populateCommandMap()
302     {
303         List JavaDoc listClasses;
304         Command command;
305         Iterator JavaDoc iterClasses;
306         Class JavaDoc commandClass;
307         try
308         {
309             listClasses =
310                 ClassFinder.findClassesThatExtend(
311                     JMeterUtils.getSearchPaths(),
312                     new Class JavaDoc[] {
313                         Class.forName(
314                             "org.apache.jmeter.gui.action.Command")});
315             commands = new HashMap JavaDoc(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 JavaDoc strClassName = (String JavaDoc) iterClasses.next();
324                 commandClass = Class.forName(strClassName);
325                 if (!Modifier.isAbstract(commandClass.getModifiers()))
326                 {
327                     command = (Command) commandClass.newInstance();
328                     Iterator JavaDoc iter = command.getActionNames().iterator();
329                     while (iter.hasNext())
330                     {
331                         String JavaDoc commandName = (String JavaDoc) iter.next();
332                         Set JavaDoc commandObjects = (Set JavaDoc) commands.get(commandName);
333                         if (commandObjects == null)
334                         {
335                             commandObjects = new HashSet JavaDoc();
336                             commands.put(commandName, commandObjects);
337                         }
338                         commandObjects.add(command);
339                     }
340                 }
341             }
342         }
343         catch (Exception JavaDoc e)
344         {
345             if ("java.awt.HeadlessException".equals(e.getClass().getName())) //JDK1.4:
346
{
347                 log.warn(e.toString());
348             }
349             else
350             {
351                 log.error("exception finding action handlers", e);
352             }
353         }
354     }
355
356     /**
357      * Gets the Instance attribute of the ActionRouter class
358      *
359      *@return The Instance value
360      */

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