1 package com.quikj.server.log; 2 3 import com.quikj.server.framework.*; 4 5 import java.io.*; 6 import java.util.*; 7 8 import javax.xml.parsers.*; 10 import org.xml.sax.*; 11 import org.w3c.dom.*; 12 13 14 17 public class LogConfiguration 19 { 20 public static final String ROOT_NODE_NAME = "log-configuration"; 21 public static final String EL_LOG_DIR = "log-directory"; 22 public static final String ATT_LOG_DIR = "dir"; 23 public static final String EL_GROUP_MASK = "log-group-mask"; 24 public static final String ATT_GROUP_MASK = "mask"; 25 public static final String EL_ARCHIVE_DIR = "archive-directory"; 26 public static final String ATT_ARCHIVE_DIR = "dir"; 27 public static final String EL_ARCHIVE_INTERVAL = "log-archive-interval"; 28 public static final String ATT_ARCHIVE_INTERVAL = "interval"; 29 public static final String EL_LOG_OPTIONS = "log-options"; 30 public static final String ATT_PRINT_CONSOLE = "console"; 31 public static final String ATT_PRINT_COLOR = "color"; 32 public static final String ATT_SAVE_FILE = "file"; 33 public static final String EL_MAIL_SERVICE = "mail-service"; public static final String ATT_MAIL_DIR = "dir"; public static final String ATT_MAIL_FILE = "file"; public static final String EL_EMAIL_ALERT = "email-alert"; public static final String ATT_TYPE = "type"; public static final String ATT_TO_EMAIL = "email"; public static final String ATT_CC_EMAIL = "cc-email"; public static final String EL_SAVE_TO_DB = "db-params"; public static final String ATT_DB_CLASS = "db-class"; 42 public static final String ATT_DBMS_URL = "dbms-url"; public static final String ATT_DB_HOST = "db-host"; public static final String ATT_DB_USER = "db-user"; public static final String ATT_DB_PASSWORD = "db-password"; public static final String ATT_DB_NAME = "db-name"; 48 49 public LogConfiguration(String dir, String file) 50 throws ArrayIndexOutOfBoundsException , FileNotFoundException, IOException, AceException, 51 SAXException, ParserConfigurationException 52 53 { 54 absPath = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA, 55 dir, file); 56 loadConfigurationFile(); 57 instance = this; 58 } 59 60 public static LogConfiguration Instance() 61 { 62 return instance; 63 } 64 65 public String getFullPathName() 66 { 67 return absPath; 68 } 69 70 public String getLogDirectory() 71 { 72 return logDir; 73 } 74 75 public int getGroupMask() 76 { 77 return groupMask; 78 } 79 80 public String getArchivesDirectory() 81 { 82 return archiveDir; 83 } 84 85 public String getTxHostName() 86 { 87 return txHost; 88 } 89 90 public String getRxHostName() 91 { 92 return rxHost; 93 } 94 95 public int getTxPort() 96 { 97 return txPort; 98 } 99 100 public int getRxPort() 101 { 102 return rxPort; 103 } 104 105 public int getArchivesInterval() 106 { 107 return archiveInterval; 108 } 109 110 public String getProcessName() 111 { 112 return processName; 113 } 114 115 public int getProcessInstance() 116 { 117 return processInstance; 118 } 119 120 public int getLogGroup() 121 { 122 return logGroup; 123 } 124 125 public Date getNextArchivesInterval() 126 { 127 Calendar cur_time = Calendar.getInstance(); 128 129 int next_archives_hour = ((cur_time.get(Calendar.HOUR_OF_DAY) / archiveInterval) * archiveInterval) 130 + archiveInterval; 131 132 cur_time.set(cur_time.get(Calendar.YEAR), 134 cur_time.get(Calendar.MONTH), 135 cur_time.get(Calendar.DAY_OF_MONTH), 136 next_archives_hour, 137 0, 0); 138 return cur_time.getTime(); 139 } 140 141 public String getArchivesFileName(String file) 142 { 143 return new String (archiveDir + File.separator + file); 144 } 145 146 public String getArchivesFileName(int msg_type, Date date, String hostname) 147 { 148 String prefix = getFilenamePrefix(msg_type); 149 if (prefix != null) 150 { 151 return getArchivesFileName(prefix + hostname + "_" + date.getTime() + ".arc.xml"); 152 } 153 else 154 { 155 return null; 156 } 157 } 158 159 public String getLogFileName(String file) 160 { 161 return new String (logDir + File.separator + file); 162 } 163 164 public String getLogFileName(int msg_type, String hostname) 165 { 166 String prefix = getFilenamePrefix(msg_type); 167 if (prefix != null) 168 { 169 return getLogFileName(prefix + hostname + ".log.xml"); 170 } 171 else 172 { 173 return null; 174 } 175 } 176 177 182 public String getMailDir() 184 { 185 return mailDir; 186 } 188 193 public String getMailFile() 195 { 196 return mailFile; 197 } 199 200 212 public boolean checkEmailType(int errorType, LogEmailInfo emailElement) 214 { 215 boolean matchType = false; 216 217 int index = 0; 220 while ((matchType == false) && (index < AceLogger.NUM_MSG_TYPES)) 221 { 222 if (emailInfo[index].getLogEmailInfo(errorType, emailElement)) 223 { 224 matchType = true; 225 } index += 1; 227 } 229 return matchType; 230 231 } 233 244 public LogDbInfo checkLogDbInfo() 246 { 247 248 LogDbInfo dbElement = new LogDbInfo(); 249 250 if (dbInfo.isDbExists()) 252 { 253 dbElement = dbInfo; 254 } 256 return dbElement; 257 258 } 260 266 public boolean saveToFile() 268 { 269 return optionSaveFile; 270 } 271 272 public boolean printToConsole() 273 { 274 return optionPrintConsole; 275 } 276 277 public boolean printColor() 278 { 279 return optionPrintColor; 280 } 281 282 public boolean isModified() 283 { 284 File file = new File(absPath); 285 long mod_time = file.lastModified(); 286 287 if (mod_time > lastModTime) 288 { 289 return true; 290 } 291 else 292 { 293 return false; 294 } 295 } 296 297 public void loadConfigurationFile() 298 throws FileNotFoundException, IOException, SAXException, AceException, ParserConfigurationException 299 300 { 301 File file = new File(absPath); 302 lastModTime = file.lastModified(); 303 304 FileInputStream fis = new FileInputStream(file); 305 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 306 307 dbf.setValidating(false); 309 dbf.setIgnoringComments(true); 310 dbf.setIgnoringElementContentWhitespace(true); 311 dbf.setCoalescing(true); 312 DocumentBuilder doc_builder = dbf.newDocumentBuilder(); 313 314 Document doc = doc_builder.parse(fis); 315 316 processDoc(doc); 317 318 fis.close(); 319 } 320 321 private String getFilenamePrefix(int msg_type) 322 { 323 String name = ""; 324 switch (msg_type) 325 { 326 case AceLogger.SYSTEM_LOG: 327 name = new String ("syslog_"); 328 break; 329 330 case AceLogger.USER_LOG: 331 name = new String ("oplog_"); 332 break; 333 334 case AceLogger.SYSTEM_REPORT: 335 name = new String ("sysrep_"); 336 break; 337 } 338 return name; 339 } 340 341 347 private void processDoc(Document doc) 349 throws AceException 350 { 351 initValues(); 352 353 if (doc.getNodeType() != Node.DOCUMENT_NODE) 354 { 355 357 throw new AceException("Document does not begin with an XML node"); 358 } 359 360 Node root_element; 361 boolean root_found = false; 362 for (root_element = doc.getFirstChild(); 363 root_element != null; 364 root_element = root_element.getNextSibling()) 365 { 366 if (root_element.getNodeType() == Node.ELEMENT_NODE) 367 { 368 if (root_element.getNodeName().equals(ROOT_NODE_NAME) == false) 369 { 370 throw new AceException("Root node name " 371 + root_element.getNodeName() 372 + " must be " 373 + ROOT_NODE_NAME); 374 } 375 root_found = true; 376 break; 377 378 } 379 } 381 382 if (root_found == false) 383 { 384 throw new AceException("Root node " 385 + ROOT_NODE_NAME 386 + " not found"); 387 } 388 389 Node ele_node; 391 for (ele_node = root_element.getFirstChild(); 392 ele_node != null; 393 ele_node = ele_node.getNextSibling()) 394 { 395 if (ele_node.getNodeType() == Node.ELEMENT_NODE) 396 { 397 String node_name = ele_node.getNodeName(); 398 399 if (node_name.equals(EL_LOG_DIR) == true) 400 { 401 processLogDir(ele_node); 402 } 403 else if (node_name.equals(EL_GROUP_MASK) == true) 404 { 405 processLogGroupMask(ele_node); 406 } 407 else if (node_name.equals(EL_ARCHIVE_DIR) == true) 408 { 409 processArchiveDir(ele_node); 410 } 411 else if (node_name.equals(EL_ARCHIVE_INTERVAL) == true) 412 { 413 processArchiveInterval(ele_node); 414 } 415 else if (node_name.equals(EL_LOG_OPTIONS) == true) 416 { 417 processLogOptions(ele_node); 418 } 419 else if (node_name.equals(AceConfigLogParams.EL_LOG_PARAM) == true) 420 { 421 AceConfigLogParams log_parms = new AceConfigLogParams(ele_node); 422 logGroup = log_parms.getLogGroup(); 423 processName = log_parms.getProcessName(); 424 processInstance = log_parms.getProcessInstance(); 425 txHost = log_parms.getTxHost(); 426 txPort = log_parms.getTxPort(); 427 rxHost = log_parms.getRxHost(); 428 rxPort = log_parms.getRxPort(); 429 } 430 else if (node_name.equals(AceCommandConfigParser.EL_COMMAND) == true) 431 { 432 AceCommandConfigParser command_parms = new AceCommandConfigParser(ele_node); 433 commandPort = command_parms.getPort(); 434 } 435 else if (node_name.equals(EL_MAIL_SERVICE) == true) 436 { 437 processLogDest(ele_node); 438 } else if (node_name.equals(EL_EMAIL_ALERT) == true) 440 { 441 processLogEmailAlert(ele_node); 442 } else if (node_name.equals(EL_SAVE_TO_DB) == true) 444 { 445 processLogToDb(ele_node); 446 } else 448 { 449 throw new AceException("Unknown element " 450 + node_name); 451 } 452 } 453 } 455 456 if (checkParams() == false) 458 { 459 throw new AceException("Mandatory parameter(s) missing"); 460 } 461 } 463 private void processLogDir(Node node) 464 throws AceException 465 { 466 String log_dir = AceXMLHelper.getXMLAttribute(node, 467 ATT_LOG_DIR); 468 if (log_dir == null) 469 { 470 throw new AceException("The syntax for " 471 + EL_LOG_DIR 472 + " is incorrect"); 473 } 474 475 try 476 { 477 logDir = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA, log_dir); 478 } 479 catch (ArrayIndexOutOfBoundsException ex) 480 { 481 throw new AceException("Internal error (LogConfiguration.processLogDir() - ArrayIndexOutOfBoundsException"); 483 } 484 485 File file = new File(logDir); 486 487 if ((file.isDirectory() == false) || 488 (file.exists() == false) || 489 (file.canWrite() == false)) 490 { 491 throw new AceException("The log directory " 492 + logDir 493 + " either does not exist or is not a directory or there is no write permission"); 494 } 495 } 496 497 private void processLogGroupMask(Node node) 498 throws AceException 499 { 500 String group_s = AceXMLHelper.getXMLAttribute(node, 501 ATT_GROUP_MASK); 502 if (group_s == null) 503 { 504 throw new AceException("The syntax for " 505 + EL_GROUP_MASK 506 + " is incorrect"); 507 } 508 try 509 { 510 groupMask = Integer.parseInt(group_s); 511 } 512 catch (NumberFormatException ex) 513 { 514 throw new AceException("The group mask must be an integer number : " 515 + group_s); 516 } 517 } 518 519 520 private void processArchiveDir(Node node) 521 throws AceException 522 { 523 String log_dir = AceXMLHelper.getXMLAttribute(node, 524 ATT_ARCHIVE_DIR); 525 if (log_dir == null) 526 { 527 throw new AceException("The syntax for " 528 + EL_ARCHIVE_DIR 529 + " is incorrect"); 530 } 531 532 try 533 { 534 archiveDir = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.GLOBAL_DATA, 535 log_dir); 536 } 537 catch (ArrayIndexOutOfBoundsException ex) 538 { 539 throw new AceException("Internal error (LogConfiguration.processArchiveDir() - ArrayIndexOutOfBoundsException"); 541 } 542 543 File file = new File(archiveDir); 544 545 if ((file.isDirectory() == false) || 546 (file.exists() == false) || 547 (file.canWrite() == false)) 548 { 549 throw new AceException("The archive directory " 550 + archiveDir 551 + " either does not exist or is not a directory or there is no write permission"); 552 } 553 } 554 555 private void processArchiveInterval(Node node) 556 throws AceException 557 { 558 String int_s = AceXMLHelper.getXMLAttribute(node, 559 ATT_ARCHIVE_INTERVAL); 560 if (int_s == null) 561 { 562 throw new AceException("The syntax for " 563 + EL_ARCHIVE_INTERVAL 564 + " is incorrect"); 565 } 566 try 567 { 568 archiveInterval = Integer.parseInt(int_s); 569 } 570 catch (NumberFormatException ex) 571 { 572 throw new AceException("The interval must be an integer number : " 573 + int_s); 574 } 575 576 if ((archiveInterval <= 0) && (archiveInterval > 24)) 578 { 579 throw new AceException("The archive interval must be a positive number between 1 and 24 : " 580 + archiveInterval); 581 } 582 583 if ((24 % archiveInterval) > 0) 584 { 585 throw new AceException("The archive interval must be one of 1, 2, 3, 4, 6, 12 and 24 : " 586 + archiveInterval); 587 } 588 } 589 590 597 private void processLogOptions(Node node) 599 throws AceException 600 { 601 String [] attributes = 602 { ATT_PRINT_CONSOLE, ATT_PRINT_COLOR, ATT_SAVE_FILE }; 603 604 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 605 606 if ((attrib_values[0] == null) || (attrib_values[1] == null) || (attrib_values[2] == null)) 607 { 608 throw new AceException("One or more options from " 609 + ATT_PRINT_CONSOLE 610 + ", " 611 + ATT_PRINT_COLOR 612 + ", " 613 + ATT_SAVE_FILE 614 + " is not specified in " 615 + EL_LOG_OPTIONS); 616 } 617 618 if (attrib_values[0].equals("yes") == true) 619 { 620 optionPrintConsole = true; 621 } 622 else if (attrib_values[0].equals("no") == true) 623 { 624 optionPrintConsole = false; 625 } 626 else 627 { 628 throw new AceException("invalid value - " 629 + attrib_values[0] 630 + " for the option " 631 + ATT_PRINT_CONSOLE); 632 } 633 634 if (attrib_values[1].equals("yes") == true) 635 { 636 optionPrintColor = true; 637 } 638 else if (attrib_values[1].equals("no") == true) 639 { 640 optionPrintColor = false; 641 } 642 else 643 { 644 throw new AceException("invalid value - " 645 + attrib_values[1] 646 + " for the option " 647 + ATT_PRINT_COLOR); 648 } 649 650 if (attrib_values[2].equals("yes") == true) 651 { 652 optionSaveFile = true; 653 } 654 else if (attrib_values[2].equals("no") == true) 655 { 656 optionSaveFile = false; 657 } 658 else 659 { 660 throw new AceException("invalid value - " 661 + attrib_values[2] 662 + " for the option " 663 + ATT_SAVE_FILE); 664 } 665 666 } 667 668 679 private void processLogDest(Node node) 681 throws AceException 682 { 683 String [] attributes = 685 { ATT_MAIL_DIR, ATT_MAIL_FILE }; 686 687 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 689 690 if ((attrib_values[0] == null) || (attrib_values[1] == null)) 692 { 693 throw new AceException("One or more options from " 694 + ATT_MAIL_DIR 695 + ", " 696 + ATT_MAIL_FILE 697 + " is not specified in " 698 + EL_MAIL_SERVICE); 699 } 701 mailDir = attrib_values[0]; 703 mailFile = attrib_values[1]; 704 705 } 707 717 private void processLogEmailAlert(Node node) 719 throws AceException 720 { 721 String [] attributes = 723 { ATT_TYPE, ATT_TO_EMAIL, ATT_CC_EMAIL }; 724 725 String [] attribValues = AceXMLHelper.getXMLAttributes(node, attributes); 727 728 if ((attribValues[0] == null) || (attribValues[1] == null)) 730 { 731 throw new AceException("One or more options from " 732 + ATT_TYPE 733 + ", " 734 + ATT_TO_EMAIL 735 + " is not specified in " 736 + EL_EMAIL_ALERT); 737 } 739 setupLogEmailAlertAtributes(attribValues); 740 741 } 743 751 private void setupLogEmailAlertAtributes(String [] attribValues) 753 throws AceException 754 { 755 756 int index = 0; 758 while (index < AceLogger.NUM_MSG_TYPES) 759 { 760 if (emailInfo[index].getEmailType() == AceLogger.NUM_MSG_TYPES) 762 { 763 boolean ok_to_update_email_info = false; 765 int convertedError = AceLogger.NUM_MSG_TYPES; 766 767 convertedError = getConvertedError(attribValues[0]); 769 boolean found = false; 771 for (int index2 = 0; index2 < AceLogger.NUM_MSG_TYPES; index2++) 772 { 773 if (emailInfo[index2].getEmailType() == convertedError) 774 { 775 found = true; 776 index2 = AceLogger.NUM_MSG_TYPES; 777 } } 780 if (found) 781 { 782 throw new AceException(EL_EMAIL_ALERT 784 + " type already exists - fix config file"); 785 } 787 updateToCcInfo(attribValues, convertedError, index); 788 789 index = AceLogger.NUM_MSG_TYPES; 791 } else index++; 793 } 795 } 797 804 private void updateToCcInfo(String [] attribValues, int error, int index) 806 { 807 808 emailInfo[index].setEmailType(error); 810 811 StringTokenizer toTokens = new StringTokenizer(attribValues[1], ";"); 814 while (toTokens.hasMoreTokens()) 815 emailInfo[index].setEmailToAddress(toTokens.nextToken()); 816 817 if (attribValues[2] != null) 820 { 821 StringTokenizer ccTokens = new StringTokenizer(attribValues[2], ";"); 822 while (ccTokens.hasMoreTokens()) 823 emailInfo[index].setEmailCcAddress(ccTokens.nextToken()); 824 } 826 } 828 835 private int getConvertedError(String error_string) 837 { 838 839 int converted_error; 840 841 if (error_string.equals("trace")) converted_error = AceLogger.TRACE; 842 else if (error_string.equals("informational")) converted_error = AceLogger.INFORMATIONAL; 843 else if (error_string.equals("warning")) converted_error = AceLogger.WARNING; 844 else if (error_string.equals("error")) converted_error = AceLogger.ERROR; 845 else if (error_string.equals("fatal")) converted_error = AceLogger.FATAL; 846 else converted_error = AceLogger.FATAL; 847 848 return converted_error; 849 850 } 852 861 private void processLogToDb(Node node) 863 throws AceException 864 { 865 String [] attributes = 867 { ATT_DB_CLASS, ATT_DBMS_URL, ATT_DB_HOST, 868 ATT_DB_USER, ATT_DB_PASSWORD, ATT_DB_NAME}; 869 870 String [] attribValues = AceXMLHelper.getXMLAttributes(node, attributes); 872 873 if ((attribValues[0] == null) || (attribValues[1] == null) || 875 (attribValues[5] == null)) 876 { 877 throw new AceException("One or more options from " 878 + ATT_DB_CLASS 879 + ", " 880 + ATT_DBMS_URL 881 + ", " 882 + ATT_DB_NAME 883 + " is not specified in " 884 + EL_SAVE_TO_DB); 885 } 887 setupDbAtributes(attribValues); 888 889 } 891 897 private void setupDbAtributes(String [] attribValues) 899 throws AceException 900 { 901 902 int index = 0; 904 if ((attribValues[0].equals(null) == true) || 906 (attribValues[1].equals(null) == true) || 907 (attribValues[5].equals(null) == true)) 908 { 909 throw new AceException("The option " 910 + attribValues[index] 911 + " must not be null."); 912 } 914 dbInfo.setDbClass(attribValues[0]); 916 dbInfo.setDbmsUrl(attribValues[1]); 917 dbInfo.setDbName(attribValues[5]); 918 919 if (!(attribValues[2] == null) && (attribValues[2].equals(null) == false)) 922 dbInfo.setDbHost(attribValues[2]); 923 if (!(attribValues[3] == null) && (attribValues[3].equals(null) == false)) 924 dbInfo.setDbUser(attribValues[3]); 925 if (!(attribValues[4] == null) && (attribValues[4].equals(null) == false)) 926 dbInfo.setDbPassword(attribValues[4]); 927 928 dbInfo.setDbExists(true); 930 931 } 933 939 private void initValues() 941 { 942 logDir = null; 943 groupMask = 0; 944 archiveDir = null; 945 archiveInterval = 0; 946 logGroup = 0; 947 txHost = null; 948 txPort = 0; 949 txHost = null; 950 txPort = 0; 951 processName = null; 952 processInstance = -1; 953 optionPrintConsole = false; 954 optionPrintColor = false; 955 optionSaveFile = true; 956 commandPort = -1; 957 emailInfo = new LogEmailInfo[AceLogger.NUM_MSG_TYPES]; 959 int index; 960 for (index = 0; index < AceLogger.NUM_MSG_TYPES; index++) 962 { 963 emailInfo[index] = new LogEmailInfo(); 964 } 966 for(index = 0; index < AceLogger.NUM_MSG_TYPES; index++) 967 { 968 emailInfo[index].setEmailType(AceLogger.NUM_MSG_TYPES); 969 } 971 dbInfo = new LogDbInfo(); 973 974 } 976 private boolean checkParams() 977 { 978 boolean ret = true; 979 980 if (optionSaveFile == true) { 982 if ((logDir == null) || 983 (archiveDir == null) || 984 (archiveInterval == 0)) 985 { 986 return false; 987 } 988 } 989 990 991 if ((groupMask == 0) || 992 (logGroup == 0) || 993 (txHost == null) || 994 (txPort == 0) || 995 (rxHost == null) || 996 (rxPort == 0) || 997 (processName == null) || 998 (processInstance < 0)) 999 { 1000 ret = false; 1001 } 1002 return ret; 1003 } 1004 1005 1008 public int getCommandPort() 1009 { 1010 return commandPort; 1011 } 1012 1013 1016 public void setCommandPort(int commandPort) 1017 { 1018 this.commandPort = commandPort; 1019 } 1020 1021 1025 public com.quikj.server.log.LogEmailInfo[] getEmailInfo() 1026 { 1027 return this.emailInfo; 1028 } 1029 1030 1034 public void setEmailInfo(com.quikj.server.log.LogEmailInfo[] emailInfo) 1035 { 1036 this.emailInfo = emailInfo; 1037 } 1038 1039 private String absPath; 1043 private long lastModTime; 1044 private String logDir; 1045 private int groupMask; 1046 private String archiveDir; 1047 private int archiveInterval; 1048 private int logGroup; 1049 private String txHost; 1050 private int txPort; 1051 private String rxHost; 1052 private int rxPort; 1053 private String processName; 1054 private int processInstance; 1055 private boolean optionPrintConsole; 1056 private boolean optionPrintColor; 1057 private boolean optionSaveFile; 1058 private int commandPort; 1059 private String mailDir; private String mailFile; private LogEmailInfo[] emailInfo; private LogDbInfo dbInfo; private static LogConfiguration instance = null; 1064 1065} | Popular Tags |