1 18 19 package org.apache.struts.plugins; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 25 import javax.servlet.ServletException ; 26 27 import org.apache.commons.digester.Digester; 28 import org.apache.commons.digester.RuleSet; 29 import org.apache.commons.digester.xmlrules.DigesterLoader; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.action.ActionServlet; 33 import org.apache.struts.action.PlugIn; 34 import org.apache.struts.config.ModuleConfig; 35 import org.apache.struts.util.RequestUtils; 36 import org.xml.sax.SAXException ; 37 38 48 public class DigestingPlugIn implements PlugIn { 49 50 53 private static Log log = LogFactory.getLog(DigestingPlugIn.class); 54 55 protected static final String SOURCE_CLASSPATH = "classpath"; 56 57 protected static final String SOURCE_FILE = "file"; 58 59 protected static final String SOURCE_SERVLET = "servlet"; 60 61 protected String configPath = null; 62 63 protected String configSource = SOURCE_SERVLET; 64 65 protected String digesterPath = null; 66 67 protected String digesterSource = SOURCE_SERVLET; 68 69 protected String key = null; 70 71 protected ModuleConfig moduleConfig = null; 72 73 protected String rulesets = null; 74 75 protected ActionServlet servlet = null; 76 77 protected boolean push = false; 78 79 82 public DigestingPlugIn() { 83 super(); 84 } 85 86 89 public void destroy() { 90 this.servlet = null; 91 this.moduleConfig = null; 92 } 93 94 107 public void init(ActionServlet servlet, ModuleConfig config) 108 throws ServletException { 109 110 this.servlet = servlet; 111 this.moduleConfig = config; 112 113 Object obj = null; 114 115 Digester digester = this.initializeDigester(); 116 117 if (this.push) { 118 log.debug("push == true; pushing plugin onto digester stack"); 119 digester.push(this); 120 } 121 122 try { 123 log.debug("XML data file: [path: " 124 + this.configPath 125 + ", source: " 126 + this.configSource + "]"); 127 128 URL configURL = this.getConfigURL(this.configPath, this.configSource); 129 if (configURL == null) throw new ServletException ("Unable to locate XML data file at [path: " 130 + this.configPath 131 + ", source: " 132 + this.configSource + "]"); 133 obj = digester.parse(configURL.openStream()); 134 135 } catch (IOException e) { 136 log.error("Exception processing config", e); 138 throw new ServletException (e); 139 140 } catch (SAXException e) { 141 log.error("Exception processing config", e); 143 throw new ServletException (e); 144 } 145 146 this.storeGeneratedObject(obj); 147 } 148 149 155 protected Digester initializeDigester() throws ServletException { 156 Digester digester = null; 157 158 if (this.digesterPath != null && this.digesterSource != null) { 159 160 try { 161 log.debug("Initialize digester from XML [path: " 162 + this.digesterPath 163 + "; source: " 164 + this.digesterSource + "]"); 165 digester = 166 this.digesterFromXml(this.digesterPath, this.digesterSource); 167 168 } catch (IOException e) { 169 log.error("Exception instantiating digester from XML ", e); 171 throw new ServletException (e); 172 173 } 174 175 } else { 176 log.debug("No XML rules for digester; call newDigesterInstance()"); 177 digester = this.newDigesterInstance(); 178 } 179 180 this.applyRuleSets(digester); 181 182 return digester; 183 } 184 185 191 protected Digester newDigesterInstance() { 192 return new Digester(); 193 } 194 195 205 protected Digester digesterFromXml(String path, String source) 206 throws IOException { 207 208 URL configURL = this.getConfigURL(path, source); 209 if (configURL == null) 210 { 211 throw new NullPointerException ("No resource '" + path + "' found in '" + source + "'"); 212 } 213 return DigesterLoader.createDigester(configURL); 214 } 215 216 223 protected void applyRuleSets(Digester digester) throws ServletException { 224 225 if (this.rulesets == null || this.rulesets.trim().length() == 0) { 226 return; 227 } 228 229 rulesets = rulesets.trim(); 230 String ruleSet = null; 231 while (rulesets.length() > 0) { 232 int comma = rulesets.indexOf(","); 233 if (comma < 0) { 234 ruleSet = rulesets.trim(); 235 rulesets = ""; 236 } else { 237 ruleSet = rulesets.substring(0, comma).trim(); 238 rulesets = rulesets.substring(comma + 1).trim(); 239 } 240 241 if (log.isDebugEnabled()) { 242 log.debug("Configuring custom Digester Ruleset of type " + ruleSet); 244 } 245 246 try { 247 RuleSet instance = 248 (RuleSet) RequestUtils.applicationInstance(ruleSet); 249 250 digester.addRuleSet(instance); 251 252 } catch (Exception e) { 253 log.error("Exception configuring custom Digester RuleSet", e); 255 throw new ServletException (e); 256 } 257 } 258 } 259 260 274 protected URL getConfigURL(String path, String source) throws IOException { 275 276 if (SOURCE_CLASSPATH.equals(source)) { 277 return this.getClassPathURL(path); 278 } 279 280 if (SOURCE_FILE.equals(source)) { 281 return this.getFileURL(path); 282 } 283 284 if (SOURCE_SERVLET.equals(source)) { 285 return this.getServletContextURL(path); 286 } 287 288 throw new IllegalArgumentException ( 290 "ConfigSource " + source + " is not recognized"); 291 } 292 293 299 protected URL getClassPathURL(String path) { 300 return getClass().getClassLoader().getResource(path); 301 } 302 303 309 protected URL getServletContextURL(String path) throws IOException { 310 return this.servlet.getServletContext().getResource(path); 311 } 312 313 320 protected URL getFileURL(String path) throws IOException { 321 File file = new File (path); 322 return file.toURL(); 323 } 324 325 329 public void setConfigPath(String configPath) { 330 this.configPath = configPath; 331 } 332 333 337 public String getConfigPath() { 338 return configPath; 339 } 340 341 360 public void setConfigSource(String configSource) { 361 this.configSource = configSource; 362 } 363 364 369 public String getConfigSource() { 370 return configSource; 371 } 372 373 379 protected void storeGeneratedObject(Object obj) { 380 log.debug("Put [" + obj + "] into application context [key:" + this.key + "]"); 381 this.servlet.getServletContext().setAttribute(this.getKey(), obj); 382 } 383 384 388 public void setKey(String key) { 389 this.key = key; 390 } 391 392 396 public String getKey() { 397 return key; 398 } 399 400 404 public void setRulesets(String ruleSets) { 405 this.rulesets = ruleSets; 406 } 407 408 411 public String getRulesets() { 412 return this.rulesets; 413 } 414 415 421 public void setDigesterPath(String digesterPath) { 422 this.digesterPath = digesterPath; 423 } 424 425 430 public String getDigesterPath() { 431 return digesterPath; 432 } 433 434 441 public void setDigesterSource(String digesterSource) { 442 this.digesterSource = digesterSource; 443 } 444 445 450 public String getDigesterSource() { 451 return this.digesterSource; 452 } 453 454 461 public void setPush(boolean push) { 462 this.push = push; 463 } 464 465 470 public boolean getPush() { 471 return this.push; 472 } 473 474 } 475 | Popular Tags |