1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.validator;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.validator.ValidatorResources;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.action.PlugIn;
28 import org.apache.struts.config.ModuleConfig;
29 import org.xml.sax.SAXException;
30
31 import javax.servlet.ServletException;
32 import javax.servlet.UnavailableException;
33
34 import java.io.IOException;
35
36 import java.net.URL;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.StringTokenizer;
41
42
43
44
45
46
47
48
49
50 public class ValidatorPlugIn implements PlugIn {
51
52
53
54 private static Log log = LogFactory.getLog(ValidatorPlugIn.class);
55
56
57
58
59 private final static String RESOURCE_DELIM = ",";
60
61
62
63
64
65 public final static String VALIDATOR_KEY =
66 "org.apache.commons.validator.VALIDATOR_RESOURCES";
67
68
69
70
71
72
73 public final static String STOP_ON_ERROR_KEY =
74 "org.apache.struts.validator.STOP_ON_ERROR";
75
76
77
78
79 private ModuleConfig config = null;
80
81
82
83
84 private ActionServlet servlet = null;
85
86
87
88
89
90 protected ValidatorResources resources = null;
91
92
93
94
95
96
97 private String pathnames = null;
98
99
100
101
102
103
104 private boolean stopOnFirstError = true;
105
106
107
108
109
110
111 public String getPathnames() {
112 return pathnames;
113 }
114
115
116
117
118
119
120 public void setPathnames(String pathnames) {
121 this.pathnames = pathnames;
122 }
123
124
125
126
127
128
129
130
131
132 public boolean isStopOnFirstError() {
133 return this.stopOnFirstError;
134 }
135
136
137
138
139
140
141
142
143
144
145 public void setStopOnFirstError(boolean stopOnFirstError) {
146 this.stopOnFirstError = stopOnFirstError;
147 }
148
149
150
151
152
153
154
155
156 public void init(ActionServlet servlet, ModuleConfig config)
157 throws ServletException {
158
159 this.config = config;
160 this.servlet = servlet;
161
162
163 try {
164 this.initResources();
165
166 servlet.getServletContext().setAttribute(VALIDATOR_KEY
167 + config.getPrefix(), resources);
168
169 servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.'
170 + config.getPrefix(),
171 (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
172 } catch (Exception e) {
173 log.error(e.getMessage(), e);
174 throw new UnavailableException(
175 "Cannot load a validator resource from '" + pathnames + "'");
176 }
177 }
178
179
180
181
182
183 public void destroy() {
184 if (log.isDebugEnabled()) {
185 log.debug("Destroying ValidatorPlugin");
186 }
187
188 servlet = null;
189 config = null;
190
191 destroyResources();
192 }
193
194
195
196
197
198
199
200 protected void initResources()
201 throws IOException, ServletException {
202 if ((pathnames == null) || (pathnames.length() <= 0)) {
203 return;
204 }
205
206 StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
207
208 List urlList = new ArrayList();
209
210 try {
211 while (st.hasMoreTokens()) {
212 String validatorRules = st.nextToken().trim();
213
214 if (log.isInfoEnabled()) {
215 log.info("Loading validation rules file from '"
216 + validatorRules + "'");
217 }
218
219 URL input =
220 servlet.getServletContext().getResource(validatorRules);
221
222
223
224 if (input == null) {
225 input = getClass().getResource(validatorRules);
226 }
227
228 if (input != null) {
229 urlList.add(input);
230 } else {
231 throw new ServletException(
232 "Skipping validation rules file from '"
233 + validatorRules + "'. No url could be located.");
234 }
235 }
236
237 int urlSize = urlList.size();
238 URL[] urlArray = new URL[urlSize];
239
240 for (int urlIndex = 0; urlIndex < urlSize; urlIndex++) {
241 urlArray[urlIndex] = (URL) urlList.get(urlIndex);
242 }
243
244 this.resources = new ValidatorResources(urlArray);
245 } catch (SAXException sex) {
246 log.error("Skipping all validation", sex);
247 throw new ServletException(sex);
248 }
249 }
250
251
252
253
254 protected void destroyResources() {
255 resources = null;
256 }
257 }