KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > DispatcherSupport


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Fires events to listeners depending on parameters in the HttpServletRequest.
24  * <ul>
25  * <li>If a listener is registered with name <em>and</em> value, it receives the event
26  * if the request contains a paramter with that name and value.
27  * <li>If a listener is registered with name only, it receives the event if the request
28  * contains a parameter with that name and any value.
29  * <li>If a listener is registered with value only, it receives the event if the request
30  * contains a parameter with that value and any name.
31  * <li>If a listener is registered with no name and no value, it receives every request.
32  * </ul>
33  */

34 public class DispatcherSupport implements Dispatcher {
35
36   // key = listener, value = name/value pair separated by '='
37
private HashMap JavaDoc map = new HashMap JavaDoc();
38
39   // key = name/value pair, value = List of listeners
40
private HashMap JavaDoc inverseMap = new HashMap JavaDoc();
41
42   /**
43    * Adds a listener. A listener can only be registered once.
44    * If its registered more than one, the last name/value
45    * pair will be used.
46    *
47    * @param name name of the request parameter or null
48    * @param value of the request parameter or null
49    * @param listener the listener to register
50    */

51   public void addRequestListener(String JavaDoc name, String JavaDoc value, RequestListener listener) {
52     removeRequestListener(listener);
53     String JavaDoc key = getKey(name, value);
54     map.put(listener, key);
55     List JavaDoc list = (List JavaDoc) inverseMap.get(key);
56     if (list == null) {
57       list = new LinkedList JavaDoc();
58       inverseMap.put(key, list);
59     }
60     list.add(listener);
61   }
62
63   /**
64    * removes a listener.
65    * @param listener the listener to remove
66    */

67   public void removeRequestListener(RequestListener listener) {
68     String JavaDoc key = (String JavaDoc) map.get(listener);
69     if (key != null) {
70       map.remove(listener);
71       List JavaDoc list = (List JavaDoc) inverseMap.get(key);
72       if (list != null)
73         list.remove(listener);
74     }
75   }
76
77   /**
78    * removes all listeners
79    */

80   public void clear() {
81     map.clear();
82     inverseMap.clear();
83   }
84
85   String JavaDoc getKey(String JavaDoc name, String JavaDoc value) {
86     if ((name != null) && (value != null)) {
87       return name + "=" + value;
88     } else if (name != null) {
89       return name + "=";
90     } else if (value != null) {
91       return "=" + value;
92     }
93     return "=";
94   }
95
96   /**
97    * finds all RequestListeners that match a name/value pair in requestParameters
98    *
99    * @param requestParameters HttpServletRequest.getParameterMap()
100    * @return List of RequestListeners
101    */

102   List JavaDoc findAll(Map JavaDoc requestParameters) {
103     List JavaDoc match = new LinkedList JavaDoc();
104     Iterator JavaDoc it = requestParameters.keySet().iterator();
105
106     while (it.hasNext()) {
107       String JavaDoc name = (String JavaDoc) it.next();
108       String JavaDoc[] values = (String JavaDoc[]) requestParameters.get(name);
109
110       if (values != null) {
111         for (int i = 0; i < values.length; i++) {
112
113           // empty string will be caught later, when we test for the name only
114
if (values[i] == null || values[i].length() == 0)
115             continue;
116
117           // try name and value
118
Object JavaDoc obj = inverseMap.get(name + "=" + values[i]);
119
120           if (obj != null) {
121             match.addAll((List JavaDoc) obj);
122           }
123
124           // try value only
125
obj = inverseMap.get("=" + values[i]);
126
127           if (obj != null) {
128             match.addAll((List JavaDoc) obj);
129           }
130         }
131       }
132
133       // try name only
134
Object JavaDoc obj = inverseMap.get(name + "=");
135       if (obj != null)
136         match.addAll((List JavaDoc) obj);
137
138       // imagebutton support
139
if (name.endsWith(".x")) {
140         name = name.substring(0, name.length() - 2);
141         obj = inverseMap.get(name + "=");
142         if (obj != null)
143           match.addAll((List JavaDoc) obj);
144       }
145     }
146
147     // try default handler
148
Object JavaDoc obj = inverseMap.get("=");
149
150     if (obj != null) {
151       match.addAll((List JavaDoc) obj);
152     }
153
154     return match;
155   }
156
157   /**
158    * fires event to all matching listeners
159    * @param request the current request
160    * @throws Exception the exception from listeners
161    */

162   public void request(RequestContext context) throws Exception JavaDoc {
163     Iterator JavaDoc it = findAll(context.getRequest().getParameterMap()).iterator();
164
165     while (it.hasNext()) {
166       RequestListener listener = (RequestListener) it.next();
167       listener.request(context);
168     }
169   }
170
171   /**
172    * returns the leaf RequestListeners that would be invoked for a http request
173    * containing <code>httpParams</code>
174    */

175   public List JavaDoc findMatchingListeners(Map JavaDoc httpParams) {
176     List JavaDoc candidates = findAll(httpParams);
177     List JavaDoc result = new ArrayList JavaDoc();
178     for (Iterator JavaDoc it = candidates.iterator(); it.hasNext();) {
179       Object JavaDoc obj = it.next();
180       if (obj instanceof Dispatcher) {
181         Dispatcher d = (Dispatcher) obj;
182         result.addAll(d.findMatchingListeners(httpParams));
183       } else {
184         result.add(obj);
185       }
186     }
187     return result;
188   }
189
190 }
Popular Tags