001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027:
028: package gov.nist.siplite.stack;
029:
030: /**
031: * This class stores a message along with some other informations
032: * Used to log messages.
033: *
034: * @version JAIN-SIP-1.1
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: */
039: class MessageLog {
040: /** Message to be logged. */
041: private String message;
042: /** Originator of this mesage. */
043: private String source;
044: /** Target recipient for the message. */
045: private String destination;
046: /** Time of the logging event. */
047: private long timeStamp;
048: /** Flag indicating if we are the message sender. */
049: private boolean isSender;
050: /** First line from the message. */
051: private String firstLine;
052: /** Status line from the transaction. */
053: private String statusMessage;
054: /** Transaction identifier. */
055: private String tid;
056: /** Caller identification. */
057: private String callId;
058:
059: /**
060: * Compares object for equivalence.
061: * @param other object to be compared
062: * @return true if the object matches
063: */
064: public boolean equals(Object other) {
065: if (!(other instanceof MessageLog)) {
066: return false;
067: } else {
068: MessageLog otherLog = (MessageLog) other;
069: return otherLog.message.equals(message)
070: && otherLog.timeStamp == timeStamp;
071: }
072: }
073:
074: /**
075: * Constructor with initial parameters.
076: * @param message the message to be logged
077: * @param source the originator of the message
078: * @param destination the target recipient
079: * @param timeStamp the logging event timestamp
080: * @param isSender true is we are the sender
081: * @param firstLine the first line from the message
082: * @param statusMessage the status line from the transaction
083: * @param tid the transaction identifier
084: * @param callId the caller identification
085: * @exception IllegalArgumentException if the message is null,
086: * or the timeStamp is not valid
087: */
088: public MessageLog(String message, String source,
089: String destination, String timeStamp, boolean isSender,
090: String firstLine, String statusMessage, String tid,
091: String callId) throws IllegalArgumentException {
092: if (message == null || message.equals(""))
093: throw new IllegalArgumentException("null msg");
094: this .message = message;
095: this .source = source;
096: this .destination = destination;
097: try {
098: long ts = Long.parseLong(timeStamp);
099: if (ts < 0)
100: throw new IllegalArgumentException("Bad time stamp ");
101: this .timeStamp = ts;
102: } catch (NumberFormatException ex) {
103: throw new IllegalArgumentException("Bad number format "
104: + timeStamp);
105: }
106: this .isSender = isSender;
107: this .firstLine = firstLine;
108: this .statusMessage = statusMessage;
109: this .tid = tid;
110: this .callId = callId;
111: }
112:
113: /**
114: * Gets the logged timestamp.
115: * @return the timestamp
116: */
117: protected long getTimeStamp() {
118: return this .timeStamp;
119: }
120:
121: /**
122: * Constructor with initial parameters.
123: * @param message the message to be logged
124: * @param source the originator of the message
125: * @param destination the target recipient
126: * @param timeStamp the logging event timestamp
127: * @param isSender true is we are the sender
128: * @param firstLine the first line from the message
129: * @param statusMessage the status line from the transaction
130: * @param tid the transaction identifier
131: * @param callId the caller identification
132: * @exception IllegalArgumentException if the message is null,
133: * or the timeStamp is not valid
134: */
135: public MessageLog(String message, String source,
136: String destination, long timeStamp, boolean isSender,
137: String firstLine, String statusMessage, String tid,
138: String callId) throws IllegalArgumentException {
139: if (message == null || message.equals(""))
140: throw new IllegalArgumentException("null msg");
141: this .message = message;
142: this .source = source;
143: this .destination = destination;
144: if (timeStamp < 0)
145: throw new IllegalArgumentException("negative ts");
146: this .timeStamp = timeStamp;
147: this .isSender = isSender;
148: this .firstLine = firstLine;
149: this .statusMessage = statusMessage;
150: this .tid = tid;
151: this .callId = callId;
152: }
153:
154: /**
155: * Constructs an XML formatted log message.
156: * @param startTime time of message output
157: * @return the encode log message
158: */
159: public String flush(long startTime) {
160: String log;
161:
162: if (statusMessage != null) {
163: log = " <message\nfrom = \"" + source + "\" \nto = \""
164: + destination + "\" \ntime = \""
165: + (timeStamp - startTime) + "\" \nisSender = \""
166: + isSender + "\" \nstatusMessage = \""
167: + statusMessage + "\" \ntransactionId = \"" + tid
168: + "\" \ncallId = \"" + callId
169: + "\" \nfirstLine = \"" + firstLine.trim()
170: + "\" > \n";
171: log += "<![CDATA[";
172: log += message;
173: log += "]]>\n";
174: log += "</message>\n";
175: } else {
176: log = " <message\nfrom = \"" + source + "\" \nto = \""
177: + destination + "\" \ntime = \""
178: + (timeStamp - startTime) + "\" \nisSender = \""
179: + isSender + "\" \ntransactionId = \"" + tid
180: + "\" \ncallId = \"" + callId
181: + "\" \nfirstLine = \"" + firstLine.trim()
182: + "\" > \n";
183: log += "<![CDATA[";
184: log += message;
185: log += "]]>\n";
186: log += "</message>\n";
187: }
188: return log;
189: }
190:
191: /**
192: * Constructs an XML formatted log message.
193: * @return the encode log message
194: */
195: public String flush() {
196: String log;
197:
198: if (statusMessage != null) {
199: log = " < message\nfrom = \"" + source + "\" \nto = \""
200: + destination + "\" \ntime = \"" + timeStamp
201: + "\" \nisSender = \"" + isSender
202: + "\" \nstatusMessage = \"" + statusMessage
203: + "\" \ntransactionId = \"" + tid
204: + "\" \nfirstLine = \"" + firstLine.trim()
205: + "\" \ncallId = \"" + callId + "\" \n > \n";
206: log += "<![CDATA[";
207: log += message;
208: log += "]]>\n";
209: log += "</message>\n";
210: } else {
211: log = " < message\nfrom = \"" + source + "\" \nto = \""
212: + destination + "\" \ntime = \"" + timeStamp
213: + "\" \nisSender = \"" + isSender
214: + "\" \ntransactionId = \"" + tid
215: + "\" \ncallId = \"" + callId
216: + "\" \nfirstLine = \"" + firstLine.trim()
217: + "\" \n > \n";
218: log += "<![CDATA[";
219: log += message;
220: log += "]]>\n";
221: log += "</message>\n";
222: }
223: return log;
224: }
225:
226: }
|