1 23 24 package org.dbforms.servlets; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.apache.commons.validator.ValidatorResources; 32 import org.apache.commons.validator.ValidatorResourcesInitializer; 33 34 import org.apache.log4j.LogManager; 35 import org.apache.log4j.PropertyConfigurator; 36 37 import org.dbforms.config.ConfigLoader; 38 import org.dbforms.config.DbFormsConfig; 39 import org.dbforms.config.DbFormsConfigRegistry; 40 import org.dbforms.config.DbFormsErrors; 41 42 import org.dbforms.util.MessageResources; 43 import org.dbforms.util.Util; 44 45 import org.dbforms.validation.ValidatorConstants; 46 47 import org.xml.sax.SAXException ; 48 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 import java.io.PrintWriter ; 52 53 import java.util.Properties ; 54 55 import javax.servlet.ServletException ; 56 import javax.servlet.UnavailableException ; 57 import javax.servlet.http.HttpServlet ; 58 import javax.servlet.http.HttpServletRequest ; 59 import javax.servlet.http.HttpServletResponse ; 60 61 62 63 70 public class ConfigServlet extends HttpServlet { 71 72 private static Log logCat; 73 74 private transient ConfigLoader loader = new ConfigLoader(); 76 77 79 83 public void destroy() { 84 log("finalizing"); 85 } 86 87 88 97 public void doGet(HttpServletRequest request, 98 HttpServletResponse response) 99 throws IOException , ServletException { 100 process(request, response); 101 } 102 103 104 113 public void doPost(HttpServletRequest request, 114 HttpServletResponse response) 115 throws IOException , ServletException { 116 process(request, response); 117 } 118 119 120 125 public void init() throws ServletException { 126 try { 127 initLogging(); 128 129 loader.setFieldClassName(getServletConfig().getInitParameter("className.Field")); 130 loader.setTableClassName(getServletConfig().getInitParameter("className.Table")); 131 loader.setQueryClassName(getServletConfig().getInitParameter("className.Query")); 132 loader.setForeignKeyClassName(getServletConfig().getInitParameter("className.ForeignKey")); 133 loader.setReferenceClassName(getServletConfig().getInitParameter("className.Reference")); 134 135 initXMLConfig(); 136 initXMLErrors(); 137 initXMLValidator(); 138 initApplicationResources(); 139 initLocaleKey(); 140 } catch (IOException ioe) { 141 ioe.printStackTrace(); 142 } catch (Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 147 148 153 public void initLogging() { 154 String configurationStr = this.getServletConfig() 155 .getInitParameter("log4j.configuration"); 156 boolean usingURL = true; 157 158 if (!Util.isNull(configurationStr)) { 159 try { 160 InputStream fis = getServletContext() 162 .getResourceAsStream(configurationStr); 163 164 if (fis != null) { 165 try { 166 Properties log4jProperties = new Properties (); 167 log4jProperties.load(fis); 168 LogManager.resetConfiguration(); 169 PropertyConfigurator.configure(log4jProperties); 170 } finally { 171 fis.close(); 172 } 173 } else { 174 System.err.println("ConfigServlet::initLogging - log4j.configuration not found!"); 175 } 176 } catch (IOException e) { 177 System.err.println("ConfigServlet::initLogging - log4j.properties not found!"); 178 179 PropertyConfigurator.configure(configurationStr); 180 usingURL = false; 181 } 182 183 logCat = LogFactory.getLog(ConfigServlet.class.getName()); 184 185 logCat.info("### LOGGING INITALIZED, USING URL: " + usingURL + " ###" 187 + configurationStr); 188 } 189 190 200 } 201 202 203 211 protected void initApplicationResources() { 212 logCat.info("initialize Application Resources."); 213 214 String value = getServletConfig() 215 .getInitParameter(ValidatorConstants.RESOURCE_BUNDLE); 216 217 if (value == null) { 218 logCat.warn(" Application Resources file not setted in Web.xml, ApplicationResources handler disabled!"); 219 220 return; 221 } 222 223 MessageResources.setSubClass(value); 224 225 logCat.info(" DbForms Application Resources : SubClass initialized "); 226 } 227 228 229 235 protected void initLocaleKey() { 236 logCat.info("initialize Locale Key for session attribute."); 237 238 String value = getServletConfig() 239 .getInitParameter("localeKey"); 240 241 if (value == null) { 242 logCat.warn(" Locale Key not setted, use \"" 243 + MessageResources.LOCALE_KEY 244 + "\" as key to access the Locale in session scope."); 245 } else { 246 MessageResources.LOCALE_KEY = value.trim(); 247 logCat.info(" Locale Key setted with \"" + MessageResources.LOCALE_KEY 248 + "\" as key to access the Locale in session scope."); 249 } 250 } 251 252 253 259 protected void initXMLConfig() throws IOException , ServletException { 260 String value = getServletConfig() 262 .getInitParameter(DbFormsConfig.CONFIG); 263 264 loader.setConfig(value); 265 266 String [] s = StringUtils.split(loader.getConfig(), ","); 267 268 for (int i = 0; i < s.length; i++) 269 initXMLConfigFile(s[i]); 270 } 271 272 273 281 protected void initXMLConfigFile(String config) 282 throws IOException , ServletException { 283 String realPath = getServletContext() 285 .getRealPath("/"); 286 287 InputStream input = getServletContext() 289 .getResourceAsStream(config); 290 291 if (input == null) { 292 throw new UnavailableException ("configMissing"); 293 } 294 295 try { 296 DbFormsConfigRegistry registry = DbFormsConfigRegistry.instance(); 299 DbFormsConfig dbFormsConfig = null; 300 301 try { 302 dbFormsConfig = registry.lookup(); 303 } catch (Exception e) { 304 dbFormsConfig = null; 305 } 306 307 if (dbFormsConfig == null) { 308 dbFormsConfig = new DbFormsConfig(realPath); 309 310 dbFormsConfig.setServletConfig(getServletConfig()); 312 313 registry.setServletContext(getServletContext()); 315 registry.register(dbFormsConfig); 316 } 317 318 try { 321 loader.loadConfig(input, dbFormsConfig); 322 } catch (SAXException e) { 323 logCat.error("::initXMLConfig - SaxException", e); 324 throw new ServletException (e.toString()); 325 } 326 } finally { 327 input.close(); 328 } 329 } 330 331 332 335 341 protected void initXMLErrors() throws IOException , ServletException { 342 logCat.info("initialize XML Errors."); 343 344 String value = getServletConfig() 346 .getInitParameter(DbFormsErrors.ERRORS); 347 348 loader.setErrors(value); 349 350 InputStream input = getServletContext() 352 .getResourceAsStream(loader.getErrors()); 353 354 if (input == null) { 355 logCat.warn("XML Errors file not found, XML error handler disabled!"); 357 358 return; 359 } 360 361 try { 362 DbFormsErrors dbFormsErrors = new DbFormsErrors(); 364 365 dbFormsErrors.setServletConfig(getServletConfig()); 367 368 getServletContext() 370 .setAttribute(DbFormsErrors.ERRORS, dbFormsErrors); 371 372 try { 373 loader.loadErrors(input, dbFormsErrors); 374 } catch (SAXException e) { 375 throw new ServletException (e.toString()); 376 } 377 378 logCat.info("DbForms Error: " + dbFormsErrors); 379 } finally { 380 input.close(); 381 } 382 } 383 384 385 391 protected void initXMLValidator() throws ServletException { 392 try { 394 System.setProperty("org.apache.commons.logging.Log", 395 "org.apache.commons.logging.impl.Log4JCategoryLog"); 396 } catch (java.security.AccessControlException e) { 397 logCat.warn("Unable map commons-logging to Log4j, due to SecurityManager", 398 e); 399 } 400 401 ValidatorResources resources = new ValidatorResources(); 402 logCat.info("initialize XML Validator."); 403 404 String value; 405 value = getServletConfig() 406 .getInitParameter(ValidatorConstants.VALIDATOR_RULES); 407 408 loader.setValidatorRules(value); 409 410 initXMLValidatorRules(resources, loader.getValidatorRules()); 411 412 value = getServletConfig() 413 .getInitParameter(ValidatorConstants.VALIDATION); 414 415 loader.setValidation(value); 416 417 String [] s = StringUtils.split(loader.getValidation(), ","); 418 419 for (int i = 0; i < s.length; i++) 420 initXMLValidatorValidation(resources, s[i]); 421 422 getServletContext() 424 .setAttribute(ValidatorConstants.VALIDATOR, resources); 425 426 logCat.info(" DbForms Validator : Loaded "); 427 } 428 429 430 439 protected void initXMLValidatorRules(ValidatorResources resources, 440 String validator_rules) 441 throws ServletException { 442 InputStream inputValidatorRules = getServletContext() 444 .getResourceAsStream(validator_rules); 445 446 if (inputValidatorRules == null) { 447 logCat.warn("XML Validator rule file not found, XML Validator handler disabled!"); 449 450 return; 451 } 452 453 try { 457 ValidatorResourcesInitializer.initialize(resources, inputValidatorRules); 458 } catch (IOException e) { 459 logCat.warn("XML Validator Exception ValidatorResourcesInitializer.initialize : " 460 + e.getMessage()); 461 throw new ServletException (e.toString()); 462 } finally { 463 try { 464 inputValidatorRules.close(); 465 } catch (Exception e) { 466 logCat.error("initXMLValidatorRules", e); 467 } 468 } 469 } 470 471 472 481 protected void initXMLValidatorValidation(ValidatorResources resources, 482 String validation) 483 throws ServletException { 484 InputStream inputValidation = getServletContext() 489 .getResourceAsStream(validation); 490 491 if (inputValidation == null) { 492 logCat.warn("XML Validation file not found, XML Validator handler disabled!"); 494 495 return; 496 } 497 498 try { 499 ValidatorResourcesInitializer.initialize(resources, inputValidation); 500 } catch (IOException e) { 501 logCat.warn("XML Validator Exception ValidatorResourcesInitializer.initialize : " 502 + e.getMessage()); 503 throw new ServletException (e.toString()); 504 } finally { 505 try { 506 inputValidation.close(); 507 } catch (Exception e) { 508 logCat.error("initXMLValidatorValidation", e); 509 } 510 } 511 } 512 513 514 523 protected void process(HttpServletRequest request, 524 HttpServletResponse response) 525 throws IOException { 526 PrintWriter out = response.getWriter(); 527 528 try { 529 DbFormsConfig dbFormsConfig = DbFormsConfigRegistry.instance() 530 .lookup(); 531 out.println(dbFormsConfig.toString()); 532 } catch (Exception e) { 533 throw new IOException (e.getMessage()); 534 } 535 } 536 } 537 | Popular Tags |