1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.validator.faces; |
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.net.MalformedURLException; |
22 | |
import java.net.URL; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import javax.faces.FacesException; |
29 | |
import javax.servlet.ServletContext; |
30 | |
import javax.servlet.ServletContextEvent; |
31 | |
import javax.servlet.ServletContextListener; |
32 | |
import org.apache.commons.beanutils.PropertyUtils; |
33 | |
import org.apache.commons.logging.Log; |
34 | |
import org.apache.commons.logging.LogFactory; |
35 | |
import org.apache.commons.validator.ValidatorAction; |
36 | |
import org.apache.commons.validator.ValidatorResources; |
37 | |
import org.apache.shale.validator.Globals; |
38 | |
import org.apache.shale.validator.util.ShaleValidatorAction; |
39 | |
import org.xml.sax.SAXException; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 93 | public class ValidatorLifecycleListener implements ServletContextListener { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 94 | private Log log = LogFactory.getLog(ValidatorLifecycleListener.class); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void contextDestroyed(ServletContextEvent event) { |
70 | |
|
71 | 93 | if (log.isInfoEnabled()) { |
72 | 93 | log.info("Finalizing Validator Integration"); |
73 | |
} |
74 | |
|
75 | |
|
76 | 93 | event.getServletContext().removeAttribute(Globals.VALIDATOR_ACTIONS); |
77 | 93 | event.getServletContext().removeAttribute(Globals.VALIDATOR_RESOURCES); |
78 | |
|
79 | |
|
80 | 93 | PropertyUtils.clearDescriptors(); |
81 | 93 | LogFactory.release(Thread.currentThread().getContextClassLoader()); |
82 | 93 | log = null; |
83 | |
|
84 | 93 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void contextInitialized(ServletContextEvent event) { |
93 | |
|
94 | 93 | if (log.isInfoEnabled()) { |
95 | 93 | log.info("Initializing Validator Integration"); |
96 | |
} |
97 | |
|
98 | |
|
99 | 93 | ServletContext context = event.getServletContext(); |
100 | 93 | ValidatorResources resources = null; |
101 | |
try { |
102 | 93 | resources = validatorResources(context); |
103 | |
} catch (IllegalArgumentException e) { |
104 | |
throw e; |
105 | |
} catch (IOException e) { |
106 | |
throw new FacesException(e); |
107 | |
} catch (SAXException e) { |
108 | |
throw new FacesException(e); |
109 | 93 | } |
110 | 93 | context.setAttribute(Globals.VALIDATOR_RESOURCES, resources); |
111 | |
|
112 | |
|
113 | |
|
114 | 93 | Map actions = validatorActions(resources); |
115 | 93 | context.setAttribute(Globals.VALIDATOR_ACTIONS, actions); |
116 | |
|
117 | 93 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private Map validatorActions(ValidatorResources resources) { |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 93 | Map map = new HashMap(); |
139 | 93 | Iterator entries = resources.getValidatorActions().entrySet().iterator(); |
140 | 1809 | while (entries.hasNext()) { |
141 | 1716 | Map.Entry entry = (Map.Entry) entries.next(); |
142 | 1716 | if ("includeJavaScriptUtilities".equals(entry.getKey())) { |
143 | 93 | continue; |
144 | |
} |
145 | 1623 | map.put((String) entry.getKey(), |
146 | |
new ShaleValidatorAction(resources, |
147 | |
(ValidatorAction) entry.getValue())); |
148 | 1623 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | 93 | ShaleValidatorAction[] result = null; |
154 | 93 | Map results = new HashMap(); |
155 | 93 | List list = null; |
156 | 93 | Iterator actions = map.entrySet().iterator(); |
157 | 1716 | while (actions.hasNext()) { |
158 | 1623 | Map.Entry action = (Map.Entry) actions.next(); |
159 | 1623 | list = |
160 | |
((ShaleValidatorAction) action.getValue()).getAction().getDependencyList(); |
161 | 1623 | if (list == null) { |
162 | |
list = new ArrayList(0); |
163 | |
} |
164 | 1623 | result = new ShaleValidatorAction[list.size() + 1]; |
165 | 1902 | for (int i = 0; i < list.size(); i++) { |
166 | 279 | result[i] = (ShaleValidatorAction) map.get((String) list.get(i)); |
167 | 279 | if (result[i] == null) { |
168 | |
throw new IllegalArgumentException((String) action.getKey()); |
169 | |
} |
170 | |
} |
171 | 1623 | result[result.length - 1] = (ShaleValidatorAction) action.getValue(); |
172 | 1623 | results.put((String) action.getKey(), result); |
173 | 1623 | } |
174 | |
|
175 | |
|
176 | 93 | return results; |
177 | |
|
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private ValidatorResources validatorResources(ServletContext context) |
200 | |
throws IOException, SAXException { |
201 | |
|
202 | |
|
203 | 93 | List urls = new ArrayList(); |
204 | 93 | URL url = null; |
205 | 93 | boolean didDefault = false; |
206 | 93 | String pathnames = context.getInitParameter(Globals.VALIDATOR_RULES); |
207 | 93 | if (pathnames != null) { |
208 | 21 | pathnames = pathnames.trim(); |
209 | 63 | while (pathnames.length() > 0) { |
210 | |
|
211 | |
|
212 | 42 | int comma = pathnames.indexOf(','); |
213 | 42 | String pathname = null; |
214 | 42 | if (comma >= 0) { |
215 | 21 | pathname = pathnames.substring(0, comma).trim(); |
216 | 21 | pathnames = pathnames.substring(comma + 1); |
217 | 21 | } else { |
218 | 21 | pathname = pathnames.trim(); |
219 | 21 | pathnames = ""; |
220 | |
} |
221 | 42 | if (pathname.length() < 1) { |
222 | |
break; |
223 | |
} |
224 | |
|
225 | |
|
226 | |
try { |
227 | 42 | url = context.getResource(pathname); |
228 | |
} catch (MalformedURLException e) { |
229 | |
throw new IllegalArgumentException("MalformedURLException:" |
230 | |
+ " The URL '" + pathname |
231 | |
+ "' specified as a validator rules resource is malformed."); |
232 | 42 | } |
233 | 42 | if (url == null) { |
234 | 42 | url = ValidatorLifecycleListener.class.getResource(pathname); |
235 | |
} |
236 | 42 | if (url == null) { |
237 | |
throw new IllegalArgumentException(pathname); |
238 | |
} |
239 | 42 | urls.add(url); |
240 | 42 | if (Globals.DEFAULT_VALIDATOR_RULES.equals(pathname)) { |
241 | 21 | didDefault = true; |
242 | |
} |
243 | |
|
244 | 42 | } |
245 | |
} |
246 | |
|
247 | |
|
248 | 93 | if (!didDefault) { |
249 | |
try { |
250 | 72 | url = context.getResource(Globals.DEFAULT_VALIDATOR_RULES); |
251 | |
} catch (MalformedURLException e) { |
252 | |
throw new IllegalArgumentException("MalformedURLException:" |
253 | |
+ " The URL '" + Globals.DEFAULT_VALIDATOR_RULES |
254 | |
+ "' specified as a validator rules resource is malformed."); |
255 | 72 | } |
256 | 72 | if (url == null) { |
257 | 72 | url = ValidatorLifecycleListener.class.getResource(Globals.DEFAULT_VALIDATOR_RULES); |
258 | |
} |
259 | 72 | if (url == null) { |
260 | |
throw new IllegalArgumentException(Globals.DEFAULT_VALIDATOR_RULES); |
261 | |
} |
262 | 72 | urls.add(url); |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | 93 | String[] array = new String[urls.size()]; |
269 | 207 | for (int i = 0; i < array.length; i++) { |
270 | 114 | array[i] = ((URL) urls.get(i)).toExternalForm(); |
271 | |
} |
272 | |
|
273 | |
|
274 | 93 | return new ValidatorResources(array); |
275 | |
|
276 | |
} |
277 | |
|
278 | |
|
279 | |
} |