001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.loggers.debug; 028 029 import java.util.*; 030 031 /** 032 * This class is responsible for formatting messages and replacing 033 * format tokens with the text value of message arguments in debug logging 034 * records. 035 */ 036 public class DebugMessageFormatter 037 { 038 /** 039 * Format the message format string with the provided arguments. 040 * 041 * @param msg the message format string to be formatted. 042 * @param msgArgs the arguments to use when replacing tokens in the message. 043 * @return the formatted message string. 044 */ 045 public static String format(String msg, Object[] msgArgs) 046 { 047 StringBuilder buffer= new StringBuilder(); 048 Object[] decoratedArgs = decorateMessageArgs(msgArgs); 049 050 if (msg == null) 051 { 052 concatenateArgs(decoratedArgs, buffer); 053 return buffer.toString(); 054 } 055 056 try 057 { 058 return String.format(msg, decoratedArgs); 059 } 060 catch (IllegalFormatException e) 061 { 062 // Make a more useful message than a stack trace. 063 buffer.append(msg); 064 concatenateArgs(decoratedArgs, buffer); 065 066 return buffer.toString(); 067 } 068 } 069 070 private static void concatenateArgs(Object[] args, StringBuilder buffer) 071 { 072 for (int i = 0; (args != null) && (i < args.length); i++) { 073 buffer.append(" ").append(args[i]); 074 } 075 } 076 077 private static Object[] decorateMessageArgs(Object[] undecoratedArgs) 078 { 079 Object[] args= null; 080 if (undecoratedArgs != null) { 081 args= new Object[undecoratedArgs.length]; 082 for (int i= 0; i < args.length; i++) { 083 args[i]= decorateArg(undecoratedArgs[i]); 084 } 085 } 086 087 return args; 088 } 089 090 private static Object decorateArg(Object arg) 091 { 092 Object decoratedArg= arg; 093 094 if (arg instanceof Map) { 095 decoratedArg= decorateMapArg((Map)arg); 096 } 097 else if (arg instanceof List) { 098 decoratedArg= decorateListArg((List)arg); 099 } 100 else if (arg instanceof Object[]) { 101 decoratedArg= decorateArrayArg((Object[])arg); 102 } 103 else if (arg instanceof boolean[]) { 104 decoratedArg = decorateArrayArg((boolean[])arg); 105 } 106 else if (arg instanceof byte[]) { 107 decoratedArg = decorateArrayArg((byte[])arg); 108 } 109 else if (arg instanceof char[]) { 110 decoratedArg = decorateArrayArg((char[])arg); 111 } 112 else if (arg instanceof double[]) { 113 decoratedArg = decorateArrayArg((double[])arg); 114 } 115 else if (arg instanceof float[]) { 116 decoratedArg = decorateArrayArg((float[])arg); 117 } 118 else if (arg instanceof int[]) { 119 decoratedArg = decorateArrayArg((int[])arg); 120 } 121 else if (arg instanceof long[]) { 122 decoratedArg = decorateArrayArg((long[])arg); 123 } 124 125 return decoratedArg; 126 } 127 128 private static String decorateArrayArg(Object[] array) 129 { 130 return decorateListArg(Arrays.asList(array)); 131 } 132 133 private static String decorateArrayArg(boolean[] array) 134 { 135 StringBuilder buffer= new StringBuilder(); 136 buffer.append("[ "); 137 boolean firstElement= true; 138 for (int i= 0; i < array.length; i++) { 139 if (i > 0) buffer.append(", "); 140 buffer.append(array[i]); 141 } 142 buffer.append(" ]"); 143 144 return buffer.toString(); 145 } 146 147 private static String decorateArrayArg(byte[] array) 148 { 149 StringBuilder buffer= new StringBuilder(); 150 buffer.append("[ "); 151 boolean firstElement= true; 152 for (int i= 0; i < array.length; i++) { 153 if (i > 0) buffer.append(", "); 154 buffer.append(array[i]); 155 } 156 buffer.append(" ]"); 157 158 return buffer.toString(); 159 } 160 161 private static String decorateArrayArg(char[] array) 162 { 163 StringBuilder buffer= new StringBuilder(); 164 buffer.append("[ "); 165 boolean firstElement= true; 166 for (int i= 0; i < array.length; i++) { 167 if (i > 0) buffer.append(", "); 168 buffer.append(array[i]); 169 } 170 buffer.append(" ]"); 171 172 return buffer.toString(); 173 } 174 175 private static String decorateArrayArg(double[] array) 176 { 177 StringBuilder buffer= new StringBuilder(); 178 buffer.append("[ "); 179 boolean firstElement= true; 180 for (int i= 0; i < array.length; i++) { 181 if (i > 0) buffer.append(", "); 182 buffer.append(array[i]); 183 } 184 buffer.append(" ]"); 185 186 return buffer.toString(); 187 } 188 189 private static String decorateArrayArg(float[] array) 190 { 191 StringBuilder buffer= new StringBuilder(); 192 buffer.append("[ "); 193 boolean firstElement= true; 194 for (int i= 0; i < array.length; i++) { 195 if (i > 0) buffer.append(", "); 196 buffer.append(array[i]); 197 } 198 buffer.append(" ]"); 199 200 return buffer.toString(); 201 } 202 203 private static String decorateArrayArg(int[] array) 204 { 205 StringBuilder buffer= new StringBuilder(); 206 buffer.append("[ "); 207 boolean firstElement= true; 208 for (int i= 0; i < array.length; i++) { 209 if (i > 0) buffer.append(", "); 210 buffer.append(array[i]); 211 } 212 buffer.append(" ]"); 213 214 return buffer.toString(); 215 } 216 217 private static String decorateArrayArg(long[] array) 218 { 219 StringBuilder buffer= new StringBuilder(); 220 buffer.append("[ "); 221 boolean firstElement= true; 222 for (int i= 0; i < array.length; i++) { 223 if (i > 0) buffer.append(", "); 224 buffer.append(array[i]); 225 } 226 buffer.append(" ]"); 227 228 return buffer.toString(); 229 } 230 231 private static String decorateListArg(List list) 232 { 233 StringBuilder buffer= new StringBuilder(); 234 Iterator iter= list.iterator(); 235 buffer.append("[ "); 236 boolean firstElement= true; 237 while (iter.hasNext()) { 238 Object lValue= iter.next(); 239 if (!firstElement) buffer.append(", "); 240 buffer.append(decorateArg(lValue)); 241 firstElement= false; 242 } 243 buffer.append(" ]"); 244 245 return buffer.toString(); 246 } 247 248 private static String decorateMapArg(Map map) 249 { 250 StringBuilder buffer= new StringBuilder(); 251 Iterator iter= map.entrySet().iterator(); 252 buffer.append("{ "); 253 boolean firstEntry= true; 254 while (iter.hasNext()) { 255 Map.Entry entry= (Map.Entry)iter.next(); 256 if (!firstEntry) buffer.append(", "); 257 buffer.append(decorateArg(entry.getKey())); 258 buffer.append("="); 259 buffer.append(decorateArg(entry.getValue())); 260 firstEntry= false; 261 } 262 buffer.append(" }"); 263 264 return buffer.toString(); 265 } 266 }