View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.util.List;
19  
20  import org.apache.commons.lang3.StringUtils;
21  import org.kuali.common.util.obscure.DefaultObscurer;
22  import org.kuali.common.util.obscure.Obscurer;
23  import org.kuali.common.util.property.Constants;
24  import org.slf4j.Logger;
25  
26  public class LoggerUtils {
27  
28  	private static final Obscurer DEFAULT_OBSCURER = new DefaultObscurer();
29  
30  	public static void log(LogMsg msg, Logger logger) {
31  		Assert.notNull(msg.getLevel(), "level is null");
32  		Assert.notNull(logger, "logger is null");
33  		logMsg(msg.getMessage(), msg.getArgs(), logger, msg.getLevel());
34  	}
35  
36  	public static int[] getPadding(List<String> columns, List<Object[]> argsList) {
37  		int[] padding = new int[columns.size()];
38  		for (int i = 0; i < padding.length; i++) {
39  			padding[i] = Math.max(padding[i], columns.get(i).length());
40  		}
41  		for (Object[] args : argsList) {
42  			Assert.isTrue(columns.size() == args.length, "Column count must equals args.length");
43  			for (int i = 0; i < args.length; i++) {
44  				padding[i] = Math.max(padding[i], args[i].toString().length());
45  			}
46  		}
47  		return padding;
48  	}
49  
50  	public static String getHeader(List<String> columns, int[] padding, boolean leftAlign) {
51  		StringBuilder sb = new StringBuilder();
52  		for (int i = 0; i < columns.size(); i++) {
53  			if (i != 0) {
54  				sb.append("  ");
55  			}
56  			if (leftAlign) {
57  				sb.append(StringUtils.rightPad(columns.get(i), padding[i]));
58  			} else {
59  				sb.append(StringUtils.leftPad(columns.get(i), padding[i]));
60  			}
61  		}
62  		return sb.toString();
63  	}
64  
65  	public static String getMsg(int count) {
66  		StringBuilder sb = new StringBuilder();
67  		for (int i = 0; i < count; i++) {
68  			if (i != 0) {
69  				sb.append("  ");
70  			}
71  			sb.append("{}");
72  		}
73  		return sb.toString();
74  	}
75  
76  	public static void updateArgsList(List<Object[]> argsList, int[] padding, boolean leftAlign) {
77  		for (Object[] args : argsList) {
78  			for (int i = 0; i < args.length; i++) {
79  				if (leftAlign) {
80  					args[i] = StringUtils.rightPad(args[i].toString(), padding[i]);
81  				} else {
82  					args[i] = StringUtils.leftPad(args[i].toString(), padding[i]);
83  				}
84  			}
85  		}
86  	}
87  
88  	public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger) {
89  		logTable(columns, rows, level, logger, false);
90  	}
91  
92  	public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger, boolean leftAlign) {
93  		int[] padding = getPadding(columns, rows);
94  		logMsg(getHeader(columns, padding, leftAlign), logger, level);
95  		String msg = getMsg(padding.length);
96  		updateArgsList(rows, padding, leftAlign);
97  		for (Object[] args : rows) {
98  			logMsg(msg, args, logger, level);
99  		}
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 }