1 18 19 package org.apache.struts.validator; 20 21 import org.xml.sax.SAXException ; 22 23 import java.util.StringTokenizer ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.io.BufferedInputStream ; 28 import java.io.InputStream ; 29 import java.io.IOException ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.UnavailableException ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.commons.validator.ValidatorResources; 36 import org.apache.struts.action.ActionServlet; 37 import org.apache.struts.action.PlugIn; 38 import org.apache.struts.config.ModuleConfig; 39 40 47 public class ValidatorPlugIn implements PlugIn { 48 49 52 private static Log log = LogFactory.getLog(ValidatorPlugIn.class); 53 54 57 private ModuleConfig config = null; 58 59 62 private ActionServlet servlet = null; 63 64 67 private final static String RESOURCE_DELIM = ","; 68 69 72 public final static String VALIDATOR_KEY = 73 "org.apache.commons.validator.VALIDATOR_RESOURCES"; 74 75 80 public final static String STOP_ON_ERROR_KEY = 81 "org.apache.struts.validator.STOP_ON_ERROR"; 82 83 87 protected ValidatorResources resources = null; 88 89 90 92 95 private String pathnames = null; 96 97 102 public String getPathnames() { 103 return pathnames; 104 } 105 106 111 public void setPathnames(String pathnames) { 112 this.pathnames = pathnames; 113 } 114 115 120 private boolean stopOnFirstError = true; 121 122 130 public boolean isStopOnFirstError() { 131 return this.stopOnFirstError; 132 } 133 134 142 public void setStopOnFirstError(boolean stopOnFirstError) { 143 this.stopOnFirstError = stopOnFirstError; 144 } 145 146 153 public void init(ActionServlet servlet, ModuleConfig config) 154 throws ServletException { 155 156 this.config = config; 158 this.servlet = servlet; 159 160 try { 162 this.initResources(); 163 164 servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(), 165 resources); 166 167 servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(), 168 (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE)); 169 170 } catch (Exception e) { 171 log.error(e.getMessage(), e); 172 throw new UnavailableException ("Cannot load a validator resource from '" + pathnames + "'"); 173 } 174 175 } 176 177 181 public void destroy() { 182 183 if (log.isDebugEnabled()) { 184 log.debug("Destroying ValidatorPlugin"); 185 } 186 187 servlet = null; 188 config = null; 189 190 destroyResources(); 191 } 192 193 199 protected void initResources() throws IOException , ServletException { 200 201 if (pathnames == null || pathnames.length() <= 0) { 202 return; 203 } 204 StringTokenizer st = new StringTokenizer (pathnames, RESOURCE_DELIM); 205 206 List streamList = new ArrayList (); 207 try { 208 while (st.hasMoreTokens()) { 209 String validatorRules = st.nextToken().trim(); 210 if (log.isInfoEnabled()) { 211 log.info("Loading validation rules file from '" + validatorRules + "'"); 212 } 213 214 InputStream input = servlet.getServletContext().getResourceAsStream(validatorRules); 215 216 if (input == null) { 219 input = getClass().getResourceAsStream(validatorRules); 220 } 221 222 if (input != null) { 223 BufferedInputStream bis = new BufferedInputStream (input); 224 streamList.add(bis); 225 } else { 226 throw new ServletException ("Skipping validation rules file from '" 227 + validatorRules + "'. No stream could be opened."); 228 } 229 } 230 int streamSize = streamList.size(); 231 InputStream [] streamArray = new InputStream [streamSize]; 232 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 233 InputStream is = (InputStream ) streamList.get(streamIndex); 234 streamArray[streamIndex] = is; 235 } 236 237 this.resources = new ValidatorResources(streamArray); 238 } catch (SAXException sex) { 239 log.error("Skipping all validation",sex); 240 throw new ServletException (sex); 241 } finally { 242 Iterator streamIterator = streamList.iterator(); 243 while (streamIterator.hasNext()) { 244 InputStream is = (InputStream ) streamIterator.next(); 245 is.close(); 246 } 247 } 248 249 } 250 251 254 protected void destroyResources() { 255 resources = null; 256 } 257 258 } 259 | Popular Tags |