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.List;
019
020 import org.apache.commons.lang3.StringUtils;
021 import org.kuali.common.util.obscure.DefaultObscurer;
022 import org.kuali.common.util.obscure.Obscurer;
023 import org.kuali.common.util.property.Constants;
024 import org.slf4j.Logger;
025
026 public class LoggerUtils {
027
028 private static final Obscurer DEFAULT_OBSCURER = new DefaultObscurer();
029
030 public static void log(LogMsg msg, Logger logger) {
031 Assert.notNull(msg.getLevel(), "level is null");
032 Assert.notNull(logger, "logger is null");
033 logMsg(msg.getMessage(), msg.getArgs(), logger, msg.getLevel());
034 }
035
036 public static int[] getPadding(List<String> columns, List<Object[]> argsList) {
037 int[] padding = new int[columns.size()];
038 for (int i = 0; i < padding.length; i++) {
039 padding[i] = Math.max(padding[i], columns.get(i).length());
040 }
041 for (Object[] args : argsList) {
042 Assert.isTrue(columns.size() == args.length, "Column count must equals args.length");
043 for (int i = 0; i < args.length; i++) {
044 padding[i] = Math.max(padding[i], args[i].toString().length());
045 }
046 }
047 return padding;
048 }
049
050 public static String getHeader(List<String> columns, int[] padding, boolean leftAlign) {
051 StringBuilder sb = new StringBuilder();
052 for (int i = 0; i < columns.size(); i++) {
053 if (i != 0) {
054 sb.append(" ");
055 }
056 if (leftAlign) {
057 sb.append(StringUtils.rightPad(columns.get(i), padding[i]));
058 } else {
059 sb.append(StringUtils.leftPad(columns.get(i), padding[i]));
060 }
061 }
062 return sb.toString();
063 }
064
065 public static String getMsg(int count) {
066 StringBuilder sb = new StringBuilder();
067 for (int i = 0; i < count; i++) {
068 if (i != 0) {
069 sb.append(" ");
070 }
071 sb.append("{}");
072 }
073 return sb.toString();
074 }
075
076 public static void updateArgsList(List<Object[]> argsList, int[] padding, boolean leftAlign) {
077 for (Object[] args : argsList) {
078 for (int i = 0; i < args.length; i++) {
079 if (leftAlign) {
080 args[i] = StringUtils.rightPad(args[i].toString(), padding[i]);
081 } else {
082 args[i] = StringUtils.leftPad(args[i].toString(), padding[i]);
083 }
084 }
085 }
086 }
087
088 public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger) {
089 logTable(columns, rows, level, logger, false);
090 }
091
092 public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger, boolean leftAlign) {
093 int[] padding = getPadding(columns, rows);
094 logMsg(getHeader(columns, padding, leftAlign), logger, level);
095 String msg = getMsg(padding.length);
096 updateArgsList(rows, padding, leftAlign);
097 for (Object[] args : rows) {
098 logMsg(msg, args, logger, level);
099 }
100 }
101
102 public static void logLines(String s, Logger logger, LoggerLevel level) {
103 if (s == null) {
104 return;
105 }
106 String[] lines = StringUtils.split(s, "\n");
107 for (String line : lines) {
108 LoggerUtils.logMsg(line, logger, level);
109 }
110 }
111
112 public static final void logMsg(String msg, Object[] args, Logger logger, LoggerLevel level) {
113 switch (level) {
114 case DEBUG:
115 logger.debug(msg, args);
116 return;
117 case TRACE:
118 logger.trace(msg, args);
119 return;
120 case INFO:
121 logger.info(msg, args);
122 return;
123 case WARN:
124 logger.warn(msg, args);
125 return;
126 case ERROR:
127 logger.error(msg, args);
128 return;
129 default:
130 throw new IllegalArgumentException("Logger level " + level + " is unknown");
131 }
132 }
133
134 public static final void logMsg(String msg, Logger logger, LoggerLevel level) {
135 logMsg(msg, null, logger, level);
136 }
137
138 public static final String getUsername(String username) {
139 return getNullAsNone(username);
140 }
141
142 public static final String getNullAsNone(String string) {
143 if (string == null) {
144 return Constants.NONE;
145 } else {
146 return string;
147 }
148 }
149
150 public static final String getPassword(String username, String password) {
151 return getPassword(username, password, DEFAULT_OBSCURER);
152 }
153
154 public static boolean isNullOrNone(String s) {
155 if (s == null) {
156 return true;
157 }
158 if (StringUtils.equalsIgnoreCase(Constants.NONE, s)) {
159 return true;
160 }
161 return StringUtils.equalsIgnoreCase(Constants.NULL, s);
162 }
163
164 public static final String getPassword(String username, String password, Obscurer obscurer) {
165 if (isNullOrNone(password)) {
166 // There is no password, return NONE
167 return Constants.NONE;
168 } else if (StringUtils.equals(username, password)) {
169 // Not exactly high security, display the clear text value
170 return password;
171 } else {
172 // Otherwise obscure it
173 return obscurer.obscure(password);
174 }
175 }
176
177 }