001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.logging;
020:
021: import java.io.*;
022: import java.text.*;
023: import java.util.*;
024: import java.util.logging.*;
025:
026: import org.openharmonise.rm.config.*;
027:
028: /**
029: * Logs events to an XML file. This class uses the Java 1.4 logging package
030: * to write events in an XML format to a file, the file is specified in the
031: * configuration settings by the property 'EVENT_LOG_FILE'.
032: *
033: * @author Michael Bell
034: * @version $Revision: 1.2 $
035: *
036: */
037: public class XMLEventLogger extends AbstractEventLogger implements
038: EventLogger {
039:
040: /**
041: * <code>Logger</code> used to write the events to a file
042: */
043: private static Logger m_logger = Logger
044: .getLogger(XMLEventLogger.class.getName());
045:
046: /**
047: * The handler used by the logger to write the events to a file
048: */
049: private static FileHandler fh = null;
050:
051: /**
052: * The configuration parameter name for the parameter which determines
053: * the name of the log file for events
054: */
055: private static String PNAME_LOG_FILENAME = "EVENT_LOG_FILE";
056:
057: /**
058: * User id tag name
059: */
060: public static final String TAG_USER_ID = "UserId";
061:
062: /**
063: * Session id tag name
064: */
065: public static final String TAG_SESSION_ID = "SessionId";
066:
067: /**
068: * Timestamp tag name
069: */
070: public static final String TAG_TIMESTAMP = "Timestamp";
071:
072: /**
073: * Event object tag name
074: */
075: public static final String TAG_OBJECT = "EventObject";
076:
077: /**
078: * Object id attribute name
079: */
080: public static final String ATTRIB_OBJECT_ID = "object_id";
081:
082: /**
083: * Label tag name
084: */
085: public static final String TAG_LABEL = "Label";
086:
087: /**
088: * IP address tag name
089: */
090: public static final String TAG_IP_ADDRESS = "IPAddress";
091:
092: /**
093: * HTTP headers tag name
094: */
095: public static final String TAG_HTTP_HEADERS = "HTTPHeaders";
096:
097: /**
098: * HTTP header name attribute name
099: */
100: public static final String ATTRIB_HEADER_NAME = "name";
101:
102: /**
103: * HTTP parameter tag name
104: */
105: public static final String TAG_HTTP_PARAMETERS = "HTTPParameter";
106:
107: /**
108: * Date formatter used for formatting event timestamps
109: */
110: private SimpleDateFormat date_formatter = new SimpleDateFormat(
111: "MM-dd-yyyy HH:mm:ss.SSS");
112: private static final String TAG_ADDITIONAL = "AdditionalData";
113:
114: /**
115: * Constructs an instance of the XML event logger.
116: */
117: public XMLEventLogger() throws LogException {
118: super ();
119:
120: try {
121: fh = new FileHandler(ConfigSettings
122: .getProperty(PNAME_LOG_FILENAME));
123:
124: fh.setFormatter(new HarmoniseFormatter());
125:
126: m_logger.addHandler(fh);
127:
128: m_logger.setLevel(LogLevel.HARMONISE_EVENT);
129:
130: } catch (SecurityException e) {
131: throw new LogException(
132: "security exception initialising log file", e);
133: } catch (ConfigException e) {
134: throw new LogException(
135: "config exception initialising log file", e);
136: } catch (IOException e) {
137: throw new LogException(
138: "io exception initialising log file", e);
139: }
140:
141: }
142:
143: /* (non-Javadoc)
144: * @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)
145: */
146: protected void saveData(int nUserId, String sSessionId,
147: int nObjectId, String sObjectType, String sAction,
148: Date timestamp, String sIP, Map headers)
149: throws LogException {
150: StringBuffer strbuf = new StringBuffer();
151:
152: addTag(strbuf, TAG_USER_ID, nUserId, null);
153: addTag(strbuf, TAG_SESSION_ID, sSessionId, null);
154: addTag(strbuf, TAG_TIMESTAMP, timestamp, null);
155: Hashtable attrs = new Hashtable();
156: attrs.put(ATTRIB_OBJECT_ID, new Integer(nObjectId));
157: addTag(strbuf, TAG_OBJECT, sObjectType, attrs);
158: addTag(strbuf, TAG_LABEL, sAction, null);
159: addTag(strbuf, TAG_IP_ADDRESS, sIP, null);
160: addHeaderTags(strbuf, headers);
161:
162: m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString());
163: }
164:
165: /**
166: * Adds a string representation of the required XML element to the given
167: * <code>StringBuffer</code>.
168: *
169: * @param strbuf the <code>StringBuffer</code> accumulating the serialised
170: * XML
171: * @param sTagname the tag name
172: * @param nTagContent the <code>int</code> content for the tag
173: * @param attrs any attributes to be added stored as name-value pairs in
174: * the <code>Map</code>
175: */
176: private void addTag(StringBuffer strbuf, String sTagname,
177: int nTagContent, Map attrs) {
178: addTag(strbuf, sTagname, String.valueOf(nTagContent), attrs);
179: }
180:
181: /**
182: * Adds a string representation of the required XML element to the given
183: * <code>StringBuffer</code>.
184: *
185: * @param strbuf the <code>StringBuffer</code> accumulating the serialised
186: * XML
187: * @param sTagname the tag name
188: * @param date the <code>Date</code> content for the tag
189: * @param attrs the <code>Map</code> of name-value attribute pairs
190: */
191: private void addTag(StringBuffer strbuf, String sTagname,
192: Date date, Map attrs) {
193: String sDate = date_formatter.format(date);
194:
195: addTag(strbuf, sTagname, sDate, attrs);
196: }
197:
198: /**
199: * Adds a string representation of the required XML element to the
200: * given <code>StringBuffer</code>.
201: *
202: * @param strbuf the <code>StringBuffer</code> accumulating the serialised
203: * XML
204: * @param sTagname the tag name
205: * @param sTagContent the <code>String</code> content for the tag
206: * @param attrs the <code>Map</code> of name-value attribute pairs
207: */
208: private void addTag(StringBuffer strbuf, String sTagname,
209: String sTagContent, Map attrs) {
210: strbuf.append(" <").append(sTagname);
211:
212: if (attrs != null) {
213: Iterator iter = attrs.keySet().iterator();
214:
215: while (iter.hasNext()) {
216: String sKey = (String) iter.next();
217:
218: strbuf.append(" ").append(sKey).append("=\"");
219: strbuf.append(attrs.get(sKey)).append("\"");
220: }
221: }
222:
223: strbuf.append(">");
224: strbuf.append(sTagContent);
225: strbuf.append("</").append(sTagname).append(">\n");
226: }
227:
228: /**
229: * Adds a string representation of the header XML elements to the given
230: * <code>StringBuffer</code>.
231: *
232: * @param strbuf the <code>StringBuffer</code> accumulating the serialised
233: * XML
234: * @param headers the <code>Map</code> containing the HTTP header
235: * name-value pairs
236: */
237: private void addHeaderTags(StringBuffer strbuf, Map headers) {
238: StringBuffer headerTags = new StringBuffer();
239:
240: if (headers != null) {
241:
242: Iterator iter = headers.keySet().iterator();
243:
244: while (iter.hasNext()) {
245: String sHeaderName = (String) iter.next();
246: String sHeaderValue = (String) headers.get(sHeaderName);
247: Hashtable attrs = new Hashtable();
248: attrs.put(ATTRIB_HEADER_NAME, sHeaderName);
249: addTag(headerTags, TAG_HTTP_PARAMETERS, sHeaderValue,
250: attrs);
251: }
252: }
253:
254: addTag(strbuf, TAG_HTTP_PARAMETERS, headerTags.toString(), null);
255: }
256:
257: /* (non-Javadoc)
258: * @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)
259: */
260: protected void saveData(int nUserId, String sSessionId,
261: int nObjectId, String sObjectType, String sAction,
262: Date timestamp, String sIP, String sAdditional)
263: throws LogException {
264: StringBuffer strbuf = new StringBuffer();
265:
266: addTag(strbuf, TAG_USER_ID, nUserId, null);
267: addTag(strbuf, TAG_SESSION_ID, sSessionId, null);
268: addTag(strbuf, TAG_TIMESTAMP, timestamp, null);
269: Hashtable attrs = new Hashtable();
270: attrs.put(ATTRIB_OBJECT_ID, new Integer(nObjectId));
271: addTag(strbuf, TAG_OBJECT, sObjectType, attrs);
272: addTag(strbuf, TAG_LABEL, sAction, null);
273: addTag(strbuf, TAG_IP_ADDRESS, sIP, null);
274: addTag(strbuf, TAG_ADDITIONAL, sAdditional, null);
275:
276: m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString());
277:
278: }
279:
280: }
|