| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.logging; |
|---|
| 34 | |
|---|
| 35 | import java.text.MessageFormat; |
|---|
| 36 | import java.text.ParseException; |
|---|
| 37 | import java.util.ArrayList; |
|---|
| 38 | import java.util.List; |
|---|
| 39 | import java.util.logging.LogRecord; |
|---|
| 40 | |
|---|
| 41 | import org.jcoderz.commons.Loggable; |
|---|
| 42 | import org.jcoderz.commons.util.Constants; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * This is the basic format for all formatters formatting continuation lines. |
|---|
| 47 | * |
|---|
| 48 | */ |
|---|
| 49 | public abstract class ContinuationLineFormat |
|---|
| 50 | extends LogLineFormat |
|---|
| 51 | { |
|---|
| 52 | /** The number of parameters of the basic format. */ |
|---|
| 53 | protected static final int NUMBER_OF_PARAMETERS = 2; |
|---|
| 54 | |
|---|
| 55 | private static final int THREADID_INDEX = 0; |
|---|
| 56 | private static final int TRACKINGID_INDEX = 1; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Common format for CA Unicenter log lines. |
|---|
| 60 | * Has the fields in following order: |
|---|
| 61 | * <ul> |
|---|
| 62 | * <li>thread id |
|---|
| 63 | * <li>tracking number |
|---|
| 64 | * </ul> |
|---|
| 65 | */ |
|---|
| 66 | private static final String LOGLINE_FORMAT_PATTERN |
|---|
| 67 | = "{0} {1}"; |
|---|
| 68 | |
|---|
| 69 | private final int mNumParameters; |
|---|
| 70 | private final String mLogFormatPattern; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Creates a new instance of this and initializes the message format. |
|---|
| 75 | * |
|---|
| 76 | * @param type THe log line type. |
|---|
| 77 | * @param additionalPattern The pattern for additional fields not included by |
|---|
| 78 | * this. Must start with the desired delimiter before the first field. |
|---|
| 79 | * @param numAdditionalParameters The number of additional fields. |
|---|
| 80 | */ |
|---|
| 81 | protected ContinuationLineFormat ( |
|---|
| 82 | final LogLineType type, |
|---|
| 83 | final String additionalPattern, |
|---|
| 84 | final int numAdditionalParameters) |
|---|
| 85 | { |
|---|
| 86 | super(type, new MessageFormat(type.getTypeSpecifier() + " " |
|---|
| 87 | + LOGLINE_FORMAT_PATTERN + additionalPattern), |
|---|
| 88 | NUMBER_OF_PARAMETERS + numAdditionalParameters); |
|---|
| 89 | |
|---|
| 90 | mNumParameters = NUMBER_OF_PARAMETERS + numAdditionalParameters; |
|---|
| 91 | mLogFormatPattern = LOGLINE_FORMAT_PATTERN + additionalPattern; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Gets the formats as array for formatting all elements of a basic log line. |
|---|
| 96 | * |
|---|
| 97 | * @param options The display options specifying which fields to display. |
|---|
| 98 | * Will be ignored and might be null if <code>ignoreOptions == true</code>. |
|---|
| 99 | * @param ignoreOptions flag whether to ignore the supplied options and |
|---|
| 100 | * return the formats for all fields. |
|---|
| 101 | * |
|---|
| 102 | * @return List filled with formats for each selected field. Might be empty, |
|---|
| 103 | * never null. |
|---|
| 104 | */ |
|---|
| 105 | protected static List getBasicFormatList ( |
|---|
| 106 | final DisplayOptions options, |
|---|
| 107 | final boolean ignoreOptions) |
|---|
| 108 | { |
|---|
| 109 | final List formatList = new ArrayList(); |
|---|
| 110 | if (ignoreOptions || options.displayThreadId()) |
|---|
| 111 | { |
|---|
| 112 | // thread id |
|---|
| 113 | formatList.add(getThreadIdFormat()); |
|---|
| 114 | } |
|---|
| 115 | if (ignoreOptions || options.displayTrackingNumber()) |
|---|
| 116 | { |
|---|
| 117 | // sequence of tracking id |
|---|
| 118 | formatList.add(getTrackingNumberFormat()); |
|---|
| 119 | } |
|---|
| 120 | return formatList; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Formats the supplied LogRecord with the encapsulated basic message format. |
|---|
| 125 | * Data for additional fields has to be set before calling this. |
|---|
| 126 | * Appends a line feed after the data is formatted into the StringBuffer. |
|---|
| 127 | * |
|---|
| 128 | * @param sb The StringBuffer where to append the formatted LogRecord. |
|---|
| 129 | * @param record The LogRecord to format. |
|---|
| 130 | * @param loggable Unused by this, might be null. |
|---|
| 131 | * @param trackingIdSequence The list containing the sequence of tracking |
|---|
| 132 | * ids contributing to this log message. |
|---|
| 133 | */ |
|---|
| 134 | protected final void basicFormat ( |
|---|
| 135 | final StringBuffer sb, |
|---|
| 136 | final LogRecord record, |
|---|
| 137 | final Loggable loggable, |
|---|
| 138 | final List trackingIdSequence) |
|---|
| 139 | { |
|---|
| 140 | setTrackingIds(trackingIdSequence); |
|---|
| 141 | |
|---|
| 142 | if (loggable != null) |
|---|
| 143 | { |
|---|
| 144 | setThreadId(loggable.getThreadId()); |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | { |
|---|
| 148 | setThreadId(record.getThreadID()); |
|---|
| 149 | } |
|---|
| 150 | format(sb); |
|---|
| 151 | sb.append(Constants.LINE_SEPARATOR); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Parses a log line, which must be formatted by this, and sets the |
|---|
| 156 | * appropriate basic values of the supplied LogFileEntry. Derived types might |
|---|
| 157 | * retrieve specific field values after this has been called. |
|---|
| 158 | * |
|---|
| 159 | * @param sb The StringBuffer containing the current log line. |
|---|
| 160 | * @param entry The LogFileEntry for which to parse the log line. |
|---|
| 161 | * |
|---|
| 162 | * @throws ParseException if an error occurs parsing <code>sb</code>. |
|---|
| 163 | * |
|---|
| 164 | * @see org.jcoderz.commons.logging.LogLineFormat#parse(java.lang.StringBuffer, org.jcoderz.commons.logging.LogFileEntry) |
|---|
| 165 | */ |
|---|
| 166 | protected final void basicParse (StringBuffer sb, LogFileEntry entry) |
|---|
| 167 | throws ParseException |
|---|
| 168 | { |
|---|
| 169 | try |
|---|
| 170 | { |
|---|
| 171 | parse(sb); |
|---|
| 172 | // this is a continuation line, so thread id and tracking id have been |
|---|
| 173 | // set already |
|---|
| 174 | /* |
|---|
| 175 | entry.setThreadId(getThreadId()); |
|---|
| 176 | final List trackingIds = getTrackingIds(); |
|---|
| 177 | entry.setTrackingNumber((String) trackingIds.get( |
|---|
| 178 | trackingIds.size() - 1)); |
|---|
| 179 | */ |
|---|
| 180 | } |
|---|
| 181 | catch (ParseException pex) |
|---|
| 182 | { |
|---|
| 183 | // just rethrow |
|---|
| 184 | throw pex; |
|---|
| 185 | } |
|---|
| 186 | catch (Exception ex) |
|---|
| 187 | { |
|---|
| 188 | final ParseException pex = new ParseException( |
|---|
| 189 | "Got an error parsing " + sb, 0); |
|---|
| 190 | pex.initCause(ex); |
|---|
| 191 | throw pex; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Sets the thread id from the message to dump. |
|---|
| 197 | * |
|---|
| 198 | * @param threadId The thread id to dump. |
|---|
| 199 | */ |
|---|
| 200 | protected final void setThreadId (final long threadId) |
|---|
| 201 | { |
|---|
| 202 | setParameter(THREADID_INDEX, String.valueOf(threadId)); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Gets the thread id of a parsed log line. |
|---|
| 207 | * |
|---|
| 208 | * @return Thread id of parsed log line. |
|---|
| 209 | */ |
|---|
| 210 | protected final long getThreadId () |
|---|
| 211 | { |
|---|
| 212 | return Long.parseLong((String) getParameter(THREADID_INDEX)); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * Sets the list of tracking ids to dump. |
|---|
| 217 | * |
|---|
| 218 | * @param trackingIds The sequence of tracking ids to dump. |
|---|
| 219 | */ |
|---|
| 220 | protected final void setTrackingIds (final List trackingIds) |
|---|
| 221 | { |
|---|
| 222 | setParameter(TRACKINGID_INDEX, trackingIds); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | * Gets the sequence of tracking ids of a parsed log line. |
|---|
| 227 | * |
|---|
| 228 | * @return Sequence of tracking ids of parsed log line. |
|---|
| 229 | */ |
|---|
| 230 | protected final List getTrackingIds () |
|---|
| 231 | { |
|---|
| 232 | return (List) getParameter(TRACKINGID_INDEX); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|