KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > log > LogConfiguration


1 package com.quikj.server.log;
2
3 import com.quikj.server.framework.*;
4
5 import java.io.*;
6 import java.util.*;
7
8 // JAXP packages
9
import javax.xml.parsers.*;
10 import org.xml.sax.*;
11 import org.w3c.dom.*;
12
13
14 /////////////////////////////////////////////////////////////////////////
15
/** Class LogConfiguration contains elements, attributes from log_cfg
16  */

17 /////////////////////////////////////////////////////////////////////////
18
public class LogConfiguration
19 {
20     public static final String JavaDoc ROOT_NODE_NAME = "log-configuration";
21     public static final String JavaDoc EL_LOG_DIR = "log-directory";
22     public static final String JavaDoc ATT_LOG_DIR = "dir";
23     public static final String JavaDoc EL_GROUP_MASK = "log-group-mask";
24     public static final String JavaDoc ATT_GROUP_MASK = "mask";
25     public static final String JavaDoc EL_ARCHIVE_DIR = "archive-directory";
26     public static final String JavaDoc ATT_ARCHIVE_DIR = "dir";
27     public static final String JavaDoc EL_ARCHIVE_INTERVAL = "log-archive-interval";
28     public static final String JavaDoc ATT_ARCHIVE_INTERVAL = "interval";
29     public static final String JavaDoc EL_LOG_OPTIONS = "log-options";
30     public static final String JavaDoc ATT_PRINT_CONSOLE = "console";
31     public static final String JavaDoc ATT_PRINT_COLOR = "color";
32     public static final String JavaDoc ATT_SAVE_FILE = "file";
33     public static final String JavaDoc EL_MAIL_SERVICE = "mail-service"; // mail element
34
public static final String JavaDoc ATT_MAIL_DIR = "dir"; // dest config dir
35
public static final String JavaDoc ATT_MAIL_FILE = "file"; // dest config file
36
public static final String JavaDoc EL_EMAIL_ALERT = "email-alert"; // email alert
37
public static final String JavaDoc ATT_TYPE = "type"; // send email if type = fatal, error
38
public static final String JavaDoc ATT_TO_EMAIL = "email"; // email "to" destination
39
public static final String JavaDoc ATT_CC_EMAIL = "cc-email"; // email "cc" destination
40
public static final String JavaDoc EL_SAVE_TO_DB = "db-params"; // store log to DB
41
public static final String JavaDoc ATT_DB_CLASS = "db-class";
42     public static final String JavaDoc ATT_DBMS_URL = "dbms-url"; // DBMS where db lives
43
public static final String JavaDoc ATT_DB_HOST = "db-host"; // location of DB host
44
public static final String JavaDoc ATT_DB_USER = "db-user"; // DB user
45
public static final String JavaDoc ATT_DB_PASSWORD = "db-password"; // DB password
46
public static final String JavaDoc ATT_DB_NAME = "db-name"; // DB name
47

48     
49     public LogConfiguration(String JavaDoc dir, String JavaDoc file)
50     throws ArrayIndexOutOfBoundsException JavaDoc, 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 JavaDoc getFullPathName()
66     {
67         return absPath;
68     }
69     
70     public String JavaDoc getLogDirectory()
71     {
72         return logDir;
73     }
74     
75     public int getGroupMask()
76     {
77         return groupMask;
78     }
79     
80     public String JavaDoc getArchivesDirectory()
81     {
82         return archiveDir;
83     }
84     
85     public String JavaDoc getTxHostName()
86     {
87         return txHost;
88     }
89     
90     public String JavaDoc 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 JavaDoc 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         // set the time to midnight + next_archives_hour
133
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 JavaDoc getArchivesFileName(String JavaDoc file)
142     {
143         return new String JavaDoc(archiveDir + File.separator + file);
144     }
145     
146     public String JavaDoc getArchivesFileName(int msg_type, Date date, String JavaDoc hostname)
147     {
148         String JavaDoc 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 JavaDoc getLogFileName(String JavaDoc file)
160     {
161         return new String JavaDoc(logDir + File.separator + file);
162     }
163     
164     public String JavaDoc getLogFileName(int msg_type, String JavaDoc hostname)
165     {
166         String JavaDoc 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     /////////////////////////////////////////////////////////////////////////
178
/** Getter for property mailDir - used for Log email notification.
179      * @return Value of property mailDir.
180      *
181      */

182     /////////////////////////////////////////////////////////////////////////
183
public String JavaDoc getMailDir()
184     {
185         return mailDir;
186     } // getMailDir
187

188     /////////////////////////////////////////////////////////////////////////
189
/** Getter for property mailFile - used for Log email notification.
190      * @return Value of property mailFile.
191      *
192      */

193     /////////////////////////////////////////////////////////////////////////
194
public String JavaDoc getMailFile()
195     {
196         return mailFile;
197     } // getMailFile
198

199     
200     /////////////////////////////////////////////////////////////////////////
201
/**
202      * checkEmailType - used for Log email notification.
203      * Determines if error sent matches error type
204      * in config file. If a match is found, true is returned; false
205      * otherwise. In addtion, object of LogEmailInfo is passed
206      * back which corresponds with this error type.
207      * @param errorType - find if match exists in table for this type
208      * @param indexIntoTable - index where match found
209      * @author elizabeth
210      * @version
211      */

212     /////////////////////////////////////////////////////////////////////////
213
public boolean checkEmailType(int errorType, LogEmailInfo emailElement)
214     {
215         boolean matchType = false;
216         
217         // Since emailInfo is an array of type LogEmailINfo, attempt to
218
// find the error_type amongst all the emailInfo elements :
219
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             } // if
226
index += 1;
227         } // while
228

229         return matchType;
230         
231     } // checkEmailType
232

233     /////////////////////////////////////////////////////////////////////////
234
/**
235      * checkLogDbInfo - used for Log DB storage.
236      * Determines if db info exists which means the log info will be
237      * stored in the designated DB. In addtion, object of LogDbInfo
238      * is passed back.
239      * @param errorType - find if match exists in table for this type
240      * @param indexIntoTable - index where match found
241      * @author elizabeth
242      * @version
243      */

244     /////////////////////////////////////////////////////////////////////////
245
public LogDbInfo checkLogDbInfo()
246     {
247         
248         LogDbInfo dbElement = new LogDbInfo();
249         
250         // All mandatory parameters must be present
251
if (dbInfo.isDbExists())
252         {
253             dbElement = dbInfo;
254         } // dbInfo exists
255

256         return dbElement;
257         
258     } // checkLogDbInfo
259

260     /////////////////////////////////////////////////////////////////////////
261
/**
262      * saveToFile
263      * @author amit
264      * @version
265      */

266     /////////////////////////////////////////////////////////////////////////
267
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         // set various configuration options
308
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 JavaDoc getFilenamePrefix(int msg_type)
322     {
323         String JavaDoc name = "";
324         switch (msg_type)
325         {
326             case AceLogger.SYSTEM_LOG:
327                 name = new String JavaDoc("syslog_");
328                 break;
329                 
330             case AceLogger.USER_LOG:
331                 name = new String JavaDoc("oplog_");
332                 break;
333                 
334             case AceLogger.SYSTEM_REPORT:
335                 name = new String JavaDoc("sysrep_");
336                 break;
337         }
338         return name;
339     }
340     
341     /////////////////////////////////////////////////////////////////////////
342
/**
343      * processDoc processes the elements parsed in the log_cfg document
344      * @author amit, elizabeth
345      * @version
346      */

347     /////////////////////////////////////////////////////////////////////////
348
private void processDoc(Document doc)
349     throws AceException
350     {
351         initValues();
352         
353         if (doc.getNodeType() != Node.DOCUMENT_NODE)
354         {
355             // the root node must be of the type document
356

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             // else discard
380
}
381         
382         if (root_found == false)
383         {
384             throw new AceException("Root node "
385             + ROOT_NODE_NAME
386             + " not found");
387         }
388         
389         // parse all the child elements
390
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 JavaDoc 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                 } // el_mail_service - log email notification
439
else if (node_name.equals(EL_EMAIL_ALERT) == true)
440                 {
441                     processLogEmailAlert(ele_node);
442                 } // el_email_alert - log email notification
443
else if (node_name.equals(EL_SAVE_TO_DB) == true)
444                 {
445                     processLogToDb(ele_node);
446                 } // el_mail_service - log email notification
447
else
448                 {
449                     throw new AceException("Unknown element "
450                     + node_name);
451                 }
452             }
453             // ignore all types other than the ELEMENT_NODE
454
}
455         
456         // check if all mandatory parameters have been received
457
if (checkParams() == false)
458         {
459             throw new AceException("Mandatory parameter(s) missing");
460         }
461     } // processDoc
462

463     private void processLogDir(Node node)
464     throws AceException
465     {
466         String JavaDoc 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 JavaDoc ex)
480         {
481             // should not happen
482
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex)
538         {
539             // should not happen
540
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 JavaDoc 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 JavaDoc ex)
571         {
572             throw new AceException("The interval must be an integer number : "
573             + int_s);
574         }
575         
576         // the valid values are 1, 2, 3, 4, 6, 12, 24
577
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     /////////////////////////////////////////////////////////////////////////
591
/**
592      * processLogOptions processes the log option element from the log_cfg
593      * document
594      * @author amit
595      * @version
596      */

597     /////////////////////////////////////////////////////////////////////////
598
private void processLogOptions(Node node)
599     throws AceException
600     {
601         String JavaDoc[] attributes =
602         { ATT_PRINT_CONSOLE, ATT_PRINT_COLOR, ATT_SAVE_FILE };
603         
604         String JavaDoc[] 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     /////////////////////////////////////////////////////////////////////////
669
/**
670      * processLogDest - used for Log email notification feature.
671      * Processes the mail-service option element from the
672      * log_cfg document - attributes = dir and file. The destination
673      * server name is found in the specified file found in the specified
674      * dir. The Mail Service process takes care of reading the server
675      * from the proper config * file.
676      * @author elizabeth
677      * @version
678      */

679     /////////////////////////////////////////////////////////////////////////
680
private void processLogDest(Node node)
681     throws AceException
682     {
683         // Identify the mail-service attributes :
684
String JavaDoc[] attributes =
685         { ATT_MAIL_DIR, ATT_MAIL_FILE };
686         
687         // Get attribute values from log_cfg doc :
688
String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
689         
690         // Error is attributes don't exist :
691
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         } // attribute doesn't exist
700

701         // Store the email attribute values :
702
mailDir = attrib_values[0];
703         mailFile = attrib_values[1];
704         
705     } // processLogDest
706

707     /////////////////////////////////////////////////////////////////////////
708
/**
709      * processLogEmailAlert - used for log email notification feature.
710      * Processes the email alert element from the
711      * log_cfg document. Attributes = ATT_TYPE & ATT_EMAIL -
712      * type = error severity and email = email address where alert
713      * notice is sent.
714      * @author elizabeth
715      * @version
716      */

717     /////////////////////////////////////////////////////////////////////////
718
private void processLogEmailAlert(Node node)
719     throws AceException
720     {
721         // Identify the email-alert attributes :
722
String JavaDoc[] attributes =
723         { ATT_TYPE, ATT_TO_EMAIL, ATT_CC_EMAIL };
724         
725         // Get attribute values from log_cfg doc :
726
String JavaDoc[] attribValues = AceXMLHelper.getXMLAttributes(node, attributes);
727         
728         // Error is attributes don't exist :
729
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         } // attribute doesn't exist
738

739         setupLogEmailAlertAtributes(attribValues);
740         
741     } // processLogEmailAlert
742

743     /////////////////////////////////////////////////////////////////////////
744
/**
745      * setupLogEmailAlertAtributes - used for Log email notification
746      * feature.
747      * Updates LogEmailInfo with the passed attributes.
748      * @author elizabeth
749      * @version
750      */

751     /////////////////////////////////////////////////////////////////////////
752
private void setupLogEmailAlertAtributes(String JavaDoc[] attribValues)
753     throws AceException
754     {
755         
756         // Store the email attribute values :
757
int index = 0;
758         while (index < AceLogger.NUM_MSG_TYPES)
759         {
760             // Put attribute value in first available element in emailInfo[] :
761
if (emailInfo[index].getEmailType() == AceLogger.NUM_MSG_TYPES)
762             {
763                 // Empty index into emailInfo :
764
boolean ok_to_update_email_info = false;
765                 int convertedError = AceLogger.NUM_MSG_TYPES;
766                 
767                 // Convert configured error type to AceLogger error type :
768
convertedError = getConvertedError(attribValues[0]);
769                 // If error-type already exists in emailInfo, this is an error :
770
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                     } // if
778
} // for
779

780                 if (found)
781                 {
782                     // Error!
783
throw new AceException(EL_EMAIL_ALERT
784                     + " type already exists - fix config file");
785                 } // if found
786

787                 updateToCcInfo(attribValues, convertedError, index);
788                 
789                 // Exit loop :
790
index = AceLogger.NUM_MSG_TYPES;
791             } // if getEmailType
792
else index++;
793         } // while
794

795     } // setupLogEmailAlertAtributes
796

797     /////////////////////////////////////////////////////////////////////////
798
/**
799      * updateToCcInfo - used for Log email notification feature.
800      * Updates LogEmailInfo with the passed CC attributes.
801      * @author elizabeth
802      * @version
803      */

804     /////////////////////////////////////////////////////////////////////////
805
private void updateToCcInfo(String JavaDoc[] attribValues, int error, int index)
806     {
807         
808         // Update error type :
809
emailInfo[index].setEmailType(error);
810         
811         // Update To address - the config file may contain many To addresses,
812
// which are separated by ";" :
813
StringTokenizer toTokens = new StringTokenizer(attribValues[1], ";");
814         while (toTokens.hasMoreTokens())
815             emailInfo[index].setEmailToAddress(toTokens.nextToken());
816         
817         // Update CC address(es) if they exist - the config file may contain
818
// many CC addresses, which are separated by ";" :
819
if (attribValues[2] != null)
820         {
821             StringTokenizer ccTokens = new StringTokenizer(attribValues[2], ";");
822             while (ccTokens.hasMoreTokens())
823                 emailInfo[index].setEmailCcAddress(ccTokens.nextToken());
824         } // if atrib_values[2]
825

826     } // updateToCcInfo
827

828     /////////////////////////////////////////////////////////////////////////
829
/**
830      * getConvertedError - used for Log email notification feature.
831      * Converts the given string to one of the AceLogger error types.
832      * @author amit, elizabeth
833      * @version
834      */

835     /////////////////////////////////////////////////////////////////////////
836
private int getConvertedError(String JavaDoc 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     } // getConvertedError
851

852     /////////////////////////////////////////////////////////////////////////
853
/**
854      * processLogToDb - used for log DB feature.
855      * Processes the db-params element from the log_cfg document.
856      * Attributes = ATT_DB_CLASS, ATT_DBMS_NAME, ATT_DB_HOST,
857      * ATT_DB_USER, ATT_DB_PASSWORD, ATT_DB_NAME
858      * @author elizabeth
859      * @version
860      */

861     /////////////////////////////////////////////////////////////////////////
862
private void processLogToDb(Node node)
863     throws AceException
864     {
865         // Identify the email-alert attributes :
866
String JavaDoc[] attributes =
867         { ATT_DB_CLASS, ATT_DBMS_URL, ATT_DB_HOST,
868           ATT_DB_USER, ATT_DB_PASSWORD, ATT_DB_NAME};
869           
870           // Get attribute values from log_cfg doc :
871
String JavaDoc[] attribValues = AceXMLHelper.getXMLAttributes(node, attributes);
872           
873           // Error is attributes don't exist :
874
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           } // attribute doesn't exist
886

887           setupDbAtributes(attribValues);
888           
889     } // processLogToDb
890

891     /////////////////////////////////////////////////////////////////////////
892
/**
893      * setupDbAtributes - used for feature which stores logs in DB.
894      * @author elizabeth
895      * @version
896      */

897     /////////////////////////////////////////////////////////////////////////
898
private void setupDbAtributes(String JavaDoc[] attribValues)
899     throws AceException
900     {
901         
902         // Store the DB attribute values :
903
int index = 0;
904         // Attributes db-class, db-url, and db-name can not be null :
905
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         } // attribute is null
913

914         // All mandatory attributes are non-null. Set the DB vars to the attribute values :
915
dbInfo.setDbClass(attribValues[0]);
916         dbInfo.setDbmsUrl(attribValues[1]);
917         dbInfo.setDbName(attribValues[5]);
918         
919         // Check if each optional parameter exists and if so, if the optional
920
// parameter contains non-null info :
921
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         // Indicate the DB can be updated w/ log info :
929
dbInfo.setDbExists(true);
930         
931     } // setupDbAtributes
932

933     /////////////////////////////////////////////////////////////////////////
934
/**
935      * initValues initializes the attributes found in log_cfg.
936      * @author amit, elizabeth
937      * @version
938      */

939     /////////////////////////////////////////////////////////////////////////
940
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         // Initialize email notification attributes :
958
emailInfo = new LogEmailInfo[AceLogger.NUM_MSG_TYPES];
959         int index;
960         // Insert a class in each index :
961
for (index = 0; index < AceLogger.NUM_MSG_TYPES; index++)
962         {
963             emailInfo[index] = new LogEmailInfo();
964         } // for
965

966         for(index = 0; index < AceLogger.NUM_MSG_TYPES; index++)
967         {
968             emailInfo[index].setEmailType(AceLogger.NUM_MSG_TYPES);
969         } // for i
970

971         // Initialize DB log storage attributes :
972
dbInfo = new LogDbInfo();
973         
974     } // initValues
975

976     private boolean checkParams()
977     {
978         boolean ret = true;
979         
980         if (optionSaveFile == true) // save the logs to a file
981
{
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    /** Getter for property commandPort.
1006     * @return Value of property commandPort.
1007     */

1008    public int getCommandPort()
1009    {
1010        return commandPort;
1011    }
1012    
1013    /** Setter for property commandPort.
1014     * @param commandPort New value of property commandPort.
1015     */

1016    public void setCommandPort(int commandPort)
1017    {
1018        this.commandPort = commandPort;
1019    }
1020    
1021    /** Getter for property emailInfo.
1022     * @return Value of property emailInfo.
1023     *
1024     */

1025    public com.quikj.server.log.LogEmailInfo[] getEmailInfo()
1026    {
1027        return this.emailInfo;
1028    }
1029    
1030    /** Setter for property emailInfo.
1031     * @param emailInfo New value of property emailInfo.
1032     *
1033     */

1034    public void setEmailInfo(com.quikj.server.log.LogEmailInfo[] emailInfo)
1035    {
1036        this.emailInfo = emailInfo;
1037    }
1038    
1039    /////////////////////////////////////////////////////////////////////////
1040
// Class Attributes
1041
/////////////////////////////////////////////////////////////////////////
1042
private String JavaDoc absPath;
1043    private long lastModTime;
1044    private String JavaDoc logDir;
1045    private int groupMask;
1046    private String JavaDoc archiveDir;
1047    private int archiveInterval;
1048    private int logGroup;
1049    private String JavaDoc txHost;
1050    private int txPort;
1051    private String JavaDoc rxHost;
1052    private int rxPort;
1053    private String JavaDoc processName;
1054    private int processInstance;
1055    private boolean optionPrintConsole;
1056    private boolean optionPrintColor;
1057    private boolean optionSaveFile;
1058    private int commandPort;
1059    private String JavaDoc mailDir; // directory for email notification feature.
1060
private String JavaDoc mailFile; // file for email notification feature.
1061
private LogEmailInfo[] emailInfo; // contains email notification error info
1062
private LogDbInfo dbInfo; // contains db info
1063
private static LogConfiguration instance = null;
1064    
1065} // LogConfiguration
Popular Tags