KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > logging > XMLEventLogger


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.logging;
20
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24 import java.util.logging.*;
25
26 import org.openharmonise.rm.config.*;
27
28
29 /**
30  * Logs events to an XML file. This class uses the Java 1.4 logging package
31  * to write events in an XML format to a file, the file is specified in the
32  * configuration settings by the property 'EVENT_LOG_FILE'.
33  *
34  * @author Michael Bell
35  * @version $Revision: 1.2 $
36  *
37  */

38 public class XMLEventLogger
39     extends AbstractEventLogger
40     implements EventLogger {
41     
42     /**
43      * <code>Logger</code> used to write the events to a file
44      */

45     private static Logger m_logger =
46         Logger.getLogger(XMLEventLogger.class.getName());
47     
48     /**
49      * The handler used by the logger to write the events to a file
50      */

51     private static FileHandler fh = null;
52     
53     /**
54      * The configuration parameter name for the parameter which determines
55      * the name of the log file for events
56      */

57     private static String JavaDoc PNAME_LOG_FILENAME = "EVENT_LOG_FILE";
58
59     /**
60      * User id tag name
61      */

62     public static final String JavaDoc TAG_USER_ID = "UserId";
63     
64     /**
65      * Session id tag name
66      */

67     public static final String JavaDoc TAG_SESSION_ID = "SessionId";
68     
69     /**
70      * Timestamp tag name
71      */

72     public static final String JavaDoc TAG_TIMESTAMP = "Timestamp";
73     
74     /**
75      * Event object tag name
76      */

77     public static final String JavaDoc TAG_OBJECT = "EventObject";
78     
79     /**
80      * Object id attribute name
81      */

82     public static final String JavaDoc ATTRIB_OBJECT_ID = "object_id";
83     
84     /**
85      * Label tag name
86      */

87     public static final String JavaDoc TAG_LABEL = "Label";
88     
89     /**
90      * IP address tag name
91      */

92     public static final String JavaDoc TAG_IP_ADDRESS = "IPAddress";
93     
94     /**
95      * HTTP headers tag name
96      */

97     public static final String JavaDoc TAG_HTTP_HEADERS = "HTTPHeaders";
98     
99     /**
100      * HTTP header name attribute name
101      */

102     public static final String JavaDoc ATTRIB_HEADER_NAME = "name";
103     
104     /**
105      * HTTP parameter tag name
106      */

107     public static final String JavaDoc TAG_HTTP_PARAMETERS = "HTTPParameter";
108     
109     /**
110      * Date formatter used for formatting event timestamps
111      */

112     private SimpleDateFormat date_formatter =
113         new SimpleDateFormat("MM-dd-yyyy HH:mm:ss.SSS");
114     private static final String JavaDoc TAG_ADDITIONAL = "AdditionalData";
115
116     /**
117      * Constructs an instance of the XML event logger.
118      */

119     public XMLEventLogger() throws LogException {
120         super();
121
122         try {
123             fh =
124                 new FileHandler(
125                     ConfigSettings.getProperty(
126                         PNAME_LOG_FILENAME));
127
128             fh.setFormatter(new HarmoniseFormatter());
129             
130             m_logger.addHandler(fh);
131
132             m_logger.setLevel(LogLevel.HARMONISE_EVENT);
133
134         } catch (SecurityException JavaDoc e) {
135             throw new LogException(
136                 "security exception initialising log file",
137                 e);
138         } catch (ConfigException e) {
139             throw new LogException("config exception initialising log file", e);
140         } catch (IOException e) {
141             throw new LogException("io exception initialising log file", e);
142         }
143
144     }
145
146     /* (non-Javadoc)
147      * @see org.openharmonise.rm.logging.AbstractEventLogger#saveData(int, java.lang.String, int, java.lang.String, java.lang.String, java.util.Date, java.lang.String, java.util.Map)
148      */

149     protected void saveData(
150         int nUserId,
151         String JavaDoc sSessionId,
152         int nObjectId,
153         String JavaDoc sObjectType,
154         String JavaDoc sAction,
155         Date timestamp,
156         String JavaDoc sIP,
157         Map headers)
158         throws LogException {
159         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
160
161         addTag(strbuf, TAG_USER_ID, nUserId, null);
162         addTag(strbuf, TAG_SESSION_ID, sSessionId, null);
163         addTag(strbuf, TAG_TIMESTAMP, timestamp, null);
164         Hashtable attrs = new Hashtable();
165         attrs.put(ATTRIB_OBJECT_ID, new Integer JavaDoc(nObjectId));
166         addTag(strbuf, TAG_OBJECT, sObjectType, attrs);
167         addTag(strbuf, TAG_LABEL, sAction, null);
168         addTag(strbuf, TAG_IP_ADDRESS, sIP, null);
169         addHeaderTags(strbuf, headers);
170
171         m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString());
172     }
173
174     /**
175      * Adds a string representation of the required XML element to the given
176      * <code>StringBuffer</code>.
177      *
178      * @param strbuf the <code>StringBuffer</code> accumulating the serialised
179      * XML
180      * @param sTagname the tag name
181      * @param nTagContent the <code>int</code> content for the tag
182      * @param attrs any attributes to be added stored as name-value pairs in
183      * the <code>Map</code>
184      */

185     private void addTag(
186         StringBuffer JavaDoc strbuf,
187         String JavaDoc sTagname,
188         int nTagContent,
189         Map attrs) {
190         addTag(strbuf, sTagname, String.valueOf(nTagContent), attrs);
191     }
192
193     /**
194      * Adds a string representation of the required XML element to the given
195      * <code>StringBuffer</code>.
196      *
197      * @param strbuf the <code>StringBuffer</code> accumulating the serialised
198      * XML
199      * @param sTagname the tag name
200      * @param date the <code>Date</code> content for the tag
201      * @param attrs the <code>Map</code> of name-value attribute pairs
202      */

203     private void addTag(
204         StringBuffer JavaDoc strbuf,
205         String JavaDoc sTagname,
206         Date date,
207         Map attrs) {
208         String JavaDoc sDate = date_formatter.format(date);
209
210         addTag(strbuf, sTagname, sDate, attrs);
211     }
212
213     /**
214      * Adds a string representation of the required XML element to the
215      * given <code>StringBuffer</code>.
216      *
217      * @param strbuf the <code>StringBuffer</code> accumulating the serialised
218      * XML
219      * @param sTagname the tag name
220      * @param sTagContent the <code>String</code> content for the tag
221      * @param attrs the <code>Map</code> of name-value attribute pairs
222      */

223     private void addTag(
224         StringBuffer JavaDoc strbuf,
225         String JavaDoc sTagname,
226         String JavaDoc sTagContent,
227         Map attrs) {
228         strbuf.append(" <").append(sTagname);
229
230         if (attrs != null) {
231             Iterator iter = attrs.keySet().iterator();
232
233             while (iter.hasNext()) {
234                 String JavaDoc sKey = (String JavaDoc) iter.next();
235
236                 strbuf.append(" ").append(sKey).append("=\"");
237                 strbuf.append(attrs.get(sKey)).append("\"");
238             }
239         }
240
241         strbuf.append(">");
242         strbuf.append(sTagContent);
243         strbuf.append("</").append(sTagname).append(">\n");
244     }
245
246     /**
247      * Adds a string representation of the header XML elements to the given
248      * <code>StringBuffer</code>.
249      *
250      * @param strbuf the <code>StringBuffer</code> accumulating the serialised
251      * XML
252      * @param headers the <code>Map</code> containing the HTTP header
253      * name-value pairs
254      */

255     private void addHeaderTags(StringBuffer JavaDoc strbuf, Map headers) {
256         StringBuffer JavaDoc headerTags = new StringBuffer JavaDoc();
257
258         if (headers != null) {
259
260             Iterator iter = headers.keySet().iterator();
261
262             while (iter.hasNext()) {
263                 String JavaDoc sHeaderName = (String JavaDoc) iter.next();
264                 String JavaDoc sHeaderValue = (String JavaDoc) headers.get(sHeaderName);
265                 Hashtable attrs = new Hashtable();
266                 attrs.put(ATTRIB_HEADER_NAME, sHeaderName);
267                 addTag(headerTags, TAG_HTTP_PARAMETERS, sHeaderValue, attrs);
268             }
269         }
270
271         addTag( strbuf, TAG_HTTP_PARAMETERS, headerTags.toString(), null);
272     }
273
274     /* (non-Javadoc)
275      * @see org.openharmonise.rm.logging.AbstractEventLogger#saveData(int, java.lang.String, int, java.lang.String, java.lang.String, java.util.Date, java.lang.String, java.lang.String)
276      */

277     protected void saveData(int nUserId, String JavaDoc sSessionId, int nObjectId, String JavaDoc sObjectType, String JavaDoc sAction, Date timestamp, String JavaDoc sIP, String JavaDoc sAdditional) throws LogException {
278         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
279
280         addTag(strbuf, TAG_USER_ID, nUserId, null);
281         addTag(strbuf, TAG_SESSION_ID, sSessionId, null);
282         addTag(strbuf, TAG_TIMESTAMP, timestamp, null);
283         Hashtable attrs = new Hashtable();
284         attrs.put(ATTRIB_OBJECT_ID, new Integer JavaDoc(nObjectId));
285         addTag(strbuf, TAG_OBJECT, sObjectType, attrs);
286         addTag(strbuf, TAG_LABEL, sAction, null);
287         addTag(strbuf, TAG_IP_ADDRESS, sIP, null);
288         addTag(strbuf, TAG_ADDITIONAL, sAdditional,null);
289
290         m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString());
291         
292     }
293
294 }
295
Popular Tags