KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceLogMessage


1 package com.quikj.server.framework;
2
3 import java.util.*;
4
5 // JAXP packages
6
import org.w3c.dom.*;
7
8 public class AceLogMessage implements AceLogMessageInterface
9 {
10     public static final String JavaDoc ATT_LOG_GROUP = "log_group";
11     public static final String JavaDoc EL_TIME_STAMP = "time_stamp";
12     public static final String JavaDoc ATT_TIME_STAMP = "time";
13     public static final String JavaDoc EL_HOST_NAME = "host_name";
14     public static final String JavaDoc ATT_HOST_NAME = "name";
15     public static final String JavaDoc EL_APPL_NAME = "appl_name";
16     public static final String JavaDoc ATT_APPL_NAME = "name";
17     public static final String JavaDoc EL_APPL_INS = "appl_instance";
18     public static final String JavaDoc ATT_APPL_INS = "instance";
19     public static final String JavaDoc EL_SEVERITY = "msg_severity";
20     public static final String JavaDoc ATT_SEVERITY = "severity";
21     public static final String JavaDoc EL_TYPE = "msg_type";
22     public static final String JavaDoc ATT_TYPE = "type";
23     public static final String JavaDoc EL_ID = "msg_id";
24     public static final String JavaDoc ATT_ID = "id";
25     public static final String JavaDoc EL_MESSAGE = "message";
26     
27     
28     // formatter constructor
29
public AceLogMessage(Date timestamp,
30     int log_group,
31     String JavaDoc host_name,
32     String JavaDoc pname,
33     int pins,
34     int severity,
35     int msg_type,
36     String JavaDoc message,
37     int msg_id,
38     boolean noxmlheader)
39     {
40         if (timestamp == null)
41         {
42             timestamp = new Date();
43         }
44         
45         logGroup = log_group;
46         timeStamp = timestamp.getTime();
47         hostName = host_name;
48         processName = pname;
49         processInstance = pins;
50         this.severity = severity;
51         msgType = msg_type;
52         msgId = msg_id;
53         this.message = message;
54         noXMLHeader = noxmlheader;
55     }
56     
57     // formatter constructor
58
public AceLogMessage(int log_group,
59     String JavaDoc host_name, String JavaDoc pname, int pins, int severity, int msg_type,
60     String JavaDoc message, int msg_id)
61     {
62         this(null, log_group, host_name, pname, pins, severity, msg_type, message, msg_id, false);
63     }
64     
65     // formatter constructor
66
public AceLogMessage(Date timestamp,
67     int log_group,
68     String JavaDoc host_name,
69     String JavaDoc pname,
70     int pins,
71     int severity,
72     int msg_type,
73     String JavaDoc message)
74     {
75         this(timestamp, log_group, host_name, pname, pins, severity, msg_type, message, -1, false);
76     }
77     
78     // formatter constructor
79
public AceLogMessage(int log_group,
80     String JavaDoc host_name,
81     String JavaDoc pname,
82     int pins,
83     int severity,
84     int msg_type,
85     String JavaDoc message)
86     {
87         this(null, log_group, host_name, pname, pins, severity, msg_type, message, -1, false);
88     }
89         
90     // parser constructor
91
public AceLogMessage(int group_mask,
92     Node node)
93     throws AceException
94     {
95         String JavaDoc group_s = AceXMLHelper.getXMLAttribute(node, ATT_LOG_GROUP);
96         
97         try
98         {
99             logGroup = Integer.parseInt(group_s);
100         }
101         catch (NumberFormatException JavaDoc ex)
102         {
103             throw new AceException("The group is not an integer number ("
104             + group_s
105             + ")");
106         }
107                         
108         // continue with further processing
109
Node next_node;
110         int state = COLLECTING_TIMESTAMP;
111         boolean stop_proc = false;
112         
113         for (next_node = node.getFirstChild();
114         (next_node != null) && (stop_proc == false);
115         next_node = next_node.getNextSibling())
116         {
117             if (next_node.getNodeType() == Node.ELEMENT_NODE)
118             {
119                 switch (state)
120                 {
121                     case COLLECTING_TIMESTAMP:
122                         String JavaDoc ts_s = AceXMLHelper.processXMLElement(next_node, EL_TIME_STAMP,
123                         ATT_TIME_STAMP);
124                         try
125                         {
126                             timeStamp = Long.parseLong(ts_s);
127                             state = COLLECTING_HOST;
128                         }
129                         catch (NumberFormatException JavaDoc ex)
130                         {
131                             throw new AceException("The time stamp is not an integer number ("
132                             + ts_s
133                             + ")");
134                         }
135                         break;
136                         
137                     case COLLECTING_HOST:
138                         hostName = AceXMLHelper.processXMLElement(next_node,
139                         EL_HOST_NAME,
140                         ATT_HOST_NAME);
141                         state = COLLECTING_PNAME;
142                         break;
143                         
144                     case COLLECTING_PNAME:
145                         processName = AceXMLHelper.processXMLElement(next_node, EL_APPL_NAME,
146                         ATT_APPL_NAME);
147                         state = COLLECTING_PINS;
148                         break;
149                         
150                     case COLLECTING_PINS:
151                         String JavaDoc pins_s = AceXMLHelper.processXMLElement(next_node, EL_APPL_INS, ATT_APPL_INS);
152                         try
153                         {
154                             processInstance = Integer.parseInt(pins_s);
155                             state = COLLECTING_SEVERITY;
156                         }
157                         catch (NumberFormatException JavaDoc ex)
158                         {
159                             throw new AceException("The process instance is not an integer number ("
160                             + pins_s
161                             + ")");
162                         }
163                         break;
164                         
165                     case COLLECTING_SEVERITY:
166                         String JavaDoc sev_s = AceXMLHelper.processXMLElement(next_node, EL_SEVERITY, ATT_SEVERITY);
167                         try
168                         {
169                             severity = Integer.parseInt(sev_s);
170                             state = COLLECTING_TYPE;
171                         }
172                         catch (NumberFormatException JavaDoc ex)
173                         {
174                             throw new AceException("The severity is not an integer number ("
175                             + sev_s
176                             + ")");
177                         }
178                         break;
179                         
180                     case COLLECTING_TYPE:
181                         String JavaDoc msgt_s = AceXMLHelper.processXMLElement(next_node, EL_TYPE, ATT_TYPE);
182                         try
183                         {
184                             msgType = Integer.parseInt(msgt_s);
185                             state = COLLECTED_TYPE;
186                         }
187                         catch (NumberFormatException JavaDoc ex)
188                         {
189                             throw new AceException("The message type is not an integer number ("
190                             + msgt_s
191                             + ")");
192                         }
193                         break;
194                         
195                     case COLLECTED_TYPE:
196                         if (next_node.getNodeName().equals(EL_ID) == true)
197                         {
198                             String JavaDoc msgid_s = AceXMLHelper.getXMLAttribute(next_node, ATT_ID);
199                             try
200                             {
201                                 msgId = Integer.parseInt(msgid_s);
202                                 state = COLLECTING_MESSAGE;
203                             }
204                             catch (NumberFormatException JavaDoc ex)
205                             {
206                                 throw new AceException("The message id is not an integer number ("
207                                 + msgid_s
208                                 + ")");
209                             }
210                         }
211                         else
212                         {
213                             processMessage(next_node);
214                             state = COLLECTED_MESSAGE;
215                             stop_proc = true;
216                         }
217                         break;
218                         
219                     case COLLECTING_MESSAGE:
220                         processMessage(next_node);
221                         state = COLLECTED_MESSAGE;
222                         stop_proc = true;;
223                         break;
224                 }
225             }
226             // else ignore any other elements
227
} // for
228

229         if (state != COLLECTED_MESSAGE)
230         {
231             // throw exception
232
throw new AceException("The log message is incomplete");
233         }
234         
235         
236     }
237     
238     public long getTimeStamp()
239     {
240         return timeStamp;
241     }
242     
243     public String JavaDoc messageType()
244     {
245         return AceLogMessageParser.LOG_MESSAGE;
246     }
247     
248     public String JavaDoc getFormattedMessage()
249     {
250         StringBuffer JavaDoc formattedMessage = null;
251         if (noXMLHeader == false)
252         {
253             formattedMessage = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"us-ascii\"?>"
254             + '\n');
255         }
256         else
257         {
258             formattedMessage = new StringBuffer JavaDoc("");
259         }
260         
261         formattedMessage.append("<" + AceLogMessageParser.LOG_MESSAGE + " "
262         + ATT_LOG_GROUP + "=\"" + logGroup + "\">"
263         + '\n'
264         + "<" + EL_TIME_STAMP + " "
265         + ATT_TIME_STAMP + "=\"" + timeStamp + "\"/>"
266         + '\n'
267         + "<" + EL_HOST_NAME + " "
268         + ATT_HOST_NAME + "=\"" + hostName + "\"/>"
269         + '\n'
270         + "<" + EL_APPL_NAME + " "
271         + ATT_APPL_NAME + "=\"" + processName + "\"/>"
272         + '\n'
273         + "<" + EL_APPL_INS + " "
274         + ATT_APPL_INS + "=\"" + processInstance + "\"/>"
275         + '\n'
276         + "<" + EL_SEVERITY + " "
277         + ATT_SEVERITY + "=\"" + severity + "\"/>"
278         + '\n'
279         + "<" + EL_TYPE + " "
280         + ATT_TYPE + "=\"" + msgType + "\"/>"
281         + '\n');
282         if (msgId != -1)
283         {
284             formattedMessage.append("<" + EL_ID + " "
285             + ATT_ID + "=\"" + msgId + "\"/>"
286             + '\n');
287         }
288                 
289         formattedMessage.append("<" + EL_MESSAGE + ">"
290         + AceXMLHelper.encodeXMLString(message)
291         + "</" + EL_MESSAGE + ">"
292         + '\n'
293         + "</" + AceLogMessageParser.LOG_MESSAGE + ">"
294         + '\n');
295                 
296         return formattedMessage.toString();
297     }
298         
299     public String JavaDoc getProcessName()
300     {
301         if (processName != null)
302         {
303             return new String JavaDoc(processName);
304         }
305         else
306         {
307             return null;
308         }
309     }
310     
311     public int getProcessInstance()
312     {
313         return processInstance;
314     }
315     
316     public int getSeverity()
317     {
318         return severity;
319     }
320     
321     public int getMessageType()
322     {
323         return msgType;
324     }
325     
326     public String JavaDoc getMessage()
327     {
328         if (message != null)
329         {
330             return new String JavaDoc(message);
331         }
332         else
333         {
334             return null;
335         }
336     }
337     
338     public int getMessageId()
339     {
340         return msgId;
341     }
342     
343     public String JavaDoc getHostName()
344     {
345         return new String JavaDoc(hostName);
346     }
347     
348     private void processMessage(Node node)
349     throws AceException
350     {
351         if (node.getNodeName().equals(EL_MESSAGE) == false)
352         {
353             // the node name do not match
354
throw new AceException("The message is not found");
355         }
356         
357         // it should have a text child node
358
Node child_node = node.getFirstChild();
359         if (child_node == null)
360         {
361             throw new AceException("The message does not have a child XML node");
362         }
363         
364         message = child_node.getNodeValue();
365     }
366     
367     private static final int COLLECTING_TIMESTAMP = 0;
368     private static final int COLLECTING_HOST = 1;
369     private static final int COLLECTING_PNAME = 2;
370     private static final int COLLECTING_PINS = 3;
371     private static final int COLLECTING_SEVERITY = 4;
372     private static final int COLLECTING_TYPE = 5;
373     private static final int COLLECTED_TYPE = 6;
374     private static final int COLLECTING_MESSAGE = 7;
375     private static final int COLLECTED_MESSAGE = 8;
376     
377     private String JavaDoc processName;
378     private int processInstance;
379     private int severity;
380     private int msgType;
381     private String JavaDoc message;
382     private int msgId = -1;
383     private String JavaDoc hostName = "";
384     private long timeStamp = 0;
385     private int logGroup = 0;
386     private boolean noXMLHeader = true;
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
Popular Tags