1 6 7 package com.quikj.application.communicator.admin.controller; 8 9 import java.util.*; 10 import java.io.*; 11 12 import javax.xml.parsers.*; 14 import org.xml.sax.*; 15 import org.w3c.dom.*; 16 17 import com.quikj.server.framework.*; 18 19 23 public class AdminConfig 24 { 25 26 27 private DBParams dBParams; 28 29 30 private PluginParameters params; 31 32 33 private ArrayList applications; 34 35 36 private String errorMessage; 37 38 private String configFilePath; 39 private long lastModTime; 40 private DocumentBuilderFactory docBuilderFactory; 41 private static AdminConfig instance = null; 42 43 private static final String ROOT_NODE_NAME = "communicator-configuration"; 44 45 private static final String EL_DB_PARAMS = "db-params"; 46 private static final String ATT_DB_CLASS = "class"; 47 private static final String ATT_DB_URL = "url"; 48 private static final String ATT_DB_HOST = "host"; 49 private static final String ATT_DB_USER = "user"; 50 private static final String ATT_DB_PASSWORD = "password"; 51 private static final String ATT_DB_DATABASE = "admin-db"; 52 53 private static final String EL_APPLICATION = "application"; 54 private static final String ATT_APP_NAME = "name"; 55 private static final String ATT_APP_CLASS = "class"; 56 private static final String ATT_APP_DISPLAY = "display-name"; 57 private static final String ATT_APP_FORWARD = "forward-name"; 58 59 private static final String EL_PARAM = "param"; 60 private static final String ATT_PARAM_NAME = "name"; 61 private static final String ATT_PARAM_VALUE = "value"; 62 63 private static final String EL_RACCESS = "remote-access"; 64 private static final String ATT_RURL = "url"; 65 private static final String ATT_RHOST = "host"; 66 private static final String ATT_HTTPS_RSERVICE = "https-service-name"; 67 68 private static final String EL_LOG_CONFIG = "log-parms"; 69 70 private static final String EL_MAIL_PARAMS = "mail-params"; 71 private static final String ATT_MAIL_PATH = "path"; 72 73 private static final String EL_LICENSE_PARAMS = "license-params"; 74 private static final String ATT_LICENSE_PATH = "path"; 75 76 private static final String EL_DISPLAY_PARAMS = "display"; 77 private static final String ATT_IMAGE = "image"; 78 private static final String ATT_COMPANY = "company"; 79 private static final String ATT_COMPANY_URL = "url"; 80 81 private static final String EL_LINK_PARAMS = "link"; 82 private static final String ATT_LINK_URL = "url"; 83 private static final String ATT_LINK_CAPTION = "caption"; 84 85 86 private RemoteAccessParams remoteAccessParams; 87 88 89 private AceConfigLogParams logParams; 90 91 92 private String mailConfigPath; 93 94 95 private String licenseConfigPath; 96 97 98 private List links; 99 100 101 private MenuLinks menuProperties; 102 103 104 public AdminConfig(String path) { 106 configFilePath = path; 107 108 docBuilderFactory = DocumentBuilderFactory.newInstance(); 109 docBuilderFactory.setValidating(false); 110 docBuilderFactory.setIgnoringComments(true); 111 docBuilderFactory.setIgnoringElementContentWhitespace(true); 112 docBuilderFactory.setCoalescing(true); 113 114 instance = this; 115 } 116 117 public static AdminConfig getInstance() 118 { 119 return instance; 120 } 121 122 public boolean loadConfigurationFile() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 124 { 125 File file = new File(configFilePath); 126 lastModTime = file.lastModified(); 127 128 FileInputStream fis = new FileInputStream(file); 129 130 DocumentBuilder doc_builder = docBuilderFactory.newDocumentBuilder(); 131 Document doc = doc_builder.parse(fis); 132 133 boolean ret = processDoc(doc); 134 135 fis.close(); 136 137 return ret; 138 } 139 140 public boolean isModified() 141 { 142 File file = new File(configFilePath); 143 long mod_time = file.lastModified(); 144 145 if (mod_time > lastModTime) 146 { 147 return true; 148 } 149 else 150 { 151 return false; 152 } 153 } 154 155 private boolean processDoc(Document doc) 156 { 157 DBParams db_params = new DBParams(); 158 PluginParameters param_list = new PluginParameters(); 159 ArrayList app_list = new ArrayList(); 160 RemoteAccessParams raccess = new RemoteAccessParams(); 161 AceConfigLogParams lparms = null; 162 String mail_path = null; 163 String license_path = null; 164 MenuLinks ml = null; 165 166 if (doc.getNodeType() != Node.DOCUMENT_NODE) 167 { 168 errorMessage = "Document does not begin with an XML node, file = " + configFilePath; 170 return false; 171 } 172 173 Node root_element; 174 for (root_element = doc.getFirstChild(); 175 root_element != null; 176 root_element = root_element.getNextSibling()) 177 { 178 if (root_element.getNodeType() == Node.ELEMENT_NODE) 179 { 180 if (root_element.getNodeName().equals(ROOT_NODE_NAME) == false) 181 { 182 errorMessage = "Root node name " 183 + root_element.getNodeName() 184 + " must be " 185 + ROOT_NODE_NAME 186 + ", file = " + configFilePath; 187 188 return false; 189 } 190 break; 191 } 192 } 194 195 if (root_element == null) 196 { 197 errorMessage = "Root node " 198 + ROOT_NODE_NAME 199 + " not found, file = " + configFilePath; 200 201 return false; 202 } 203 204 Node ele_node; 206 errorMessage = null; 207 208 for (ele_node = root_element.getFirstChild(); 209 ele_node != null && errorMessage == null; 210 ele_node = ele_node.getNextSibling()) 211 { 212 if (ele_node.getNodeType() == Node.ELEMENT_NODE) 213 { 214 String node_name = ele_node.getNodeName(); 215 216 if (node_name.equals(EL_DB_PARAMS) == true) 217 { 218 processDBParams(ele_node, db_params); 219 } 220 else if (node_name.equals(EL_PARAM) == true) 221 { 222 processParam(ele_node, param_list); 223 } 224 else if (node_name.equals(EL_APPLICATION) == true) 225 { 226 processApplication(ele_node, app_list); 227 } 228 else if (node_name.equals(EL_RACCESS) == true) 229 { 230 processRemoteAccess(ele_node, raccess); 231 } 232 else if (node_name.equals(EL_MAIL_PARAMS) == true) 233 { 234 mail_path = processMailParam(ele_node); 235 } 236 else if (node_name.equals(EL_LICENSE_PARAMS) == true) 237 { 238 license_path = processLicenseParam(ele_node); 239 } 240 else if (node_name.equals(EL_DISPLAY_PARAMS) == true) 241 { 242 ml = processDisplayParam(ele_node); 243 } 244 else if (node_name.equals(EL_LOG_CONFIG) == true) 245 { 246 try 247 { 248 lparms = new AceConfigLogParams(ele_node); 249 } 250 catch (AceException ex) 251 { 252 errorMessage = ex.getMessage(); 253 } 254 } 255 else 256 { 257 errorMessage = "Unknown element " 258 + node_name + " in file " + configFilePath; 259 return false; 260 } 261 } 262 264 if (errorMessage != null) 265 { 266 break; 267 } 268 } 269 270 if (errorMessage != null) 271 { 272 return false; 273 } 274 275 if (db_params.getUser() == null) { 278 errorMessage = "Mandatory database parameter(s) missing in file " 279 + configFilePath; 280 return false; 281 } 282 283 if (lparms == null) 284 { 285 errorMessage = "Mandatory log parameter(s) missing in file " 286 + configFilePath; 287 return false; 288 } 289 290 if (license_path == null) 291 { 292 errorMessage = "Mandatory license parameter missing in file " 293 + configFilePath; 294 return false; 295 } 296 297 dBParams = db_params; 298 params = param_list; 299 applications = app_list; 300 remoteAccessParams = raccess; 301 logParams = lparms; 302 mailConfigPath = mail_path; 303 licenseConfigPath = license_path; 304 menuProperties = ml; 305 return true; 306 } 307 308 private MenuLinks processDisplayParam(Node node) 309 { 310 MenuLinks ml = new MenuLinks(); 311 String [] attributes = 312 { 313 ATT_IMAGE, 314 ATT_COMPANY, 315 ATT_COMPANY_URL 316 }; 317 318 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 319 ml.setImage(attrib_values[0]); 320 ml.setCompany(attrib_values[1]); 321 ml.setUrl(attrib_values[2]); 322 323 for (Node ele_node = node.getFirstChild(); 325 ele_node != null && errorMessage == null; 326 ele_node = ele_node.getNextSibling()) 327 { 328 if (ele_node.getNodeType() == Node.ELEMENT_NODE) 329 { 330 String node_name = ele_node.getNodeName(); 331 332 if (node_name.equals(EL_LINK_PARAMS) == true) 333 { 334 Link l = processLink(ele_node); 335 if (l == null) 336 { 337 return null; 338 } 339 340 ml.addLink(l); 341 } 342 } 343 } 344 return ml; 345 } 346 347 private Link processLink (Node node) 348 { 349 String [] attributes = 350 { 351 ATT_LINK_URL, 352 ATT_LINK_CAPTION 353 }; 354 355 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 356 357 if ((attrib_values[0] == null) || (attrib_values[1] == null)) 358 { 359 errorMessage = "Attribute(s) missing from the link node"; 360 return null; 361 } 362 363 Link l = new Link(); 364 l.setSource(attrib_values[0]); 365 l.setCaption(attrib_values[1]); 366 return l; 367 } 368 369 private String processMailParam(Node node) 370 { 371 try 372 { 373 return AceXMLHelper.getXMLAttribute(node, ATT_MAIL_PATH); 374 } 375 catch (AceException ex) 376 { 377 errorMessage = ex.getMessage(); 378 return null; 379 } 380 } 381 382 private String processLicenseParam(Node node) 383 { 384 try 385 { 386 return AceXMLHelper.getXMLAttribute(node, ATT_LICENSE_PATH); 387 } 388 catch (AceException ex) 389 { 390 errorMessage = ex.getMessage(); 391 return null; 392 } 393 } 394 395 private void processRemoteAccess(Node node, RemoteAccessParams params) 396 { 397 String [] attributes = 398 { 399 ATT_RHOST, 400 ATT_RURL, 401 ATT_HTTPS_RSERVICE 402 }; 403 404 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 405 if (attrib_values[0] != null) 406 { 407 params.setHost(attrib_values[0]); 408 } 409 410 if (attrib_values[1] != null) 411 { 412 params.setUrl(attrib_values[1]); 413 } 414 415 if (attrib_values[2] != null) 416 { 417 params.setHttpsServiceName(attrib_values[2]); 418 } 419 } 420 421 private void processDBParams(Node node, DBParams db_params) 422 { 423 String [] attributes = 424 { 425 ATT_DB_CLASS, 426 ATT_DB_URL, 427 ATT_DB_HOST, 428 ATT_DB_USER, 429 ATT_DB_PASSWORD, 430 ATT_DB_DATABASE 431 }; 432 433 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 434 435 if ((attrib_values[0] == null) || 436 (attrib_values[1] == null) || 437 (attrib_values[2] == null) || 438 (attrib_values[3] == null) || 439 (attrib_values[4] == null) || 440 (attrib_values[5] == null)) 441 { 442 errorMessage = "One or more options from " 443 + EL_DB_PARAMS 444 + " is not specified, file = " + configFilePath; 445 446 return; 447 } 448 449 db_params.setDriverClass(attrib_values[0]); 450 db_params.setUrl(attrib_values[1]); 451 452 if (attrib_values[2].equals("localhost") == true) 453 { 454 attrib_values[2] = "127.0.0.1"; 455 } 456 457 db_params.setHost(attrib_values[2]); 458 db_params.setUser(attrib_values[3]); 459 db_params.setPassword(attrib_values[4]); 460 db_params.setAdminDb(attrib_values[5]); 461 462 } 463 464 private void processParam(Node ele_node, PluginParameters param_list) 465 { 466 String [] sattributes = 467 { ATT_PARAM_NAME, ATT_PARAM_VALUE}; 468 String [] sattrib_values = AceXMLHelper.getXMLAttributes(ele_node, sattributes); 469 470 if ((sattrib_values[0] == null) || (sattrib_values[1] == null)) 471 { 472 errorMessage = "One or more options from " 473 + ATT_PARAM_NAME 474 + ", " 475 + ATT_PARAM_VALUE 476 + " is not specified for top-level " 477 + EL_PARAM 478 + " in file " + configFilePath; 479 480 return; 481 } 482 483 param_list.addParameter(sattrib_values[0], sattrib_values[1]); 484 } 485 486 private void processApplication(Node node, ArrayList app_list) 487 { 488 String [] attributes = 489 { ATT_APP_NAME, 490 ATT_APP_CLASS, 491 ATT_APP_DISPLAY, 492 ATT_APP_FORWARD }; 493 494 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 495 496 if ((attrib_values[0] == null) || 497 (attrib_values[2] == null) || 498 (attrib_values[3] == null)) 499 { 500 errorMessage = "One or more attributes from " 501 + EL_APPLICATION 502 + " is not specified, file = " + configFilePath; 503 504 return; 505 } 506 507 ApplicationElement app_element = new ApplicationElement(); 508 509 app_element.setName(attrib_values[0]); 510 app_element.setInitClass(attrib_values[1]); 511 app_element.setDisplayName(attrib_values[2]); 512 app_element.setForwardName(attrib_values[3]); 513 514 515 PluginParameters params = new PluginParameters(); 517 518 Node ele_node; 519 for (ele_node = node.getFirstChild(); 520 ele_node != null; 521 ele_node = ele_node.getNextSibling()) 522 { 523 if (ele_node.getNodeType() == Node.ELEMENT_NODE) 524 { 525 if (ele_node.getNodeName().equals(EL_PARAM) == true) 526 { 527 String [] sattributes = 528 { ATT_PARAM_NAME, ATT_PARAM_VALUE}; 529 String [] sattrib_values = AceXMLHelper.getXMLAttributes(ele_node, sattributes); 530 531 if ((sattrib_values[0] == null) || (sattrib_values[1] == null)) 532 { 533 errorMessage = "One or more options from " 534 + ATT_PARAM_NAME 535 + ", " 536 + ATT_PARAM_VALUE 537 + " is not specified for " 538 + EL_PARAM 539 + " for application " 540 + app_element.getName() 541 + " in file " + configFilePath; 542 543 return; 544 } 545 546 params.addParameter(sattrib_values[0], sattrib_values[1]); 547 } 548 else 549 { 550 errorMessage = "invalid node name - " 551 + ele_node.getNodeName() 552 + " for application " 553 + app_element.getName() 554 + " in file " + configFilePath; 555 556 return; 557 } 558 } 559 } 561 562 app_element.setParams(params); 563 564 app_list.add(app_element); 565 } 566 567 571 public DBParams getDBParams() 572 { 573 return this.dBParams; 574 } 575 576 580 public ArrayList getApplications() 581 { 582 return this.applications; 583 } 584 585 public int getApplicationsSize() 586 { 587 return applications.size(); 588 } 589 590 594 public String getErrorMessage() 595 { 596 return this.errorMessage; 597 } 598 599 603 public PluginParameters getParams() 604 { 605 return this.params; 606 } 607 608 public int getParamsSize() 609 { 610 return this.params.numParameters(); 611 } 612 613 617 public RemoteAccessParams getRemoteAccessParams() 618 { 619 return this.remoteAccessParams; 620 } 621 622 626 public void setRemoteAccessParams(RemoteAccessParams remoteAccessParams) 627 { 628 this.remoteAccessParams = remoteAccessParams; 629 } 630 631 635 public AceConfigLogParams getLogParams() 636 { 637 return this.logParams; 638 } 639 640 644 public void setLogParams(AceConfigLogParams logParams) 645 { 646 this.logParams = logParams; 647 } 648 649 653 public String getMailConfigPath() 654 { 655 return this.mailConfigPath; 656 } 657 658 662 public String getLicenseConfigPath() 663 { 664 return this.licenseConfigPath; 665 } 666 667 671 public List getLinks() 672 { 673 return this.links; 674 } 675 676 680 public MenuLinks getMenuProperties() 681 { 682 return this.menuProperties; 683 } 684 685 } 686 | Popular Tags |