001 /** 002 * Copyright 2010-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.common.util; 017 018 import java.util.ArrayList; 019 import java.util.List; 020 import java.util.Properties; 021 022 import org.apache.commons.lang3.StringUtils; 023 import org.kuali.common.util.nullify.NullUtils; 024 import org.kuali.common.util.obscure.DefaultObscurer; 025 import org.kuali.common.util.obscure.Obscurer; 026 import org.kuali.common.util.property.Constants; 027 import org.slf4j.Logger; 028 import org.slf4j.LoggerFactory; 029 import org.springframework.util.PropertyPlaceholderHelper; 030 031 /** 032 * @deprecated 033 */ 034 @Deprecated 035 public class LoggerUtils { 036 037 public static final Logger LOGGER_UTILS_LOGGER = LoggerFactory.getLogger(LoggerUtils.class); 038 private static final Obscurer DEFAULT_OBSCURER = new DefaultObscurer(); 039 private static final PropertyPlaceholderHelper HELPER = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER; 040 041 public static String getLogMsg(List<String> includes, List<String> excludes) { 042 if (CollectionUtils.isEmpty(includes) && CollectionUtils.isEmpty(excludes)) { 043 return ""; 044 } 045 String includesCSV = StringUtils.trimToNull(CollectionUtils.getSpaceSeparatedCSV(includes)); 046 String excludesCSV = StringUtils.trimToNull(CollectionUtils.getSpaceSeparatedCSV(excludes)); 047 List<KeyValue> msgs = new ArrayList<KeyValue>(); 048 if (!StringUtils.isBlank(includesCSV)) { 049 msgs.add(new KeyValue("includes", includesCSV)); 050 } 051 if (!StringUtils.isBlank(excludesCSV)) { 052 msgs.add(new KeyValue("excludes", excludesCSV)); 053 } 054 StringBuilder sb = new StringBuilder(); 055 sb.append("["); 056 for (int i = 0; i < msgs.size(); i++) { 057 if (i != 0) { 058 sb.append(" "); 059 } 060 KeyValue msg = msgs.get(i); 061 sb.append(msg.getKey()); 062 sb.append(": "); 063 sb.append(msg.getValue()); 064 } 065 sb.append("]"); 066 return sb.toString(); 067 } 068 069 public static String getLogMsg(StringFilter filter) { 070 Assert.notNull(filter, "filter is null"); 071 return getLogMsg(filter.getIncludes(), filter.getExcludes()); 072 } 073 074 public static Object[] getLogMsgArgs(StringFilter filter) { 075 Assert.notNull(filter, "filter is null"); 076 String includes = CollectionUtils.getSpaceSeparatedCSV(filter.getIncludes()); 077 String excludes = CollectionUtils.getSpaceSeparatedCSV(filter.getExcludes()); 078 return new Object[] { includes, excludes }; 079 } 080 081 public static void log(LogMsg msg, Logger logger) { 082 Assert.notNull(msg.getLevel(), "level is null"); 083 Assert.notNull(logger, "logger is null"); 084 logMsg(msg.getMessage(), msg.getArgs(), logger, msg.getLevel()); 085 } 086 087 public static int[] getPadding(List<String> columns, List<Object[]> argsList) { 088 int[] padding = new int[columns.size()]; 089 for (int i = 0; i < padding.length; i++) { 090 padding[i] = Math.max(padding[i], columns.get(i).length()); 091 } 092 for (Object[] args : argsList) { 093 Assert.isTrue(columns.size() == args.length, "Column count must equals args.length"); 094 for (int i = 0; i < args.length; i++) { 095 padding[i] = Math.max(padding[i], args[i].toString().length()); 096 } 097 } 098 return padding; 099 } 100 101 public static String getHeader(List<String> columns, int[] padding, boolean leftAlign) { 102 StringBuilder sb = new StringBuilder(); 103 for (int i = 0; i < columns.size(); i++) { 104 if (i == 0) { 105 sb.append("|| "); 106 } else { 107 sb.append("| "); 108 } 109 if (leftAlign) { 110 sb.append(StringUtils.rightPad(columns.get(i), padding[i])); 111 } else { 112 sb.append(StringUtils.leftPad(columns.get(i), padding[i])); 113 } 114 if (i == columns.size() - 1) { 115 sb.append(" ||"); 116 } else { 117 sb.append(" |"); 118 } 119 } 120 return sb.toString(); 121 } 122 123 public static void updateArgsList(List<Object[]> argsList, int[] padding, boolean leftAlign) { 124 for (Object[] args : argsList) { 125 for (int i = 0; i < args.length; i++) { 126 if (leftAlign) { 127 args[i] = StringUtils.rightPad(args[i].toString(), padding[i]); 128 } else { 129 args[i] = StringUtils.leftPad(args[i].toString(), padding[i]); 130 } 131 } 132 } 133 } 134 135 public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger) { 136 logTable(columns, rows, level, logger, false); 137 } 138 139 public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger, boolean leftAlign) { 140 LogTableContext context = new LogTableContext(); 141 context.setColumns(columns); 142 context.setRows(rows); 143 context.setLevel(level); 144 context.setLogger(logger); 145 context.setLeftAlign(leftAlign); 146 147 logTable(context); 148 } 149 150 public static void logTable(List<String> columns, List<Object[]> rows) { 151 LogTableContext context = new LogTableContext(columns, rows); 152 logTable(context); 153 } 154 155 public static String getTable(LogTableContext context) { 156 Assert.notNull(context, "context is null"); 157 Assert.notNull(context.getColumns(), "columns is null"); 158 Assert.notNull(context.getRows(), "rows is null"); 159 int[] padding = getPadding(context.getColumns(), context.getRows()); 160 int cols = context.getColumns().size(); 161 int rows = context.getRows().size(); 162 163 String header = getHeader(context.getColumns(), padding, context.isLeftAlign()); 164 updateArgsList(context.getRows(), padding, context.isLeftAlign()); 165 Properties properties = getProperties(context.getRows()); 166 String tableString = getTableString(rows, cols); 167 168 String resolved = HELPER.replacePlaceholders(tableString, properties); 169 170 return header + "\n" + resolved; 171 } 172 173 public static void logTable(LogTableContext context) { 174 175 String table = getTable(context); 176 177 Assert.notNull(context.getLogger(), "logger is null"); 178 Assert.notNull(context.getLevel(), "level is null"); 179 180 String msg = context.getTitle() + "\n\n" + table; 181 182 logMsg(msg, context.getLogger(), context.getLevel()); 183 184 } 185 186 protected static String getTableString(int rows, int cols) { 187 StringBuilder sb = new StringBuilder(); 188 for (int row = 0; row < rows; row++) { 189 for (int col = 0; col < cols; col++) { 190 sb.append("${" + getPropertyKey(row, col) + "}"); 191 } 192 sb.append("\n"); 193 } 194 return sb.toString(); 195 } 196 197 protected static Properties getProperties(List<Object[]> rows) { 198 Properties properties = new Properties(); 199 for (int row = 0; row < rows.size(); row++) { 200 Object[] rowData = rows.get(row); 201 for (int col = 0; col < rowData.length; col++) { 202 String key = getPropertyKey(row, col); 203 StringBuilder sb = new StringBuilder(); 204 if (col == 0) { 205 sb.append("|| "); 206 } else { 207 sb.append("| "); 208 } 209 sb.append(rowData[col] + ""); 210 if (col == rowData.length - 1) { 211 sb.append(" ||"); 212 } else { 213 sb.append(" |"); 214 } 215 properties.setProperty(key, sb.toString()); 216 } 217 } 218 return properties; 219 } 220 221 protected static String getPropertyKey(int row, int col) { 222 return "log.table.row." + row + ".col." + col; 223 } 224 225 public static void logLines(String s, Logger logger, LoggerLevel level) { 226 if (s == null) { 227 return; 228 } 229 String[] lines = StringUtils.split(s, "\n"); 230 for (String line : lines) { 231 LoggerUtils.logMsg(line, logger, level); 232 } 233 } 234 235 public static final void logMsg(String msg, Object[] args, Logger logger, LoggerLevel level) { 236 switch (level) { 237 case DEBUG: 238 logger.debug(msg, args); 239 return; 240 case TRACE: 241 logger.trace(msg, args); 242 return; 243 case INFO: 244 logger.info(msg, args); 245 return; 246 case WARN: 247 logger.warn(msg, args); 248 return; 249 case ERROR: 250 logger.error(msg, args); 251 return; 252 default: 253 throw new IllegalArgumentException("Logger level " + level + " is unknown"); 254 } 255 } 256 257 public static final void logMsg(String msg, Logger logger, LoggerLevel level) { 258 logMsg(msg, null, logger, level); 259 } 260 261 public static final String getUsername(String username) { 262 return getNullAsNone(username); 263 } 264 265 public static final String getNullAsNone(String string) { 266 if (string == null) { 267 return NullUtils.NONE; 268 } else { 269 return string; 270 } 271 } 272 273 public static final String getPassword(String username, String password) { 274 return getPassword(username, password, DEFAULT_OBSCURER); 275 } 276 277 public static boolean isNullOrNone(String s) { 278 return NullUtils.isNullOrNone(s); 279 } 280 281 public static final String getPassword(String username, String password, Obscurer obscurer) { 282 if (isNullOrNone(password)) { 283 // There is no password, return NONE 284 return NullUtils.NONE; 285 } else if (StringUtils.equals(username, password)) { 286 // Not exactly high security, display the clear text value 287 return password; 288 } else { 289 // Otherwise obscure it 290 return obscurer.obscure(password); 291 } 292 } 293 294 }