| 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; |
|---|
| 34 | |
|---|
| 35 | import java.text.MessageFormat; |
|---|
| 36 | import java.util.ArrayList; |
|---|
| 37 | import java.util.Arrays; |
|---|
| 38 | import java.util.Collections; |
|---|
| 39 | import java.util.Iterator; |
|---|
| 40 | import java.util.List; |
|---|
| 41 | import java.util.Map; |
|---|
| 42 | import java.util.logging.Level; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * This abstract class implements the interface |
|---|
| 47 | * {@link org.jcoderz.commons.LogMessageInfo}. |
|---|
| 48 | * <p> |
|---|
| 49 | * All parameters are stored as immutable fields. |
|---|
| 50 | * Besides the parameters from the interface, an (unique) error |
|---|
| 51 | * id must be supplied during construction. |
|---|
| 52 | * |
|---|
| 53 | * @author Michael Griffel |
|---|
| 54 | */ |
|---|
| 55 | public abstract class LogMessageInfoImpl |
|---|
| 56 | implements LogMessageInfo |
|---|
| 57 | { |
|---|
| 58 | /** use this serialVersionUID for serialization. */ |
|---|
| 59 | static final long serialVersionUID = 1L; |
|---|
| 60 | |
|---|
| 61 | private final String mErrorSymbol; |
|---|
| 62 | private final int mErrorId; |
|---|
| 63 | private final Level mLogLevel; |
|---|
| 64 | private final String mMessagePattern; |
|---|
| 65 | private final String mSolution; |
|---|
| 66 | private final List mParameters; |
|---|
| 67 | private final BusinessImpact mBusinessImpact; |
|---|
| 68 | private final Category mCategory; |
|---|
| 69 | private final String mApplicationName; |
|---|
| 70 | private final String mApplicationNameAbbreviation; |
|---|
| 71 | private final String mGroupName; |
|---|
| 72 | private final String mGroupNameAbbreviation; |
|---|
| 73 | |
|---|
| 74 | protected LogMessageInfoImpl (String symbol, int id, Level level, |
|---|
| 75 | String text, String solution, BusinessImpact businessImpact, |
|---|
| 76 | Category category, String[] params, String appName, |
|---|
| 77 | String appNameAbbr, String groupName, String groupNameAbbr) |
|---|
| 78 | { |
|---|
| 79 | mErrorSymbol = symbol; |
|---|
| 80 | mErrorId = id; |
|---|
| 81 | mLogLevel = level; |
|---|
| 82 | mMessagePattern = text; |
|---|
| 83 | mSolution = solution; |
|---|
| 84 | mBusinessImpact = businessImpact; |
|---|
| 85 | mCategory = category; |
|---|
| 86 | mParameters = Collections.unmodifiableList(Arrays.asList(params)); |
|---|
| 87 | mApplicationName = appName; |
|---|
| 88 | mApplicationNameAbbreviation = appNameAbbr; |
|---|
| 89 | mGroupName = groupName; |
|---|
| 90 | mGroupNameAbbreviation = groupNameAbbr; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Returns the (unique) integer representation of the log message info. |
|---|
| 95 | * This integer representation is also called the <b>error identifier</b> |
|---|
| 96 | * an is constructed as defined below: |
|---|
| 97 | * <pre> |
|---|
| 98 | * +=========+=========+=========+=========+ |
|---|
| 99 | * ID (32 bit) : |0XXX XXXX|YYYY YYYY|ZZZZ ZZZZ|ZZZZ ZZZZ| |
|---|
| 100 | * +=========+=========+=========+=========+ |
|---|
| 101 | * |
|---|
| 102 | * XXX (7 bit) -> application @id |
|---|
| 103 | * YYY (8 bit) -> group @id |
|---|
| 104 | * ZZZ (16 bit) -> message @id |
|---|
| 105 | * </pre> |
|---|
| 106 | * <b>Note:</b> |
|---|
| 107 | * This value should also be printed in its hexadecimal string |
|---|
| 108 | * representation since it could then be easily <i>masked</i> by |
|---|
| 109 | * human readers. |
|---|
| 110 | * @return the integer representation of the log message info. |
|---|
| 111 | */ |
|---|
| 112 | public final int toInt () |
|---|
| 113 | { |
|---|
| 114 | return mErrorId; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Returns the string representation of the log message info. This is the |
|---|
| 119 | * symbolic string representation as defined in the parameter |
|---|
| 120 | * <code>symbol</code> during construction. |
|---|
| 121 | * @see java.lang.Object#toString() |
|---|
| 122 | * @return the string representation of the log message info. |
|---|
| 123 | */ |
|---|
| 124 | public final String toString () |
|---|
| 125 | { |
|---|
| 126 | return mErrorSymbol; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | // LogMessageInfo interface |
|---|
| 130 | |
|---|
| 131 | /** {@inheritDoc} */ |
|---|
| 132 | public final String getSymbol () |
|---|
| 133 | { |
|---|
| 134 | return mErrorSymbol; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** {@inheritDoc} */ |
|---|
| 138 | public final Level getLogLevel () |
|---|
| 139 | { |
|---|
| 140 | return mLogLevel; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** {@inheritDoc} */ |
|---|
| 144 | public final String getMessagePattern () |
|---|
| 145 | { |
|---|
| 146 | return mMessagePattern; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** {@inheritDoc} */ |
|---|
| 150 | public final StringBuffer formatMessage (Map parameters, StringBuffer buffer) |
|---|
| 151 | { |
|---|
| 152 | final StringBuffer result |
|---|
| 153 | = buffer != null ? buffer : new StringBuffer(); |
|---|
| 154 | try |
|---|
| 155 | { |
|---|
| 156 | final MessageFormat formatter = new MessageFormat(getMessagePattern()); |
|---|
| 157 | |
|---|
| 158 | final List parameter = new ArrayList(); |
|---|
| 159 | |
|---|
| 160 | if (parameters != null && !getParameterList().isEmpty()) |
|---|
| 161 | { |
|---|
| 162 | final Iterator i = getParameterList().iterator(); |
|---|
| 163 | while (i.hasNext()) |
|---|
| 164 | { |
|---|
| 165 | final String parameterName = (String) i.next(); |
|---|
| 166 | final List parameterValues = (List) parameters.get(parameterName); |
|---|
| 167 | if (parameterValues == null || parameterValues.isEmpty()) |
|---|
| 168 | { |
|---|
| 169 | parameter.add(null); |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | parameter.add(parameterValues.get(0)); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | formatter.format(parameter.toArray(), result, null); |
|---|
| 178 | } |
|---|
| 179 | // could be caused by invalid message format! |
|---|
| 180 | catch (Exception ex) |
|---|
| 181 | { |
|---|
| 182 | result.append(parameters); |
|---|
| 183 | result.append(' '); |
|---|
| 184 | result.append(getMessagePattern()); |
|---|
| 185 | } |
|---|
| 186 | return result; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** {@inheritDoc} */ |
|---|
| 190 | public final String getSolution () |
|---|
| 191 | { |
|---|
| 192 | return mSolution; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** {@inheritDoc} */ |
|---|
| 196 | public final BusinessImpact getBusinessImpact () |
|---|
| 197 | { |
|---|
| 198 | return mBusinessImpact; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** {@inheritDoc} */ |
|---|
| 202 | public final Category getCategory () |
|---|
| 203 | { |
|---|
| 204 | return mCategory; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** {@inheritDoc} */ |
|---|
| 208 | public final List getParameterList () |
|---|
| 209 | { |
|---|
| 210 | return mParameters; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /** {@inheritDoc} */ |
|---|
| 214 | public String getAppName () |
|---|
| 215 | { |
|---|
| 216 | return mApplicationName; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** {@inheritDoc} */ |
|---|
| 220 | public String getAppNameAbbreviation () |
|---|
| 221 | { |
|---|
| 222 | return mApplicationNameAbbreviation; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /** {@inheritDoc} */ |
|---|
| 226 | public String getGroupName () |
|---|
| 227 | { |
|---|
| 228 | return mGroupName; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** {@inheritDoc} */ |
|---|
| 232 | public String getGroupNameAbbreviation () |
|---|
| 233 | { |
|---|
| 234 | return mGroupNameAbbreviation; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|