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