1 package net.sourceforge.formview.struts.plugin; 2 3 import java.io.BufferedInputStream ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.StringTokenizer ; 10 11 import javax.servlet.ServletException ; 12 import javax.servlet.UnavailableException ; 13 14 import net.sourceforge.formview.FormViewResources; 15 import net.sourceforge.formview.displayer.DisplayersConfigResources; 16 import net.sourceforge.formview.util.WEBFormViewUtil; 17 import net.sourceforge.formview.validator.ExtendedValidatorResources; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.action.ActionServlet; 22 import org.apache.struts.action.PlugIn; 23 import org.apache.struts.config.ModuleConfig; 24 import org.xml.sax.SAXException ; 25 26 31 public class FormViewPlugIn implements PlugIn { 32 33 36 private static Log log = LogFactory.getLog(FormViewPlugIn.class); 37 38 41 private ActionServlet servlet = null; 42 43 46 private final static String RESOURCE_DELIM = ","; 47 48 52 protected FormViewResources resources = null; 53 54 56 59 private String formViewConfig = null; 60 61 66 public String getFormViewConfig() { 67 return formViewConfig; 68 } 69 70 75 public void setFormViewConfig(String formViewConfig) { 76 this.formViewConfig = formViewConfig; 77 } 78 79 82 private String validatorConfig = null; 83 84 89 public String getValidatorConfig() { 90 return validatorConfig; 91 } 92 93 98 public void setValidatorConfig(String validatorConfig) { 99 this.validatorConfig = validatorConfig; 100 } 101 102 105 private String displayerConfig = null; 106 107 112 public String getDisplayerConfig() { 113 return displayerConfig; 114 } 115 116 121 public void setDisplayerConfig(String displayerConfig) { 122 this.displayerConfig = displayerConfig; 123 } 124 125 public void init(ActionServlet servlet, ModuleConfig config) 126 throws ServletException { 127 129 this.servlet = servlet; 130 131 try { 133 this.initFormViewResources(); 134 WEBFormViewUtil.saveFormViewResources(servlet.getServletContext(), resources); 136 137 } catch (Exception e) { 138 log.error(e.getMessage(), e); 139 throw new UnavailableException ("Cannot load a form view resource from '" + formViewConfig + "'"); 140 } 141 if (validatorConfig != null && validatorConfig.length() > 0) { 143 try { 144 this.mergeWithValidator(); 145 } catch (Exception e) { 146 log.error(e.getMessage(), e); 147 throw new UnavailableException ("Cannot merge validator with form view resource from '" + validatorConfig + "'"); 148 } 149 } 150 try { 152 this.mergeWithDisplayerConfig(); 153 } catch (Exception e) { 154 log.error(e.getMessage(), e); 155 throw new UnavailableException ("Cannot merge displayer config with form view resource from '" + displayerConfig + "'"); 156 } 157 } 158 159 160 public void destroy() { 161 if (log.isDebugEnabled()) { 162 log.debug("Destroying FormViewPlugIn"); 163 } 164 } 165 166 172 protected void initFormViewResources() throws IOException , ServletException { 173 174 if (formViewConfig == null || formViewConfig.length() <= 0) { 175 return; 176 } 177 StringTokenizer st = new StringTokenizer (formViewConfig, RESOURCE_DELIM); 178 179 List streamList = new ArrayList (); 180 try { 181 while (st.hasMoreTokens()) { 182 String formViewRules = st.nextToken().trim(); 183 if (log.isInfoEnabled()) { 184 log.info("Loading form-view rules file from '" + formViewRules + "'"); 185 } 186 187 InputStream input = servlet.getServletContext().getResourceAsStream(formViewRules); 188 189 if (input == null) { 192 input = getClass().getResourceAsStream(formViewRules); 193 } 194 195 if (input != null) { 196 BufferedInputStream bis = new BufferedInputStream (input); 197 streamList.add(bis); 198 } else { 199 throw new ServletException ("Skipping form-view rules file from '" 200 + formViewRules + "'. No stream could be opened."); 201 } 202 } 203 int streamSize = streamList.size(); 204 InputStream [] streamArray = new InputStream [streamSize]; 205 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 206 InputStream is = (InputStream ) streamList.get(streamIndex); 207 streamArray[streamIndex] = is; 208 } 209 210 this.resources = new FormViewResources(streamArray); 211 } catch (SAXException sex) { 212 log.error("Skipping all form-view",sex); 213 throw new ServletException (sex); 214 } finally { 215 Iterator streamIterator = streamList.iterator(); 216 while (streamIterator.hasNext()) { 217 InputStream is = (InputStream ) streamIterator.next(); 218 is.close(); 219 } 220 } 221 } 222 223 224 230 protected void mergeWithValidator() throws IOException , ServletException { 231 232 if (validatorConfig == null || validatorConfig.length() <= 0) { 233 return; 234 } 235 StringTokenizer st = new StringTokenizer (validatorConfig, RESOURCE_DELIM); 236 237 List streamList = new ArrayList (); 238 try { 239 while (st.hasMoreTokens()) { 240 String validatorRules = st.nextToken().trim(); 241 if (log.isInfoEnabled()) { 242 log.info("Loading validation rules file from '" + validatorRules + "'"); 243 } 244 245 InputStream input = servlet.getServletContext().getResourceAsStream(validatorRules); 246 247 if (input == null) { 250 input = getClass().getResourceAsStream(validatorRules); 251 } 252 253 if (input != null) { 254 BufferedInputStream bis = new BufferedInputStream (input); 255 streamList.add(bis); 256 } else { 257 throw new ServletException ("Skipping validation rules file from '" 258 + validatorRules + "'. No stream could be opened."); 259 } 260 } 261 int streamSize = streamList.size(); 262 InputStream [] streamArray = new InputStream [streamSize]; 263 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 264 InputStream is = (InputStream ) streamList.get(streamIndex); 265 streamArray[streamIndex] = is; 266 } 267 268 ExtendedValidatorResources validatorResources = new ExtendedValidatorResources(streamArray); 269 this.resources.mergeFormSetWithValidator(validatorResources); 270 271 } catch (SAXException sex) { 272 log.error("Skipping all validation",sex); 273 throw new ServletException (sex); 274 } finally { 275 Iterator streamIterator = streamList.iterator(); 276 while (streamIterator.hasNext()) { 277 InputStream is = (InputStream ) streamIterator.next(); 278 is.close(); 279 } 280 } 281 282 } 283 284 public void mergeWithDisplayerConfig() throws IOException , ServletException { 285 DisplayersConfigResources displayersResources = null; 287 if (displayerConfig == null || displayerConfig.length() <= 0) { 288 InputStream inDisplayerConfig = null; 289 try { 290 inDisplayerConfig = DisplayersConfigResources.class.getResourceAsStream("displayers-config.xml"); 292 displayersResources = new DisplayersConfigResources(inDisplayerConfig); 293 } catch (SAXException sex) { 294 log.error("Skipping default displayers config",sex); 295 throw new ServletException (sex); 296 } finally { 297 if (inDisplayerConfig != null) { 299 inDisplayerConfig.close(); 300 } 301 } 302 } 303 else { 304 StringTokenizer st = new StringTokenizer (displayerConfig, RESOURCE_DELIM); 305 306 List streamList = new ArrayList (); 307 try { 308 while (st.hasMoreTokens()) { 309 String displayerConfigRules = st.nextToken().trim(); 310 if (log.isInfoEnabled()) { 311 log.info("Loading displayer-config rules file from '" + displayerConfigRules + "'"); 312 } 313 314 InputStream input = servlet.getServletContext().getResourceAsStream(displayerConfigRules); 315 316 if (input == null) { 319 input = getClass().getResourceAsStream(displayerConfigRules); 320 } 321 322 if (input != null) { 323 BufferedInputStream bis = new BufferedInputStream (input); 324 streamList.add(bis); 325 } else { 326 throw new ServletException ("Skipping displayer-config rules file from '" 327 + displayerConfigRules + "'. No stream could be opened."); 328 } 329 } 330 int streamSize = streamList.size(); 331 InputStream [] streamArray = new InputStream [streamSize]; 332 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 333 InputStream is = (InputStream ) streamList.get(streamIndex); 334 streamArray[streamIndex] = is; 335 } 336 337 displayersResources = new DisplayersConfigResources(streamArray); 338 } catch (SAXException sex) { 339 log.error("Skipping all form-view",sex); 340 throw new ServletException (sex); 341 } finally { 342 Iterator streamIterator = streamList.iterator(); 343 while (streamIterator.hasNext()) { 344 InputStream is = (InputStream ) streamIterator.next(); 345 is.close(); 346 } 347 } 348 } 349 if (displayersResources != null) 351 resources.mergeDisplayersWithDisplayerConfig(displayersResources); 352 353 } 354 355 358 protected void destroyResources() { 359 resources = null; 360 } 361 362 } 363 | Popular Tags |