1 package net.sourceforge.formview.servlet; 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.ServletConfig ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.UnavailableException ; 14 import javax.servlet.http.HttpServlet ; 15 16 import net.sourceforge.formview.FormViewResources; 17 import net.sourceforge.formview.displayer.DisplayersConfigResources; 18 import net.sourceforge.formview.util.WEBFormViewUtil; 19 import net.sourceforge.formview.validator.ExtendedValidatorResources; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.xml.sax.SAXException ; 24 25 55 public class FormViewServlet extends HttpServlet { 56 57 public static final long serialVersionUID = 1L; 58 59 62 private static Log log = LogFactory.getLog(FormViewServlet.class); 63 64 67 private final static String RESOURCE_DELIM = ","; 68 69 73 protected FormViewResources resources = null; 74 75 77 80 private String formViewConfig = null; 81 82 85 private String validatorConfig = null; 86 87 88 91 private String displayerConfig = null; 92 93 public void init() 94 throws ServletException { 95 ServletConfig servletConfig = getServletConfig(); 96 this.formViewConfig = servletConfig.getInitParameter("formViewConfig"); 98 if (formViewConfig == null && formViewConfig.length() < 1) { 99 log.error("Error FormViewServlet : The parameter with name 'formViewConfig' (see section <init-param> of your web.xml) is required."); 100 throw new UnavailableException ("Error FormViewServlet : The parameter with name 'formViewConfig' (see section <init-param> of your web.xml) is required."); 101 } 102 this.displayerConfig = servletConfig.getInitParameter("displayerConfig"); 104 105 this.validatorConfig = servletConfig.getInitParameter("validatorConfig"); 107 108 try { 110 this.initFormViewResources(); 111 WEBFormViewUtil.saveFormViewResources(getServletContext(), resources); 113 114 } catch (Exception e) { 115 log.error(e.getMessage(), e); 116 throw new UnavailableException ("Cannot load a form view resource from '" + formViewConfig + "'"); 117 } 118 if (validatorConfig != null && validatorConfig.length() > 0) { 120 try { 121 this.mergeWithValidator(); 122 } catch (Exception e) { 123 log.error(e.getMessage(), e); 124 throw new UnavailableException ("Cannot merge validator with form view resource from '" + validatorConfig + "'"); 125 } 126 } 127 try { 129 this.mergeWithDisplayerConfig(); 130 } catch (Exception e) { 131 log.error(e.getMessage(), e); 132 throw new UnavailableException ("Cannot merge displayer config with form view resource from '" + displayerConfig + "'"); 133 } 134 } 135 136 137 public void destroy() { 138 if (log.isDebugEnabled()) { 139 log.debug("Destroying FormViewServlet"); 140 } 141 } 142 143 149 protected void initFormViewResources() throws IOException , ServletException { 150 151 if (formViewConfig == null || formViewConfig.length() <= 0) { 152 return; 153 } 154 StringTokenizer st = new StringTokenizer (formViewConfig, RESOURCE_DELIM); 155 156 List streamList = new ArrayList (); 157 try { 158 while (st.hasMoreTokens()) { 159 String formViewRules = st.nextToken().trim(); 160 if (log.isInfoEnabled()) { 161 log.info("Loading form-view rules file from '" + formViewRules + "'"); 162 } 163 164 InputStream input = getServletContext().getResourceAsStream(formViewRules); 165 166 if (input == null) { 169 input = getClass().getResourceAsStream(formViewRules); 170 } 171 172 if (input != null) { 173 BufferedInputStream bis = new BufferedInputStream (input); 174 streamList.add(bis); 175 } else { 176 throw new ServletException ("Skipping form-view rules file from '" 177 + formViewRules + "'. No stream could be opened."); 178 } 179 } 180 int streamSize = streamList.size(); 181 InputStream [] streamArray = new InputStream [streamSize]; 182 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 183 InputStream is = (InputStream ) streamList.get(streamIndex); 184 streamArray[streamIndex] = is; 185 } 186 187 this.resources = new FormViewResources(streamArray); 188 } catch (SAXException sex) { 189 log.error("Skipping all form-view",sex); 190 throw new ServletException (sex); 191 } finally { 192 Iterator streamIterator = streamList.iterator(); 193 while (streamIterator.hasNext()) { 194 InputStream is = (InputStream ) streamIterator.next(); 195 is.close(); 196 } 197 } 198 } 199 200 201 207 protected void mergeWithValidator() throws IOException , ServletException { 208 209 if (validatorConfig == null || validatorConfig.length() <= 0) { 210 return; 211 } 212 StringTokenizer st = new StringTokenizer (validatorConfig, RESOURCE_DELIM); 213 214 List streamList = new ArrayList (); 215 try { 216 while (st.hasMoreTokens()) { 217 String validatorRules = st.nextToken().trim(); 218 if (log.isInfoEnabled()) { 219 log.info("Loading validation rules file from '" + validatorRules + "'"); 220 } 221 222 InputStream input = getServletContext().getResourceAsStream(validatorRules); 223 224 if (input == null) { 227 input = getClass().getResourceAsStream(validatorRules); 228 } 229 230 if (input != null) { 231 BufferedInputStream bis = new BufferedInputStream (input); 232 streamList.add(bis); 233 } else { 234 throw new ServletException ("Skipping validation rules file from '" 235 + validatorRules + "'. No stream could be opened."); 236 } 237 } 238 int streamSize = streamList.size(); 239 InputStream [] streamArray = new InputStream [streamSize]; 240 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 241 InputStream is = (InputStream ) streamList.get(streamIndex); 242 streamArray[streamIndex] = is; 243 } 244 245 ExtendedValidatorResources validatorResources = new ExtendedValidatorResources(streamArray); 246 this.resources.mergeFormSetWithValidator(validatorResources); 247 248 } catch (SAXException sex) { 249 log.error("Skipping all validation",sex); 250 throw new ServletException (sex); 251 } finally { 252 Iterator streamIterator = streamList.iterator(); 253 while (streamIterator.hasNext()) { 254 InputStream is = (InputStream ) streamIterator.next(); 255 is.close(); 256 } 257 } 258 259 } 260 261 public void mergeWithDisplayerConfig() throws IOException , ServletException { 262 DisplayersConfigResources displayersResources = null; 264 if (displayerConfig == null || displayerConfig.length() <= 0) { 265 InputStream inDisplayerConfig = null; 266 try { 267 inDisplayerConfig = DisplayersConfigResources.class.getResourceAsStream("displayers-config.xml"); 269 displayersResources = new DisplayersConfigResources(inDisplayerConfig); 270 } catch (SAXException sex) { 271 log.error("Skipping default displayers config",sex); 272 throw new ServletException (sex); 273 } finally { 274 if (inDisplayerConfig != null) { 276 inDisplayerConfig.close(); 277 } 278 } 279 } 280 else { 281 StringTokenizer st = new StringTokenizer (displayerConfig, RESOURCE_DELIM); 282 283 List streamList = new ArrayList (); 284 try { 285 while (st.hasMoreTokens()) { 286 String displayerConfigRules = st.nextToken().trim(); 287 if (log.isInfoEnabled()) { 288 log.info("Loading displayer-config rules file from '" + displayerConfigRules + "'"); 289 } 290 291 InputStream input = getServletContext().getResourceAsStream(displayerConfigRules); 292 293 if (input == null) { 296 input = getClass().getResourceAsStream(displayerConfigRules); 297 } 298 299 if (input != null) { 300 BufferedInputStream bis = new BufferedInputStream (input); 301 streamList.add(bis); 302 } else { 303 throw new ServletException ("Skipping displayer-config rules file from '" 304 + displayerConfigRules + "'. No stream could be opened."); 305 } 306 } 307 int streamSize = streamList.size(); 308 InputStream [] streamArray = new InputStream [streamSize]; 309 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) { 310 InputStream is = (InputStream ) streamList.get(streamIndex); 311 streamArray[streamIndex] = is; 312 } 313 314 displayersResources = new DisplayersConfigResources(streamArray); 315 } catch (SAXException sex) { 316 log.error("Skipping all form-view",sex); 317 throw new ServletException (sex); 318 } finally { 319 Iterator streamIterator = streamList.iterator(); 320 while (streamIterator.hasNext()) { 321 InputStream is = (InputStream ) streamIterator.next(); 322 is.close(); 323 } 324 } 325 } 326 if (displayersResources != null) 328 resources.mergeDisplayersWithDisplayerConfig(displayersResources); 329 330 } 331 332 } 333 | Popular Tags |