View Javadoc

1   /**
2    * Copyright 2010-2012 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 int[] getPadding(List<String> columns, List<Object[]> argsList) {
31  		int[] padding = new int[columns.size()];
32  		for (int i = 0; i < padding.length; i++) {
33  			padding[i] = Math.max(padding[i], columns.get(i).length());
34  		}
35  		for (Object[] args : argsList) {
36  			Assert.isTrue(columns.size() == args.length, "Column count must equals args.length");
37  			for (int i = 0; i < args.length; i++) {
38  				padding[i] = Math.max(padding[i], args[i].toString().length());
39  			}
40  		}
41  		return padding;
42  	}
43  
44  	public static String getHeader(List<String> columns, int[] padding, boolean leftAlign) {
45  		StringBuilder sb = new StringBuilder();
46  		for (int i = 0; i < columns.size(); i++) {
47  			if (i != 0) {
48  				sb.append("  ");
49  			}
50  			if (leftAlign) {
51  				sb.append(StringUtils.rightPad(columns.get(i), padding[i]));
52  			} else {
53  				sb.append(StringUtils.leftPad(columns.get(i), padding[i]));
54  			}
55  		}
56  		return sb.toString();
57  	}
58  
59  	public static String getMsg(int count) {
60  		StringBuilder sb = new StringBuilder();
61  		for (int i = 0; i < count; i++) {
62  			if (i != 0) {
63  				sb.append("  ");
64  			}
65  			sb.append("{}");
66  		}
67  		return sb.toString();
68  	}
69  
70  	public static void updateArgsList(List<Object[]> argsList, int[] padding, boolean leftAlign) {
71  		for (Object[] args : argsList) {
72  			for (int i = 0; i < args.length; i++) {
73  				if (leftAlign) {
74  					args[i] = StringUtils.rightPad(args[i].toString(), padding[i]);
75  				} else {
76  					args[i] = StringUtils.leftPad(args[i].toString(), padding[i]);
77  				}
78  			}
79  		}
80  	}
81  
82  	public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger) {
83  		logTable(columns, rows, level, logger, false);
84  	}
85  
86  	public static void logTable(List<String> columns, List<Object[]> rows, LoggerLevel level, Logger logger, boolean leftAlign) {
87  		int[] padding = getPadding(columns, rows);
88  		logMsg(getHeader(columns, padding, leftAlign), logger, level);
89  		String msg = getMsg(padding.length);
90  		updateArgsList(rows, padding, leftAlign);
91  		for (Object[] args : rows) {
92  			logMsg(msg, args, logger, level);
93  		}
94  	}
95  
96  	public static void logLines(String s, Logger logger, LoggerLevel level) {
97  		if (s == null) {
98  			return;
99  		}
100 		String[] lines = StringUtils.split(s, "\n");
101 		for (String line : lines) {
102 			LoggerUtils.logMsg(line, logger, level);
103 		}
104 	}
105 
106 	public static final void logMsg(String msg, Object[] args, Logger logger, LoggerLevel level) {
107 		switch (level) {
108 		case DEBUG:
109 			logger.debug(msg, args);
110 			return;
111 		case TRACE:
112 			logger.trace(msg, args);
113 			return;
114 		case INFO:
115 			logger.info(msg, args);
116 			return;
117 		case WARN:
118 			logger.warn(msg, args);
119 			return;
120 		case ERROR:
121 			logger.error(msg, args);
122 			return;
123 		default:
124 			throw new IllegalArgumentException("Logger level " + level + " is unknown");
125 		}
126 	}
127 
128 	public static final void logMsg(String msg, Logger logger, LoggerLevel level) {
129 		logMsg(msg, null, logger, level);
130 	}
131 
132 	public static final String getUsername(String username) {
133 		return getNullAsNone(username);
134 	}
135 
136 	public static final String getNullAsNone(String string) {
137 		if (string == null) {
138 			return Constants.NONE;
139 		} else {
140 			return string;
141 		}
142 	}
143 
144 	public static final String getPassword(String username, String password) {
145 		return getPassword(username, password, DEFAULT_OBSCURER);
146 	}
147 
148 	public static boolean isNullOrNone(String s) {
149 		if (s == null) {
150 			return true;
151 		}
152 		if (StringUtils.equalsIgnoreCase(Constants.NONE, s)) {
153 			return true;
154 		}
155 		return StringUtils.equalsIgnoreCase(Constants.NULL, s);
156 	}
157 
158 	public static final String getPassword(String username, String password, Obscurer obscurer) {
159 		if (isNullOrNone(password)) {
160 			// There is no password, return NONE
161 			return Constants.NONE;
162 		} else if (StringUtils.equals(username, password)) {
163 			// Not exactly high security, display the clear text value
164 			return password;
165 		} else {
166 			// Otherwise obscure it
167 			return obscurer.obscure(password);
168 		}
169 	}
170 
171 }